Merge pull request #3665 from tstromberg/sudo-poweroff

Run poweroff before delete, only call uninstall if driver is None
pull/3661/head^2
Thomas Strömberg 2019-02-14 10:30:31 -08:00 committed by GitHub
commit 10d8487417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 11 deletions

View File

@ -54,10 +54,13 @@ associated files.`,
cc, err := pkg_config.Load() cc, err := pkg_config.Load()
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
console.ErrLn("Error loading profile config: %v", err) console.ErrLn("Error loading profile config: %v", err)
} else if err == nil { }
// In the case of "none", we want to uninstall Kubernetes as there is no VM to delete
if err == nil && cc.MachineConfig.VMDriver == "none" {
kc := cc.KubernetesConfig kc := cc.KubernetesConfig
bsName := viper.GetString(cmdcfg.Bootstrapper) // Name ? bsName := viper.GetString(cmdcfg.Bootstrapper)
console.OutStyle("resetting", "Reverting Kubernetes %s using %s ...", kc.KubernetesVersion, bsName) console.OutStyle("resetting", "Uninstalling Kubernetes %s using %s ...", kc.KubernetesVersion, bsName)
clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper))
if err == nil { if err == nil {
if err = clusterBootstrapper.DeleteCluster(kc); err != nil { if err = clusterBootstrapper.DeleteCluster(kc); err != nil {
@ -66,7 +69,6 @@ associated files.`,
} }
} }
console.OutStyle("deleting-vm", "Deleting %q Kubernetes VM ...", profile)
if err = cluster.DeleteHost(api); err != nil { if err = cluster.DeleteHost(api); err != nil {
switch err := errors.Cause(err).(type) { switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist: case mcnerror.ErrHostDoesNotExist:
@ -75,8 +77,6 @@ associated files.`,
console.Fatal("Failed to delete VM: %v", err) console.Fatal("Failed to delete VM: %v", err)
os.Exit(1) os.Exit(1)
} }
} else {
console.OutStyle("crushed", "VM deleted.")
} }
if err := cmdUtil.KillMountProcess(); err != nil { if err := cmdUtil.KillMountProcess(); err != nil {
@ -91,7 +91,7 @@ associated files.`,
console.Fatal("Failed to remove profile: %v", err) console.Fatal("Failed to remove profile: %v", err)
os.Exit(1) os.Exit(1)
} }
console.Success("Removed %q profile!", profile) console.OutStyle("crushed", "The %q cluster is now deleted. I hope you are happy.", profile)
}, },
} }

View File

@ -40,8 +40,6 @@ var stopCmd = &cobra.Command{
itself, leaving all files intact. The cluster can be started again with the "start" command.`, itself, leaving all files intact. The cluster can be started again with the "start" command.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
profile := viper.GetString(pkg_config.MachineProfile) profile := viper.GetString(pkg_config.MachineProfile)
console.OutStyle("stopping", "Stopping %q Kubernetes cluster...", profile)
api, err := machine.NewAPIClient() api, err := machine.NewAPIClient()
if err != nil { if err != nil {
console.Fatal("Error getting client: %v", err) console.Fatal("Error getting client: %v", err)

View File

@ -125,12 +125,34 @@ func StartHost(api libmachine.API, config cfg.MachineConfig) (*host.Host, error)
return h, nil return h, nil
} }
// StopHost stops the host VM. // tryPowerOff runs the poweroff command on the guest VM to speed up deletion
func tryPowerOff(h *host.Host) {
if h.Driver.DriverName() == "none" {
return
}
s, err := h.Driver.GetState()
if err != nil {
glog.Warningf("unable to get state: %v", err)
return
}
if s != state.Running {
glog.Infof("host is in state %s", s)
return
}
console.OutStyle("shutdown", "Powering off %q via SSH ...", cfg.GetMachineName())
out, err := h.RunSSHCommand("sudo poweroff")
// poweroff always results in an error, since the host disconnects.
glog.Infof("poweroff result: out=%s, err=%v", out, err)
}
// StopHost stops the host VM, saving state to disk.
func StopHost(api libmachine.API) error { func StopHost(api libmachine.API) error {
host, err := api.Load(cfg.GetMachineName()) host, err := api.Load(cfg.GetMachineName())
if err != nil { if err != nil {
return errors.Wrapf(err, "load") return errors.Wrapf(err, "load")
} }
console.OutStyle("stopping", "Stopping %q in %s ...", cfg.GetMachineName(), host.DriverName)
if err := host.Stop(); err != nil { if err := host.Stop(); err != nil {
alreadyInStateError, ok := err.(mcnerror.ErrHostAlreadyInState) alreadyInStateError, ok := err.(mcnerror.ErrHostAlreadyInState)
if ok && alreadyInStateError.State == state.Stopped { if ok && alreadyInStateError.State == state.Stopped {
@ -147,6 +169,8 @@ func DeleteHost(api libmachine.API) error {
if err != nil { if err != nil {
return errors.Wrap(err, "load") return errors.Wrap(err, "load")
} }
tryPowerOff(host)
console.OutStyle("deleting-host", "Deleting %q from %s ...", cfg.GetMachineName(), host.DriverName)
if err := host.Driver.Remove(); err != nil { if err := host.Driver.Remove(); err != nil {
return errors.Wrap(err, "host remove") return errors.Wrap(err, "host remove")
} }

View File

@ -58,7 +58,7 @@ var styles = map[string]style{
"starting-vm": {Prefix: "🔥 "}, "starting-vm": {Prefix: "🔥 "},
"starting-none": {Prefix: "🤹 "}, "starting-none": {Prefix: "🤹 "},
"resetting": {Prefix: "🔄 "}, "resetting": {Prefix: "🔄 "},
"deleting-vm": {Prefix: "🔥 "}, "deleting-host": {Prefix: "🔥 "},
"copying": {Prefix: "✨ "}, "copying": {Prefix: "✨ "},
"connectivity": {Prefix: "📶 "}, "connectivity": {Prefix: "📶 "},
"internet": {Prefix: "🌐 "}, "internet": {Prefix: "🌐 "},
@ -70,6 +70,7 @@ var styles = map[string]style{
"containerd": {Prefix: "📦 "}, "containerd": {Prefix: "📦 "},
"permissions": {Prefix: "🔑 "}, "permissions": {Prefix: "🔑 "},
"enabling": {Prefix: "🔌 "}, "enabling": {Prefix: "🔌 "},
"shutdown": {Prefix: "🛑 "},
"pulling": {Prefix: "🚜 "}, "pulling": {Prefix: "🚜 "},
"verifying": {Prefix: "🤔 "}, "verifying": {Prefix: "🤔 "},
"verifying-noline": {Prefix: "🤔 ", OmitNewline: true}, "verifying-noline": {Prefix: "🤔 ", OmitNewline: true},