Fix BMCS builder step error reporting style

See: https://github.com/hashicorp/packer/pull/4554#discussion_r130214247
This commit is contained in:
Andrew Pryde 2017-08-02 11:53:14 +01:00
parent fc17f69bad
commit 8c81bb5dac
4 changed files with 37 additions and 14 deletions

View File

@ -26,7 +26,9 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
instanceID, err := driver.CreateInstance(publicKey)
if err != nil {
state.Put("error", fmt.Errorf("Problem creating instance: %s", err))
err = fmt.Errorf("Problem creating instance: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
@ -36,9 +38,10 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
ui.Say("Waiting for instance to enter 'RUNNING' state...")
err = driver.WaitForInstanceState(instanceID, []string{"STARTING", "PROVISIONING"}, "RUNNING")
if err != nil {
state.Put("error", fmt.Errorf("Error waiting for instance to start: %s", err))
if err = driver.WaitForInstanceState(instanceID, []string{"STARTING", "PROVISIONING"}, "RUNNING"); err != nil {
err = fmt.Errorf("Error waiting for instance to start: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
@ -60,13 +63,17 @@ func (s *stepCreateInstance) Cleanup(state multistep.StateBag) {
ui.Say(fmt.Sprintf("Terminating instance (%s)...", id))
if err := driver.TerminateInstance(id); err != nil {
state.Put("error", fmt.Sprintf("Error terminating instance. Please terminate manually: %s", err))
err = fmt.Errorf("Error terminating instance. Please terminate manually: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return
}
err := driver.WaitForInstanceState(id, []string{"TERMINATING"}, "TERMINATED")
if err != nil {
state.Put("error", fmt.Sprintf("Error terminating instance. Please terminate manually: %s", err))
err = fmt.Errorf("Error terminating instance. Please terminate manually: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return
}

View File

@ -32,7 +32,9 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
priv, err := rsa.GenerateKey(rand.Reader, 2014)
if err != nil {
state.Put("error", fmt.Errorf("Error creating temporary SSH key: %s", err))
err = fmt.Errorf("Error creating temporary SSH key: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
@ -50,7 +52,9 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
// Marshal the public key into SSH compatible format
pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil {
state.Put("error", fmt.Errorf("Error marshaling temporary SSH public key: %s", err))
err = fmt.Errorf("Error marshaling temporary SSH public key: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
@ -62,21 +66,27 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
f, err := os.Create(s.DebugKeyPath)
if err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
err = fmt.Errorf("Error saving debug key: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
defer f.Close()
// Write the key out
if _, err := f.Write(pem.EncodeToMemory(&privBlk)); err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
err = fmt.Errorf("Error saving debug key: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
// Chmod it so that it is SSH ready
if runtime.GOOS != "windows" {
if err := f.Chmod(0600); err != nil {
state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err))
err = fmt.Errorf("Error setting permissions of debug key: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
}

View File

@ -26,13 +26,17 @@ func (s *stepImage) Run(state multistep.StateBag) multistep.StepAction {
image, err := driver.CreateImage(instanceID)
if err != nil {
state.Put("error", fmt.Errorf("Error creating image from instance: %s", err))
err = fmt.Errorf("Error creating image from instance: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
err = driver.WaitForImageCreation(image.ID)
if err != nil {
state.Put("error", fmt.Errorf("Error waiting for image creation to finish: %s", err))
err = fmt.Errorf("Error waiting for image creation to finish: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}

View File

@ -24,7 +24,9 @@ func (s *stepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
ip, err := driver.GetInstanceIP(id)
if err != nil {
state.Put("error", fmt.Errorf("Error getting instance's public IP: %s", err))
err = fmt.Errorf("Error getting instance's public IP: %s", err)
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}