Exemple #1
0
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 := mcnutils.CopyFile(filepath.Join(config.CaPath, "ca.pem"), filepath.Join(certsPath, "ca.pem"))
		if err != nil {
			return nil, errors.Wrap(err, "failed to copy ca file")
		}
		err = mcnutils.CopyFile(filepath.Join(config.CaPath, "ca-key.pem"), filepath.Join(certsPath, "ca-key.pem"))
		if err != nil {
			return nil, errors.Wrap(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)
	}
	log.SetDebug(config.IsDebug)
	client := libmachine.NewClient(storePath, certsPath)
	client.IsDebug = config.IsDebug
	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
}
Exemple #2
0
func ExampleWithMessage() {
	cause := errors.New("whoops")
	err := errors.WithMessage(cause, "oh noes")
	fmt.Println(err)

	// Output: oh noes: whoops
}
Exemple #3
0
// RegisterMachine registers an iaas.Machine as an Machine and a host on
// the current running DockerMachine. It expects all data needed to Marshal
// the host/driver to be available on CustomData.
func (d *DockerMachine) RegisterMachine(opts RegisterMachineOpts) (*Machine, error) {
	if !d.temp {
		return nil, errors.New("register is only available without user defined StorePath")
	}
	if opts.Base.CustomData == nil {
		return nil, errors.New("custom data is required")
	}
	opts.Base.CustomData["SSHKeyPath"] = filepath.Join(d.client.GetMachinesDir(), opts.Base.Id, "id_rsa")
	rawDriver, err := json.Marshal(opts.Base.CustomData)
	if err != nil {
		return nil, errors.WithMessage(err, "failed to marshal driver data")
	}
	h, err := d.client.NewHost(opts.DriverName, rawDriver)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	err = ioutil.WriteFile(h.Driver.GetSSHKeyPath(), opts.SSHPrivateKey, 0700)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	err = ioutil.WriteFile(h.AuthOptions().CaCertPath, opts.Base.CaCert, 0700)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	err = ioutil.WriteFile(h.AuthOptions().ClientCertPath, opts.Base.ClientCert, 0700)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	err = ioutil.WriteFile(h.AuthOptions().ClientKeyPath, opts.Base.ClientKey, 0700)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	err = d.client.Save(h)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	savedHost, err := d.client.Load(h.Name)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	return &Machine{
		Base: opts.Base,
		Host: savedHost,
	}, nil
}
Exemple #4
0
func (d *DockerMachine) CreateMachine(opts CreateMachineOpts) (*Machine, error) {
	rawDriver, err := json.Marshal(&drivers.BaseDriver{
		MachineName: opts.Name,
		StorePath:   d.StorePath,
	})
	if err != nil {
		return nil, errors.Wrap(err, "failed to marshal base driver")
	}
	h, err := d.client.NewHost(opts.DriverName, rawDriver)
	if err != nil {
		return nil, errors.Wrap(err, "failed to initialize host")
	}
	err = configureDriver(h.Driver, opts.Params)
	if err != nil {
		return nil, errors.WithMessage(err, "failed to configure driver")
	}
	engineOpts := h.HostOptions.EngineOptions
	if opts.InsecureRegistry != "" {
		engineOpts.InsecureRegistry = []string{opts.InsecureRegistry}
	}
	if opts.DockerEngineInstallURL != "" {
		engineOpts.InstallURL = opts.DockerEngineInstallURL
	}
	if opts.RegistryMirror != "" {
		engineOpts.RegistryMirror = []string{opts.RegistryMirror}
	}
	engineOpts.ArbitraryFlags = opts.ArbitraryFlags
	if h.AuthOptions() != nil {
		h.AuthOptions().StorePath = d.StorePath
	}
	errCreate := d.client.Create(h)
	machine, err := newMachine(h)
	if errCreate != nil {
		return machine, errors.Wrap(errCreate, "failed to create host")
	}
	return machine, errors.Wrap(err, "failed to create machine")
}
Exemple #5
0
func (i *dockerMachineIaaS) CreateMachine(params map[string]string) (*iaas.Machine, error) {
	caPath, _ := i.base.GetConfigString("ca-path")
	driverName, ok := params["driver"]
	if !ok {
		name, errConf := i.base.GetConfigString("driver:name")
		if errConf != nil {
			return nil, errDriverNotSet
		}
		driverName = name
		params["driver"] = driverName
	}
	dockerEngineInstallURL, _ := i.getParamOrConfigString("docker-install-url", params)
	insecureRegistry, _ := i.getParamOrConfigString("insecure-registry", params)
	var engineFlags []string
	if f, err := i.getParamOrConfigString("docker-flags", params); err == nil {
		engineFlags = strings.Split(f, ",")
	}
	machineName, ok := params["name"]
	if !ok {
		name, err := generateMachineName(params["pool"])
		if err != nil {
			return nil, err
		}
		machineName = name
	} else {
		delete(params, "name")
	}
	userDataFileParam, err := i.base.GetConfigString("driver:user-data-file-param")
	if err == nil {
		f, errTemp := ioutil.TempFile("", "")
		if errTemp != nil {
			return nil, errors.Wrap(errTemp, "failed to create userdata file")
		}
		defer os.RemoveAll(f.Name())
		userData, errData := i.base.ReadUserData()
		if errData != nil {
			return nil, errors.WithMessage(errData, "failed to read userdata")
		}
		_, errWrite := f.WriteString(userData)
		if errWrite != nil {
			return nil, errors.Wrap(errWrite, "failed to write local userdata file")
		}
		params[userDataFileParam] = f.Name()
	}
	driverOpts := i.buildDriverOpts(driverName, params)
	if userDataFileParam != "" {
		delete(params, userDataFileParam)
	}
	buf := &bytes.Buffer{}
	debugConf, _ := i.base.GetConfigString("debug")
	if debugConf == "" {
		debugConf = "false"
	}
	isDebug, err := strconv.ParseBool(debugConf)
	if err != nil {
		return nil, errors.Wrap(err, "failed to parse debug config")
	}
	dockerMachine, err := i.apiFactory(DockerMachineConfig{
		CaPath:    caPath,
		OutWriter: buf,
		ErrWriter: buf,
		IsDebug:   isDebug,
	})
	if err != nil {
		return nil, err
	}
	defer func() {
		dockerMachine.Close()
		log.Debug(buf.String())
	}()
	m, err := dockerMachine.CreateMachine(CreateMachineOpts{
		Name:                   machineName,
		DriverName:             driverName,
		Params:                 driverOpts,
		InsecureRegistry:       insecureRegistry,
		DockerEngineInstallURL: dockerEngineInstallURL,
		ArbitraryFlags:         engineFlags,
	})
	if err != nil {
		if m != nil {
			errRem := dockerMachine.DeleteMachine(m.Base)
			if errRem != nil {
				err = tsuruErrors.NewMultiError(err, errors.WithMessage(errRem, "failed to remove machine after error"))
			}
		}
		return nil, err
	}
	m.Base.CreationParams = params
	return m.Base, nil
}