Add more error handling
This commit is contained in:
parent
70e57a2d66
commit
8a41cc693a
@ -111,7 +111,7 @@ func (client *XenAPIClient) APICall(result *APIResult, method string, params ...
|
|||||||
if result.Status != "Success" {
|
if result.Status != "Success" {
|
||||||
fmt.Println("Encountered an API error: ", result.Status)
|
fmt.Println("Encountered an API error: ", result.Status)
|
||||||
fmt.Println(res["ErrorDescription"])
|
fmt.Println(res["ErrorDescription"])
|
||||||
return errors.New("API Error occurred")
|
return fmt.Errorf("API Error: %s", res["ErrorDescription"])
|
||||||
} else {
|
} else {
|
||||||
result.Value = res["Value"]
|
result.Value = res["Value"]
|
||||||
}
|
}
|
||||||
|
@ -34,10 +34,29 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
|
|||||||
template := vms[0]
|
template := vms[0]
|
||||||
|
|
||||||
// Clone that VM template
|
// Clone that VM template
|
||||||
instance, _ := template.Clone(config.InstanceName)
|
instance, err := template.Clone(config.InstanceName)
|
||||||
instance.SetIsATemplate(false)
|
if err != nil {
|
||||||
instance.SetStaticMemoryRange(config.InstanceMemory, config.InstanceMemory)
|
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)
|
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
|
// Create VDI for the instance
|
||||||
var sr *SR
|
var sr *SR
|
||||||
@ -73,9 +92,17 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
|
|||||||
sr = srs[0]
|
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
|
// Connect Network
|
||||||
|
|
||||||
@ -90,7 +117,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
|
|||||||
pifs, err := client.GetPIFs()
|
pifs, err := client.GetPIFs()
|
||||||
|
|
||||||
if err != nil {
|
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
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,10 +190,20 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
|
|||||||
//iso, _ := client.GetVdiByUuid(config.IsoUuid)
|
//iso, _ := client.GetVdiByUuid(config.IsoUuid)
|
||||||
//ui.Say("Using VDI: " + iso_vdi_uuid)
|
//ui.Say("Using VDI: " + iso_vdi_uuid)
|
||||||
//iso, _ := client.GetVdiByUuid(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
|
// 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_uuid", self.InstanceId)
|
||||||
state.Put("instance", instance)
|
state.Put("instance", instance)
|
||||||
ui.Say(fmt.Sprintf("Created instance '%s'", self.InstanceId))
|
ui.Say(fmt.Sprintf("Created instance '%s'", self.InstanceId))
|
||||||
|
@ -19,7 +19,11 @@ func (self *stepGetVNCPort) Run(state multistep.StateBag) multistep.StepAction {
|
|||||||
domid := state.Get("domid").(string)
|
domid := state.Get("domid").(string)
|
||||||
cmd := fmt.Sprintf("xenstore-read /local/domain/%s/console/vnc-port", domid)
|
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)
|
remote_port, err := strconv.ParseUint(remote_vncport, 10, 16)
|
||||||
|
|
||||||
|
@ -49,13 +49,21 @@ func (stepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction
|
|||||||
client := state.Get("client").(XenAPIClient)
|
client := state.Get("client").(XenAPIClient)
|
||||||
instance_uuid := state.Get("instance_uuid").(string)
|
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")
|
ui.Say("Step: Shutdown and export VPX")
|
||||||
|
|
||||||
// Shutdown the VM
|
// Shutdown the VM
|
||||||
ui.Say("Shutting down 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
|
//Export the VM
|
||||||
|
|
||||||
@ -69,9 +77,17 @@ func (stepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction
|
|||||||
ui.Say("Getting metadata " + export_url)
|
ui.Say("Getting metadata " + export_url)
|
||||||
downloadFile(export_url, export_filename)
|
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 {
|
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
|
// Basic auth in URL request is required as session token is not
|
||||||
// accepted for some reason.
|
// accepted for some reason.
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package xenserver
|
package xenserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
@ -14,11 +15,24 @@ func (self *stepStartVmPaused) Run(state multistep.StateBag) multistep.StepActio
|
|||||||
|
|
||||||
ui.Say("Step: Start VM Paused")
|
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)
|
state.Put("domid", domid)
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
|
Loading…
Reference in New Issue
Block a user