forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_wait_for_install_to_complete.go
90 lines (69 loc) · 1.91 KB
/
step_wait_for_install_to_complete.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package common
import (
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"time"
)
const (
SleepSeconds = 10
)
type StepWaitForPowerOff struct {
}
func (s *StepWaitForPowerOff) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
ui.Say("Waiting for vm to be powered down...")
for {
isOff, err := driver.IsOff(vmName)
if err != nil {
err := fmt.Errorf("Error checking if vm is off: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if isOff {
break
} else {
time.Sleep(time.Second * SleepSeconds)
}
}
return multistep.ActionContinue
}
func (s *StepWaitForPowerOff) Cleanup(state multistep.StateBag) {
}
type StepWaitForInstallToComplete struct {
ExpectedRebootCount uint
ActionName string
}
func (s *StepWaitForInstallToComplete) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
if len(s.ActionName) > 0 {
ui.Say(fmt.Sprintf("%v ! Waiting for VM to reboot %v times...", s.ActionName, s.ExpectedRebootCount))
}
var rebootCount uint
var lastUptime uint64
for rebootCount < s.ExpectedRebootCount {
uptime, err := driver.Uptime(vmName)
if err != nil {
err := fmt.Errorf("Error checking uptime: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if uptime < lastUptime {
rebootCount++
ui.Say(fmt.Sprintf("%v -> Detected reboot %v after %v seconds...", s.ActionName, rebootCount, lastUptime))
}
lastUptime = uptime
if rebootCount < s.ExpectedRebootCount {
time.Sleep(time.Second * SleepSeconds)
}
}
return multistep.ActionContinue
}
func (s *StepWaitForInstallToComplete) Cleanup(state multistep.StateBag) {
}