Skip to content

Commit

Permalink
Merge pull request #5106 from tstromberg/just-delete
Browse files Browse the repository at this point in the history
delete: Clean up machine directory if DeleteHost fails to
  • Loading branch information
tstromberg authored Aug 16, 2019
2 parents a176cc9 + 9a8275b commit 96fcb91
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
27 changes: 19 additions & 8 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,46 @@ func runDelete(cmd *cobra.Command, args []string) {

cc, err := pkg_config.Load()
if err != nil && !os.IsNotExist(err) {
out.ErrT(out.Sad, "Error loading profile config: {{.error}}", out.V{"name": profile})
out.ErrT(out.Sad, "Error loading profile {{.name}}: {{.error}}", out.V{"name": profile, "error": err})
}

// In the case of "none", we want to uninstall Kubernetes as there is no VM to delete
if err == nil && cc.MachineConfig.VMDriver == constants.DriverNone {
uninstallKubernetes(api, cc.KubernetesConfig, viper.GetString(cmdcfg.Bootstrapper))
}

if err := killMountProcess(); err != nil {
out.T(out.FailureType, "Failed to kill mount process: {{.error}}", out.V{"error": err})
}

if err = cluster.DeleteHost(api); err != nil {
switch err := errors.Cause(err).(type) {
switch errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
out.T(out.Meh, `"{{.name}}" cluster does not exist`, out.V{"name": profile})
default:
exit.WithError("Failed to delete cluster", err)
out.T(out.FailureType, "Failed to delete cluster: {{.error}}", out.V{"error": err})
out.T(out.Notice, `You may need to manually remove the "{{.name}}" VM from your hypervisor`, out.V{"name": profile})
}
}

if err := killMountProcess(); err != nil {
out.FatalT("Failed to kill mount process: {{.error}}", out.V{"error": err})
// In case DeleteHost didn't complete the job.
machineDir := filepath.Join(constants.GetMinipath(), "machines", profile)
if _, err := os.Stat(machineDir); err == nil {
out.T(out.DeletingHost, `Removing {{.directory}} ...`, out.V{"directory": machineDir})
err := os.RemoveAll(machineDir)
if err != nil {
exit.WithError("Unable to remove machine directory: %v", err)
}
}

if err := pkg_config.DeleteProfile(viper.GetString(pkg_config.MachineProfile)); err != nil {
if err := pkg_config.DeleteProfile(profile); err != nil {
if os.IsNotExist(err) {
out.T(out.Meh, `"{{.profile_name}}" profile does not exist`, out.V{"profile_name": profile})
out.T(out.Meh, `"{{.name}}" profile does not exist`, out.V{"name": profile})
os.Exit(0)
}
exit.WithError("Failed to remove profile", err)
}
out.T(out.Crushed, `The "{{.cluster_name}}" cluster has been deleted.`, out.V{"cluster_name": profile})
out.T(out.Crushed, `The "{{.name}}" cluster has been deleted.`, out.V{"name": profile})

machineName := pkg_config.GetMachineName()
if err := kubeconfig.DeleteContext(constants.KubeconfigPath, machineName); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/machine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (api *LocalClient) NewHost(driverName string, rawDriver []byte) (*host.Host
func (api *LocalClient) Load(name string) (*host.Host, error) {
h, err := api.Filestore.Load(name)
if err != nil {
return nil, errors.Wrap(err, "filestore")
return nil, errors.Wrapf(err, "filestore %q", name)
}

var def registry.DriverDef
Expand Down

0 comments on commit 96fcb91

Please sign in to comment.