Skip to content

Commit 5e20d32

Browse files
committed
Merge branch 'rm-test' into 'main'
bug: Fix rm on linux Closes #29 See merge request bootc-org/podman-bootc-cli!64
2 parents 5655cbb + 6f6426d commit 5e20d32

File tree

7 files changed

+21
-65
lines changed

7 files changed

+21
-65
lines changed

cmd/rm.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ func prune(id string) error {
6767

6868
defer bootcVM.CloseConnection()
6969

70-
vmExists, err := bootcVM.Exists()
71-
if err != nil {
72-
return fmt.Errorf("unable to check if VM exists: %v", err)
73-
}
74-
75-
if !vmExists {
76-
logrus.Debugf("VM %s is not running", id)
77-
return nil
78-
}
79-
8070
if force {
8171
err := forceKillVM(bootcVM)
8272
if err != nil {
@@ -136,7 +126,7 @@ func killVM(bootcVM vm.BootcVM) (err error) {
136126
}
137127

138128
func forceKillVM(bootcVM vm.BootcVM) (err error) {
139-
err = bootcVM.ForceDelete()
129+
err = bootcVM.Delete()
140130
if err != nil {
141131
return
142132
}

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func doRun(flags *cobra.Command, args []string) error {
184184

185185
// Always remove when executing a command
186186
if vmConfig.RemoveVm || len(cmd) > 0 {
187-
err = bootcVM.ForceDelete() //delete the VM, but keep the disk image
187+
err = bootcVM.Delete() //delete the VM, but keep the disk image
188188
if err != nil {
189189
return fmt.Errorf("unable to remove VM from cache: %w", err)
190190
}

cmd/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ func doStop(_ *cobra.Command, args []string) (err error) {
3636
return err
3737
}
3838
defer bootcVM.CloseConnection()
39-
return bootcVM.ForceDelete()
39+
return bootcVM.Delete()
4040
}

pkg/vm/vm.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"golang.org/x/crypto/ssh"
2121
)
2222

23-
// getVMCachePath returns the path to the VM cache directory
24-
func getVMCachePath(imageId string, user user.User) (path string, err error) {
23+
// GetVMCachePath returns the path to the VM cache directory
24+
func GetVMCachePath(imageId string, user user.User) (path string, err error) {
2525
files, err := os.ReadDir(user.CacheDir())
2626
if err != nil {
2727
return "", err
@@ -61,8 +61,6 @@ type RunVMParameters struct {
6161

6262
type BootcVM interface {
6363
Run(RunVMParameters) error
64-
ForceDelete() error
65-
Shutdown() error
6664
Delete() error
6765
IsRunning() (bool, error)
6866
WriteConfig(bootc.BootcDisk) error

pkg/vm/vm_darwin.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewVM(params NewVMParameters) (vm *BootcVMMac, err error) {
2525
return nil, fmt.Errorf("image ID is required")
2626
}
2727

28-
cacheDir, err := getVMCachePath(params.ImageID, params.User)
28+
cacheDir, err := GetVMCachePath(params.ImageID, params.User)
2929
if err != nil {
3030
return nil, fmt.Errorf("unable to get VM cache path: %w", err)
3131
}
@@ -163,48 +163,26 @@ func (b *BootcVMMac) Run(params RunVMParameters) (err error) {
163163
func (b *BootcVMMac) Delete() error {
164164
logrus.Debugf("Deleting Mac VM %s", b.cacheDir)
165165

166-
pid, err := utils.ReadPidFile(b.pidFile)
167-
if err != nil {
168-
return fmt.Errorf("reading pid file: %w", err)
169-
}
170-
171-
process, err := os.FindProcess(pid)
172-
if err != nil {
173-
return fmt.Errorf("process not found while attempting to delete VM: %w", err)
174-
}
175-
176-
return process.Signal(os.Interrupt)
177-
}
178-
179-
func (b *BootcVMMac) Shutdown() error {
180-
b.SetUser("root") //TODO the stop command should accept a user parameter
181-
182166
isRunning, err := b.IsRunning()
183167
if err != nil {
184-
return fmt.Errorf("unable to determine if VM is running: %w", err)
168+
return fmt.Errorf("checking if VM is running: %w", err)
185169
}
186170

187-
if isRunning {
188-
poweroff := []string{"poweroff"}
189-
return b.RunSSH(poweroff)
190-
} else {
191-
logrus.Infof("Unable to shutdown VM. It is not not running.")
171+
if !isRunning {
192172
return nil
193173
}
194-
}
195174

196-
func (b *BootcVMMac) ForceDelete() error {
197-
err := b.Shutdown()
175+
pid, err := utils.ReadPidFile(b.pidFile)
198176
if err != nil {
199-
return fmt.Errorf("unable to shutdown VM: %w", err)
177+
return fmt.Errorf("reading pid file: %w", err)
200178
}
201179

202-
err = b.Delete()
180+
process, err := os.FindProcess(pid)
203181
if err != nil {
204-
return fmt.Errorf("unable to delete VM: %w", err)
182+
return fmt.Errorf("process not found while attempting to delete VM: %w", err)
205183
}
206184

207-
return nil
185+
return process.Signal(os.Interrupt)
208186
}
209187

210188
func (b *BootcVMMac) IsRunning() (bool, error) {

pkg/vm/vm_linux.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewVM(params NewVMParameters) (vm *BootcVMLinux, err error) {
3939
return nil, fmt.Errorf("libvirt URI is required")
4040
}
4141

42-
cacheDir, err := getVMCachePath(params.ImageID, params.User)
42+
cacheDir, err := GetVMCachePath(params.ImageID, params.User)
4343
if err != nil {
4444
return nil, fmt.Errorf("unable to get VM cache path: %w", err)
4545
}
@@ -275,6 +275,11 @@ func (v *BootcVMLinux) loadExistingDomain() (err error) {
275275

276276
// Delete the VM definition
277277
func (v *BootcVMLinux) Delete() (err error) {
278+
err = v.Shutdown()
279+
if err != nil {
280+
return fmt.Errorf("unable to shutdown VM: %w", err)
281+
}
282+
278283
domainExists, err := v.Exists()
279284
if err != nil {
280285
return fmt.Errorf("unable to check if VM exists: %w", err)
@@ -312,21 +317,6 @@ func (v *BootcVMLinux) Shutdown() (err error) {
312317
return
313318
}
314319

315-
// ForceDelete stops and removes the VM
316-
func (v *BootcVMLinux) ForceDelete() (err error) {
317-
err = v.Shutdown()
318-
if err != nil {
319-
return fmt.Errorf("unable to shutdown VM: %w", err)
320-
}
321-
322-
err = v.Delete()
323-
if err != nil {
324-
return fmt.Errorf("unable to remove VM: %w", err)
325-
}
326-
327-
return
328-
}
329-
330320
func (v *BootcVMLinux) Exists() (bool, error) {
331321
var flags libvirt.ConnectListAllDomainsFlags
332322
domains, err := v.libvirtConnection.ListAllDomains(flags)

pkg/vm/vm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ var _ = Describe("VM", func() {
181181
})
182182

183183
Context("is running", func() {
184-
It("should remove the VM from the hypervisor after calling ForceDelete", func() {
184+
It("should remove the VM from the hypervisor after calling Delete", func() {
185185
//create vm and start it
186186
bootcVM := createTestVM(testImageID)
187187
runTestVM(bootcVM)
@@ -192,7 +192,7 @@ var _ = Describe("VM", func() {
192192
Expect(exists).To(BeTrue())
193193

194194
//attempt to stop and delete the VM
195-
err = bootcVM.ForceDelete()
195+
err = bootcVM.Delete()
196196
Expect(err).To(Not(HaveOccurred()))
197197

198198
//assert that the VM is stopped and deleted

0 commit comments

Comments
 (0)