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
This commit is contained in:
Cheng Sun 2014-12-17 18:45:25 +00:00
parent 313f5a7354
commit e308c0759f
8 changed files with 559 additions and 171 deletions

View File

@ -9,6 +9,8 @@ import (
"github.com/mitchellh/packer/packer"
"log"
"os"
"path"
"strings"
"time"
)
@ -21,15 +23,14 @@ type config struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
HostIp string `mapstructure:"host_ip"`
IsoUrl string `mapstructure:"iso_url"`
InstanceName string `mapstructure:"instance_name"`
InstanceMemory string `mapstructure:"instance_memory"`
RootDiskSize string `mapstructure:"root_disk_size"`
CloneTemplate string `mapstructure:"clone_template"`
IsoName string `mapstructure:"iso_name"`
SrName string `mapstructure:"sr_name"`
NetworkName string `mapstructure:"network_name"`
InstanceName string `mapstructure:"instance_name"`
InstanceMemory string `mapstructure:"instance_memory"`
RootDiskSize string `mapstructure:"root_disk_size"`
CloneTemplate string `mapstructure:"clone_template"`
SrName string `mapstructure:"sr_name"`
FloppyFiles []string `mapstructure:"floppy_files"`
NetworkName string `mapstructure:"network_name"`
HostPortMin uint `mapstructure:"host_port_min"`
HostPortMax uint `mapstructure:"host_port_max"`
@ -45,6 +46,8 @@ type config struct {
ISOUrls []string `mapstructure:"iso_urls"`
ISOUrl string `mapstructure:"iso_url"`
ToolsIsoName string `mapstructure:"tools_iso_name"`
HTTPDir string `mapstructure:"http_directory"`
HTTPPortMin uint `mapstructure:"http_port_min"`
HTTPPortMax uint `mapstructure:"http_port_max"`
@ -111,6 +114,10 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
self.config.RawInstallTimeout = "200m"
}
if self.config.ToolsIsoName == "" {
self.config.ToolsIsoName = "xs-tools.iso"
}
if self.config.HTTPPortMin == 0 {
self.config.HTTPPortMin = 8000
}
@ -131,6 +138,10 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
self.config.CloneTemplate = "Other install media"
}
if self.config.FloppyFiles == nil {
self.config.FloppyFiles = make([]string, 0)
}
if self.config.OutputDir == "" {
self.config.OutputDir = fmt.Sprintf("output-%s", self.config.PackerBuildName)
}
@ -160,18 +171,18 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
"username": &self.config.Username,
"password": &self.config.Password,
"host_ip": &self.config.HostIp,
"iso_url": &self.config.IsoUrl,
"instance_name": &self.config.InstanceName,
"instance_memory": &self.config.InstanceMemory,
"root_disk_size": &self.config.RootDiskSize,
"clone_template": &self.config.CloneTemplate,
"iso_name": &self.config.IsoName,
"sr_name": &self.config.SrName,
"network_name": &self.config.NetworkName,
"shutdown_command": &self.config.ShutdownCommand,
"boot_wait": &self.config.RawBootWait,
"iso_checksum": &self.config.ISOChecksum,
"iso_checksum_type": &self.config.ISOChecksumType,
"iso_url": &self.config.ISOUrl,
"tools_iso_name": &self.config.ToolsIsoName,
"http_directory": &self.config.HTTPDir,
"local_ip": &self.config.LocalIp,
"install_timeout": &self.config.RawInstallTimeout,
@ -184,6 +195,13 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
"keep_instance": &self.config.KeepInstance,
}
for i := range self.config.FloppyFiles {
templates[fmt.Sprintf("floppy_files[%d]", i)] = &self.config.FloppyFiles[i]
}
for i := range self.config.ISOUrls {
templates[fmt.Sprintf("iso_urls[%d]", i)] = &self.config.ISOUrls[i]
}
for n, ptr := range templates {
var err error
*ptr, err = self.config.tpl.Process(*ptr, nil)
@ -194,18 +212,6 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
// Validation
/*
if self.config.IsoUrl == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("a iso url must be specified"))
}
*/
if self.config.IsoName == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("an iso_name must be specified"))
}
self.config.BootWait, err = time.ParseDuration(self.config.RawBootWait)
if err != nil {
errs = packer.MultiErrorAppend(
@ -301,43 +307,48 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
errs = packer.MultiErrorAppend(
errs, errors.New("the host min port must be less than the max"))
}
/*
if self.config.ISOChecksumType == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("The iso_checksum_type must be specified."))
} else {
self.config.ISOChecksumType = strings.ToLower(self.config.ISOChecksumType)
if self.config.ISOChecksumType != "none" {
if self.config.ISOChecksum == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("Due to the file size being large, an iso_checksum is required."))
} else {
self.config.ISOChecksum = strings.ToLower(self.config.ISOChecksum)
}
if hash := common.HashForType(self.config.ISOChecksumType); hash == nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Unsupported checksum type: %s", self.config.ISOChecksumType))
}
if self.config.ISOChecksumType == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("The iso_checksum_type must be specified."))
} else {
self.config.ISOChecksumType = strings.ToLower(self.config.ISOChecksumType)
if self.config.ISOChecksumType != "none" {
if self.config.ISOChecksum == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("Due to the file size being large, an iso_checksum is required."))
} else {
self.config.ISOChecksum = strings.ToLower(self.config.ISOChecksum)
}
}
}
if hash := common.HashForType(self.config.ISOChecksumType); hash == nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Unsupported checksum type: %s", self.config.ISOChecksumType))
}
if self.config.ISOUrl == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("A ISO URL must be specfied."))
} else {
self.config.ISOUrls = []string{self.config.ISOUrl}
}
}
}
if len(self.config.ISOUrls) == 0 {
if self.config.ISOUrl == "" {
errs = packer.MultiErrorAppend(
errs, errors.New("One of iso_url or iso_urls must be specified."))
} else {
self.config.ISOUrls = []string{self.config.ISOUrl}
}
} else if self.config.ISOUrl != "" {
errs = packer.MultiErrorAppend(
errs, errors.New("Only one of iso_url or iso_urls may be specified."))
}
for i, url := range self.config.ISOUrls {
self.config.ISOUrls[i], err = common.DownloadableURL(url)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Failed to parse iso_urls[%d]: %s", i, err))
}
}
for i, url := range self.config.ISOUrls {
self.config.ISOUrls[i], err = common.DownloadableURL(url)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Failed to parse the iso_url (%d): %s", i, err))
}
}
*/
if len(errs.Errors) > 0 {
retErr = errors.New(errs.Error())
}
@ -368,19 +379,52 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
//Build the steps
steps := []multistep.Step{
/*
&common.StepDownload{
Checksum: self.config.ISOChecksum,
ChecksumType: self.config.ISOChecksumType,
Description: "ISO",
ResultKey: "iso_path",
Url: self.config.ISOUrls,
},
*/
&common.StepDownload{
Checksum: self.config.ISOChecksum,
ChecksumType: self.config.ISOChecksumType,
Description: "ISO",
ResultKey: "iso_path",
Url: self.config.ISOUrls,
},
new(stepPrepareOutputDir),
&common.StepCreateFloppy{
Files: self.config.FloppyFiles,
},
new(stepHTTPServer),
//new(stepUploadIso),
&stepUploadVdi{
VdiName: "Packer-floppy-disk",
ImagePathFunc: func() string {
if floppyPath, ok := state.GetOk("floppy_path"); ok {
return floppyPath.(string)
}
return ""
},
VdiUuidKey: "floppy_vdi_uuid",
},
&stepUploadVdi{
VdiName: path.Base(self.config.ISOUrls[0]),
ImagePathFunc: func() string {
return state.Get("iso_path").(string)
},
VdiUuidKey: "iso_vdi_uuid",
},
&stepFindVdi{
VdiName: self.config.ToolsIsoName,
VdiUuidKey: "tools_vdi_uuid",
},
new(stepCreateInstance),
&stepAttachVdi{
VdiUuidKey: "floppy_vdi_uuid",
VdiType: Floppy,
},
&stepAttachVdi{
VdiUuidKey: "iso_vdi_uuid",
VdiType: CD,
},
&stepAttachVdi{
VdiUuidKey: "tools_vdi_uuid",
VdiType: CD,
},
new(stepStartVmPaused),
new(stepGetVNCPort),
&stepForwardPortOverSSH{
@ -393,6 +437,12 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
new(stepBootWait),
new(stepTypeBootCommand),
new(stepWait),
&stepDetachVdi{
VdiUuidKey: "floppy_vdi_uuid",
},
&stepDetachVdi{
VdiUuidKey: "iso_vdi_uuid",
},
new(stepRemoveDevices),
new(stepStartOnHIMN),
&stepForwardPortOverSSH{

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/nilshell/xmlrpc"
"log"
)
type XenAPIClient struct {
@ -603,6 +604,36 @@ func (self *VM) ConnectVdi(vdi *VDI, vdiType VDIType) (err error) {
return
}
func (self *VM) DisconnectVdi(vdi *VDI) error {
vbds, err := self.GetVBDs()
if err != nil {
return fmt.Errorf("Unable to get VM VBDs: %s", err.Error())
}
for _, vbd := range vbds {
rec, err := vbd.GetRecord()
if err != nil {
return fmt.Errorf("Could not get record for VBD '%s': %s", vbd.Ref, err.Error())
}
if recVdi, ok := rec["VDI"].(string); ok {
if recVdi == vdi.Ref {
_ = vbd.Unplug()
err = vbd.Destroy()
if err != nil {
return fmt.Errorf("Could not destroy VBD '%s': %s", vbd.Ref, err.Error())
}
return nil
}
} else {
log.Printf("Could not find VDI record in VBD '%s'", vbd.Ref)
}
}
return fmt.Errorf("Could not find VBD for VDI '%s'", vdi.Ref)
}
func (self *VM) SetPlatform(params map[string]string) (err error) {
result := APIResult{}
platform_rec := make(xmlrpc.Struct)
@ -766,6 +797,24 @@ func (self *VBD) Eject() (err error) {
return nil
}
func (self *VBD) Unplug() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VBD.unplug", self.Ref)
if err != nil {
return err
}
return nil
}
func (self *VBD) Destroy() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VBD.destroy", self.Ref)
if err != nil {
return err
}
return nil
}
// VIF associated functions
func (self *VIF) Destroy() (err error) {
@ -789,6 +838,23 @@ func (self *VDI) GetUuid() (vdi_uuid string, err error) {
return vdi_uuid, nil
}
func (self *VDI) GetVBDs() (vbds []VBD, err error) {
vbds = make([]VBD, 0)
result := APIResult{}
err = self.Client.APICall(&result, "VDI.get_VBDs", self.Ref)
if err != nil {
return vbds, err
}
for _, elem := range result.Value.([]interface{}) {
vbd := VBD{}
vbd.Ref = elem.(string)
vbd.Client = self.Client
vbds = append(vbds, vbd)
}
return vbds, nil
}
func (self *VDI) Destroy() (err error) {
result := APIResult{}
err = self.Client.APICall(&result, "VDI.destroy", self.Ref)
@ -834,13 +900,16 @@ func (self *Task) GetProgress() (progress float64, err error) {
return
}
func (self *Task) GetErrorInfo() (errorInfo string, err error) {
func (self *Task) GetErrorInfo() (errorInfo []string, err error) {
result := APIResult{}
err = self.Client.APICall(&result, "task.get_error_info", self.Ref)
if err != nil {
return
}
errorInfo = result.Value.(string)
errorInfo = make([]string, 0)
for _, infoRaw := range result.Value.([]interface{}) {
errorInfo = append(errorInfo, infoRaw.(string))
}
return
}

View File

@ -0,0 +1,80 @@
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)
}

View File

@ -148,32 +148,6 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
ui.Say(err.Error())
}
// Connect the ISO
//iso_vdi_uuid := state.Get("iso_vdi_uuid").(string)
isos, err := client.GetVdiByNameLabel(config.IsoName)
switch {
case len(isos) == 0:
ui.Error(fmt.Sprintf("Couldn't find an ISO named '%s'. Aborting", config.IsoName))
return multistep.ActionHalt
case len(isos) > 1:
ui.Error(fmt.Sprintf("Found more than one VDI with name '%s'. Name must be unique. Aborting.", config.IsoName))
return multistep.ActionHalt
}
iso := isos[0]
//iso, _ := client.GetVdiByUuid(config.IsoUuid)
//ui.Say("Using VDI: " + iso_vdi_uuid)
//iso, _ := client.GetVdiByUuid(iso_vdi_uuid)
err = instance.ConnectVdi(iso, CD)
if err != nil {
ui.Error(fmt.Sprintf("Unable to connect ISO VDI: %s", err.Error()))
return multistep.ActionHalt
}
instanceId, err := instance.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM UUID: %s", err.Error()))

View File

@ -0,0 +1,50 @@
package xenserver
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
)
type stepDetachVdi struct {
VdiUuidKey string
}
func (self *stepDetachVdi) 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 detach of '%s'", self.VdiUuidKey)
return multistep.ActionContinue
}
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.DisconnectVdi(vdi)
if err != nil {
ui.Error(fmt.Sprintf("Unable to detach VDI '%s': %s", vdiUuid, err.Error()))
return multistep.ActionHalt
}
log.Printf("Detached VDI '%s'", vdiUuid)
return multistep.ActionContinue
}
func (self *stepDetachVdi) Cleanup(state multistep.StateBag) {}

View File

@ -0,0 +1,42 @@
package xenserver
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type stepFindVdi struct {
VdiName string
ImagePathFunc func() string
VdiUuidKey string
}
func (self *stepFindVdi) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(XenAPIClient)
vdis, err := client.GetVdiByNameLabel(self.VdiName)
switch {
case len(vdis) == 0:
ui.Error(fmt.Sprintf("Couldn't find a VDI named '%s'", self.VdiName))
return multistep.ActionHalt
case len(vdis) > 1:
ui.Error(fmt.Sprintf("Found more than one VDI with name '%s'. Name must be unique", self.VdiName))
return multistep.ActionHalt
}
vdi := vdis[0]
vdiUuid, err := vdi.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", self.VdiName, err.Error()))
return multistep.ActionHalt
}
state.Put(self.VdiUuidKey, vdiUuid)
return multistep.ActionContinue
}
func (self *stepFindVdi) Cleanup(state multistep.StateBag) {}

View File

@ -1,78 +0,0 @@
package xenserver
import (
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"os"
"os/exec"
"strconv"
)
type stepUploadIso struct{}
func (self *stepUploadIso) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(XenAPIClient)
config := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)
ui.Say("Step: Upload ISO to server")
iso_path := state.Get("iso_path").(string)
// Determine the ISO's filesize
file, err := os.Open(iso_path)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
stat, err := file.Stat()
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
iso_filesize := stat.Size()
// Create a VDI with the write size
srs, err := client.GetSRByNameLabel(config.SrName)
sr := srs[0]
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
filesize_str := strconv.FormatInt(iso_filesize, 10)
log.Printf("Filesize of the ISO is %d", filesize_str)
vdi, err := sr.CreateVdi("Packer Gen "+stat.Name(), filesize_str)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
// Upload the ISO to this VDI
vdi_uuid, _ := vdi.GetUuid()
host_url := "https://" + client.Host
log.Printf("Host URL: %s", host_url)
ui.Say("Uploading ISO to " + vdi_uuid)
out, err := exec.Command("/usr/bin/importdisk.py", host_url, client.Username, client.Password, vdi_uuid, iso_path).CombinedOutput()
log.Printf("Output: %s", out)
if err != nil {
log.Printf("%s", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Stash the vdi uuid to be used in preference
state.Put("iso_vdi_uuid", vdi_uuid)
return multistep.ActionContinue
}
func (self *stepUploadIso) Cleanup(state multistep.StateBag) {
}

View File

@ -0,0 +1,201 @@
package xenserver
import (
"crypto/tls"
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"net/http"
"os"
"time"
)
type stepUploadVdi struct {
VdiName string
ImagePathFunc func() string
VdiUuidKey string
}
func (self *stepUploadVdi) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(XenAPIClient)
imagePath := self.ImagePathFunc()
if imagePath == "" {
// skip if no disk image to attach
return multistep.ActionContinue
}
ui.Say(fmt.Sprintf("Step: Upload VDI '%s'", self.VdiName))
// Create VDI for the image
sr, err := config.GetSR(client)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
return multistep.ActionHalt
}
// Open the file for reading (NB: putFile closes the file for us)
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
vdi, err := sr.CreateVdi(self.VdiName, fmt.Sprintf("%d", fileLength))
if err != nil {
ui.Error(fmt.Sprintf("Unable to create VDI '%s': %s", self.VdiName, err.Error()))
return multistep.ActionHalt
}
vdiUuid, err := vdi.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", self.VdiName, err.Error()))
return multistep.ActionHalt
}
state.Put(self.VdiUuidKey, vdiUuid)
task, err := client.CreateTask()
if err != nil {
ui.Error(fmt.Sprintf("Unable to create task: %s", err.Error()))
return multistep.ActionHalt
}
defer task.Destroy()
import_url := fmt.Sprintf("https://%s/import_raw_vdi?vdi=%s&session_id=%s&task_id=%s",
client.Host,
vdi.Ref,
client.Session.(string),
task.Ref,
)
// Define a new transport which allows self-signed certs
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Create a client
httpClient := &http.Client{Transport: tr}
// Create request and download file
request, err := http.NewRequest("PUT", import_url, fh)
request.ContentLength = fileLength
ui.Say(fmt.Sprintf("PUT disk image '%s'", import_url))
resp, err := httpClient.Do(request) // Do closes fh for us, according to docs
if err != nil {
ui.Error(fmt.Sprintf("Unable to upload disk image: %s", err.Error()))
return multistep.ActionHalt
}
if resp.StatusCode != 200 {
ui.Error(fmt.Sprintf("Unable to upload disk image: PUT request got non-200 status code: %s", resp.Status))
return multistep.ActionHalt
}
logInterval := 0
err = InterruptibleWait{
Predicate: func() (bool, error) {
status, err := task.GetStatus()
if err != nil {
return false, fmt.Errorf("Failed to get task status: %s", err.Error())
}
switch status {
case Pending:
progress, err := task.GetProgress()
if err != nil {
return false, fmt.Errorf("Failed to get progress: %s", err.Error())
}
logInterval = logInterval + 1
if logInterval%5 == 0 {
log.Printf("Upload %.0f%% complete", progress*100)
}
return false, nil
case Success:
return true, nil
case Failure:
errorInfo, err := task.GetErrorInfo()
if err != nil {
errorInfo = []string{fmt.Sprintf("furthermore, failed to get error info: %s", err.Error())}
}
return false, fmt.Errorf("Task failed: %s", errorInfo)
case Cancelling, Cancelled:
return false, fmt.Errorf("Task cancelled")
default:
return false, fmt.Errorf("Unknown task status %v", status)
}
},
PredicateInterval: 1 * time.Second,
Timeout: 24 * time.Hour,
}.Wait(state)
resp.Body.Close()
if err != nil {
ui.Error(fmt.Sprintf("Error uploading: %s", err.Error()))
return multistep.ActionHalt
}
log.Printf("Upload complete")
return multistep.ActionContinue
}
func (self *stepUploadVdi) Cleanup(state multistep.StateBag) {
config := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(XenAPIClient)
if config.ShouldKeepInstance(state) {
return
}
vdiUuidRaw, ok := state.GetOk(self.VdiUuidKey)
if !ok {
// VDI doesn't exist
return
}
vdiUuid := vdiUuidRaw.(string)
if vdiUuid == "" {
// VDI already cleaned up
return
}
vdi, err := client.GetVdiByUuid(vdiUuid)
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...")
err = vdi.Destroy()
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
}
ui.Say(fmt.Sprintf("Destroyed VDI '%s'", self.VdiName))
state.Put(self.VdiUuidKey, "")
}