packer-plugin-xenserver/builder/xenserver/step_attach_vdi.go
Cheng Sun e308c0759f VDI upload, floppy and tools support
This commit add new VDI steps named step_{attach,detach,find,upload}_vdi
  - attach/detach are self-explanatory; can specify CD or Floppy
  - find finds a VDI by name-label and stores the uuid
  - upload uploads a VDI from a local disk image and stores the uuid

In order to demonstrate the new VDI code, this commit trivially adds:
  - support for uploading ISOs again (dependent on unmerged XAPI patch)
  - initial floppy upload/attach support (dependent on currently-broken
    XAPI patch)
  - initial XenServer Tools support
2014-12-19 12:46:36 +00:00

81 lines
1.9 KiB
Go

package xenserver
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
)
type stepAttachVdi struct {
VdiUuidKey string
VdiType VDIType
vdi *VDI
}
func (self *stepAttachVdi) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(XenAPIClient)
var vdiUuid string
if vdiUuidRaw, ok := state.GetOk(self.VdiUuidKey); ok {
vdiUuid = vdiUuidRaw.(string)
} else {
log.Printf("Skipping attach of '%s'", self.VdiUuidKey)
return multistep.ActionContinue
}
var err error
self.vdi, err = client.GetVdiByUuid(vdiUuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VDI from UUID '%s': %s", vdiUuid, err.Error()))
return multistep.ActionHalt
}
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
}
err = instance.ConnectVdi(self.vdi, self.VdiType)
if err != nil {
ui.Error(fmt.Sprintf("Error attaching VDI '%s': '%s'", vdiUuid, err.Error()))
return multistep.ActionHalt
}
log.Printf("Attached VDI '%s'", vdiUuid)
return multistep.ActionContinue
}
func (self *stepAttachVdi) Cleanup(state multistep.StateBag) {
config := state.Get("config").(config)
client := state.Get("client").(XenAPIClient)
if config.ShouldKeepInstance(state) {
return
}
if self.vdi == nil {
return
}
uuid := state.Get("instance_uuid").(string)
instance, err := client.GetVMByUuid(uuid)
if err != nil {
log.Printf("Unable to get VM from UUID '%s': %s", uuid, err.Error())
return
}
vdiUuid := state.Get(self.VdiUuidKey).(string)
err = instance.DisconnectVdi(self.vdi)
if err != nil {
log.Printf("Unable to disconnect VDI '%s': %s", vdiUuid, err.Error())
return
}
log.Printf("Detached VDI '%s'", vdiUuid)
}