From c4ef9bcd3ffed238ce34115d6cf8d15b15533127 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Thu, 6 Jul 2017 23:31:13 +0200 Subject: [PATCH] Make sure we always print and return any errors --- builder/cloudstack/builder.go | 10 +++++----- builder/cloudstack/step_configure_networking.go | 12 ++++++------ builder/cloudstack/step_create_instance.go | 17 +++++++++-------- builder/cloudstack/step_create_template.go | 8 ++++---- builder/cloudstack/step_prepare_config.go | 2 +- builder/cloudstack/step_shutdown_instance.go | 4 ++-- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/builder/cloudstack/builder.go b/builder/cloudstack/builder.go index f905d4a9e..593065537 100644 --- a/builder/cloudstack/builder.go +++ b/builder/cloudstack/builder.go @@ -88,17 +88,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Run the steps. b.runner.Run(state) - // If there are no templates, then just return - template, ok := state.Get("template").(*cloudstack.CreateTemplateResponse) - if !ok || template == nil { - return nil, nil + // If there was an error, return that + if rawErr, ok := state.GetOk("error"); ok { + ui.Error(rawErr.(error).Error()) + return nil, rawErr.(error) } // Build the artifact and return it artifact := &Artifact{ client: client, config: b.config, - template: template, + template: state.Get("template").(*cloudstack.CreateTemplateResponse), } return artifact, nil diff --git a/builder/cloudstack/step_configure_networking.go b/builder/cloudstack/step_configure_networking.go index 9d525c255..df1054270 100644 --- a/builder/cloudstack/step_configure_networking.go +++ b/builder/cloudstack/step_configure_networking.go @@ -47,7 +47,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction // Retrieve the instance ID from the previously saved state. instanceID, ok := state.Get("instance_id").(string) 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 } @@ -56,7 +56,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction cloudstack.WithProject(config.Project), ) 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 } @@ -79,7 +79,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction // Associate a new public IP address. ipAddr, err := client.Address.AssociateIpAddress(p) 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 } @@ -117,7 +117,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction ui.Message("Creating network ACL rule...") 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 } @@ -135,7 +135,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction // Create the network ACL rule. aclRule, err := client.NetworkACL.CreateNetworkACL(p) 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 } @@ -154,7 +154,7 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction fwRule, err := client.Firewall.CreateFirewallRule(p) 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 } diff --git a/builder/cloudstack/step_create_instance.go b/builder/cloudstack/step_create_instance.go index cf22f3c08..62c0ca163 100644 --- a/builder/cloudstack/step_create_instance.go +++ b/builder/cloudstack/step_create_instance.go @@ -7,6 +7,7 @@ import ( "net" "strings" + "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" "github.com/mitchellh/multistep" @@ -61,7 +62,7 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction // Retrieve the zone object. zone, _, err := client.Zone.GetZoneByID(config.Zone) if err != nil { - ui.Error(err.Error()) + state.Put("error", err) return multistep.ActionHalt } @@ -77,27 +78,27 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction if config.UserData != "" { httpPort := state.Get("http_port").(uint) - hostIp, err := hostIP() + httpIP, err := hostIP() if err != nil { - ui.Error(err.Error()) + state.Put("error", err) return multistep.ActionHalt } + common.SetHTTPIP(httpIP) s.Ctx.Data = &userDataTemplateData{ - hostIp, + httpIP, httpPort, } renderedUserData, err := interpolate.Render(config.UserData, &s.Ctx) if err != nil { - err := fmt.Errorf("Error rendering user_data: %s", err) - ui.Error(err.Error()) + state.Put("error", fmt.Errorf("Error rendering user_data: %s", err)) return multistep.ActionHalt } ud, err := getUserData(renderedUserData, config.HTTPGetOnly) if err != nil { - ui.Error(err.Error()) + state.Put("error", err) return multistep.ActionHalt } @@ -107,7 +108,7 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction // Create the new instance. instance, err := client.VirtualMachine.DeployVirtualMachine(p) 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 } diff --git a/builder/cloudstack/step_create_template.go b/builder/cloudstack/step_create_template.go index 9845a3166..12f851802 100644 --- a/builder/cloudstack/step_create_template.go +++ b/builder/cloudstack/step_create_template.go @@ -21,7 +21,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction // Retrieve the instance ID from the previously saved state. instanceID, ok := state.Get("instance_id").(string) 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 } @@ -50,7 +50,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction ui.Message("Retrieving the ROOT volume ID...") volumeID, err := getRootVolumeID(client, instanceID) if err != nil { - ui.Error(err.Error()) + state.Put("error", err) return multistep.ActionHalt } @@ -60,7 +60,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction ui.Message("Creating the new template...") template, err := client.Template.CreateTemplate(p) 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 } @@ -72,7 +72,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction ui.Message("Template has been created!") - // Store the template ID. + // Store the template. state.Put("template", template) return multistep.ActionContinue diff --git a/builder/cloudstack/step_prepare_config.go b/builder/cloudstack/step_prepare_config.go index 82b75dccf..2021f8214 100644 --- a/builder/cloudstack/step_prepare_config.go +++ b/builder/cloudstack/step_prepare_config.go @@ -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 // nil interface. if errs != nil && len(errs.Errors) > 0 { - ui.Error(errs.Error()) + state.Put("error", errs) return multistep.ActionHalt } diff --git a/builder/cloudstack/step_shutdown_instance.go b/builder/cloudstack/step_shutdown_instance.go index d472a79d8..5333c0755 100644 --- a/builder/cloudstack/step_shutdown_instance.go +++ b/builder/cloudstack/step_shutdown_instance.go @@ -20,7 +20,7 @@ func (s *stepShutdownInstance) Run(state multistep.StateBag) multistep.StepActio // Retrieve the instance ID from the previously saved state. instanceID, ok := state.Get("instance_id").(string) 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 } @@ -30,7 +30,7 @@ func (s *stepShutdownInstance) Run(state multistep.StateBag) multistep.StepActio // Shutdown the virtual machine. _, err := client.VirtualMachine.StopVirtualMachine(p) 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 }