packer-cn/builder/virtualbox/step_run.go

62 lines
1.3 KiB
Go
Raw Normal View History

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...")
2013-07-02 00:13:24 -04:00
guiArgument := "gui"
if config.Headless == true {
guiArgument = "headless"
}
command := []string{"startvm", vmName, "--type", guiArgument}
2013-06-11 23:46:32 -04:00
if err := driver.VBoxManage(command...); err != nil {
err := fmt.Errorf("Error starting VM: %s", err)
state["error"] = err
ui.Error(err.Error())
2013-06-11 23:46:32 -04:00
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 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
}
}