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 {
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"`

View File

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

View File

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