From fd42a21f48040ffa24c216ec8297e992976c6fc6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 7 Jun 2013 16:20:58 -0700 Subject: [PATCH] builder/vmware: support custom vmx data --- builder/vmware/builder.go | 33 ++++++++++++++++--------------- builder/vmware/builder_test.go | 19 ++++++++++++++++++ builder/vmware/step_create_vmx.go | 28 ++++++++++++++++++-------- 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index 9bf6637a5..a46c6e868 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -21,22 +21,23 @@ type Builder struct { } type config struct { - DiskName string `mapstructure:"vmdk_name"` - ISOUrl string `mapstructure:"iso_url"` - VMName string `mapstructure:"vm_name"` - OutputDir string `mapstructure:"output_directory"` - HTTPDir string `mapstructure:"http_directory"` - HTTPPortMin uint `mapstructure:"http_port_min"` - HTTPPortMax uint `mapstructure:"http_port_max"` - BootCommand []string `mapstructure:"boot_command"` - BootWait time.Duration - ShutdownCommand string `mapstructure:"shutdown_command"` - ShutdownTimeout time.Duration - SSHUser string `mapstructure:"ssh_username"` - SSHPassword string `mapstructure:"ssh_password"` - SSHWaitTimeout time.Duration - VNCPortMin uint `mapstructure:"vnc_port_min"` - VNCPortMax uint `mapstructure:"vnc_port_max"` + DiskName string `mapstructure:"vmdk_name"` + ISOUrl string `mapstructure:"iso_url"` + VMName string `mapstructure:"vm_name"` + OutputDir string `mapstructure:"output_directory"` + HTTPDir string `mapstructure:"http_directory"` + HTTPPortMin uint `mapstructure:"http_port_min"` + HTTPPortMax uint `mapstructure:"http_port_max"` + BootCommand []string `mapstructure:"boot_command"` + BootWait time.Duration `` + ShutdownCommand string `mapstructure:"shutdown_command"` + ShutdownTimeout time.Duration `` + SSHUser string `mapstructure:"ssh_username"` + SSHPassword string `mapstructure:"ssh_password"` + SSHWaitTimeout time.Duration `` + VMXData map[string]string `mapstructure:"vmx_data"` + VNCPortMin uint `mapstructure:"vnc_port_min"` + VNCPortMax uint `mapstructure:"vnc_port_max"` RawBootWait string `mapstructure:"boot_wait"` RawShutdownTimeout string `mapstructure:"shutdown_timeout"` diff --git a/builder/vmware/builder_test.go b/builder/vmware/builder_test.go index a81ce5006..bb45b6241 100644 --- a/builder/vmware/builder_test.go +++ b/builder/vmware/builder_test.go @@ -193,3 +193,22 @@ func TestBuilderPrepare_VNCPort(t *testing.T) { 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", + } + + err := b.Prepare(config) + 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") + } +} diff --git a/builder/vmware/step_create_vmx.go b/builder/vmware/step_create_vmx.go index ea5f0f739..a0971ac1f 100644 --- a/builder/vmware/step_create_vmx.go +++ b/builder/vmware/step_create_vmx.go @@ -1,10 +1,11 @@ package vmware import ( + "bytes" "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" - "os" + "log" "path/filepath" "text/template" ) @@ -30,12 +31,7 @@ func (stepCreateVMX) Run(state map[string]interface{}) multistep.StepAction { config := state["config"].(*config) ui := state["ui"].(packer.Ui) - vmxPath := filepath.Join(config.OutputDir, config.VMName+".vmx") - f, err := os.Create(vmxPath) - if err != nil { - ui.Error(fmt.Sprintf("Error creating VMX: %s", err)) - return multistep.ActionHalt - } + ui.Say("Building and writing VMX file") tplData := &vmxTemplateData{ config.VMName, @@ -44,8 +40,24 @@ func (stepCreateVMX) Run(state map[string]interface{}) multistep.StepAction { config.ISOUrl, } + var buf bytes.Buffer t := template.Must(template.New("vmx").Parse(DefaultVMXTemplate)) - t.Execute(f, tplData) + t.Execute(&buf, tplData) + + vmxData := ParseVMX(buf.String()) + if config.VMXData != nil { + log.Println("Setting custom VMX data...") + for k, v := range config.VMXData { + log.Printf("Setting VMX: '%s' = '%s'", k, v) + vmxData[k] = v + } + } + + vmxPath := filepath.Join(config.OutputDir, config.VMName+".vmx") + if err := WriteVMX(vmxPath, vmxData); err != nil { + ui.Error(fmt.Sprintf("Error creating VMX: %s", err)) + return multistep.ActionHalt + } state["vmx_path"] = vmxPath