From cac0f49bb8a752833c45ecfdcbf96f9dd90de501 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 19 Jun 2013 20:54:02 -0700 Subject: [PATCH] builder/amazonebs: Return proper errors --- builder/amazonebs/builder.go | 5 +++++ builder/amazonebs/step_connect_ssh.go | 13 ++++++++++--- builder/amazonebs/step_create_ami.go | 4 ++++ builder/amazonebs/step_keypair.go | 2 ++ builder/amazonebs/step_run_source_instance.go | 4 ++++ builder/amazonebs/step_security_group.go | 2 ++ builder/amazonebs/step_stop_instance.go | 5 +++++ 7 files changed, 32 insertions(+), 3 deletions(-) diff --git a/builder/amazonebs/builder.go b/builder/amazonebs/builder.go index f79ac065a..149c6284e 100644 --- a/builder/amazonebs/builder.go +++ b/builder/amazonebs/builder.go @@ -155,6 +155,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe b.runner.Run(state) + // If there was an error, return that + if rawErr, ok := state["error"]; ok { + return nil, rawErr.(error) + } + // If there are no AMIs, then just return if _, ok := state["amis"]; !ok { return nil, nil diff --git a/builder/amazonebs/step_connect_ssh.go b/builder/amazonebs/step_connect_ssh.go index 4c5e1238a..770ec27c2 100644 --- a/builder/amazonebs/step_connect_ssh.go +++ b/builder/amazonebs/step_connect_ssh.go @@ -2,6 +2,7 @@ package amazonebs import ( gossh "code.google.com/p/go.crypto/ssh" + "errors" "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" @@ -27,7 +28,9 @@ func (s *stepConnectSSH) Run(state map[string]interface{}) multistep.StepAction keyring := &ssh.SimpleKeychain{} err := keyring.AddPEMKey(privateKey) if err != nil { - ui.Say(fmt.Sprintf("Error setting up SSH config: %s", err)) + err := fmt.Errorf("Error setting up SSH config: %s", err) + state["error"] = err + ui.Error(err.Error()) return multistep.ActionHalt } @@ -85,7 +88,9 @@ ConnectWaitLoop: // We connected. Just break the loop. break ConnectWaitLoop case <-timeout: - ui.Error("Timeout while waiting to connect to SSH.") + err := errors.New("Timeout waiting for SSH to become available.") + state["error"] = err + ui.Error(err.Error()) return multistep.ActionHalt case <-time.After(1 * time.Second): if _, ok := state[multistep.StateCancelled]; ok { @@ -101,7 +106,9 @@ ConnectWaitLoop: } if err != nil { - ui.Error(fmt.Sprintf("Error connecting to SSH: %s", err)) + err := fmt.Errorf("Error connecting to SSH: %s", err) + state["error"] = err + ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/amazonebs/step_create_ami.go b/builder/amazonebs/step_create_ami.go index f088e8fc6..8fa1c005f 100644 --- a/builder/amazonebs/step_create_ami.go +++ b/builder/amazonebs/step_create_ami.go @@ -42,6 +42,8 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction { createResp, err := ec2conn.CreateImage(createOpts) if err != nil { + err := fmt.Errorf("Error creating AMI: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt } @@ -57,6 +59,8 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction { for { imageResp, err := ec2conn.Images([]string{createResp.ImageId}, ec2.NewFilter()) if err != nil { + err := fmt.Errorf("Error querying images: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/amazonebs/step_keypair.go b/builder/amazonebs/step_keypair.go index 62a32f7fb..5b598b7ea 100644 --- a/builder/amazonebs/step_keypair.go +++ b/builder/amazonebs/step_keypair.go @@ -23,6 +23,8 @@ func (s *stepKeyPair) Run(state map[string]interface{}) multistep.StepAction { log.Printf("temporary keypair name: %s", keyName) keyResp, err := ec2conn.CreateKeyPair(keyName) if err != nil { + err := fmt.Errorf("Error creating temporary keypair: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/amazonebs/step_run_source_instance.go b/builder/amazonebs/step_run_source_instance.go index 37d9f50f8..d7531668e 100644 --- a/builder/amazonebs/step_run_source_instance.go +++ b/builder/amazonebs/step_run_source_instance.go @@ -31,6 +31,8 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step ui.Say("Launching a source AWS instance...") runResp, err := ec2conn.RunInstances(runOpts) if err != nil { + err := fmt.Errorf("Error launching source instance: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt } @@ -41,6 +43,8 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step ui.Say("Waiting for instance to become ready...") s.instance, err = waitForState(ec2conn, s.instance, []string{"pending"}, "running") if err != nil { + err := fmt.Errorf("Error waiting for instance to become ready: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/amazonebs/step_security_group.go b/builder/amazonebs/step_security_group.go index 3537d2ebd..ce6ee6fd1 100644 --- a/builder/amazonebs/step_security_group.go +++ b/builder/amazonebs/step_security_group.go @@ -44,6 +44,8 @@ func (s *stepSecurityGroup) Run(state map[string]interface{}) multistep.StepActi ui.Say("Authorizing SSH access on the temporary security group...") if _, err := ec2conn.AuthorizeSecurityGroup(groupResp.SecurityGroup, perms); err != nil { + err := fmt.Errorf("Error creating temporary security group: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/amazonebs/step_stop_instance.go b/builder/amazonebs/step_stop_instance.go index 72c7f4224..09dca43d2 100644 --- a/builder/amazonebs/step_stop_instance.go +++ b/builder/amazonebs/step_stop_instance.go @@ -1,6 +1,7 @@ package amazonebs import ( + "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" @@ -17,6 +18,8 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio ui.Say("Stopping the source instance...") _, err := ec2conn.StopInstances(instance.InstanceId) if err != nil { + err := fmt.Errorf("Error stopping instance: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt } @@ -27,6 +30,8 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio instance.State.Name = "stopping" instance, err = waitForState(ec2conn, instance, []string{"running", "stopping"}, "stopped") if err != nil { + err := fmt.Errorf("Error waiting for instance to stop: %s", err) + state["error"] = err ui.Error(err.Error()) return multistep.ActionHalt }