2013-12-21 17:27:00 -05:00
|
|
|
package iso
|
2013-06-11 23:46:32 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
2013-12-21 18:00:48 -05:00
|
|
|
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
2013-06-11 23:46:32 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step starts the virtual machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
//
|
|
|
|
// Produces:
|
2013-06-12 00:08:45 -04:00
|
|
|
type stepRun struct {
|
2013-06-11 23:46:32 -04:00
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*config)
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-11 23:46:32 -04:00
|
|
|
|
|
|
|
ui.Say("Starting the virtual machine...")
|
2013-07-02 00:13:24 -04:00
|
|
|
guiArgument := "gui"
|
|
|
|
if config.Headless == true {
|
2013-07-02 15:20:26 -04:00
|
|
|
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" +
|
2013-07-03 19:11:37 -04:00
|
|
|
"won't be easily visible. Use at your own discretion.")
|
2013-07-02 00:13:24 -04:00
|
|
|
guiArgument = "headless"
|
|
|
|
}
|
|
|
|
command := []string{"startvm", vmName, "--type", guiArgument}
|
2013-06-11 23:46:32 -04:00
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error starting VM: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 23:46:32 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmName = vmName
|
|
|
|
|
2013-07-14 08:22:11 -04:00
|
|
|
if int64(config.bootWait) > 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Waiting %s for boot...", config.bootWait))
|
2013-11-12 11:19:20 -05:00
|
|
|
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-11 23:56:44 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 23:46:32 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepRun) Cleanup(state multistep.StateBag) {
|
2013-06-11 23:46:32 -04:00
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-24 01:00:40 -04:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2013-06-11 23:46:32 -04:00
|
|
|
}
|
|
|
|
}
|