2013-06-11 23:46:32 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction {
|
2013-06-11 23:56:44 -04:00
|
|
|
config := state["config"].(*config)
|
2013-06-11 23:46:32 -04:00
|
|
|
driver := state["driver"].(Driver)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
vmName := state["vmName"].(string)
|
|
|
|
|
|
|
|
ui.Say("Starting the virtual machine...")
|
|
|
|
command := []string{"startvm", vmName, "--type", "gui"}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error starting VM: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmName = vmName
|
|
|
|
|
2013-06-11 23:56:44 -04:00
|
|
|
if int64(config.BootWait) > 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Waiting %s for boot...", config.BootWait))
|
|
|
|
time.Sleep(config.BootWait)
|
|
|
|
}
|
|
|
|
|
2013-06-11 23:46:32 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepRun) Cleanup(state map[string]interface{}) {
|
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state["driver"].(Driver)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
if err := driver.VBoxManage("controlvm", s.vmName, "poweroff"); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error shutting down VM: %s", err))
|
|
|
|
}
|
|
|
|
}
|