Clean up VM instance and root VDI

This commit is contained in:
Cheng Sun 2014-12-09 16:40:32 +00:00
parent acd16bb984
commit 1450fd0568
2 changed files with 49 additions and 15 deletions

View File

@ -320,6 +320,15 @@ func (self *VM) Clone(label string) (new_instance *VM, err error) {
return return
} }
func (self *VM) Destroy() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.destroy", self.Ref)
if err != nil {
return err
}
return
}
func (self *VM) Start(paused, force bool) (err error) { func (self *VM) Start(paused, force bool) (err error) {
result := APIResult{} result := APIResult{}
err = self.Client.APICall(&result, "VM.start", self.Ref, paused, force) err = self.Client.APICall(&result, "VM.start", self.Ref, paused, force)
@ -338,6 +347,15 @@ func (self *VM) CleanShutdown() (err error) {
return return
} }
func (self *VM) HardShutdown() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VM.hard_shutdown", self.Ref)
if err != nil {
return err
}
return
}
func (self *VM) Unpause() (err error) { func (self *VM) Unpause() (err error) {
result := APIResult{} result := APIResult{}
err = self.Client.APICall(&result, "VM.unpause", self.Ref) err = self.Client.APICall(&result, "VM.unpause", self.Ref)
@ -725,6 +743,15 @@ func (self *VDI) GetUuid() (vdi_uuid string, err error) {
return vdi_uuid, nil return vdi_uuid, nil
} }
func (self *VDI) Destroy() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VDI.destroy", self.Ref)
if err != nil {
return err
}
return
}
// Client Initiator // Client Initiator
func NewXenAPIClient(host, username, password string) (client XenAPIClient) { func NewXenAPIClient(host, username, password string) (client XenAPIClient) {

View File

@ -7,7 +7,8 @@ import (
) )
type stepCreateInstance struct { type stepCreateInstance struct {
InstanceId string instance *VM
vdi *VDI
} }
func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction { func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
@ -39,6 +40,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
ui.Error(fmt.Sprintf("Error cloning VM: %s", err.Error())) ui.Error(fmt.Sprintf("Error cloning VM: %s", err.Error()))
return multistep.ActionHalt return multistep.ActionHalt
} }
self.instance = instance
err = instance.SetIsATemplate(false) err = instance.SetIsATemplate(false)
if err != nil { if err != nil {
@ -97,6 +99,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error())) ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error()))
return multistep.ActionHalt return multistep.ActionHalt
} }
self.vdi = vdi
err = instance.ConnectVdi(vdi, false) err = instance.ConnectVdi(vdi, false)
if err != nil { if err != nil {
@ -197,32 +200,36 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt return multistep.ActionHalt
} }
// Stash the VM reference instanceId, err := instance.GetUuid()
self.InstanceId, err = instance.GetUuid()
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error())) ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("instance_uuid", self.InstanceId) state.Put("instance_uuid", 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'", instanceId))
return multistep.ActionContinue return multistep.ActionContinue
} }
func (self *stepCreateInstance) Cleanup(state multistep.StateBag) { func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
// client := state.Get("client").(*XenAPIClient) if self.instance != nil {
// config := state.Get("config").(config) ui.Say("Destroying VM")
// ui := state.Get("ui").(packer.Ui) _ = self.instance.HardShutdown()
err := self.instance.Destroy()
// If instance hasn't been created, we have nothing to do. if err != nil {
if self.InstanceId == "" { ui.Error(err.Error())
return }
} }
// @todo: destroy the created instance. if self.vdi != nil {
ui.Say("Destroying VDI")
return err := self.vdi.Destroy()
if err != nil {
ui.Error(err.Error())
}
}
} }