Clean up VM instance and root VDI
This commit is contained in:
parent
acd16bb984
commit
1450fd0568
@ -320,6 +320,15 @@ func (self *VM) Clone(label string) (new_instance *VM, err error) {
|
||||
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) {
|
||||
result := APIResult{}
|
||||
err = self.Client.APICall(&result, "VM.start", self.Ref, paused, force)
|
||||
@ -338,6 +347,15 @@ func (self *VM) CleanShutdown() (err error) {
|
||||
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) {
|
||||
result := APIResult{}
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
func NewXenAPIClient(host, username, password string) (client XenAPIClient) {
|
||||
|
@ -7,7 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type stepCreateInstance struct {
|
||||
InstanceId string
|
||||
instance *VM
|
||||
vdi *VDI
|
||||
}
|
||||
|
||||
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()))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
self.instance = instance
|
||||
|
||||
err = instance.SetIsATemplate(false)
|
||||
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()))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
self.vdi = vdi
|
||||
|
||||
err = instance.ConnectVdi(vdi, false)
|
||||
if err != nil {
|
||||
@ -197,32 +200,36 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
// Stash the VM reference
|
||||
self.InstanceId, err = instance.GetUuid()
|
||||
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", instanceId)
|
||||
state.Put("instance", instance)
|
||||
ui.Say(fmt.Sprintf("Created instance '%s'", self.InstanceId))
|
||||
ui.Say(fmt.Sprintf("Created instance '%s'", instanceId))
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
|
||||
// client := state.Get("client").(*XenAPIClient)
|
||||
// config := state.Get("config").(config)
|
||||
// ui := state.Get("ui").(packer.Ui)
|
||||
|
||||
// If instance hasn't been created, we have nothing to do.
|
||||
if self.InstanceId == "" {
|
||||
return
|
||||
if self.instance != nil {
|
||||
ui.Say("Destroying VM")
|
||||
_ = self.instance.HardShutdown()
|
||||
err := self.instance.Destroy()
|
||||
if err != nil {
|
||||
ui.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// @todo: destroy the created instance.
|
||||
|
||||
return
|
||||
if self.vdi != nil {
|
||||
ui.Say("Destroying VDI")
|
||||
err := self.vdi.Destroy()
|
||||
if err != nil {
|
||||
ui.Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user