2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2020-09-13 03:21:07 -05:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
xenapi "github.com/terra-farm/go-xen-api-client"
|
2014-12-17 12:45:25 -06:00
|
|
|
)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
type StepUploadVdi struct {
|
2015-02-23 12:30:57 -06:00
|
|
|
VdiNameFunc func() string
|
2014-12-17 12:45:25 -06:00
|
|
|
ImagePathFunc func() string
|
|
|
|
VdiUuidKey string
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepUploadVdi) Run(state multistep.StateBag) multistep.StepAction {
|
2014-12-17 12:45:25 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-13 03:21:07 -05:00
|
|
|
c := state.Get("client").(*Connection)
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
imagePath := self.ImagePathFunc()
|
2015-05-18 10:13:02 -05:00
|
|
|
vdiName := self.VdiNameFunc()
|
2014-12-17 12:45:25 -06:00
|
|
|
if imagePath == "" {
|
|
|
|
// skip if no disk image to attach
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2015-02-23 12:30:57 -06:00
|
|
|
ui.Say(fmt.Sprintf("Step: Upload VDI '%s'", vdiName))
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
// Create VDI for the image
|
2020-09-13 03:21:07 -05:00
|
|
|
srs, err := c.client.SR.GetAll(c.session)
|
|
|
|
ui.Say(fmt.Sprintf("Step: Found SRs '%v'", srs))
|
|
|
|
|
|
|
|
srs, err = c.client.SR.GetByNameLabel(c.session, "LocalISO")
|
|
|
|
sr := srs[0]
|
|
|
|
ui.Say(fmt.Sprintf("Step: Found SRs '%v' Choosing: '%v'", srs, sr))
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
// Open the file for reading (NB: HTTPUpload closes the file for us)
|
2014-12-17 12:45:25 -06:00
|
|
|
fh, err := os.Open(imagePath)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to open disk image '%s': %s", imagePath, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file length
|
|
|
|
fstat, err := fh.Stat()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to stat disk image '%s': %s", imagePath, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
fileLength := fstat.Size()
|
|
|
|
|
|
|
|
// Create the VDI
|
2020-09-13 03:21:07 -05:00
|
|
|
// vdi, err := sr.CreateVdi(vdiName, fileLength)
|
|
|
|
vdi, err := c.client.VDI.Create(c.session, xenapi.VDIRecord{
|
|
|
|
NameLabel: vdiName,
|
|
|
|
VirtualSize: int(fileLength),
|
|
|
|
Type: "user",
|
|
|
|
Sharable: false,
|
|
|
|
ReadOnly: false,
|
|
|
|
SR: sr,
|
|
|
|
OtherConfig: map[string]string{
|
|
|
|
"temp": "temp",
|
|
|
|
},
|
|
|
|
})
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
2015-02-23 12:30:57 -06:00
|
|
|
ui.Error(fmt.Sprintf("Unable to create VDI '%s': %s", vdiName, err.Error()))
|
2014-12-17 12:45:25 -06:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
vdiUuid, err := c.client.VDI.GetUUID(c.session, vdi)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
2015-02-23 12:30:57 -06:00
|
|
|
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", vdiName, err.Error()))
|
2014-12-17 12:45:25 -06:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put(self.VdiUuidKey, vdiUuid)
|
|
|
|
|
2014-12-31 09:21:54 -06:00
|
|
|
_, err = HTTPUpload(fmt.Sprintf("https://%s/import_raw_vdi?vdi=%s&session_id=%s",
|
2020-09-13 03:21:07 -05:00
|
|
|
c.Host,
|
|
|
|
vdi,
|
|
|
|
c.GetSession(),
|
2014-12-31 09:21:15 -06:00
|
|
|
), fh, state)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
2014-12-31 09:21:15 -06:00
|
|
|
ui.Error(fmt.Sprintf("Unable to upload VDI: %s", err.Error()))
|
2014-12-17 12:45:25 -06:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepUploadVdi) Cleanup(state multistep.StateBag) {
|
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2014-12-17 12:45:25 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-13 03:21:07 -05:00
|
|
|
c := state.Get("client").(*Connection)
|
2014-12-17 12:45:25 -06:00
|
|
|
|
2015-05-18 10:13:02 -05:00
|
|
|
vdiName := self.VdiNameFunc()
|
2015-02-23 12:30:57 -06:00
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if config.ShouldKeepVM(state) {
|
2014-12-17 12:45:25 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vdiUuidRaw, ok := state.GetOk(self.VdiUuidKey)
|
|
|
|
if !ok {
|
|
|
|
// VDI doesn't exist
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vdiUuid := vdiUuidRaw.(string)
|
|
|
|
if vdiUuid == "" {
|
|
|
|
// VDI already cleaned up
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
vdi, err := c.client.VDI.GetByUUID(c.session, vdiUuid)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Can't get VDI '%s': %s", vdiUuid, err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// an interrupted import_raw_vdi takes a while to release the VDI
|
|
|
|
// so try several times
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
log.Printf("Trying to destroy VDI...")
|
2020-09-13 03:21:07 -05:00
|
|
|
err = c.client.VDI.Destroy(c.session, vdi)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Can't destroy VDI '%s': %s", vdiUuid, err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2015-02-23 12:30:57 -06:00
|
|
|
ui.Say(fmt.Sprintf("Destroyed VDI '%s'", vdiName))
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
state.Put(self.VdiUuidKey, "")
|
|
|
|
}
|