step_start_vm_paused: cleanup forces VM shutdown

This allows steps between create_instance and start_vm_paused to assume
that the VM is shut down when cleaning up
This commit is contained in:
Cheng Sun 2014-12-17 16:11:22 +00:00
parent b67b96c82a
commit f101aa8511
2 changed files with 20 additions and 1 deletions

View File

@ -196,7 +196,7 @@ func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
if self.instance != nil {
ui.Say("Destroying VM")
_ = self.instance.HardShutdown()
_ = self.instance.HardShutdown() // redundant, just in case
err := self.instance.Destroy()
if err != nil {
ui.Error(err.Error())

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
)
type stepStartVmPaused struct{}
@ -39,4 +40,22 @@ func (self *stepStartVmPaused) Run(state multistep.StateBag) multistep.StepActio
}
func (self *stepStartVmPaused) Cleanup(state multistep.StateBag) {
config := state.Get("config").(config)
client := state.Get("client").(XenAPIClient)
if config.ShouldKeepInstance(state) {
return
}
uuid := state.Get("instance_uuid").(string)
instance, err := client.GetVMByUuid(uuid)
if err != nil {
log.Printf(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return
}
err = instance.HardShutdown()
if err != nil {
log.Printf(fmt.Sprintf("Unable to force shutdown VM '%s': %s", uuid, err.Error()))
}
}