Support export with the vmx builder.

This commit is contained in:
Alexander Laamanen 2017-03-07 09:03:04 +02:00 committed by Megan Marsh
parent a68a639a1a
commit 3193f50f17
4 changed files with 60 additions and 9 deletions

View File

@ -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
}

View File

@ -37,6 +37,7 @@ type Config struct {
vmwcommon.SSHConfig `mapstructure:",squash"` vmwcommon.SSHConfig `mapstructure:",squash"`
vmwcommon.ToolsConfig `mapstructure:",squash"` vmwcommon.ToolsConfig `mapstructure:",squash"`
vmwcommon.VMXConfig `mapstructure:",squash"` vmwcommon.VMXConfig `mapstructure:",squash"`
vmwcommon.ExportConfig `mapstructure:",squash"`
// disk drives // disk drives
AdditionalDiskSize []uint `mapstructure:"disk_additional_size"` AdditionalDiskSize []uint `mapstructure:"disk_additional_size"`
@ -67,12 +68,13 @@ type Config struct {
Parallel string `mapstructure:"parallel"` Parallel string `mapstructure:"parallel"`
// booting a guest // booting a guest
KeepRegistered bool `mapstructure:"keep_registered"` KeepRegistered bool `mapstructure:"keep_registered"`
OVFToolOptions []string `mapstructure:"ovftool_options"` OVFToolOptions []string `mapstructure:"ovftool_options"`
SkipCompaction bool `mapstructure:"skip_compaction"` SkipCompaction bool `mapstructure:"skip_compaction"`
SkipExport bool `mapstructure:"skip_export"` SkipExport bool `mapstructure:"skip_export"`
VMXDiskTemplatePath string `mapstructure:"vmx_disk_template_path"`
VMXTemplatePath string `mapstructure:"vmx_template_path"` VMXDiskTemplatePath string `mapstructure:"vmx_disk_template_path"`
VMXTemplatePath string `mapstructure:"vmx_template_path"`
CommConfig communicator.Config `mapstructure:",squash"` 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.VMXConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.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.VNCConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.ExportConfig.Prepare(&b.config.ctx)...)
if b.config.DiskName == "" { if b.config.DiskName == "" {
b.config.DiskName = "disk" b.config.DiskName = "disk"
@ -188,6 +191,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, errs = packer.MultiErrorAppend(errs,
fmt.Errorf("remote_host must be specified")) fmt.Errorf("remote_host must be specified"))
} }
if b.config.RemoteType != "esx5" { if b.config.RemoteType != "esx5" {
errs = packer.MultiErrorAppend(errs, errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Only 'esx5' value is accepted for remote_type")) 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{ &vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXDataPost, CustomData: b.config.VMXDataPost,
SkipFloppy: true, SkipFloppy: true,
VMName: b.config.VMName,
}, },
&vmwcommon.StepCleanVMX{ &vmwcommon.StepCleanVMX{
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet, RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,

View File

@ -47,6 +47,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
default: default:
dir = new(vmwcommon.LocalOutputDir) dir = new(vmwcommon.LocalOutputDir)
} }
if b.config.RemoteType != "" && b.config.Format != "" {
b.config.OutputDir = b.config.VMName
}
dir.SetOutputDir(b.config.OutputDir) dir.SetOutputDir(b.config.OutputDir)
// Set up the state. // 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("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
state.Put("sshConfig", &b.config.SSHConfig) state.Put("sshConfig", &b.config.SSHConfig)
state.Put("driverConfig", &b.config.DriverConfig)
// Build the steps. // Build the steps.
steps := []multistep.Step{ 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, VNCDisablePassword: b.config.VNCDisablePassword,
}, },
&vmwcommon.StepRegister{ &vmwcommon.StepRegister{
Format: "", Format: b.config.Format,
KeepRegistered: false, KeepRegistered: b.config.KeepRegistered,
}, },
&vmwcommon.StepRun{ &vmwcommon.StepRun{
DurationBeforeStop: 5 * time.Second, DurationBeforeStop: 5 * time.Second,
@ -140,6 +144,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&vmwcommon.StepConfigureVMX{ &vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXDataPost, CustomData: b.config.VMXDataPost,
SkipFloppy: true, SkipFloppy: true,
VMName: b.config.VMName,
}, },
&vmwcommon.StepCleanVMX{ &vmwcommon.StepCleanVMX{
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet, 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{ &vmwcommon.StepUploadVMX{
RemoteType: b.config.RemoteType, 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. // 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 { if _, ok := state.GetOk(multistep.StateHalted); ok {
return nil, errors.New("Build was halted.") 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -26,6 +26,7 @@ type Config struct {
vmwcommon.SSHConfig `mapstructure:",squash"` vmwcommon.SSHConfig `mapstructure:",squash"`
vmwcommon.ToolsConfig `mapstructure:",squash"` vmwcommon.ToolsConfig `mapstructure:",squash"`
vmwcommon.VMXConfig `mapstructure:",squash"` vmwcommon.VMXConfig `mapstructure:",squash"`
vmwcommon.ExportConfig `mapstructure:",squash"`
Linked bool `mapstructure:"linked"` Linked bool `mapstructure:"linked"`
RemoteType string `mapstructure:"remote_type"` 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.VMXConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.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.VNCConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...)
if c.DriverConfig.RemoteType == "" { if c.DriverConfig.RemoteType == "" {
if c.SourcePath == "" { if c.SourcePath == "" {