github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_wait_for_install_to_complete.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/multistep" 6 "github.com/mitchellh/packer/packer" 7 "time" 8 ) 9 10 const ( 11 SleepSeconds = 10 12 ) 13 14 type StepWaitForPowerOff struct { 15 } 16 17 func (s *StepWaitForPowerOff) Run(state multistep.StateBag) multistep.StepAction { 18 driver := state.Get("driver").(Driver) 19 ui := state.Get("ui").(packer.Ui) 20 vmName := state.Get("vmName").(string) 21 ui.Say("Waiting for vm to be powered down...") 22 23 for { 24 isOff, err := driver.IsOff(vmName) 25 26 if err != nil { 27 err := fmt.Errorf("Error checking if vm is off: %s", err) 28 state.Put("error", err) 29 ui.Error(err.Error()) 30 return multistep.ActionHalt 31 } 32 33 if isOff { 34 break 35 } else { 36 time.Sleep(time.Second * SleepSeconds) 37 } 38 } 39 40 return multistep.ActionContinue 41 } 42 43 func (s *StepWaitForPowerOff) Cleanup(state multistep.StateBag) { 44 } 45 46 type StepWaitForInstallToComplete struct { 47 ExpectedRebootCount uint 48 ActionName string 49 } 50 51 func (s *StepWaitForInstallToComplete) Run(state multistep.StateBag) multistep.StepAction { 52 driver := state.Get("driver").(Driver) 53 ui := state.Get("ui").(packer.Ui) 54 vmName := state.Get("vmName").(string) 55 56 if len(s.ActionName) > 0 { 57 ui.Say(fmt.Sprintf("%v ! Waiting for VM to reboot %v times...", s.ActionName, s.ExpectedRebootCount)) 58 } 59 60 var rebootCount uint 61 var lastUptime uint64 62 63 for rebootCount < s.ExpectedRebootCount { 64 uptime, err := driver.Uptime(vmName) 65 66 if err != nil { 67 err := fmt.Errorf("Error checking uptime: %s", err) 68 state.Put("error", err) 69 ui.Error(err.Error()) 70 return multistep.ActionHalt 71 } 72 73 if uptime < lastUptime { 74 rebootCount++ 75 ui.Say(fmt.Sprintf("%v -> Detected reboot %v after %v seconds...", s.ActionName, rebootCount, lastUptime)) 76 } 77 78 lastUptime = uptime 79 80 if rebootCount < s.ExpectedRebootCount { 81 time.Sleep(time.Second * SleepSeconds) 82 } 83 } 84 85 return multistep.ActionContinue 86 } 87 88 func (s *StepWaitForInstallToComplete) Cleanup(state multistep.StateBag) { 89 90 }