move removeFilesAndConnections to a common file

Moves `removeFilesAndConnections` to the common file
`pkg/machine/connections.go` to be reused by multiple hypervisors.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
This commit is contained in:
Jake Correnti
2023-08-01 18:37:42 -04:00
parent 75a8f13c4a
commit 98cf8462ad
4 changed files with 17 additions and 45 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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

View File

@ -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
}