2013-12-24 20:12:43 -05:00
|
|
|
package common
|
2013-06-05 17:49:04 -04:00
|
|
|
|
|
|
|
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-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-12-24 20:12:43 -05:00
|
|
|
type StepRun struct {
|
|
|
|
BootWait time.Duration
|
|
|
|
DurationBeforeStop time.Duration
|
|
|
|
Headless bool
|
|
|
|
|
2013-06-05 20:59:33 -04:00
|
|
|
bootTime time.Time
|
|
|
|
vmxPath string
|
2013-06-05 17:49:04 -04:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:12:43 -05:00
|
|
|
func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
2013-06-05 17:49:04 -04:00
|
|
|
|
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-12-24 20:12:43 -05:00
|
|
|
if s.Headless {
|
|
|
|
vncIpRaw, vncIpOk := state.GetOk("vnc_ip")
|
|
|
|
vncPortRaw, vncPortOk := state.GetOk("vnc_port")
|
2015-06-26 04:29:33 -04:00
|
|
|
vncPasswordRaw, vncPasswordOk := state.GetOk("vnc_password")
|
2013-12-24 20:12:43 -05:00
|
|
|
|
2015-06-26 04:29:33 -04:00
|
|
|
if vncIpOk && vncPortOk && vncPasswordOk {
|
2013-12-24 20:12:43 -05:00
|
|
|
vncIp := vncIpRaw.(string)
|
|
|
|
vncPort := vncPortRaw.(uint)
|
2015-06-26 04:29:33 -04:00
|
|
|
vncPassword := vncPasswordRaw.(string)
|
2013-12-24 20:12:43 -05:00
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"The VM will be run headless, without a GUI. If you want to\n"+
|
2016-08-19 06:49:23 -04:00
|
|
|
"view the screen of the VM, connect via VNC with the password \"%s\" to\n"+
|
2017-02-02 04:55:28 -05:00
|
|
|
"vnc://%s:%d", vncPassword, vncIp, vncPort))
|
2013-12-24 20:12:43 -05:00
|
|
|
} else {
|
|
|
|
ui.Message("The VM will be run headless, without a GUI, as configured.\n" +
|
|
|
|
"If the run isn't succeeding as you expect, please enable the GUI\n" +
|
|
|
|
"to inspect the progress of the build.")
|
|
|
|
}
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:12:43 -05:00
|
|
|
if err := driver.Start(vmxPath, s.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
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:48:13 -04:00
|
|
|
// Wait the wait amount
|
2013-12-24 20:12:43 -05:00
|
|
|
if int64(s.BootWait) > 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Waiting %s for boot...", s.BootWait.String()))
|
|
|
|
wait := time.After(s.BootWait)
|
2013-11-12 11:20:52 -05:00
|
|
|
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 18:48:13 -04:00
|
|
|
}
|
|
|
|
|
2013-06-05 17:49:04 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:12:43 -05:00
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-05 17:49:04 -04:00
|
|
|
|
|
|
|
// 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)
|
2013-12-24 20:12:43 -05:00
|
|
|
waitBootTime := s.DurationBeforeStop
|
2013-06-05 20:59:33 -04:00
|
|
|
if sinceBootTime < waitBootTime {
|
2013-06-06 15:19:38 -04:00
|
|
|
sleepTime := waitBootTime - sinceBootTime
|
2013-12-24 20:12:43 -05:00
|
|
|
ui.Say(fmt.Sprintf(
|
|
|
|
"Waiting %s to give VMware time to clean up...", sleepTime.String()))
|
2013-06-06 15:19:38 -04:00
|
|
|
time.Sleep(sleepTime)
|
2013-06-05 20:59:33 -04:00
|
|
|
}
|
|
|
|
|
2013-06-06 19:14:07 -04:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|