Add proper cleanup of chroot disk

Closes #3
This commit is contained in:
Miłosz Smółka 2019-02-03 00:24:06 +01:00
parent 5e8eade205
commit f7de056f23
2 changed files with 27 additions and 26 deletions

View File

@ -75,17 +75,13 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
return multistep.ActionHalt return multistep.ActionHalt
} }
var diskIDs []string
for _, hdd := range hdds { for _, hdd := range hdds {
if hdd.Disk.Name == chrootDiskName { if hdd.Disk.Name == chrootDiskName {
state.Put("chroot_disk_id", hdd.Disk.Id) state.Put("chroot_disk_id", hdd.Disk.Id)
} else { break
diskIDs = append(diskIDs, hdd.Disk.Id)
} }
} }
state.Put("disk_ids", diskIDs)
netadp, _, err := client.VmApi.VmListNetadp(ctx, vm.Id) netadp, _, err := client.VmApi.VmListNetadp(ctx, vm.Id)
if err != nil { if err != nil {
err := fmt.Errorf("error listing netadp: %s", formatOpenAPIError(err)) err := fmt.Errorf("error listing netadp: %s", formatOpenAPIError(err))
@ -169,19 +165,31 @@ func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
return return
} }
client := state.Get("client").(*openapi.APIClient)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting VM...") ui.Say(fmt.Sprintf("Deleting VM %s...", s.vmID))
err := deleteVMWithDisks(s.vmID, state)
if err != nil {
ui.Error(err.Error())
}
}
func deleteVMWithDisks(vmID string, state multistep.StateBag) error {
client := state.Get("client").(*openapi.APIClient)
hdds, _, err := client.VmApi.VmListHdd(context.TODO(), vmID)
if err != nil {
return fmt.Errorf("error listing hdd: %s", formatOpenAPIError(err))
}
deleteOptions := openapi.VmDelete{} deleteOptions := openapi.VmDelete{}
diskIDs, ok := state.Get("disk_ids").([]string) for _, hdd := range hdds {
if ok && len(diskIDs) > 0 { deleteOptions.RemoveDisks = append(deleteOptions.RemoveDisks, hdd.Disk.Id)
deleteOptions.RemoveDisks = diskIDs
} }
_, err := client.VmApi.VmDelete(context.TODO(), s.vmID, deleteOptions) _, err = client.VmApi.VmDelete(context.TODO(), vmID, deleteOptions)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error deleting server '%s' - please delete it manually: %s", s.vmID, formatOpenAPIError(err))) return fmt.Errorf("Error deleting server '%s' - please delete it manually: %s", vmID, formatOpenAPIError(err))
} }
return nil
} }

View File

@ -53,18 +53,11 @@ func (s *stepCreateVMFromDisk) Cleanup(state multistep.StateBag) {
return return
} }
client := state.Get("client").(*openapi.APIClient)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
chrootDiskID := state.Get("chroot_disk_id").(string)
ui.Say("Deleting VM (from disk)...") ui.Say(fmt.Sprintf("Deleting VM %s (from chroot disk)...", s.vmID))
err := deleteVMWithDisks(s.vmID, state)
deleteOptions := openapi.VmDelete{
RemoveDisks: []string{chrootDiskID},
}
_, err := client.VmApi.VmDelete(context.TODO(), s.vmID, deleteOptions)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error deleting server '%s' - please delete it manually: %s", s.vmID, formatOpenAPIError(err))) ui.Error(err.Error())
} }
} }