builder/vmware: support custom vmx data

This commit is contained in:
Mitchell Hashimoto 2013-06-07 16:20:58 -07:00
parent d3ea7956bc
commit fd42a21f48
3 changed files with 56 additions and 24 deletions

View File

@ -21,22 +21,23 @@ type Builder struct {
} }
type config struct { type config struct {
DiskName string `mapstructure:"vmdk_name"` DiskName string `mapstructure:"vmdk_name"`
ISOUrl string `mapstructure:"iso_url"` ISOUrl string `mapstructure:"iso_url"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
OutputDir string `mapstructure:"output_directory"` OutputDir string `mapstructure:"output_directory"`
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"`
BootWait time.Duration BootWait time.Duration ``
ShutdownCommand string `mapstructure:"shutdown_command"` ShutdownCommand string `mapstructure:"shutdown_command"`
ShutdownTimeout time.Duration ShutdownTimeout time.Duration ``
SSHUser string `mapstructure:"ssh_username"` SSHUser string `mapstructure:"ssh_username"`
SSHPassword string `mapstructure:"ssh_password"` SSHPassword string `mapstructure:"ssh_password"`
SSHWaitTimeout time.Duration SSHWaitTimeout time.Duration ``
VNCPortMin uint `mapstructure:"vnc_port_min"` VMXData map[string]string `mapstructure:"vmx_data"`
VNCPortMax uint `mapstructure:"vnc_port_max"` VNCPortMin uint `mapstructure:"vnc_port_min"`
VNCPortMax uint `mapstructure:"vnc_port_max"`
RawBootWait string `mapstructure:"boot_wait"` RawBootWait string `mapstructure:"boot_wait"`
RawShutdownTimeout string `mapstructure:"shutdown_timeout"` RawShutdownTimeout string `mapstructure:"shutdown_timeout"`

View File

@ -193,3 +193,22 @@ 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",
}
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")
}
}

View File

@ -1,10 +1,11 @@
package vmware package vmware
import ( import (
"bytes"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"os" "log"
"path/filepath" "path/filepath"
"text/template" "text/template"
) )
@ -30,12 +31,7 @@ func (stepCreateVMX) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(*config) config := state["config"].(*config)
ui := state["ui"].(packer.Ui) ui := state["ui"].(packer.Ui)
vmxPath := filepath.Join(config.OutputDir, config.VMName+".vmx") ui.Say("Building and writing VMX file")
f, err := os.Create(vmxPath)
if err != nil {
ui.Error(fmt.Sprintf("Error creating VMX: %s", err))
return multistep.ActionHalt
}
tplData := &vmxTemplateData{ tplData := &vmxTemplateData{
config.VMName, config.VMName,
@ -44,8 +40,24 @@ func (stepCreateVMX) Run(state map[string]interface{}) multistep.StepAction {
config.ISOUrl, config.ISOUrl,
} }
var buf bytes.Buffer
t := template.Must(template.New("vmx").Parse(DefaultVMXTemplate)) 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 state["vmx_path"] = vmxPath