2016-01-11 06:22:41 -05:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-01-11 06:22:41 -05:00
|
|
|
"fmt"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2016-01-11 06:22:41 -05:00
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepShutdownInstance struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepShutdownInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-01-11 06:22:41 -05:00
|
|
|
client := state.Get("client").(*cloudstack.CloudStackClient)
|
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2016-01-11 06:22:41 -05:00
|
|
|
|
|
|
|
ui.Say("Shutting down instance...")
|
|
|
|
|
|
|
|
// Retrieve the instance ID from the previously saved state.
|
|
|
|
instanceID, ok := state.Get("instance_id").(string)
|
|
|
|
if !ok || instanceID == "" {
|
2017-07-06 17:31:13 -04:00
|
|
|
state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!"))
|
2016-01-11 06:22:41 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parameter struct.
|
|
|
|
p := client.VirtualMachine.NewStopVirtualMachineParams(instanceID)
|
|
|
|
|
|
|
|
// Shutdown the virtual machine.
|
|
|
|
_, err := client.VirtualMachine.StopVirtualMachine(p)
|
|
|
|
if err != nil {
|
2017-07-14 01:11:30 -04:00
|
|
|
err := fmt.Errorf("Error shutting down instance %s: %s", config.InstanceName, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
2016-01-11 06:22:41 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Instance has been shutdown!")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup any resources that may have been created during the Run phase.
|
|
|
|
func (s *stepShutdownInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
// Nothing to cleanup for this step.
|
|
|
|
}
|