From 13ca6ef95b3f33d618ecc33e2dba6e40fd1027ce Mon Sep 17 00:00:00 2001 From: Cheng Sun Date: Thu, 11 Dec 2014 14:30:39 +0000 Subject: [PATCH] export_format: allow export format to be configured "export_format" can be one of "xva" (default), "vdi_raw" --- builder/xenserver/builder.go | 15 +++- builder/xenserver/step_shutdown_and_export.go | 76 ++++++++++--------- 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/builder/xenserver/builder.go b/builder/xenserver/builder.go index c6de7c8..50107bf 100644 --- a/builder/xenserver/builder.go +++ b/builder/xenserver/builder.go @@ -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: diff --git a/builder/xenserver/step_shutdown_and_export.go b/builder/xenserver/step_shutdown_and_export.go index a966c25..96d53d2 100644 --- a/builder/xenserver/step_shutdown_and_export.go +++ b/builder/xenserver/step_shutdown_and_export.go @@ -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)