export_format: allow export format to be configured

"export_format" can be one of "xva" (default), "vdi_raw"
This commit is contained in:
Cheng Sun 2014-12-11 14:30:39 +00:00
parent 322817e827
commit 13ca6ef95b
2 changed files with 56 additions and 35 deletions

View File

@ -61,7 +61,8 @@ type config struct {
SSHUser string `mapstructure:"ssh_username"`
SSHKeyPath string `mapstructure:"ssh_key_path"`
OutputDir string `mapstructure:"output_directory"`
OutputDir string `mapstructure:"output_directory"`
ExportFormat string `mapstructure:"export_format"`
KeepInstance string `mapstructure:"keep_instance"`
@ -133,6 +134,10 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
self.config.OutputDir = fmt.Sprintf("output-%s", self.config.PackerBuildName)
}
if self.config.ExportFormat == "" {
self.config.ExportFormat = "xva"
}
if self.config.KeepInstance == "" {
self.config.KeepInstance = "never"
}
@ -173,6 +178,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
"ssh_password": &self.config.SSHPassword,
"ssh_key_path": &self.config.SSHKeyPath,
"output_directory": &self.config.OutputDir,
"export_format": &self.config.ExportFormat,
"keep_instance": &self.config.KeepInstance,
}
@ -258,6 +264,13 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
errs, errors.New("A root disk size must be specified."))
}
switch self.config.ExportFormat {
case "xva", "vdi_raw":
default:
errs = packer.MultiErrorAppend(
errs, errors.New("export_format must be one of 'xva', 'vdi_raw'"))
}
switch self.config.KeepInstance {
case "always", "never", "on_success":
default:

View File

@ -65,44 +65,52 @@ func (stepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction
return multistep.ActionHalt
}
//Export the VM
switch config.ExportFormat {
case "xva":
// export the VM
export_url := fmt.Sprintf("https://%s/export?vm=%s&session_id=%s",
client.Host,
instance_uuid,
client.Session.(string),
)
export_filename := fmt.Sprintf("%s/%s.xva", config.OutputDir, config.InstanceName)
ui.Say("Getting metadata " + export_url)
downloadFile(export_url, export_filename)
disks, err := instance.GetDisks()
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM disks: %s", err.Error()))
return multistep.ActionHalt
}
for _, disk := range disks {
disk_uuid, err := disk.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Could not get disk with UUID '%s': %s", disk_uuid, err.Error()))
return multistep.ActionHalt
}
// Basic auth in URL request is required as session token is not
// accepted for some reason.
// @todo: raise with XAPI team.
disk_export_url := fmt.Sprintf("https://%s:%s@%s/export_raw_vdi?vdi=%s",
client.Username,
client.Password,
export_url := fmt.Sprintf("https://%s/export?vm=%s&session_id=%s",
client.Host,
disk_uuid,
instance_uuid,
client.Session.(string),
)
ui.Say("Getting " + disk_export_url)
disk_export_filename := fmt.Sprintf("%s/%s.raw", config.OutputDir, disk_uuid)
ui.Say("Downloading " + disk_uuid)
downloadFile(disk_export_url, disk_export_filename)
export_filename := fmt.Sprintf("%s/%s.xva", config.OutputDir, config.InstanceName)
ui.Say("Getting XVA " + export_url)
downloadFile(export_url, export_filename)
case "vdi_raw":
// export the disks
disks, err := instance.GetDisks()
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM disks: %s", err.Error()))
return multistep.ActionHalt
}
for _, disk := range disks {
disk_uuid, err := disk.GetUuid()
if err != nil {
ui.Error(fmt.Sprintf("Could not get disk with UUID '%s': %s", disk_uuid, err.Error()))
return multistep.ActionHalt
}
// Basic auth in URL request is required as session token is not
// accepted for some reason.
// @todo: raise with XAPI team.
disk_export_url := fmt.Sprintf("https://%s:%s@%s/export_raw_vdi?vdi=%s",
client.Username,
client.Password,
client.Host,
disk_uuid,
)
disk_export_filename := fmt.Sprintf("%s/%s.raw", config.OutputDir, disk_uuid)
ui.Say("Getting VDI " + disk_export_url)
downloadFile(disk_export_url, disk_export_filename)
}
default:
panic(fmt.Sprintf("Unknown export_format '%s'", config.ExportFormat))
}
ui.Say("Download completed: " + config.OutputDir)