Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

machine: qemu only remove connection after confirmation #18359

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions cmd/podman/machine/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"strings"

"github.com/containers/common/pkg/config"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/machine"
Expand Down Expand Up @@ -91,24 +90,5 @@ func rm(_ *cobra.Command, args []string) error {
return err
}
newMachineEvent(events.Remove, events.Event{Name: vmName})
err = updateDefaultMachineInConfig(vmName)
if err != nil {
return fmt.Errorf("failed to update default machine: %v", err)
}
return nil
}

func updateDefaultMachineInConfig(vmName string) error {
cfg, err := config.ReadCustomConfig()
if err != nil {
return err
}
if cfg.Engine.ActiveService == vmName {
cfg.Engine.ActiveService = ""
for machine := range cfg.Engine.ServiceDestinations {
cfg.Engine.ActiveService = machine
break
}
}
return cfg.Write()
}
20 changes: 15 additions & 5 deletions pkg/machine/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ func ChangeDefault(name string) error {
return cfg.Write()
}

func RemoveConnection(name string) error {
func RemoveConnections(names ...string) error {
cfg, err := config.ReadCustomConfig()
if err != nil {
return err
}
if _, ok := cfg.Engine.ServiceDestinations[name]; ok {
delete(cfg.Engine.ServiceDestinations, name)
} else {
return fmt.Errorf("unable to find connection named %q", name)
for _, name := range names {
if _, ok := cfg.Engine.ServiceDestinations[name]; ok {
delete(cfg.Engine.ServiceDestinations, name)
} else {
return fmt.Errorf("unable to find connection named %q", name)
}

if cfg.Engine.ActiveService == name {
cfg.Engine.ActiveService = ""
for service := range cfg.Engine.ServiceDestinations {
cfg.Engine.ActiveService = service
break
}
}
}
return cfg.Write()
}
5 changes: 1 addition & 4 deletions pkg/machine/hyperv/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,7 @@ func (m *HyperVMachine) Remove(_ string, opts machine.RemoveOptions) (string, fu
logrus.Error(err)
}
}
if err := machine.RemoveConnection(m.Name); err != nil {
logrus.Error(err)
}
if err := machine.RemoveConnection(m.Name + "-root"); err != nil {
if err := machine.RemoveConnections(m.Name, m.Name+"-root"); err != nil {
logrus.Error(err)
}

Expand Down
10 changes: 3 additions & 7 deletions pkg/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,13 +964,6 @@ func (v *MachineVM) Remove(_ string, opts machine.RemoveOptions) (string, func()
files = append(files, socketPath.Path)
files = append(files, v.archRemovalFiles()...)

if err := machine.RemoveConnection(v.Name); err != nil {
logrus.Error(err)
}
if err := machine.RemoveConnection(v.Name + "-root"); err != nil {
logrus.Error(err)
}

vmConfigDir, err := machine.GetConfDir(vmtype)
if err != nil {
return "", nil, err
Expand Down Expand Up @@ -1001,6 +994,9 @@ func (v *MachineVM) Remove(_ string, opts machine.RemoveOptions) (string, func()
logrus.Error(err)
}
}
if err := machine.RemoveConnections(v.Name, v.Name+"-root"); err != nil {
logrus.Error(err)
}
return nil
}, nil
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/machine/wsl/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1368,10 +1368,7 @@ func (v *MachineVM) Remove(name string, opts machine.RemoveOptions) (string, fun

confirmationMessage += "\n"
return confirmationMessage, func() error {
if err := machine.RemoveConnection(v.Name); err != nil {
logrus.Error(err)
}
if err := machine.RemoveConnection(v.Name + "-root"); err != nil {
if err := machine.RemoveConnections(v.Name, v.Name+"-root"); err != nil {
logrus.Error(err)
}
if err := runCmdPassThrough("wsl", "--unregister", toDist(v.Name)); err != nil {
Expand Down