Make sure we always print and return any errors

This commit is contained in:
Sander van Harmelen 2017-07-06 23:31:13 +02:00
parent 4313f98061
commit c4ef9bcd3f
6 changed files with 27 additions and 26 deletions

View File

@ -88,17 +88,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Run the steps. // Run the steps.
b.runner.Run(state) b.runner.Run(state)
// If there are no templates, then just return // If there was an error, return that
template, ok := state.Get("template").(*cloudstack.CreateTemplateResponse) if rawErr, ok := state.GetOk("error"); ok {
if !ok || template == nil { ui.Error(rawErr.(error).Error())
return nil, nil return nil, rawErr.(error)
} }
// Build the artifact and return it // Build the artifact and return it
artifact := &Artifact{ artifact := &Artifact{
client: client, client: client,
config: b.config, config: b.config,
template: template, template: state.Get("template").(*cloudstack.CreateTemplateResponse),
} }
return artifact, nil return artifact, nil

View File

@ -47,7 +47,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
// Retrieve the instance ID from the previously saved state. // Retrieve the instance ID from the previously saved state.
instanceID, ok := state.Get("instance_id").(string) instanceID, ok := state.Get("instance_id").(string)
if !ok || instanceID == "" { if !ok || instanceID == "" {
ui.Error("Could not retrieve instance_id from state!") state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!"))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -56,7 +56,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
cloudstack.WithProject(config.Project), cloudstack.WithProject(config.Project),
) )
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Failed to retrieve the network object: %s", err)) state.Put("error", fmt.Errorf("Failed to retrieve the network object: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -79,7 +79,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
// Associate a new public IP address. // Associate a new public IP address.
ipAddr, err := client.Address.AssociateIpAddress(p) ipAddr, err := client.Address.AssociateIpAddress(p)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Failed to associate public IP address: %s", err)) state.Put("error", fmt.Errorf("Failed to associate public IP address: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -117,7 +117,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
ui.Message("Creating network ACL rule...") ui.Message("Creating network ACL rule...")
if network.Aclid == "" { if network.Aclid == "" {
ui.Error("Failed to configure the firewall: no ACL connected to the VPC network") state.Put("error", fmt.Errorf("Failed to configure the firewall: no ACL connected to the VPC network"))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -135,7 +135,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
// Create the network ACL rule. // Create the network ACL rule.
aclRule, err := client.NetworkACL.CreateNetworkACL(p) aclRule, err := client.NetworkACL.CreateNetworkACL(p)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Failed to create network ACL rule: %s", err)) state.Put("error", fmt.Errorf("Failed to create network ACL rule: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -154,7 +154,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction
fwRule, err := client.Firewall.CreateFirewallRule(p) fwRule, err := client.Firewall.CreateFirewallRule(p)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Failed to create firewall rule: %s", err)) state.Put("error", fmt.Errorf("Failed to create firewall rule: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -7,6 +7,7 @@ import (
"net" "net"
"strings" "strings"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
@ -61,7 +62,7 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
// Retrieve the zone object. // Retrieve the zone object.
zone, _, err := client.Zone.GetZoneByID(config.Zone) zone, _, err := client.Zone.GetZoneByID(config.Zone)
if err != nil { if err != nil {
ui.Error(err.Error()) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -77,27 +78,27 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
if config.UserData != "" { if config.UserData != "" {
httpPort := state.Get("http_port").(uint) httpPort := state.Get("http_port").(uint)
hostIp, err := hostIP() httpIP, err := hostIP()
if err != nil { if err != nil {
ui.Error(err.Error()) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
common.SetHTTPIP(httpIP)
s.Ctx.Data = &userDataTemplateData{ s.Ctx.Data = &userDataTemplateData{
hostIp, httpIP,
httpPort, httpPort,
} }
renderedUserData, err := interpolate.Render(config.UserData, &s.Ctx) renderedUserData, err := interpolate.Render(config.UserData, &s.Ctx)
if err != nil { if err != nil {
err := fmt.Errorf("Error rendering user_data: %s", err) state.Put("error", fmt.Errorf("Error rendering user_data: %s", err))
ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
ud, err := getUserData(renderedUserData, config.HTTPGetOnly) ud, err := getUserData(renderedUserData, config.HTTPGetOnly)
if err != nil { if err != nil {
ui.Error(err.Error()) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -107,7 +108,7 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
// Create the new instance. // Create the new instance.
instance, err := client.VirtualMachine.DeployVirtualMachine(p) instance, err := client.VirtualMachine.DeployVirtualMachine(p)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error creating new instance %s: %s", config.InstanceName, err)) state.Put("error", fmt.Errorf("Error creating new instance %s: %s", config.InstanceName, err))
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -21,7 +21,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction
// Retrieve the instance ID from the previously saved state. // Retrieve the instance ID from the previously saved state.
instanceID, ok := state.Get("instance_id").(string) instanceID, ok := state.Get("instance_id").(string)
if !ok || instanceID == "" { if !ok || instanceID == "" {
ui.Error("Could not retrieve instance_id from state!") state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!"))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -50,7 +50,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction
ui.Message("Retrieving the ROOT volume ID...") ui.Message("Retrieving the ROOT volume ID...")
volumeID, err := getRootVolumeID(client, instanceID) volumeID, err := getRootVolumeID(client, instanceID)
if err != nil { if err != nil {
ui.Error(err.Error()) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -60,7 +60,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction
ui.Message("Creating the new template...") ui.Message("Creating the new template...")
template, err := client.Template.CreateTemplate(p) template, err := client.Template.CreateTemplate(p)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error creating the new template %s: %s", config.TemplateName, err)) state.Put("error", fmt.Errorf("Error creating the new template %s: %s", config.TemplateName, err))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -72,7 +72,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction
ui.Message("Template has been created!") ui.Message("Template has been created!")
// Store the template ID. // Store the template.
state.Put("template", template) state.Put("template", template)
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -133,7 +133,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction {
// an interface with type *packer.MultiError and value nil which is different then a // an interface with type *packer.MultiError and value nil which is different then a
// nil interface. // nil interface.
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
ui.Error(errs.Error()) state.Put("error", errs)
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -20,7 +20,7 @@ func (s *stepShutdownInstance) Run(state multistep.StateBag) multistep.StepActio
// Retrieve the instance ID from the previously saved state. // Retrieve the instance ID from the previously saved state.
instanceID, ok := state.Get("instance_id").(string) instanceID, ok := state.Get("instance_id").(string)
if !ok || instanceID == "" { if !ok || instanceID == "" {
ui.Error("Could not retrieve instance_id from state!") state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!"))
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -30,7 +30,7 @@ func (s *stepShutdownInstance) Run(state multistep.StateBag) multistep.StepActio
// Shutdown the virtual machine. // Shutdown the virtual machine.
_, err := client.VirtualMachine.StopVirtualMachine(p) _, err := client.VirtualMachine.StopVirtualMachine(p)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error shutting down instance %s: %s", config.InstanceName, err)) state.Put("error", fmt.Errorf("Error shutting down instance %s: %s", config.InstanceName, err))
return multistep.ActionHalt return multistep.ActionHalt
} }