packer-cn/builder/vmware/step_run.go

120 lines
2.9 KiB
Go
Raw Normal View History

2013-06-05 17:49:04 -04:00
package vmware
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"time"
2013-06-05 17:49:04 -04:00
)
// This step runs the created virtual machine.
//
// Uses:
// 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>
type stepRun struct {
bootTime time.Time
vmxPath string
registered bool
2013-06-05 17:49:04 -04:00
}
2013-08-31 15:50:25 -04:00
func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config)
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
vmxPath := state.Get("vmx_path").(string)
vncIp := state.Get("vnc_ip").(string)
2013-08-31 15:50:25 -04:00
vncPort := state.Get("vnc_port").(uint)
2013-06-05 17:49:04 -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...")
if config.Headless {
ui.Message(fmt.Sprintf(
"The VM will be run headless, without a GUI. If you want to\n"+
"view the screen of the VM, connect via VNC without a password to\n"+
"%s:%d", vncIp, vncPort))
}
if remoteDriver, ok := driver.(RemoteDriver); ok {
if err := remoteDriver.Register(vmxPath); err != nil {
err := fmt.Errorf("Error registering VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.registered = true
}
2013-07-02 00:13:13 -04:00
if err := driver.Start(vmxPath, config.Headless); err != nil {
2013-06-20 00:20:48 -04:00
err := fmt.Errorf("Error starting VM: %s", err)
2013-08-31 15:50:25 -04:00
state.Put("error", err)
2013-06-20 00:20:48 -04:00
ui.Error(err.Error())
2013-06-05 17:49:04 -04:00
return multistep.ActionHalt
}
// Wait the wait amount
if int64(config.bootWait) > 0 {
ui.Say(fmt.Sprintf("Waiting %s for boot...", config.bootWait.String()))
wait := time.After(config.bootWait)
WAITLOOP:
for {
select {
case <-wait:
break WAITLOOP
case <-time.After(1 * time.Second):
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return multistep.ActionHalt
}
}
}
}
2013-06-05 17:49:04 -04:00
return multistep.ActionContinue
}
2013-08-31 15:50:25 -04:00
func (s *stepRun) Cleanup(state multistep.StateBag) {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
2013-06-05 17:49:04 -04:00
// If we started the machine... stop it.
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 {
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)
}
// See if it is running
running, _ := driver.IsRunning(s.vmxPath)
if running {
ui.Say("Stopping virtual machine...")
if err := driver.Stop(s.vmxPath); err != nil {
ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
}
2013-06-05 17:49:04 -04:00
}
if remoteDriver, ok := driver.(RemoteDriver); ok && s.registered {
ui.Say("Unregistering virtual machine...")
if err := remoteDriver.Unregister(s.vmxPath); err != nil {
ui.Error(fmt.Sprintf("Error unregistering VM: %s", err))
}
s.registered = false
}
2013-06-05 17:49:04 -04:00
}
}