2013-06-05 17:49:04 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-05 18:48:13 -04:00
|
|
|
"time"
|
2013-06-05 17:49:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step runs the created virtual machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-06-05 18:48:13 -04:00
|
|
|
// config *config
|
2013-06-06 15:19:38 -04:00
|
|
|
// driver Driver
|
2013-06-05 17:49:04 -04:00
|
|
|
// ui packer.Ui
|
|
|
|
// vmx_path string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2013-06-05 18:48:13 -04:00
|
|
|
type stepRun struct {
|
2013-06-05 20:59:33 -04:00
|
|
|
bootTime time.Time
|
|
|
|
vmxPath string
|
2013-06-05 17:49:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction {
|
2013-06-05 18:48:13 -04:00
|
|
|
config := state["config"].(*config)
|
2013-06-06 15:19:38 -04:00
|
|
|
driver := state["driver"].(Driver)
|
2013-06-05 17:49:04 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
vmxPath := state["vmx_path"].(string)
|
|
|
|
|
2013-06-05 20:59:33 -04:00
|
|
|
// Set the VMX path so that we know we started the machine
|
|
|
|
s.bootTime = time.Now()
|
|
|
|
s.vmxPath = vmxPath
|
|
|
|
|
2013-06-05 17:49:04 -04:00
|
|
|
ui.Say("Starting virtual machine...")
|
2013-06-06 15:19:38 -04:00
|
|
|
if err := driver.Start(vmxPath); err != nil {
|
2013-06-05 17:49:04 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error starting VM: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:48:13 -04:00
|
|
|
// Wait the wait amount
|
2013-06-06 12:21:50 -04:00
|
|
|
if int64(config.BootWait) > 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Waiting %s for boot...", config.BootWait.String()))
|
|
|
|
time.Sleep(config.BootWait)
|
2013-06-05 18:48:13 -04:00
|
|
|
}
|
|
|
|
|
2013-06-05 17:49:04 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepRun) Cleanup(state map[string]interface{}) {
|
2013-06-06 15:19:38 -04:00
|
|
|
driver := state["driver"].(Driver)
|
2013-06-05 17:49:04 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
|
|
|
// If we started the machine... stop it.
|
|
|
|
if s.vmxPath != "" {
|
2013-06-05 20:59:33 -04:00
|
|
|
// If we started it less than 5 seconds ago... wait.
|
|
|
|
sinceBootTime := time.Since(s.bootTime)
|
|
|
|
waitBootTime := 5 * time.Second
|
|
|
|
if sinceBootTime < waitBootTime {
|
2013-06-06 15:19:38 -04:00
|
|
|
sleepTime := waitBootTime - sinceBootTime
|
|
|
|
ui.Say(fmt.Sprintf("Waiting %s to give VMware time to clean up...", sleepTime.String()))
|
|
|
|
time.Sleep(sleepTime)
|
2013-06-05 20:59:33 -04:00
|
|
|
}
|
|
|
|
|
2013-06-05 17:49:04 -04:00
|
|
|
ui.Say("Stopping virtual machine...")
|
2013-06-06 15:19:38 -04:00
|
|
|
if err := driver.Stop(s.vmxPath); err != nil {
|
2013-06-05 17:49:04 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|