diff --git a/builder/xenserver/client.go b/builder/xenserver/client.go index 4957ba7..a1bb3dd 100644 --- a/builder/xenserver/client.go +++ b/builder/xenserver/client.go @@ -111,7 +111,7 @@ func (client *XenAPIClient) APICall(result *APIResult, method string, params ... if result.Status != "Success" { fmt.Println("Encountered an API error: ", result.Status) fmt.Println(res["ErrorDescription"]) - return errors.New("API Error occurred") + return fmt.Errorf("API Error: %s", res["ErrorDescription"]) } else { result.Value = res["Value"] } diff --git a/builder/xenserver/step_create_instance.go b/builder/xenserver/step_create_instance.go index bbf05d2..5a6c9be 100644 --- a/builder/xenserver/step_create_instance.go +++ b/builder/xenserver/step_create_instance.go @@ -34,10 +34,29 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi template := vms[0] // Clone that VM template - instance, _ := template.Clone(config.InstanceName) - instance.SetIsATemplate(false) - instance.SetStaticMemoryRange(config.InstanceMemory, config.InstanceMemory) + instance, err := template.Clone(config.InstanceName) + if err != nil { + ui.Error(fmt.Sprintf("Error cloning VM: %s", err.Error())) + return multistep.ActionHalt + } + + err = instance.SetIsATemplate(false) + if err != nil { + ui.Error(fmt.Sprintf("Error setting is_a_template=false: %s", err.Error())) + return multistep.ActionHalt + } + + err = instance.SetStaticMemoryRange(config.InstanceMemory, config.InstanceMemory) + if err != nil { + ui.Error(fmt.Sprintf("Error setting VM memory=%s: %s", config.InstanceMemory, err.Error())) + return multistep.ActionHalt + } + instance.SetPlatform(config.PlatformArgs) + if err != nil { + ui.Error(fmt.Sprintf("Error setting VM platform: %s", err.Error())) + return multistep.ActionHalt + } // Create VDI for the instance var sr *SR @@ -73,9 +92,17 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi sr = srs[0] } - vdi, _ := sr.CreateVdi("Packer-disk", config.RootDiskSize) + vdi, err := sr.CreateVdi("Packer-disk", config.RootDiskSize) + if err != nil { + ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error())) + return multistep.ActionHalt + } - instance.ConnectVdi(vdi, false) + err = instance.ConnectVdi(vdi, false) + if err != nil { + ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error())) + return multistep.ActionHalt + } // Connect Network @@ -90,7 +117,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi pifs, err := client.GetPIFs() if err != nil { - ui.Error(fmt.Sprintf("Error getting PIFs %s", err.Error())) + ui.Error(fmt.Sprintf("Error getting PIFs: %s", err.Error())) return multistep.ActionHalt } @@ -163,10 +190,20 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi //iso, _ := client.GetVdiByUuid(config.IsoUuid) //ui.Say("Using VDI: " + iso_vdi_uuid) //iso, _ := client.GetVdiByUuid(iso_vdi_uuid) - instance.ConnectVdi(iso, true) + + err = instance.ConnectVdi(iso, true) + if err != nil { + ui.Error(fmt.Sprintf("Unable to connect ISO VDI: %s", err.Error())) + return multistep.ActionHalt + } // Stash the VM reference - self.InstanceId, _ = instance.GetUuid() + self.InstanceId, err = instance.GetUuid() + if err != nil { + ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error())) + return multistep.ActionHalt + } + state.Put("instance_uuid", self.InstanceId) state.Put("instance", instance) ui.Say(fmt.Sprintf("Created instance '%s'", self.InstanceId)) diff --git a/builder/xenserver/step_get_vnc_port.go b/builder/xenserver/step_get_vnc_port.go index 838da6d..ff3ef22 100644 --- a/builder/xenserver/step_get_vnc_port.go +++ b/builder/xenserver/step_get_vnc_port.go @@ -19,7 +19,11 @@ func (self *stepGetVNCPort) Run(state multistep.StateBag) multistep.StepAction { domid := state.Get("domid").(string) cmd := fmt.Sprintf("xenstore-read /local/domain/%s/console/vnc-port", domid) - remote_vncport, _ := execute_ssh_cmd(cmd, config.HostIp, "22", config.Username, config.Password) + remote_vncport, err := execute_ssh_cmd(cmd, config.HostIp, "22", config.Username, config.Password) + if err != nil { + ui.Error(fmt.Sprintf("Unable to get VNC port (is the VM running?): %s", err.Error())) + return multistep.ActionHalt + } remote_port, err := strconv.ParseUint(remote_vncport, 10, 16) diff --git a/builder/xenserver/step_shutdown_and_export.go b/builder/xenserver/step_shutdown_and_export.go index 81dca51..ed25fd4 100644 --- a/builder/xenserver/step_shutdown_and_export.go +++ b/builder/xenserver/step_shutdown_and_export.go @@ -49,13 +49,21 @@ func (stepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction client := state.Get("client").(XenAPIClient) instance_uuid := state.Get("instance_uuid").(string) - instance, _ := client.GetVMByUuid(instance_uuid) + instance, err := client.GetVMByUuid(instance_uuid) + if err != nil { + ui.Error(fmt.Sprintf("Could not get VM with UUID '%s': %s", instance_uuid, err.Error())) + return multistep.ActionHalt + } ui.Say("Step: Shutdown and export VPX") // Shutdown the VM ui.Say("Shutting down the VM...") - instance.CleanShutdown() + err = instance.CleanShutdown() + if err != nil { + ui.Error(fmt.Sprintf("Could not shut down VM: %s", err.Error())) + return multistep.ActionHalt + } //Export the VM @@ -69,9 +77,17 @@ func (stepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction ui.Say("Getting metadata " + export_url) downloadFile(export_url, export_filename) - disks, _ := instance.GetDisks() + disks, err := instance.GetDisks() + if err != nil { + ui.Error(fmt.Sprintf("Could not get VM disks: %s", err.Error())) + return multistep.ActionHalt + } for _, disk := range disks { - disk_uuid, _ := disk.GetUuid() + disk_uuid, err := disk.GetUuid() + if err != nil { + ui.Error(fmt.Sprintf("Could not get disk with UUID '%s': %s", disk_uuid, err.Error())) + return multistep.ActionHalt + } // Basic auth in URL request is required as session token is not // accepted for some reason. diff --git a/builder/xenserver/step_start_vm_paused.go b/builder/xenserver/step_start_vm_paused.go index fd10f4d..8a0e246 100644 --- a/builder/xenserver/step_start_vm_paused.go +++ b/builder/xenserver/step_start_vm_paused.go @@ -1,6 +1,7 @@ package xenserver import ( + "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" ) @@ -14,11 +15,24 @@ func (self *stepStartVmPaused) Run(state multistep.StateBag) multistep.StepActio ui.Say("Step: Start VM Paused") - instance, _ := client.GetVMByUuid(state.Get("instance_uuid").(string)) + uuid := state.Get("instance_uuid").(string) + instance, err := client.GetVMByUuid(uuid) + if err != nil { + ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error())) + return multistep.ActionHalt + } - instance.Start(true, false) + err = instance.Start(true, false) + if err != nil { + ui.Error(fmt.Sprintf("Unable to start VM with UUID '%s': %s", uuid, err.Error())) + return multistep.ActionHalt + } - domid, _ := instance.GetDomainId() + domid, err := instance.GetDomainId() + if err != nil { + ui.Error(fmt.Sprintf("Unable to get domid of VM with UUID '%s': %s", uuid, err.Error())) + return multistep.ActionHalt + } state.Put("domid", domid) return multistep.ActionContinue