2015-01-05 10:58:26 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-13 03:21:07 -05:00
|
|
|
"time"
|
|
|
|
|
2015-01-05 10:58:26 -06:00
|
|
|
"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"
|
2015-01-05 10:58:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2015-01-05 10:58:26 -06:00
|
|
|
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)
|
2015-01-05 10:58:26 -06:00
|
|
|
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")
|
2015-01-05 10:58:26 -06:00
|
|
|
|
|
|
|
// 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
|
2015-01-05 10:58:26 -06:00
|
|
|
},
|
|
|
|
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)
|
2015-01-05 10:58:26 -06:00
|
|
|
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)
|
2015-01-05 10:58:26 -06:00
|
|
|
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) {}
|