forked from askholme/packer-builder-vultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_halt.go
60 lines (52 loc) · 1.49 KB
/
step_halt.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
package main
import (
"fmt"
"log"
"github.com/DarkDNA/vultr"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type stepHalt struct{}
func (s *stepHalt) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*vultr.Client)
c := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui)
serverId := state.Get("server_id").(string)
serverInfo, err := client.GetServer(serverId)
if err != nil {
err := fmt.Errorf("Error checking server state: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if serverInfo.Status != "active" {
err := fmt.Errorf("Error server is not active, status is: ", serverInfo.Status)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if serverInfo.Power != "running" {
// Already off, don't do anything
return multistep.ActionContinue
}
// Pull the plug on the Server
ui.Say("Forcefully shutting down Server...")
err = client.HaltServer(serverId)
if err != nil {
err := fmt.Errorf("Error powering off server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Println("Waiting for halt to complete...")
_, err = waitForServerState("active", "stopped", serverId, client, c.stateTimeout)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *stepHalt) Cleanup(state multistep.StateBag) {
// no cleanup
}