77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package iso
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/mitchellh/multistep"
|
|
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
|
"github.com/mitchellh/packer/packer"
|
|
"time"
|
|
)
|
|
|
|
// This step starts the virtual machine.
|
|
//
|
|
// Uses:
|
|
//
|
|
// Produces:
|
|
type stepRun struct {
|
|
vmName string
|
|
}
|
|
|
|
func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction {
|
|
config := state.Get("config").(*config)
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
|
ui := state.Get("ui").(packer.Ui)
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
ui.Say("Starting the virtual machine...")
|
|
guiArgument := "gui"
|
|
if config.Headless == true {
|
|
ui.Message("WARNING: The VM will be started in headless mode, as configured.\n" +
|
|
"In headless mode, errors during the boot sequence or OS setup\n" +
|
|
"won't be easily visible. Use at your own discretion.")
|
|
guiArgument = "headless"
|
|
}
|
|
command := []string{"startvm", vmName, "--type", guiArgument}
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
err := fmt.Errorf("Error starting VM: %s", err)
|
|
state.Put("error", err)
|
|
ui.Error(err.Error())
|
|
return multistep.ActionHalt
|
|
}
|
|
|
|
s.vmName = vmName
|
|
|
|
if int64(config.bootWait) > 0 {
|
|
ui.Say(fmt.Sprintf("Waiting %s for boot...", config.bootWait))
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
func (s *stepRun) Cleanup(state multistep.StateBag) {
|
|
if s.vmName == "" {
|
|
return
|
|
}
|
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
if running, _ := driver.IsRunning(s.vmName); running {
|
|
if err := driver.VBoxManage("controlvm", s.vmName, "poweroff"); err != nil {
|
|
ui.Error(fmt.Sprintf("Error shutting down VM: %s", err))
|
|
}
|
|
}
|
|
}
|