/
step_start_instance.go
64 lines (52 loc) · 1.78 KB
/
step_start_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package aliyun
import (
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/denverdino/aliyungo/ecs"
"fmt"
)
type stepStartInstance struct{}
func (s *stepStartInstance) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*ecs.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
instanceId := state.Get("instance_id").(string)
ui.Say("Starting the instance...")
err := client.StartInstance(instanceId)
if err != nil {
err := fmt.Errorf("Error starting instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say("Waiting for instance to become running status...")
err = waitForInstanceStatus(ecs.Running, instanceId, c.RegionId, client, c.StatusTimeout)
if err != nil {
err := fmt.Errorf("Error waiting for instance to become running: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Message("Instance has been started!")
return multistep.ActionContinue
}
func (s *stepStartInstance) Cleanup(state multistep.StateBag) {
client := state.Get("client").(*ecs.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
instanceId := state.Get("instance_id").(string)
// Stop the instance we just started
ui.Say("Stop instance...")
err := client.StopInstance(instanceId, true)
if err != nil {
ui.Error(fmt.Sprintf(
"Error stopping instance: %s", err))
} else {
err = waitForInstanceStatus(ecs.Stopped, instanceId, c.RegionId, client, c.StatusTimeout)
if err != nil {
ui.Error(fmt.Sprintf("Error waiting for instance to become stopped: %s", err))
} else {
ui.Message("Instance has been stopped!")
}
}
}