packer-plugin-xenserver/builder/xenserver/common/step_shutdown.go

82 lines
2.1 KiB
Go
Raw Normal View History

package common
import (
"fmt"
2020-09-13 03:21:07 -05:00
"time"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
2020-09-13 03:21:07 -05:00
xenapi "github.com/terra-farm/go-xen-api-client"
)
type StepShutdown struct{}
func (StepShutdown) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
2020-09-13 03:21:07 -05:00
c := state.Get("client").(*Connection)
instance_uuid := state.Get("instance_uuid").(string)
2020-09-13 03:21:07 -05:00
instance, err := c.client.VM.GetByUUID(c.session, instance_uuid)
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM with UUID '%s': %s", instance_uuid, err.Error()))
return multistep.ActionHalt
}
2015-01-06 07:49:10 -06:00
ui.Say("Step: Shutting down VM")
// Shutdown the VM
success := func() bool {
if config.ShutdownCommand != "" {
ui.Message("Executing shutdown command...")
_, err := ExecuteGuestSSHCmd(state, config.ShutdownCommand)
if err != nil {
ui.Error(fmt.Sprintf("Shutdown command failed: %s", err.Error()))
return false
}
ui.Message("Waiting for VM to enter Halted state...")
err = InterruptibleWait{
Predicate: func() (bool, error) {
2020-09-13 03:21:07 -05:00
power_state, err := c.client.VM.GetPowerState(c.session, instance)
return power_state == xenapi.VMPowerStateHalted, err
},
PredicateInterval: 5 * time.Second,
Timeout: 300 * time.Second,
}.Wait(state)
if err != nil {
ui.Error(fmt.Sprintf("Error waiting for VM to halt: %s", err.Error()))
return false
}
} else {
ui.Message("Attempting to cleanly shutdown the VM...")
2020-09-13 03:21:07 -05:00
err = c.client.VM.CleanShutdown(c.session, instance)
if err != nil {
ui.Error(fmt.Sprintf("Could not shut down VM: %s", err.Error()))
return false
}
}
return true
}()
if !success {
ui.Say("WARNING: Forcing hard shutdown of the VM...")
2020-09-13 03:21:07 -05:00
err = c.client.VM.HardShutdown(c.session, instance)
if err != nil {
ui.Error(fmt.Sprintf("Could not hard shut down VM -- giving up: %s", err.Error()))
return multistep.ActionHalt
}
}
ui.Message("Successfully shut down VM")
return multistep.ActionContinue
}
func (StepShutdown) Cleanup(state multistep.StateBag) {}