From 3193f50f1725dfa7079a35f512c29acd1923b76f Mon Sep 17 00:00:00 2001 From: Alexander Laamanen Date: Tue, 7 Mar 2017 09:03:04 +0200 Subject: [PATCH] Support export with the vmx builder. --- builder/vmware/common/export_config.go | 25 +++++++++++++++++++++++++ builder/vmware/iso/builder.go | 17 +++++++++++------ builder/vmware/vmx/builder.go | 25 ++++++++++++++++++++++--- builder/vmware/vmx/config.go | 2 ++ 4 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 builder/vmware/common/export_config.go diff --git a/builder/vmware/common/export_config.go b/builder/vmware/common/export_config.go new file mode 100644 index 000000000..fae944cb9 --- /dev/null +++ b/builder/vmware/common/export_config.go @@ -0,0 +1,25 @@ +package common + +import ( + "fmt" + + "github.com/mitchellh/packer/template/interpolate" +) + +type ExportConfig struct { + Format string `mapstructure:"format"` + OVFToolOptions []string `mapstructure:"ovftool_options"` + SkipExport bool `mapstructure:"skip_export"` + KeepRegistered bool `mapstructure:"keep_registered"` +} + +func (c *ExportConfig) Prepare(ctx *interpolate.Context) []error { + var errs []error + if c.Format != "" { + if !(c.Format == "ova" || c.Format == "ovf" || c.Format == "vmx") { + errs = append( + errs, fmt.Errorf("format must be one of ova, ovf, or vmx")) + } + } + return errs +} diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 03b4cfc5d..91050d0f2 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -37,6 +37,7 @@ type Config struct { vmwcommon.SSHConfig `mapstructure:",squash"` vmwcommon.ToolsConfig `mapstructure:",squash"` vmwcommon.VMXConfig `mapstructure:",squash"` + vmwcommon.ExportConfig `mapstructure:",squash"` // disk drives AdditionalDiskSize []uint `mapstructure:"disk_additional_size"` @@ -67,12 +68,13 @@ type Config struct { Parallel string `mapstructure:"parallel"` // booting a guest - KeepRegistered bool `mapstructure:"keep_registered"` - OVFToolOptions []string `mapstructure:"ovftool_options"` - SkipCompaction bool `mapstructure:"skip_compaction"` - SkipExport bool `mapstructure:"skip_export"` - VMXDiskTemplatePath string `mapstructure:"vmx_disk_template_path"` - VMXTemplatePath string `mapstructure:"vmx_template_path"` + KeepRegistered bool `mapstructure:"keep_registered"` + OVFToolOptions []string `mapstructure:"ovftool_options"` + SkipCompaction bool `mapstructure:"skip_compaction"` + SkipExport bool `mapstructure:"skip_export"` + + VMXDiskTemplatePath string `mapstructure:"vmx_disk_template_path"` + VMXTemplatePath string `mapstructure:"vmx_template_path"` CommConfig communicator.Config `mapstructure:",squash"` @@ -112,6 +114,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.VMXConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VNCConfig.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.ExportConfig.Prepare(&b.config.ctx)...) if b.config.DiskName == "" { b.config.DiskName = "disk" @@ -188,6 +191,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, fmt.Errorf("remote_host must be specified")) } + if b.config.RemoteType != "esx5" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("Only 'esx5' value is accepted for remote_type")) @@ -353,6 +357,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vmwcommon.StepConfigureVMX{ CustomData: b.config.VMXDataPost, SkipFloppy: true, + VMName: b.config.VMName, }, &vmwcommon.StepCleanVMX{ RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet, diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index b63216318..f5cab8ae4 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -47,6 +47,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe default: dir = new(vmwcommon.LocalOutputDir) } + if b.config.RemoteType != "" && b.config.Format != "" { + b.config.OutputDir = b.config.VMName + } dir.SetOutputDir(b.config.OutputDir) // Set up the state. @@ -58,6 +61,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state.Put("hook", hook) state.Put("ui", ui) state.Put("sshConfig", &b.config.SSHConfig) + state.Put("driverConfig", &b.config.DriverConfig) // Build the steps. steps := []multistep.Step{ @@ -99,8 +103,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe VNCDisablePassword: b.config.VNCDisablePassword, }, &vmwcommon.StepRegister{ - Format: "", - KeepRegistered: false, + Format: b.config.Format, + KeepRegistered: b.config.KeepRegistered, }, &vmwcommon.StepRun{ DurationBeforeStop: 5 * time.Second, @@ -140,6 +144,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vmwcommon.StepConfigureVMX{ CustomData: b.config.VMXDataPost, SkipFloppy: true, + VMName: b.config.VMName, }, &vmwcommon.StepCleanVMX{ RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet, @@ -148,6 +153,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vmwcommon.StepUploadVMX{ RemoteType: b.config.RemoteType, }, + &vmwcommon.StepExport{ + Format: b.config.Format, + SkipExport: b.config.SkipExport, + VMName: b.config.VMName, + OVFToolOptions: b.config.OVFToolOptions, + }, } // Run the steps. @@ -167,7 +178,15 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe if _, ok := state.GetOk(multistep.StateHalted); ok { return nil, errors.New("Build was halted.") } - files, err := state.Get("dir").(vmwcommon.OutputDir).ListFiles() + // Compile the artifact list + var files []string + if b.config.RemoteType != "" && b.config.Format != "" { + dir = new(vmwcommon.LocalOutputDir) + dir.SetOutputDir(b.config.OutputDir) + files, err = dir.ListFiles() + } else { + files, err = state.Get("dir").(vmwcommon.OutputDir).ListFiles() + } if err != nil { return nil, err } diff --git a/builder/vmware/vmx/config.go b/builder/vmware/vmx/config.go index 9a52c8ea9..e21daba2f 100644 --- a/builder/vmware/vmx/config.go +++ b/builder/vmware/vmx/config.go @@ -26,6 +26,7 @@ type Config struct { vmwcommon.SSHConfig `mapstructure:",squash"` vmwcommon.ToolsConfig `mapstructure:",squash"` vmwcommon.VMXConfig `mapstructure:",squash"` + vmwcommon.ExportConfig `mapstructure:",squash"` Linked bool `mapstructure:"linked"` RemoteType string `mapstructure:"remote_type"` @@ -73,6 +74,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.VNCConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...) if c.DriverConfig.RemoteType == "" { if c.SourcePath == "" {