builder/virtualbox: properly handle errors

This commit is contained in:
Mitchell Hashimoto 2013-06-19 21:07:53 -07:00
parent da4dfbcd12
commit b8103ff9c0
12 changed files with 53 additions and 16 deletions

View File

@ -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
} }

View File

@ -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
} }

View File

@ -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
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
"--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
} }

View File

@ -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
} }

View File

@ -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
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

View File

@ -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
} }

View File

@ -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
} }

View File

@ -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
} }

View File

@ -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
} }

View File

@ -1,6 +1,7 @@
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 {
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 {
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 {
} 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
} }
} }

View File

@ -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
} }

View File

@ -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
} }
} }