diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index 2a90d41da..2cb149091 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -130,6 +130,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer new(stepCreateDisk), new(stepAttachISO), new(stepForwardSSH), + new(stepRun), } // Setup the state bag diff --git a/builder/virtualbox/step_forward_ssh.go b/builder/virtualbox/step_forward_ssh.go index a627939da..8fcf78f45 100644 --- a/builder/virtualbox/step_forward_ssh.go +++ b/builder/virtualbox/step_forward_ssh.go @@ -7,7 +7,6 @@ import ( "log" "math/rand" "net" - "time" ) // This step adds a NAT port forwarding definition so that SSH is available @@ -52,7 +51,6 @@ func (s *stepForwardSSH) Run(state map[string]interface{}) multistep.StepAction // Save the port we're using so that future steps can use it state["sshHostPort"] = sshHostPort - time.Sleep(15 * time.Second) return multistep.ActionContinue } diff --git a/builder/virtualbox/step_run.go b/builder/virtualbox/step_run.go new file mode 100644 index 000000000..cfca9eebf --- /dev/null +++ b/builder/virtualbox/step_run.go @@ -0,0 +1,47 @@ +package virtualbox + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "time" +) + +// This step starts the virtual machine. +// +// Uses: +// +// Produces: +type stepRun struct{ + vmName string +} + +func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction { + 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 + + time.Sleep(15 * time.Second) + 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)) + } +}