示例#1
0
func (h *Handlers) version(r *kite.Request, v *vagrantutil.Vagrant) (interface{}, error) {
	if r.Args == nil {
		return nil, errors.New("missing arguments")
	}

	var req versionRequest

	if err := r.Args.One().Unmarshal(&req); err != nil {
		return nil, err
	}

	var resp versionResponse

	switch req.Name {
	case "klient":
		resp.Klient = konfig.Version
	case "vagrant":
		ver, err := v.Version()
		if err != nil {
			return nil, err
		}

		resp.Vagrant = ver
	case "":
		// Backward-compatibility with old kloud version.
		//
		// TODO(rjeczalik): If you read this probably it can be removed.
		return v.Version()
	}

	return &resp, nil
}
示例#2
0
func (h *Handlers) status(r *kite.Request, v *vagrantutil.Vagrant) (interface{}, error) {
	status, err := v.Status()
	if err != nil {
		return nil, err
	}

	return Info{
		FilePath: v.VagrantfilePath,
		State:    status.String(),
	}, nil
}
示例#3
0
func (h *Handlers) list(r *kite.Request, v *vagrantutil.Vagrant) (interface{}, error) {
	vagrants, err := v.List()
	if err != nil {
		return nil, err
	}

	response := make([]Info, len(vagrants))
	for i, vg := range vagrants {
		response[i] = Info{
			FilePath: vg.VagrantfilePath,
			State:    vg.State,
		}
	}

	return response, nil
}
示例#4
0
func (h *Handlers) download(v *vagrantutil.Vagrant, box, filePath string, queue <-chan chan error) {
	h.log().Debug("downloading %q box...", box)

	var listeners []chan error
	done := make(chan error)

	go func() {
		err := vagrantutil.Wait(v.BoxAdd(&vagrantutil.Box{Name: box}))
		if err == vagrantutil.ErrBoxAlreadyExists {
			// Ignore the above error.
			err = nil
		}

		done <- err
	}()

	for {
		select {
		case l := <-queue:
			listeners = append(listeners, l)
		case err := <-done:
			// Remove the box from in progress.
			h.boxMu.Lock()
			delete(h.boxNames, box)
			delete(h.boxPaths, filePath)
			h.boxMu.Unlock()

			// Defensive channel drain: try to collect listeners
			// that may have registered after receiving from done
			// but before taking boxMu lock.
			listeners = append(listeners, drain(queue)...)

			// Notify all listeners.
			for _, l := range listeners {
				l <- err
			}

			h.log().Debug("downloading %q box finished: err=%v", box, err)

			return
		}
	}
}
示例#5
0
func (h *Handlers) provider(r *kite.Request, v *vagrantutil.Vagrant) (interface{}, error) {
	return v.Provider()
}
示例#6
0
func (h *Handlers) create(r *kite.Request, v *vagrantutil.Vagrant) (interface{}, error) {
	if r.Args == nil {
		return nil, errors.New("missing arguments")
	}

	var params VagrantCreateOptions
	if err := r.Args.One().Unmarshal(&params); err != nil {
		return nil, err
	}

	params.FilePath = h.absolute(params.FilePath)

	if params.Box == "" {
		params.Box = "ubuntu/trusty64"
	}

	if params.Hostname == "" {
		params.Hostname = r.LocalKite.Config.Username
	}

	if params.Memory == 0 {
		params.Memory = 1024
	}

	if params.Cpus == 0 {
		params.Cpus = 1
	}

	if params.TLSProxyHostname == "" {
		params.TLSProxyHostname = pem.Hostname
	}

	switch {
	case !params.Dirty:
		// Ensure vagrant working dir has no machine provisioned.
		err := vagrantutil.Wait(v.Destroy())
		if err != nil {
			h.log().Error("unable to destroy before create: %s", err)
			break
		}

		status, err := v.Status()
		if err != nil {
			h.log().Error("unable to check status: %s", err)
			break
		}

		if status != vagrantutil.NotCreated {
			h.log().Error("dirty Vagrant directory: want status to be %v, was %v", vagrantutil.NotCreated, status)
		}
	}

	h.boxAdd(v, params.Box, params.FilePath)

	vagrantFile, err := createTemplate(&params)
	if err != nil {
		return nil, err
	}

	if err := v.Create(vagrantFile); err != nil {
		return nil, err
	}

	return params, nil
}