Skip to content

Commit

Permalink
Merge pull request #5548 from blueelvis/5358-purge-flag-for-delete
Browse files Browse the repository at this point in the history
Added flags to purge configuration with minikube delete
  • Loading branch information
tstromberg authored Oct 21, 2019
2 parents 22c49c8 + 3ff4c44 commit cd9a652
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
42 changes: 33 additions & 9 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/docker/machine/libmachine/mcnerror"
"github.com/golang/glog"
ps "github.com/mitchellh/go-ps"
"github.com/mitchellh/go-ps"
"github.com/pkg/errors"

"github.com/docker/machine/libmachine"
Expand All @@ -43,6 +43,7 @@ import (
)

var deleteAll bool
var purge bool

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Expand Down Expand Up @@ -74,6 +75,16 @@ func (error DeletionError) Error() string {
return error.Err.Error()
}

func init() {
deleteCmd.Flags().BoolVar(&deleteAll, "all", false, "Set flag to delete all profiles")
deleteCmd.Flags().BoolVar(&purge, "purge", false, "Set this flag to delete the '.minikube' folder from your user directory.")

if err := viper.BindPFlags(deleteCmd.Flags()); err != nil {
exit.WithError("unable to bind flags", err)
}
RootCmd.AddCommand(deleteCmd)
}

// runDelete handles the executes the flow of "minikube delete"
func runDelete(cmd *cobra.Command, args []string) {
if len(args) > 0 {
Expand All @@ -84,14 +95,23 @@ func runDelete(cmd *cobra.Command, args []string) {
exit.WithError("Could not get profile flag", err)
}

validProfiles, invalidProfiles, err := pkg_config.ListProfiles()
profilesToDelete := append(validProfiles, invalidProfiles...)

// If the purge flag is set, go ahead and delete the .minikube directory.
if purge && len(profilesToDelete) > 1 && !deleteAll {
out.ErrT(out.Notice, "Multiple minikube profiles were found - ")
for _, p := range profilesToDelete {
out.T(out.Notice, " - {{.profile}}", out.V{"profile": p.Name})
}
exit.UsageT("Usage: minikube delete --all --purge")
}

if deleteAll {
if profileFlag != constants.DefaultMachineName {
exit.UsageT("usage: minikube delete --all")
}

validProfiles, invalidProfiles, err := pkg_config.ListProfiles()
profilesToDelete := append(validProfiles, invalidProfiles...)

if err != nil {
exit.WithError("Error getting profiles to delete", err)
}
Expand Down Expand Up @@ -120,6 +140,15 @@ func runDelete(cmd *cobra.Command, args []string) {
out.T(out.DeletingHost, "Successfully deleted profile \"{{.name}}\"", out.V{"name": profileName})
}
}

// If the purge flag is set, go ahead and delete the .minikube directory.
if purge {
glog.Infof("Purging the '.minikube' directory located at %s", localpath.MiniPath())
if err := os.RemoveAll(localpath.MiniPath()); err != nil {
exit.WithError("unable to delete minikube config folder", err)
}
out.T(out.Crushed, "Successfully purged minikube directory located at - [{{.minikubeDirectory}}]", out.V{"minikubeDirectory": localpath.MiniPath()})
}
}

// DeleteProfiles deletes one or more profiles
Expand Down Expand Up @@ -350,8 +379,3 @@ func killMountProcess() error {
}
return nil
}

func init() {
deleteCmd.Flags().BoolVar(&deleteAll, "all", false, "Set flag to delete all profiles")
RootCmd.AddCommand(deleteCmd)
}
7 changes: 4 additions & 3 deletions pkg/minikube/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,16 @@ func StopHost(api libmachine.API) error {

// DeleteHost deletes the host VM.
func DeleteHost(api libmachine.API) error {
name := cfg.GetMachineName()
host, err := api.Load(name)
host, err := api.Load(cfg.GetMachineName())
if err != nil {
return errors.Wrap(err, "load")
}

// Get the status of the host. Ensure that it exists before proceeding ahead.
status, err := GetHostStatus(api)
if err != nil {
// Warn, but proceed
out.WarningT("Unable to get the status of the {{.name}} cluster.", out.V{"name": name})
out.WarningT("Unable to get the status of the {{.name}} cluster.", out.V{"name": cfg.GetMachineName()})
}

if status == state.None.String() {
Expand All @@ -291,6 +291,7 @@ func DeleteHost(api libmachine.API) error {
if err := trySSHPowerOff(host); err != nil {
glog.Infof("Unable to power off minikube because the host was not found.")
}
out.T(out.DeletingHost, "Successfully powered off Hyper-V. minikube driver -- {{.driver}}", out.V{"driver": host.Driver.DriverName()})
}

out.T(out.DeletingHost, `Deleting "{{.profile_name}}" in {{.driver_name}} ...`, out.V{"profile_name": cfg.GetMachineName(), "driver_name": host.DriverName})
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/drivers/hyperv/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func init() {
registry.Register(registry.DriverDef{
_ = registry.Register(registry.DriverDef{
Name: constants.DriverHyperv,
Builtin: true,
ConfigCreator: createHypervHost,
Expand All @@ -45,7 +45,7 @@ func createHypervHost(config cfg.MachineConfig) interface{} {
d.VSwitch = config.HypervVirtualSwitch
d.MemSize = config.Memory
d.CPU = config.CPUs
d.DiskSize = int(config.DiskSize)
d.DiskSize = config.DiskSize
d.SSHUser = "docker"
d.DisableDynamicMemory = true // default to disable dynamic memory as minikube is unlikely to work properly with dynamic memory

Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/tunnel/route_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (router *osRouter) parseTable(table []byte) routingTable {
},
line: line,
}
glog.V(4).Infof("adding line %s", tableLine)
glog.V(4).Infof("adding line %v", tableLine)
t = append(t, tableLine)
}
}
Expand Down
24 changes: 24 additions & 0 deletions site/content/en/docs/Reference/Commands/delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ associated files.
minikube delete [flags]
```

##### Delete all profiles
```
minikube delete --all
```

##### Delete profile & `.minikube` directory
Do note that the following command only works if you have only 1 profile. If there are multiple profiles, the command will error out.
```
minikube delete --purge
```

##### Delete all profiles & `.minikube` directory
This will delete all the profiles and `.minikube` directory.
```
minikube delete --purge --all
```

### Flags

```
--all: Set flag to delete all profiles
--purge: Set this flag to delete the '.minikube' folder from your user directory.
```

### Options inherited from parent commands

```
Expand Down

0 comments on commit cd9a652

Please sign in to comment.