2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-03-13 08:23:20 -05:00
|
|
|
xsclient "github.com/xenserver/go-xenserver-client"
|
2014-12-17 12:45:25 -06:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
type StepAttachVdi struct {
|
2014-12-17 12:45:25 -06:00
|
|
|
VdiUuidKey string
|
2015-03-13 08:23:20 -05:00
|
|
|
VdiType xsclient.VDIType
|
2014-12-17 12:45:25 -06:00
|
|
|
|
2015-03-13 08:23:20 -05:00
|
|
|
vdi *xsclient.VDI
|
2014-12-17 12:45:25 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepAttachVdi) Run(state multistep.StateBag) multistep.StepAction {
|
2014-12-17 12:45:25 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-03-13 08:23:20 -05:00
|
|
|
client := state.Get("client").(xsclient.XenAPIClient)
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-16 15:37:46 -05:00
|
|
|
err = instance.ConnectVdi(self.vdi, self.VdiType, "")
|
2014-12-17 12:45:25 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepAttachVdi) Cleanup(state multistep.StateBag) {
|
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2015-03-13 08:23:20 -05:00
|
|
|
client := state.Get("client").(xsclient.XenAPIClient)
|
2014-12-29 06:46:54 -06:00
|
|
|
if config.ShouldKeepVM(state) {
|
2014-12-17 12:45:25 -06:00
|
|
|
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)
|
|
|
|
}
|