Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cleanup for step_import_instance #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions builder/xenserver/xva/step_import_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

type stepImportInstance struct {
instance *xsclient.VM
vdi *xsclient.VDI
}

func (self *stepImportInstance) Run(state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -60,7 +59,13 @@ func (self *stepImportInstance) Run(state multistep.StateBag) multistep.StepActi
}
state.Put("instance_uuid", instanceId)

instance.SetDescription(config.VMDescription)
self.instance, err = client.GetVMByUuid(instanceId)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", instanceId, err.Error()))
return multistep.ActionHalt
}

self.instance.SetDescription(config.VMDescription)
if err != nil {
ui.Error(fmt.Sprintf("Error setting VM description: %s", err.Error()))
return multistep.ActionHalt
Expand All @@ -72,29 +77,56 @@ func (self *stepImportInstance) Run(state multistep.StateBag) multistep.StepActi
}

func (self *stepImportInstance) Cleanup(state multistep.StateBag) {
/*
config := state.Get("config").(config)
if config.ShouldKeepVM(state) {
return
}
config := state.Get("config").(config)
if config.ShouldKeepVM(state) {
return
}

ui := state.Get("ui").(packer.Ui)
ui := state.Get("ui").(packer.Ui)

if self.instance != nil {
ui.Say("Destroying VM")
_ = self.instance.HardShutdown() // redundant, just in case
err := self.instance.Destroy()
if err != nil {
ui.Error(err.Error())
}
// We want to perform a 'xe vm-uninstall'
// Uninstall a VM. This operation will destroy those VDIs that are marked RW and connected to this VM only.
if self.instance != nil {
vbds, err := self.instance.GetVBDs()
if err != nil {
ui.Error(err.Error())
}

if self.vdi != nil {
ui.Say("Destroying VDI")
err := self.vdi.Destroy()
// Destroy VDIs before destroying VM, since vm.Destroy() also destroys VBDs,
// in which case we will be unable to find the VDIs
for _, vbd := range vbds {
vbd_rec, err := vbd.GetRecord()
if err != nil {
ui.Error(err.Error())
continue
}
if vbd_rec["mode"].(string) == "RW" {
vdi, err := vbd.GetVDI()
if err != nil {
ui.Error(err.Error())
continue
}
vdi_vbds, err := vdi.GetVBDs()
if err != nil {
ui.Error(err.Error())
continue
}
// If connected to this VM only
if len(vdi_vbds) <= 1 {
ui.Say("Destroying VDI")
err = vdi.Destroy()
if err != nil {
ui.Error(err.Error())
}
}
}
}
*/

ui.Say("Destroying VM")
_ = self.instance.HardShutdown()
err = self.instance.Destroy()
if err != nil {
ui.Error(err.Error())
}
}
}