forked from hashicorp/packer
/
step_shutdown_instance.go
45 lines (34 loc) · 1.25 KB
/
step_shutdown_instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cloudstack
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/xanzy/go-cloudstack/cloudstack"
)
type stepShutdownInstance struct{}
func (s *stepShutdownInstance) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*cloudstack.CloudStackClient)
config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui)
ui.Say("Shutting down instance...")
// Retrieve the instance ID from the previously saved state.
instanceID, ok := state.Get("instance_id").(string)
if !ok || instanceID == "" {
ui.Error("Could not retrieve instance_id from state!")
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 {
ui.Error(fmt.Sprintf("Error shutting down instance %s: %s", config.InstanceName, err))
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.
}