Commit b8103ff9 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/virtualbox: properly handle errors

parent da4dfbcd
...@@ -226,6 +226,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -226,6 +226,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
b.runner.Run(state) b.runner.Run(state)
// If there was an error, return that
if rawErr, ok := state["error"]; ok {
return nil, rawErr.(error)
}
return nil, nil return nil, nil
} }
......
...@@ -31,7 +31,9 @@ func (s *stepAttachISO) Run(state map[string]interface{}) multistep.StepAction { ...@@ -31,7 +31,9 @@ func (s *stepAttachISO) Run(state map[string]interface{}) multistep.StepAction {
"--medium", isoPath, "--medium", isoPath,
} }
if err := driver.VBoxManage(command...); err != nil { if err := driver.VBoxManage(command...); err != nil {
ui.Error(fmt.Sprintf("Error attaching hard drive: %s", err)) err := fmt.Errorf("Error attaching ISO: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
......
...@@ -32,7 +32,9 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction ...@@ -32,7 +32,9 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction
ui.Say("Creating hard drive...") ui.Say("Creating hard drive...")
err := driver.VBoxManage(command...) err := driver.VBoxManage(command...)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error creating hard drive: %s", err)) err := fmt.Errorf("Error creating hard drive: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -40,7 +42,9 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction ...@@ -40,7 +42,9 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction
controllerName := "IDE Controller" controllerName := "IDE Controller"
err = driver.VBoxManage("storagectl", vmName, "--name", controllerName, "--add", "ide") err = driver.VBoxManage("storagectl", vmName, "--name", controllerName, "--add", "ide")
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error creating disk controller: %s", err)) err := fmt.Errorf("Error creating disk controller: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -54,7 +58,9 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction ...@@ -54,7 +58,9 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction
"--medium", path, "--medium", path,
} }
if err := driver.VBoxManage(command...); err != nil { if err := driver.VBoxManage(command...); err != nil {
ui.Error(fmt.Sprintf("Error attaching hard drive: %s", err)) err := fmt.Errorf("Error attaching hard drive: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
......
...@@ -34,7 +34,9 @@ func (s *stepCreateVM) Run(state map[string]interface{}) multistep.StepAction { ...@@ -34,7 +34,9 @@ func (s *stepCreateVM) Run(state map[string]interface{}) multistep.StepAction {
for _, command := range commands { for _, command := range commands {
err := driver.VBoxManage(command...) err := driver.VBoxManage(command...)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error creating VM: %s", err)) err := fmt.Errorf("Error creating VM: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
......
...@@ -29,7 +29,9 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction ...@@ -29,7 +29,9 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
checksum, err := hex.DecodeString(config.ISOMD5) checksum, err := hex.DecodeString(config.ISOMD5)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error parsing checksum: %s", err)) err := fmt.Errorf("Error parsing checksum: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -57,12 +59,14 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction ...@@ -57,12 +59,14 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
progressTicker := time.NewTicker(5 * time.Second) progressTicker := time.NewTicker(5 * time.Second)
defer progressTicker.Stop() defer progressTicker.Stop()
DownloadWaitLoop: DownloadWaitLoop:
for { for {
select { select {
case err := <-downloadCompleteCh: case err := <-downloadCompleteCh:
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error downloading ISO: %s", err)) err := fmt.Errorf("Error downloading ISO: %s", err)
state["error"] = err
ui.Error(err.Error())
} }
break DownloadWaitLoop break DownloadWaitLoop
......
...@@ -34,7 +34,9 @@ func (s *stepExport) Run(state map[string]interface{}) multistep.StepAction { ...@@ -34,7 +34,9 @@ func (s *stepExport) Run(state map[string]interface{}) multistep.StepAction {
ui.Say("Exporting virtual machine...") ui.Say("Exporting virtual machine...")
err := driver.VBoxManage(command...) err := driver.VBoxManage(command...)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error exporting virtual machine: %s", err)) err := fmt.Errorf("Error exporting virtual machine: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
......
...@@ -44,7 +44,9 @@ func (s *stepForwardSSH) Run(state map[string]interface{}) multistep.StepAction ...@@ -44,7 +44,9 @@ func (s *stepForwardSSH) Run(state map[string]interface{}) multistep.StepAction
fmt.Sprintf("packerssh,tcp,127.0.0.1,%d,,%d", sshHostPort, config.SSHPort), fmt.Sprintf("packerssh,tcp,127.0.0.1,%d,,%d", sshHostPort, config.SSHPort),
} }
if err := driver.VBoxManage(command...); err != nil { if err := driver.VBoxManage(command...); err != nil {
ui.Error(fmt.Sprintf("Error creating port forwarding rule: %s", err)) err := fmt.Errorf("Error creating port forwarding rule: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
......
...@@ -11,6 +11,7 @@ func (stepPrepareOutputDir) Run(state map[string]interface{}) multistep.StepActi ...@@ -11,6 +11,7 @@ func (stepPrepareOutputDir) Run(state map[string]interface{}) multistep.StepActi
config := state["config"].(*config) config := state["config"].(*config)
if err := os.MkdirAll(config.OutputDir, 0755); err != nil { if err := os.MkdirAll(config.OutputDir, 0755); err != nil {
state["error"] = err
return multistep.ActionHalt return multistep.ActionHalt
} }
......
...@@ -25,7 +25,9 @@ func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction { ...@@ -25,7 +25,9 @@ func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction {
ui.Say("Starting the virtual machine...") ui.Say("Starting the virtual machine...")
command := []string{"startvm", vmName, "--type", "gui"} command := []string{"startvm", vmName, "--type", "gui"}
if err := driver.VBoxManage(command...); err != nil { if err := driver.VBoxManage(command...); err != nil {
ui.Error(fmt.Sprintf("Error starting VM: %s", err)) err := fmt.Errorf("Error starting VM: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
......
package virtualbox package virtualbox
import ( import (
"errors"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
...@@ -34,7 +35,9 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction { ...@@ -34,7 +35,9 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction {
log.Printf("Executing shutdown command: %s", config.ShutdownCommand) log.Printf("Executing shutdown command: %s", config.ShutdownCommand)
cmd := &packer.RemoteCmd{Command: config.ShutdownCommand} cmd := &packer.RemoteCmd{Command: config.ShutdownCommand}
if err := comm.Start(cmd); err != nil { if err := comm.Start(cmd); err != nil {
ui.Error(fmt.Sprintf("Failed to send shutdown command: %s", err)) err := fmt.Errorf("Failed to send shutdown command: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -52,7 +55,9 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction { ...@@ -52,7 +55,9 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction {
select { select {
case <-shutdownTimer: case <-shutdownTimer:
ui.Error("Timeout while waiting for machine to shut down.") err := errors.New("Timeout while waiting for machine to shut down.")
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
default: default:
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
...@@ -61,7 +66,9 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction { ...@@ -61,7 +66,9 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction {
} else { } else {
ui.Say("Halting the virtual machine...") ui.Say("Halting the virtual machine...")
if err := driver.Stop(vmName); err != nil { if err := driver.Stop(vmName); err != nil {
ui.Error(fmt.Sprintf("Error stopping VM: %s", err)) err := fmt.Errorf("Error stopping VM: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
} }
......
...@@ -17,7 +17,9 @@ func (stepSuppressMessages) Run(state map[string]interface{}) multistep.StepActi ...@@ -17,7 +17,9 @@ func (stepSuppressMessages) Run(state map[string]interface{}) multistep.StepActi
log.Println("Suppressing annoying messages in VirtualBox") log.Println("Suppressing annoying messages in VirtualBox")
if err := driver.SuppressMessages(); err != nil { if err := driver.SuppressMessages(); err != nil {
ui.Error(fmt.Sprintf("Error configuring VirtualBox to suppress messages: %s", err)) err := fmt.Errorf("Error configuring VirtualBox to suppress messages: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
......
...@@ -66,7 +66,9 @@ func (s *stepTypeBootCommand) Run(state map[string]interface{}) multistep.StepAc ...@@ -66,7 +66,9 @@ func (s *stepTypeBootCommand) Run(state map[string]interface{}) multistep.StepAc
} }
if err := driver.VBoxManage("controlvm", vmName, "keyboardputscancode", code); err != nil { if err := driver.VBoxManage("controlvm", vmName, "keyboardputscancode", code); err != nil {
ui.Error(fmt.Sprintf("Error sending boot command: %s", err)) err := fmt.Errorf("Error sending boot command: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment