builder/vmware: Wait a minimum time before shutting down VM

This commit is contained in:
Mitchell Hashimoto 2013-06-05 17:59:33 -07:00
parent daf076bbd9
commit d5cfa42e05
1 changed files with 14 additions and 5 deletions

View File

@ -18,7 +18,8 @@ import (
// Produces: // Produces:
// <nothing> // <nothing>
type stepRun struct { type stepRun struct {
vmxPath string bootTime time.Time
vmxPath string
} }
func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction { func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction {
@ -28,16 +29,17 @@ func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction {
vmrun_path := "/Applications/VMware Fusion.app/Contents/Library/vmrun" vmrun_path := "/Applications/VMware Fusion.app/Contents/Library/vmrun"
// Set the VMX path so that we know we started the machine
s.bootTime = time.Now()
s.vmxPath = vmxPath
ui.Say("Starting virtual machine...") ui.Say("Starting virtual machine...")
cmd := exec.Command(vmrun_path, "-T", "fusion", "start", vmxPath, "gui") cmd := exec.Command(vmrun_path, "-T", "fusion", "start", s.vmxPath, "gui")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
ui.Error(fmt.Sprintf("Error starting VM: %s", err)) ui.Error(fmt.Sprintf("Error starting VM: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
// Set the VMX path so that we know we started the machine
s.vmxPath = vmxPath
// Wait the wait amount // Wait the wait amount
if config.BootWait > 0 { if config.BootWait > 0 {
ui.Say(fmt.Sprintf("Waiting %d seconds for boot...", config.BootWait)) ui.Say(fmt.Sprintf("Waiting %d seconds for boot...", config.BootWait))
@ -54,6 +56,13 @@ func (s *stepRun) Cleanup(state map[string]interface{}) {
// If we started the machine... stop it. // If we started the machine... stop it.
if s.vmxPath != "" { if s.vmxPath != "" {
// If we started it less than 5 seconds ago... wait.
sinceBootTime := time.Since(s.bootTime)
waitBootTime := 5 * time.Second
if sinceBootTime < waitBootTime {
time.Sleep(waitBootTime - sinceBootTime)
}
ui.Say("Stopping virtual machine...") ui.Say("Stopping virtual machine...")
cmd := exec.Command(vmrun_path, "-T", "fusion", "stop", s.vmxPath, "hard") cmd := exec.Command(vmrun_path, "-T", "fusion", "stop", s.vmxPath, "hard")
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {