func cmdConfig(c CommandLine, api libmachine.API) error { // Ensure that log messages always go to stderr when this command is // being run (it is intended to be run in a subshell) log.SetOutWriter(os.Stderr) if len(c.Args()) != 1 { return ErrExpectedOneMachine } host, err := api.Load(c.Args().First()) if err != nil { return err } dockerHost, authOptions, err := runConnectionBoilerplate(host, c) if err != nil { return fmt.Errorf("Error running connection boilerplate: %s", err) } log.Debug(dockerHost) fmt.Printf("--tlsverify --tlscacert=%q --tlscert=%q --tlskey=%q -H=%s", authOptions.CaCertPath, authOptions.ClientCertPath, authOptions.ClientKeyPath, dockerHost) return nil }
func cmdRm(c CommandLine, api libmachine.API) error { if len(c.Args()) == 0 { c.ShowHelp() return errors.New("You must specify a machine name") } force := c.Bool("force") for _, hostName := range c.Args() { h, err := api.Load(hostName) if err != nil { return fmt.Errorf("Error removing host %q: %s", hostName, err) } if err := h.Driver.Remove(); err != nil { if !force { log.Errorf("Provider error removing machine %q: %s", hostName, err) continue } } if err := api.Remove(hostName); err != nil { log.Errorf("Error removing machine %q from store: %s", hostName, err) } else { log.Infof("Successfully removed %s", hostName) } } return nil }
func cmdSSH(c CommandLine, api libmachine.API) error { // Check for help flag -- Needed due to SkipFlagParsing for _, arg := range c.Args() { if arg == "-help" || arg == "--help" || arg == "-h" { c.ShowHelp() return nil } } name := c.Args().First() if name == "" { return ErrExpectedOneMachine } host, err := api.Load(name) if err != nil { return err } currentState, err := host.Driver.GetState() if err != nil { return err } if currentState != state.Running { return fmt.Errorf("Error: Cannot run SSH command: Host %q is not running", host.Name) } client, err := host.CreateSSHClient() if err != nil { return err } return client.Shell(c.Args().Tail()...) }
func removeLocalMachine(hostName string, api libmachine.API) error { exist, _ := api.Exists(hostName) if !exist { return errors.New(hostName + " does not exist.") } return api.Remove(hostName) }
func cmdConfig(c CommandLine, api libmachine.API) error { // Ensure that log messages always go to stderr when this command is // being run (it is intended to be run in a subshell) log.SetOutWriter(os.Stderr) target, err := targetHost(c, api) if err != nil { return err } host, err := api.Load(target) if err != nil { return err } dockerHost, authOptions, err := check.DefaultConnChecker.Check(host, c.Bool("swarm")) if err != nil { return fmt.Errorf("Error running connection boilerplate: %s", err) } log.Debug(dockerHost) fmt.Printf("--tlsverify\n--tlscacert=%q\n--tlscert=%q\n--tlskey=%q\n-H=%s\n", authOptions.CaCertPath, authOptions.ClientCertPath, authOptions.ClientKeyPath, dockerHost) return nil }
func runAction(actionName string, c CommandLine, api libmachine.API) error { hosts, hostsInError := persist.LoadHosts(api, c.Args()) if len(hostsInError) > 0 { errs := []error{} for _, err := range hostsInError { errs = append(errs, err) } return consolidateErrs(errs) } if len(hosts) == 0 { return ErrNoMachineSpecified } if errs := runActionForeachMachine(actionName, hosts); len(errs) > 0 { return consolidateErrs(errs) } for _, h := range hosts { if err := api.Save(h); err != nil { return fmt.Errorf("Error saving host to store: %s", err) } } return nil }
func cmdSSH(c CommandLine, api libmachine.API) error { // Check for help flag -- Needed due to SkipFlagParsing firstArg := c.Args().First() if firstArg == "-help" || firstArg == "--help" || firstArg == "-h" { c.ShowHelp() return nil } name := firstArg if name == "" { return ErrExpectedOneMachine } host, err := api.Load(name) if err != nil { return err } currentState, err := host.Driver.GetState() if err != nil { return err } if currentState != state.Running { return errStateInvalidForSSH{host.Name} } client, err := host.CreateSSHClient() if err != nil { return err } return client.Shell(c.Args().Tail()...) }
func removeRemoteMachine(hostName string, api libmachine.API) error { currentHost, loaderr := api.Load(hostName) if loaderr != nil { return loaderr } return currentHost.Driver.Remove() }
func loadOneMachine(api libmachine.API, args map[string]string) (*host.Host, error) { name, present := args["name"] if !present { return nil, errRequireMachineName } return api.Load(name) }
func cmdCreateOuter(c CommandLine, api libmachine.API) error { const ( flagLookupMachineName = "flag-lookup" ) // We didn't recognize the driver name. driverName := flagHackLookup("--driver") if driverName == "" { c.ShowHelp() return nil // ? } // TODO: Fix hacky JSON solution rawDriver, err := json.Marshal(&drivers.BaseDriver{ MachineName: flagLookupMachineName, }) if err != nil { return fmt.Errorf("Error attempting to marshal bare driver data: %s", err) } driver, err := api.NewPluginDriver(driverName, rawDriver) if err != nil { return fmt.Errorf("Error loading driver %q: %s", driverName, err) } if _, ok := driver.(*errdriver.Driver); ok { return errdriver.NotLoadable{Name: driverName} } // TODO: So much flag manipulation and voodoo here, it seems to be // asking for trouble. // // mcnFlags is the data we get back over the wire (type mcnflag.Flag) // to indicate which parameters are available. mcnFlags := driver.GetCreateFlags() // This bit will actually make "create" display the correct flags based // on the requested driver. cliFlags, err := convertMcnFlagsToCliFlags(mcnFlags) if err != nil { return fmt.Errorf("Error trying to convert provided driver flags to cli flags: %s", err) } for i := range c.Application().Commands { cmd := &c.Application().Commands[i] if cmd.HasName("create") { cmd = addDriverFlagsToCommand(cliFlags, cmd) } } if serialDriver, ok := driver.(*drivers.SerialDriver); ok { driver = serialDriver.Driver } return c.Application().Run(os.Args) }
// StopHost stops the host VM. func StopHost(api libmachine.API) error { host, err := api.Load(constants.MachineName) if err != nil { return err } if err := host.Stop(); err != nil { return err } return nil }
// DeleteHost deletes the host VM. func DeleteHost(api libmachine.API) error { host, err := api.Load(constants.MachineName) if err != nil { return err } m := multiError{} m.Collect(host.Driver.Remove()) m.Collect(api.Remove(constants.MachineName)) return m.ToError() }
// StopHost stops the host VM. func StopHost(api libmachine.API) error { host, err := api.Load(constants.MachineName) if err != nil { return errors.Wrapf(err, "Error loading host: %s", constants.MachineName) } if err := host.Stop(); err != nil { return errors.Wrapf(err, "Error stopping host: %s", constants.MachineName) } return nil }
// DeleteHost deletes the host VM. func DeleteHost(api libmachine.API) error { host, err := api.Load(constants.MachineName) if err != nil { return errors.Wrapf(err, "Error deleting host: %s", constants.MachineName) } m := util.MultiError{} m.Collect(host.Driver.Remove()) m.Collect(api.Remove(constants.MachineName)) return m.ToError() }
func cmdInspect(c CommandLine, api libmachine.API) error { if len(c.Args()) > 1 { c.ShowHelp() return ErrExpectedOneMachine } target, err := targetHost(c, api) if err != nil { return err } host, err := api.Load(target) if err != nil { return err } tmplString := c.String("format") if tmplString != "" { var tmpl *template.Template var err error if tmpl, err = template.New("").Funcs(funcMap).Parse(tmplString); err != nil { return fmt.Errorf("Template parsing error: %v\n", err) } jsonHost, err := json.Marshal(host) if err != nil { return err } obj := make(map[string]interface{}) if err := json.Unmarshal(jsonHost, &obj); err != nil { return err } if err := tmpl.Execute(os.Stdout, obj); err != nil { return err } os.Stdout.Write([]byte{'\n'}) } else { prettyJSON, err := json.MarshalIndent(host, "", " ") if err != nil { return err } fmt.Println(string(prettyJSON)) } return nil }
func checkIfApiExistsAndLoad(api libmachine.API) (*host.Host, error) { exists, err := api.Exists(constants.MachineName) if err != nil { return nil, err } if !exists { return nil, fmt.Errorf("Machine does not exist for api.Exists(%s)", constants.MachineName) } host, err := api.Load(constants.MachineName) if err != nil { return nil, err } return host, nil }
func checkIfApiExistsAndLoad(api libmachine.API) (*host.Host, error) { exists, err := api.Exists(constants.MachineName) if err != nil { return nil, errors.Wrapf(err, "Error checking that api exists for: ", constants.MachineName) } if !exists { return nil, errors.Errorf("Machine does not exist for api.Exists(%s)", constants.MachineName) } host, err := api.Load(constants.MachineName) if err != nil { return nil, errors.Wrapf(err, "Error loading api for: ", constants.MachineName) } return host, nil }
// targetHost returns a specific host name if one is indicated by the first CLI // arg, or the default host name if no host is specified. func targetHost(c CommandLine, api libmachine.API) (string, error) { if len(c.Args()) == 0 { defaultExists, err := api.Exists(defaultMachineName) if err != nil { return "", fmt.Errorf("Error checking if host %q exists: %s", defaultMachineName, err) } if defaultExists { return defaultMachineName, nil } return "", ErrNoDefault } return c.Args()[0], nil }
func runAction(actionName string, c CommandLine, api libmachine.API) error { var ( hostsToLoad []string ) // If user did not specify a machine name explicitly, use the 'default' // machine if it exists. This allows short form commands such as // 'docker-machine stop' for convenience. if len(c.Args()) == 0 { target, err := targetHost(c, api) if err != nil { return err } hostsToLoad = []string{target} } else { hostsToLoad = c.Args() } hosts, hostsInError := persist.LoadHosts(api, hostsToLoad) if len(hostsInError) > 0 { errs := []error{} for _, err := range hostsInError { errs = append(errs, err) } return consolidateErrs(errs) } if len(hosts) == 0 { return ErrHostLoad } if errs := runActionForeachMachine(actionName, hosts); len(errs) > 0 { return consolidateErrs(errs) } for _, h := range hosts { if err := api.Save(h); err != nil { return fmt.Errorf("Error saving host to store: %s", err) } } return nil }
func cmdRm(c CommandLine, api libmachine.API) error { if len(c.Args()) == 0 { c.ShowHelp() return ErrNoMachineSpecified } force := c.Bool("force") confirm := c.Bool("y") for _, hostName := range c.Args() { h, loaderr := api.Load(hostName) if loaderr != nil { // On --force, continue to remove on-disk files/dir if !force { return fmt.Errorf("Error removing host %q: %s", hostName, loaderr) } log.Errorf("Error removing host %q: %s. Continuing on `-f`, host instance may by running", hostName, loaderr) } if !confirm && !force { userinput, err := confirmInput(fmt.Sprintf("Do you really want to remove %q?", hostName)) if !userinput { return err } } if loaderr == nil { if err := h.Driver.Remove(); err != nil { if !force { log.Errorf("Provider error removing machine %q: %s", hostName, err) continue } } } if err := api.Remove(hostName); err != nil { log.Errorf("Error removing machine %q from store: %s", hostName, err) } else { log.Infof("Successfully removed %s", hostName) } } return nil }
func cmdURL(c CommandLine, api libmachine.API) error { if len(c.Args()) != 1 { return ErrExpectedOneMachine } host, err := api.Load(c.Args().First()) if err != nil { return err } url, err := host.URL() if err != nil { return err } fmt.Println(url) return nil }
func cmdStatus(c CommandLine, api libmachine.API) error { if len(c.Args()) != 1 { return ErrExpectedOneMachine } host, err := api.Load(c.Args().First()) if err != nil { return err } currentState, err := host.Driver.GetState() if err != nil { log.Errorf("error getting state for host %s: %s", host.Name, err) } log.Info(currentState) return nil }
// GetHostStatus gets the status of the host VM. func GetHostStatus(api libmachine.API) (string, error) { dne := "Does Not Exist" exists, err := api.Exists(constants.MachineName) if err != nil { return "", err } if !exists { return dne, nil } host, err := api.Load(constants.MachineName) if err != nil { return "", err } s, err := host.Driver.GetState() if s.String() == "" { return dne, err } return s.String(), err }
// Remove removes a Docker Machine func Remove(api libmachine.API, args map[string]string, form map[string][]string) (interface{}, error) { name, present := args["name"] if !present { return nil, errRequireMachineName } exist, _ := api.Exists(name) if !exist { return Success{"removed", name}, nil } currentHost, err := api.Load(name) if err != nil { return nil, err } if err := currentHost.Driver.Remove(); err != nil { return nil, err } if err := api.Remove(name); err != nil { return nil, err } return Success{"removed", name}, nil }
// StartHost starts a host VM. func StartHost(api libmachine.API, config MachineConfig) (*host.Host, error) { exists, err := api.Exists(constants.MachineName) if err != nil { return nil, errors.Wrapf(err, "Error checking if host exists: %s", constants.MachineName) } if !exists { return createHost(api, config) } glog.Infoln("Machine exists!") h, err := api.Load(constants.MachineName) if err != nil { return nil, errors.Wrap(err, "Error loading existing host. Please try running [minikube delete], then run [minikube start] again.") } s, err := h.Driver.GetState() glog.Infoln("Machine state: ", s) if err != nil { return nil, errors.Wrap(err, "Error getting state for host") } if s != state.Running { if err := h.Driver.Start(); err != nil { return nil, errors.Wrapf(err, "Error starting stopped host") } if err := api.Save(h); err != nil { return nil, errors.Wrapf(err, "Error saving started host") } } if err := h.ConfigureAuth(); err != nil { return nil, errors.Wrap(err, "Error configuring auth on host: %s") } return h, nil }
// StartHost starts a host VM. func StartHost(api libmachine.API, config MachineConfig) (*host.Host, error) { if exists, err := api.Exists(constants.MachineName); err != nil { return nil, fmt.Errorf("Error checking if host exists: %s", err) } else if exists { glog.Infoln("Machine exists!") h, err := api.Load(constants.MachineName) if err != nil { return nil, fmt.Errorf("Error loading existing host: %s", err) } s, err := h.Driver.GetState() if err != nil { return nil, fmt.Errorf("Error getting state for host: %s", err) } if s != state.Running { if err := h.Driver.Start(); err != nil { return nil, fmt.Errorf("Error starting stopped host: %s", err) } if err := api.Save(h); err != nil { return nil, fmt.Errorf("Error saving started host: %s", err) } } return h, nil } else { return createHost(api, config) } }
func cmdRm(c CommandLine, api libmachine.API) error { if len(c.Args()) == 0 { c.ShowHelp() return ErrNoMachineSpecified } log.Info(fmt.Sprintf("About to remove %s", strings.Join(c.Args(), ","))) force := c.Bool("force") confirm := c.Bool("y") var errorOccured string if !userConfirm(confirm, force) { return nil } for _, hostName := range c.Args() { err := removeRemoteMachine(hostName, api) if err != nil { errorOccured = fmt.Sprintf("Error removing host %q: %s", hostName, err) log.Errorf(errorOccured) } if err == nil || force { removeErr := api.Remove(hostName) if removeErr != nil { log.Errorf("Error removing machine %q from store: %s", hostName, removeErr) } else { log.Infof("Successfully removed %s", hostName) } } } if errorOccured != "" { return errors.New(errorOccured) } return nil }
func cmdURL(c CommandLine, api libmachine.API) error { if len(c.Args()) > 1 { return ErrExpectedOneMachine } target, err := targetHost(c, api) if err != nil { return err } host, err := api.Load(target) if err != nil { return err } url, err := host.URL() if err != nil { return err } fmt.Println(url) return nil }
// GetHostStatus gets the status of the host VM. func GetHostStatus(api libmachine.API) (string, error) { dne := "Does Not Exist" exists, err := api.Exists(constants.MachineName) if err != nil { return "", errors.Wrapf(err, "Error checking that api exists for: ", constants.MachineName) } if !exists { return dne, nil } host, err := api.Load(constants.MachineName) if err != nil { return "", errors.Wrapf(err, "Error loading api for: ", constants.MachineName) } s, err := host.Driver.GetState() if s.String() == "" { return dne, nil } if err != nil { return "", errors.Wrap(err, "Error getting host state") } return s.String(), nil }
func printVersion(c CommandLine, api libmachine.API, out io.Writer) error { if len(c.Args()) == 0 { c.ShowVersion() return nil } if len(c.Args()) != 1 { return ErrExpectedOneMachine } host, err := api.Load(c.Args().First()) if err != nil { return err } version, err := mcndockerclient.DockerVersion(host) if err != nil { return err } fmt.Fprintln(out, version) return nil }