builder/vmware/vmx: configure VMX

This commit is contained in:
Mitchell Hashimoto 2013-12-26 15:14:19 -07:00
parent 03fb5fb0f1
commit 286edcb2b4
6 changed files with 88 additions and 65 deletions

View File

@ -0,0 +1,37 @@
package common
import (
"fmt"
"github.com/mitchellh/packer/packer"
)
type VMXConfig struct {
VMXData map[string]string `mapstructure:"vmx_data"`
}
func (c *VMXConfig) Prepare(t *packer.ConfigTemplate) []error {
errs := make([]error, 0)
newVMXData := make(map[string]string)
for k, v := range c.VMXData {
var err error
k, err = t.Process(k, nil)
if err != nil {
errs = append(errs,
fmt.Errorf("Error processing VMX data key %s: %s", k, err))
continue
}
v, err = t.Process(v, nil)
if err != nil {
errs = append(errs,
fmt.Errorf("Error processing VMX data value '%s': %s", v, err))
continue
}
newVMXData[k] = v
}
c.VMXData = newVMXData
return errs
}

View File

@ -0,0 +1,22 @@
package common
import (
"testing"
)
func TestVMXConfigPrepare(t *testing.T) {
c := new(VMXConfig)
c.VMXData = map[string]string{
"one": "foo",
"two": "bar",
}
errs := c.Prepare(testConfigTemplate(t))
if len(errs) > 0 {
t.Fatalf("bad: %#v", errs)
}
if len(c.VMXData) != 2 {
t.Fatal("should have two items in VMXData")
}
}

View File

@ -27,29 +27,29 @@ type config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
vmwcommon.OutputConfig `mapstructure:",squash"` vmwcommon.OutputConfig `mapstructure:",squash"`
vmwcommon.SSHConfig `mapstructure:",squash"` vmwcommon.SSHConfig `mapstructure:",squash"`
vmwcommon.VMXConfig `mapstructure:",squash"`
DiskName string `mapstructure:"vmdk_name"` DiskName string `mapstructure:"vmdk_name"`
DiskSize uint `mapstructure:"disk_size"` DiskSize uint `mapstructure:"disk_size"`
DiskTypeId string `mapstructure:"disk_type_id"` DiskTypeId string `mapstructure:"disk_type_id"`
FloppyFiles []string `mapstructure:"floppy_files"` FloppyFiles []string `mapstructure:"floppy_files"`
GuestOSType string `mapstructure:"guest_os_type"` GuestOSType string `mapstructure:"guest_os_type"`
ISOChecksum string `mapstructure:"iso_checksum"` ISOChecksum string `mapstructure:"iso_checksum"`
ISOChecksumType string `mapstructure:"iso_checksum_type"` ISOChecksumType string `mapstructure:"iso_checksum_type"`
ISOUrls []string `mapstructure:"iso_urls"` ISOUrls []string `mapstructure:"iso_urls"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
Headless bool `mapstructure:"headless"` Headless bool `mapstructure:"headless"`
HTTPDir string `mapstructure:"http_directory"` HTTPDir string `mapstructure:"http_directory"`
HTTPPortMin uint `mapstructure:"http_port_min"` HTTPPortMin uint `mapstructure:"http_port_min"`
HTTPPortMax uint `mapstructure:"http_port_max"` HTTPPortMax uint `mapstructure:"http_port_max"`
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
SkipCompaction bool `mapstructure:"skip_compaction"` SkipCompaction bool `mapstructure:"skip_compaction"`
ShutdownCommand string `mapstructure:"shutdown_command"` ShutdownCommand string `mapstructure:"shutdown_command"`
ToolsUploadFlavor string `mapstructure:"tools_upload_flavor"` ToolsUploadFlavor string `mapstructure:"tools_upload_flavor"`
ToolsUploadPath string `mapstructure:"tools_upload_path"` ToolsUploadPath string `mapstructure:"tools_upload_path"`
VMXData map[string]string `mapstructure:"vmx_data"` VMXTemplatePath string `mapstructure:"vmx_template_path"`
VMXTemplatePath string `mapstructure:"vmx_template_path"` VNCPortMin uint `mapstructure:"vnc_port_min"`
VNCPortMin uint `mapstructure:"vnc_port_min"` VNCPortMax uint `mapstructure:"vnc_port_max"`
VNCPortMax uint `mapstructure:"vnc_port_max"`
RemoteType string `mapstructure:"remote_type"` RemoteType string `mapstructure:"remote_type"`
RemoteDatastore string `mapstructure:"remote_datastore"` RemoteDatastore string `mapstructure:"remote_datastore"`
@ -84,6 +84,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, errs = packer.MultiErrorAppend(errs,
b.config.OutputConfig.Prepare(b.config.tpl, &b.config.PackerConfig)...) b.config.OutputConfig.Prepare(b.config.tpl, &b.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(b.config.tpl)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(b.config.tpl)...)
errs = packer.MultiErrorAppend(errs, b.config.VMXConfig.Prepare(b.config.tpl)...)
warnings := make([]string, 0) warnings := make([]string, 0)
if b.config.DiskName == "" { if b.config.DiskName == "" {
@ -207,27 +208,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
} }
} }
newVMXData := make(map[string]string)
for k, v := range b.config.VMXData {
k, err = b.config.tpl.Process(k, nil)
if err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing VMX data key %s: %s", k, err))
continue
}
v, err = b.config.tpl.Process(v, nil)
if err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing VMX data value '%s': %s", v, err))
continue
}
newVMXData[k] = v
}
b.config.VMXData = newVMXData
if b.config.HTTPPortMin > b.config.HTTPPortMax { if b.config.HTTPPortMin > b.config.HTTPPortMax {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, errors.New("http_port_min must be less than http_port_max")) errs, errors.New("http_port_min must be less than http_port_max"))

View File

@ -568,25 +568,3 @@ func TestBuilderPrepare_VNCPort(t *testing.T) {
t.Fatalf("should not have error: %s", err) t.Fatalf("should not have error: %s", err)
} }
} }
func TestBuilderPrepare_VMXData(t *testing.T) {
var b Builder
config := testConfig()
config["vmx_data"] = map[interface{}]interface{}{
"one": "foo",
"two": "bar",
}
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if len(b.config.VMXData) != 2 {
t.Fatal("should have two items in VMXData")
}
}

View File

@ -58,6 +58,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Path: b.config.SourcePath, Path: b.config.SourcePath,
VMName: b.config.VMName, VMName: b.config.VMName,
}, },
&vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXData,
},
&vmwcommon.StepSuppressMessages{},
} }
// Run the steps. // Run the steps.

View File

@ -14,6 +14,7 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
vmwcommon.OutputConfig `mapstructure:",squash"` vmwcommon.OutputConfig `mapstructure:",squash"`
vmwcommon.SSHConfig `mapstructure:",squash"` vmwcommon.SSHConfig `mapstructure:",squash"`
vmwcommon.VMXConfig `mapstructure:",squash"`
SourcePath string `mapstructure:"source_path"` SourcePath string `mapstructure:"source_path"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
@ -43,6 +44,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...) errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...) errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(c.tpl)...)
templates := map[string]*string{ templates := map[string]*string{
"source_path": &c.SourcePath, "source_path": &c.SourcePath,