From 44089b2f55864b6b9d9fe9d978e979ab22fca59b Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Fri, 14 Jul 2017 07:11:30 +0200 Subject: [PATCH 1/2] cloudstack: Improved error reporting Ensure that errors are shown when they happens. --- .../cloudstack/step_configure_networking.go | 29 ++++++++++++++----- builder/cloudstack/step_create_instance.go | 10 ++++++- builder/cloudstack/step_create_template.go | 9 ++++-- builder/cloudstack/step_prepare_config.go | 10 +++++++ builder/cloudstack/step_shutdown_instance.go | 4 ++- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/builder/cloudstack/step_configure_networking.go b/builder/cloudstack/step_configure_networking.go index b7161f281..48a3742ab 100644 --- a/builder/cloudstack/step_configure_networking.go +++ b/builder/cloudstack/step_configure_networking.go @@ -47,7 +47,9 @@ 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 == "" { - state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!")) + err := fmt.Errorf("Could not retrieve instance_id from state!") + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -56,7 +58,9 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction cloudstack.WithProject(config.Project), ) if err != nil { - state.Put("error", fmt.Errorf("Failed to retrieve the network object: %s", err)) + err := fmt.Errorf("Failed to retrieve the network object: %s", err) + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -79,7 +83,9 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction // Associate a new public IP address. ipAddr, err := client.Address.AssociateIpAddress(p) if err != nil { - state.Put("error", fmt.Errorf("Failed to associate public IP address: %s", err)) + err := fmt.Errorf("Failed to associate public IP address: %s", err) + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -107,7 +113,10 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction // Create the port forward. forward, err := client.Firewall.CreatePortForwardingRule(p) if err != nil { - ui.Error(fmt.Sprintf("Failed to create port forward: %s", err)) + err := fmt.Errorf("Failed to create port forward: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } // Store the port forward ID. @@ -117,7 +126,9 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction ui.Message("Creating network ACL rule...") if network.Aclid == "" { - state.Put("error", fmt.Errorf("Failed to configure the firewall: no ACL connected to the VPC network")) + err := fmt.Errorf("Failed to configure the firewall: no ACL connected to the VPC network") + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -135,7 +146,9 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction // Create the network ACL rule. aclRule, err := client.NetworkACL.CreateNetworkACL(p) if err != nil { - state.Put("error", fmt.Errorf("Failed to create network ACL rule: %s", err)) + err := fmt.Errorf("Failed to create network ACL rule: %s", err) + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -154,7 +167,9 @@ func (s *stepSetupNetworking) Run(state multistep.StateBag) multistep.StepAction fwRule, err := client.Firewall.CreateFirewallRule(p) if err != nil { - state.Put("error", fmt.Errorf("Failed to create firewall rule: %s", err)) + err := fmt.Errorf("Failed to create firewall rule: %s", err) + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/cloudstack/step_create_instance.go b/builder/cloudstack/step_create_instance.go index 1693b654f..3cc3e5ac4 100644 --- a/builder/cloudstack/step_create_instance.go +++ b/builder/cloudstack/step_create_instance.go @@ -62,7 +62,9 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction // Retrieve the zone object. zone, _, err := client.Zone.GetZoneByID(config.Zone) if err != nil { + err := fmt.Errorf("Failed to get Zone by ID: %s - %s", config.Zone, err) state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -80,7 +82,9 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction httpPort := state.Get("http_port").(uint) httpIP, err := hostIP() if err != nil { + err := fmt.Errorf("Failed to determine host IP: %s", err) state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } common.SetHTTPIP(httpIP) @@ -92,7 +96,9 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction ud, err := s.generateUserData(config.UserData, config.HTTPGetOnly) if err != nil { + err := fmt.Errorf("Failed to interpolate user_data: %s", err) state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -102,7 +108,9 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction // Create the new instance. instance, err := client.VirtualMachine.DeployVirtualMachine(p) if err != nil { - state.Put("error", fmt.Errorf("Error creating new instance %s: %s", config.InstanceName, err)) + err := fmt.Errorf("Error creating new instance %s: %s", config.InstanceName, err) + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/cloudstack/step_create_template.go b/builder/cloudstack/step_create_template.go index 12f851802..18d6c1fb0 100644 --- a/builder/cloudstack/step_create_template.go +++ b/builder/cloudstack/step_create_template.go @@ -21,7 +21,9 @@ 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 == "" { - state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!")) + err := fmt.Errorf("Could not retrieve instance_id from state!") + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -51,6 +53,7 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction volumeID, err := getRootVolumeID(client, instanceID) if err != nil { state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } @@ -60,7 +63,9 @@ func (s *stepCreateTemplate) Run(state multistep.StateBag) multistep.StepAction ui.Message("Creating the new template...") template, err := client.Template.CreateTemplate(p) if err != nil { - state.Put("error", fmt.Errorf("Error creating the new template %s: %s", config.TemplateName, err)) + err := fmt.Errorf("Error creating the new template %s: %s", config.TemplateName, err) + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } diff --git a/builder/cloudstack/step_prepare_config.go b/builder/cloudstack/step_prepare_config.go index b1390b9da..80668cb8f 100644 --- a/builder/cloudstack/step_prepare_config.go +++ b/builder/cloudstack/step_prepare_config.go @@ -26,6 +26,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if config.Project != "" && !isUUID(config.Project) { config.Project, _, err = client.Project.GetProjectID(config.Project) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"project", config.Project, err}) } } @@ -33,6 +34,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if config.UserDataFile != "" { userdata, err := ioutil.ReadFile(config.UserDataFile) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, fmt.Errorf("problem reading user data file: %s", err)) } config.UserData = string(userdata) @@ -41,6 +43,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if !isUUID(config.Zone) { config.Zone, _, err = client.Zone.GetZoneID(config.Zone) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"zone", config.Zone, err}) } } @@ -49,6 +52,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if config.DiskOffering != "" && !isUUID(config.DiskOffering) { config.DiskOffering, _, err = client.DiskOffering.GetDiskOfferingID(config.DiskOffering) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"disk offering", config.DiskOffering, err}) } } @@ -66,6 +70,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { ipAddrs, err := client.Address.ListPublicIpAddresses(p) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"IP address", config.PublicIPAddress, err}) } if err == nil && ipAddrs.Count != 1 { @@ -79,6 +84,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if !isUUID(config.Network) { config.Network, _, err = client.Network.GetNetworkID(config.Network, cloudstack.WithProject(config.Project)) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"network", config.Network, err}) } } @@ -86,6 +92,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if !isUUID(config.ServiceOffering) { config.ServiceOffering, _, err = client.ServiceOffering.GetServiceOfferingID(config.ServiceOffering) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"service offering", config.ServiceOffering, err}) } } @@ -96,6 +103,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { } else { config.instanceSource, _, err = client.ISO.GetIsoID(config.SourceISO, "executable", config.Zone) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"ISO", config.SourceISO, err}) } } @@ -107,6 +115,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { } else { config.instanceSource, _, err = client.Template.GetTemplateID(config.SourceTemplate, "executable", config.Zone) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"template", config.SourceTemplate, err}) } } @@ -118,6 +127,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { types, err := client.GuestOS.ListOsTypes(p) if err != nil { + ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"OS type", config.TemplateOS, err}) } if err == nil && types.Count != 1 { diff --git a/builder/cloudstack/step_shutdown_instance.go b/builder/cloudstack/step_shutdown_instance.go index 638b0e58b..b090a5836 100644 --- a/builder/cloudstack/step_shutdown_instance.go +++ b/builder/cloudstack/step_shutdown_instance.go @@ -30,7 +30,9 @@ func (s *stepShutdownInstance) Run(state multistep.StateBag) multistep.StepActio // Shutdown the virtual machine. _, err := client.VirtualMachine.StopVirtualMachine(p) if err != nil { - state.Put("error", fmt.Errorf("Error shutting down instance %s: %s", config.InstanceName, err)) + err := fmt.Errorf("Error shutting down instance %s: %s", config.InstanceName, err) + state.Put("error", err) + ui.Error(err.Error()) return multistep.ActionHalt } From 62dac4a16827e25da12835e7ae6fa1c786ccc39f Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Fri, 14 Jul 2017 11:09:21 +0200 Subject: [PATCH 2/2] cloudstack: some updates after review --- builder/cloudstack/step_create_instance.go | 3 +-- builder/cloudstack/step_prepare_config.go | 11 +---------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/builder/cloudstack/step_create_instance.go b/builder/cloudstack/step_create_instance.go index 3cc3e5ac4..3b624de3c 100644 --- a/builder/cloudstack/step_create_instance.go +++ b/builder/cloudstack/step_create_instance.go @@ -62,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 { - err := fmt.Errorf("Failed to get Zone by ID: %s - %s", config.Zone, err) + err := fmt.Errorf("Failed to get zone %s by ID: %s", config.Zone, err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt @@ -96,7 +96,6 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction ud, err := s.generateUserData(config.UserData, config.HTTPGetOnly) if err != nil { - err := fmt.Errorf("Failed to interpolate user_data: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt diff --git a/builder/cloudstack/step_prepare_config.go b/builder/cloudstack/step_prepare_config.go index 80668cb8f..af28a76a8 100644 --- a/builder/cloudstack/step_prepare_config.go +++ b/builder/cloudstack/step_prepare_config.go @@ -26,7 +26,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if config.Project != "" && !isUUID(config.Project) { config.Project, _, err = client.Project.GetProjectID(config.Project) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"project", config.Project, err}) } } @@ -34,7 +33,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if config.UserDataFile != "" { userdata, err := ioutil.ReadFile(config.UserDataFile) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, fmt.Errorf("problem reading user data file: %s", err)) } config.UserData = string(userdata) @@ -43,7 +41,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if !isUUID(config.Zone) { config.Zone, _, err = client.Zone.GetZoneID(config.Zone) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"zone", config.Zone, err}) } } @@ -52,7 +49,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if config.DiskOffering != "" && !isUUID(config.DiskOffering) { config.DiskOffering, _, err = client.DiskOffering.GetDiskOfferingID(config.DiskOffering) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"disk offering", config.DiskOffering, err}) } } @@ -70,7 +66,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { ipAddrs, err := client.Address.ListPublicIpAddresses(p) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"IP address", config.PublicIPAddress, err}) } if err == nil && ipAddrs.Count != 1 { @@ -84,7 +79,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if !isUUID(config.Network) { config.Network, _, err = client.Network.GetNetworkID(config.Network, cloudstack.WithProject(config.Project)) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"network", config.Network, err}) } } @@ -92,7 +86,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { if !isUUID(config.ServiceOffering) { config.ServiceOffering, _, err = client.ServiceOffering.GetServiceOfferingID(config.ServiceOffering) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"service offering", config.ServiceOffering, err}) } } @@ -103,7 +96,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { } else { config.instanceSource, _, err = client.ISO.GetIsoID(config.SourceISO, "executable", config.Zone) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"ISO", config.SourceISO, err}) } } @@ -115,7 +107,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { } else { config.instanceSource, _, err = client.Template.GetTemplateID(config.SourceTemplate, "executable", config.Zone) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"template", config.SourceTemplate, err}) } } @@ -127,7 +118,6 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { types, err := client.GuestOS.ListOsTypes(p) if err != nil { - ui.Error(err.Error()) errs = packer.MultiErrorAppend(errs, &retrieveErr{"OS type", config.TemplateOS, err}) } if err == nil && types.Count != 1 { @@ -144,6 +134,7 @@ func (s *stepPrepareConfig) Run(state multistep.StateBag) multistep.StepAction { // nil interface. if errs != nil && len(errs.Errors) > 0 { state.Put("error", errs) + ui.Error(errs.Error()) return multistep.ActionHalt }