diff --git a/pkg/machine/applehv/machine.go b/pkg/machine/applehv/machine.go index 68b93ae76f..78bf01f4fc 100644 --- a/pkg/machine/applehv/machine.go +++ b/pkg/machine/applehv/machine.go @@ -360,22 +360,6 @@ func (m *MacMachine) collectFilesToDestroy(opts machine.RemoveOptions) []string return files } -// removeFilesAndConnections removes any files and connections associated with -// the machine during `Remove` -func (m *MacMachine) removeFilesAndConnections(files []string) { - for _, f := range files { - if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) { - logrus.Error(err) - } - } - if err := machine.RemoveConnections(m.Name); err != nil { - logrus.Error(err) - } - if err := machine.RemoveConnections(m.Name + "-root"); err != nil { - logrus.Error(err) - } -} - func (m *MacMachine) Remove(name string, opts machine.RemoveOptions) (string, func() error, error) { var ( files []string @@ -404,7 +388,7 @@ func (m *MacMachine) Remove(name string, opts machine.RemoveOptions) (string, fu confirmationMessage += "\n" return confirmationMessage, func() error { - m.removeFilesAndConnections(files) + machine.RemoveFilesAndConnections(files, m.Name, m.Name+"-root") // TODO We will need something like this for applehv too i think /* // Remove the HVSOCK for networking diff --git a/pkg/machine/connection.go b/pkg/machine/connection.go index 74db4ff50f..5633cd5cce 100644 --- a/pkg/machine/connection.go +++ b/pkg/machine/connection.go @@ -6,8 +6,10 @@ package machine import ( "errors" "fmt" + "os" "github.com/containers/common/pkg/config" + "github.com/sirupsen/logrus" ) const LocalhostIP = "127.0.0.1" @@ -89,3 +91,15 @@ func RemoveConnections(names ...string) error { } return cfg.Write() } + +// removeFilesAndConnections removes any files and connections with the given names +func RemoveFilesAndConnections(files []string, names ...string) { + for _, f := range files { + if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) { + logrus.Error(err) + } + } + if err := RemoveConnections(names...); err != nil { + logrus.Error(err) + } +} diff --git a/pkg/machine/hyperv/machine.go b/pkg/machine/hyperv/machine.go index 353b3a3502..185efd6561 100644 --- a/pkg/machine/hyperv/machine.go +++ b/pkg/machine/hyperv/machine.go @@ -328,19 +328,6 @@ func (m *HyperVMachine) collectFilesToDestroy(opts machine.RemoveOptions, diskPa return files } -// removeFilesAndConnections removes any files and connections associated with -// the machine during `Remove` -func (m *HyperVMachine) removeFilesAndConnections(files []string) { - for _, f := range files { - if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) { - logrus.Error(err) - } - } - if err := machine.RemoveConnections(m.Name, m.Name+"-root"); err != nil { - logrus.Error(err) - } -} - // removeNetworkAndReadySocketsFromRegistry removes the Network and Ready sockets // from the Windows Registry func (m *HyperVMachine) removeNetworkAndReadySocketsFromRegistry() { @@ -385,7 +372,7 @@ func (m *HyperVMachine) Remove(_ string, opts machine.RemoveOptions) (string, fu confirmationMessage += "\n" return confirmationMessage, func() error { - m.removeFilesAndConnections(files) + machine.RemoveFilesAndConnections(files, m.Name, m.Name+"-root") m.removeNetworkAndReadySocketsFromRegistry() return vm.Remove(diskPath) }, nil diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index 953b4ce302..9a3f2cf68f 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -1127,19 +1127,6 @@ func (v *MachineVM) removeQMPMonitorSocketAndVMPidFile() { } } -// removeFilesAndConnections removes any files and connections associated with -// the machine during `Remove` -func (v *MachineVM) removeFilesAndConnections(files []string) { - for _, f := range files { - if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) { - logrus.Error(err) - } - } - if err := machine.RemoveConnections(v.Name, v.Name+"-root"); err != nil { - logrus.Error(err) - } -} - // Remove deletes all the files associated with a machine including ssh keys, the image itself func (v *MachineVM) Remove(_ string, opts machine.RemoveOptions) (string, func() error, error) { var ( @@ -1181,7 +1168,7 @@ func (v *MachineVM) Remove(_ string, opts machine.RemoveOptions) (string, func() confirmationMessage += "\n" return confirmationMessage, func() error { - v.removeFilesAndConnections(files) + machine.RemoveFilesAndConnections(files, v.Name, v.Name+"-root") return nil }, nil }