<commit_before>// Copyright 2016 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package dockermachine

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/docker/machine/libmachine"
	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/drivers/rpc"
	"github.com/docker/machine/libmachine/engine"
	"github.com/docker/machine/libmachine/host"
	"github.com/docker/machine/libmachine/log"
	"github.com/pkg/errors"
	"github.com/tsuru/tsuru/iaas"
)

var defaultWriter = ioutil.Discard

type DockerMachine struct {
	io.Closer
	client    libmachine.API
	StorePath string
	CertsPath string
	temp      bool
}

type DockerMachineConfig struct {
	CaPath    string
	OutWriter io.Writer
	ErrWriter io.Writer
	StorePath string
}

type DockerMachineAPI interface {
	io.Closer
	CreateMachine(CreateMachineOpts) (*Machine, error)
	DeleteMachine(*iaas.Machine) error
	RegisterMachine(RegisterMachineOpts) (*Machine, error)
	List() ([]*Machine, error)
	DeleteAll() error
}

type CreateMachineOpts struct {
	Name                   string
	DriverName             string
	Params                 map[string]interface{}
	InsecureRegistry       string
	DockerEngineInstallURL string
	RegistryMirror         string
}

type RegisterMachineOpts struct {
	Base          *iaas.Machine
	DriverName    string
	SSHPrivateKey []byte
}

type Machine struct {
	Base *iaas.Machine
	Host *host.Host
}

func NewDockerMachine(config DockerMachineConfig) (DockerMachineAPI, error) {
	storePath := config.StorePath
	temp := false
	if storePath == "" {
		tempPath, err := ioutil.TempDir("", "")
		if err != nil {
			return nil, errors.Wrap(err, "failed to create temp dir")
		}
		storePath = tempPath
		temp = true
	}
	certsPath := filepath.Join(storePath, "certs")
	if _, err := os.Stat(certsPath); os.IsNotExist(err) {
		err := os.MkdirAll(certsPath, 0700)
		if err != nil {
			return nil, errors.WithMessage(err, "failed to create certs dir")
		}
	}
	if config.CaPath != "" {
		err := copy(filepath.Join(config.CaPath, "ca.pem"), filepath.Join(certsPath, "ca.pem"))
		if err != nil {
			return nil, errors.WithMessage(err, "failed to copy ca file")
		}
		err = copy(filepath.Join(config.CaPath, "ca-key.pem"), filepath.Join(certsPath, "ca-key.pem"))
		if err != nil {
			return nil, errors.WithMessage(err, "failed to copy ca key file")
		}
	}
	if config.OutWriter != nil {
		log.SetOutWriter(config.OutWriter)
	} else {
		log.SetOutWriter(defaultWriter)
	}
	if config.ErrWriter != nil {
		log.SetOutWriter(config.ErrWriter)
	} else {
		log.SetOutWriter(defaultWriter)
	}
	client := libmachine.NewClient(storePath, certsPath)
	if _, err := os.Stat(client.GetMachinesDir()); os.IsNotExist(err) {
		err := os.MkdirAll(client.GetMachinesDir(), 0700)
		if err != nil {
			return nil, errors.WithMessage(err, "failed to create machines dir")
		}
	}
	return &DockerMachine{
		StorePath: storePath,
		CertsPath: certsPath,
		client:    client,
		temp:      temp,
	}, nil
}<commit_msg>iaas/dockermachine: remove global default writer<commit_after>// Copyright 2016 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package dockermachine

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/docker/machine/libmachine"
	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/drivers/rpc"
	"github.com/docker/machine/libmachine/engine"
	"github.com/docker/machine/libmachine/host"
	"github.com/docker/machine/libmachine/log"
	"github.com/pkg/errors"
	"github.com/tsuru/tsuru/iaas"
)

type DockerMachine struct {
	io.Closer
	client    libmachine.API
	StorePath string
	CertsPath string
	temp      bool
}

type DockerMachineConfig struct {
	CaPath    string
	OutWriter io.Writer
	ErrWriter io.Writer
	StorePath string
}

type DockerMachineAPI interface {
	io.Closer
	CreateMachine(CreateMachineOpts) (*Machine, error)
	DeleteMachine(*iaas.Machine) error
	RegisterMachine(RegisterMachineOpts) (*Machine, error)
	List() ([]*Machine, error)
	DeleteAll() error
}

type CreateMachineOpts struct {
	Name                   string
	DriverName             string
	Params                 map[string]interface{}
	InsecureRegistry       string
	DockerEngineInstallURL string
	RegistryMirror         string
}

type RegisterMachineOpts struct {
	Base          *iaas.Machine
	DriverName    string
	SSHPrivateKey []byte
}

type Machine struct {
	Base *iaas.Machine
	Host *host.Host
}

func NewDockerMachine(config DockerMachineConfig) (DockerMachineAPI, error) {
	storePath := config.StorePath
	temp := false
	if storePath == "" {
		tempPath, err := ioutil.TempDir("", "")
		if err != nil {
			return nil, errors.Wrap(err, "failed to create temp dir")
		}
		storePath = tempPath
		temp = true
	}
	certsPath := filepath.Join(storePath, "certs")
	if _, err := os.Stat(certsPath); os.IsNotExist(err) {
		err := os.MkdirAll(certsPath, 0700)
		if err != nil {
			return nil, errors.WithMessage(err, "failed to create certs dir")
		}
	}
	if config.CaPath != "" {
		err := copy(filepath.Join(config.CaPath, "ca.pem"), filepath.Join(certsPath, "ca.pem"))
		if err != nil {
			return nil, errors.WithMessage(err, "failed to copy ca file")
		}
		err = copy(filepath.Join(config.CaPath, "ca-key.pem"), filepath.Join(certsPath, "ca-key.pem"))
		if err != nil {
			return nil, errors.WithMessage(err, "failed to copy ca key file")
		}
	}
	if config.OutWriter != nil {
		log.SetOutWriter(config.OutWriter)
	} else {
		log.SetOutWriter(ioutil.Discard)
	}
	if config.ErrWriter != nil {
		log.SetOutWriter(config.ErrWriter)
	} else {
		log.SetOutWriter(ioutil.Discard)
	}
	client := libmachine.NewClient(storePath, certsPath)
	if _, err := os.Stat(client.GetMachinesDir()); os.IsNotExist(err) {
		err := os.MkdirAll(client.GetMachinesDir(), 0700)
		if err != nil {
			return nil, errors.WithMessage(err, "failed to create machines dir")
		}
	}
	return &DockerMachine{
		StorePath: storePath,
		CertsPath: certsPath,
		client:    client,
		temp:      temp,
	}, nil
}
{'fraction_non_alphanumeric': 0.11928236625181833, 'fraction_numerical': 0.0038791013415225475, 'mean_word_length': 3.811819595645412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7539300', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2123, 'n_words': 493}