Skip to content

Commit

Permalink
rewrote Cleanup process (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxtof authored Feb 14, 2023
1 parent 193f7ef commit 54e2cdc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 55 deletions.
3 changes: 0 additions & 3 deletions builder/nutanix/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
&stepCopyImage{
Config: &b.config,
},
&stepDestroyVM{
Config: &b.config,
},
}

b.runner = &multistep.BasicRunner{Steps: steps}
Expand Down
43 changes: 33 additions & 10 deletions builder/nutanix/step_build_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (s *stepBuildVM) Run(ctx context.Context, state multistep.StateBag) multist
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say("CD disk uploaded")
state.Put("imageUUID", *cdfilesImage.image.Metadata.UUID)
ui.Message("CD disk uploaded " + *cdfilesImage.image.Spec.Name)
state.Put("cd_uuid", *cdfilesImage.image.Metadata.UUID)
temp_cd := VmDisk{
ImageType: "ISO_IMAGE",
SourceImageUUID: *cdfilesImage.image.Metadata.UUID,
Expand All @@ -59,29 +59,52 @@ func (s *stepBuildVM) Run(ctx context.Context, state multistep.StateBag) multist
return multistep.ActionHalt
}

ui.Message(fmt.Sprintf("virtual machine %s created", config.VMName))
ui.Message(fmt.Sprintf("Virtual machine %s created", config.VMName))
log.Printf("Nutanix VM UUID: %s", *vmInstance.nutanix.Metadata.UUID)
state.Put("vmUUID", *vmInstance.nutanix.Metadata.UUID)
state.Put("vm_uuid", *vmInstance.nutanix.Metadata.UUID)
state.Put("ip", vmInstance.Addresses()[0])
state.Put("destroy_vm", true)
ui.Message("Found IP for virtual machine: " + vmInstance.Addresses()[0])

return multistep.ActionContinue
}

// Cleanup will tear down the VM once the build is complete
func (s *stepBuildVM) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
vmUUID := state.Get("vm_uuid")
if vmUUID == nil {
return
}

_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)

d := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)

if vmUUID, ok := state.GetOk("vmUUID"); ok {
if vmUUID != "" {
ui.Say("Cleaning up Nutanix VM.")
if cancelled || halted {
ui.Say("Task cancelled, virtual machine is not deleted")
return
}

ui.Say("Deleting virtual machine...")

if cdUUID, ok := state.GetOk("cd_uuid"); ok {
err := d.DeleteImage(cdUUID.(string))
if err != nil {
ui.Error("An error occurred while deleting CD disk")
return
} else {
ui.Message("CD disk successfully deleted")
}
}

err := d.Delete(vmUUID.(string))
if err != nil {
ui.Error("An error occurred while deleting the Virtual machine")
return
} else {
ui.Message("Virtual machine successfully deleted")
}

}
2 changes: 1 addition & 1 deletion builder/nutanix/step_copy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type stepCopyImage struct {

func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vmUUID := state.Get("vmUUID").(string)
vmUUID := state.Get("vm_uuid").(string)
d := state.Get("driver").(Driver)
vm, _ := d.GetVM(vmUUID)

Expand Down
40 changes: 0 additions & 40 deletions builder/nutanix/step_destroy_vm.go

This file was deleted.

2 changes: 1 addition & 1 deletion builder/nutanix/step_shutdown_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multis
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)
config := state.Get("config").(*Config)
vmUUID := state.Get("vmUUID").(string)
vmUUID := state.Get("vm_uuid").(string)

if config.CommConfig.Type == "none" {
ui.Say("No Communicator configured, halting the virtual machine...")
Expand Down

0 comments on commit 54e2cdc

Please sign in to comment.