builder/vmware: use new template stuff

This commit is contained in:
Mitchell Hashimoto 2013-08-08 16:18:01 -07:00
parent 191520bc0b
commit 8bb5d40537
3 changed files with 82 additions and 13 deletions

View File

@ -58,6 +58,7 @@ type config struct {
bootWait time.Duration `` bootWait time.Duration ``
shutdownTimeout time.Duration `` shutdownTimeout time.Duration ``
sshWaitTimeout time.Duration `` sshWaitTimeout time.Duration ``
tpl *common.Template
} }
func (b *Builder) Prepare(raws ...interface{}) error { func (b *Builder) Prepare(raws ...interface{}) error {
@ -66,6 +67,11 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return err return err
} }
b.config.tpl, err = common.NewTemplate()
if err != nil {
return err
}
// Accumulate any errors // Accumulate any errors
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
@ -121,6 +127,72 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.ToolsUploadPath = "{{ .Flavor }}.iso" b.config.ToolsUploadPath = "{{ .Flavor }}.iso"
} }
// Errors
templates := map[string]*string{
"disk_name": &b.config.DiskName,
"guest_os_type": &b.config.GuestOSType,
"http_directory": &b.config.HTTPDir,
"iso_checksum": &b.config.ISOChecksum,
"iso_checksum_type": &b.config.ISOChecksumType,
"iso_url": &b.config.ISOUrl,
"output_directory": &b.config.OutputDir,
"shutdown_command": &b.config.ShutdownCommand,
"ssh_password": &b.config.SSHPassword,
"ssh_username": &b.config.SSHUser,
"tools_upload_flavor": &b.config.ToolsUploadFlavor,
"vm_name": &b.config.VMName,
"boot_wait": &b.config.RawBootWait,
"shutdown_timeout": &b.config.RawShutdownTimeout,
"ssh_wait_timeout": &b.config.RawSSHWaitTimeout,
}
for n, ptr := range templates {
var err error
*ptr, err = b.config.tpl.Process(*ptr, nil)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
for i, command := range b.config.BootCommand {
if err := b.config.tpl.Validate(command); err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing boot_command[%d]: %s", i, err))
}
}
for i, file := range b.config.FloppyFiles {
var err error
b.config.FloppyFiles[i], err = b.config.tpl.Process(file, nil)
if err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing floppy_files[%d]: %s",
i, err))
}
}
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

@ -1,7 +1,6 @@
package vmware package vmware
import ( import (
"bytes"
"fmt" "fmt"
"github.com/mitchellh/go-vnc" "github.com/mitchellh/go-vnc"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
@ -10,7 +9,6 @@ import (
"net" "net"
"runtime" "runtime"
"strings" "strings"
"text/template"
"time" "time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -90,11 +88,15 @@ func (s *stepTypeBootCommand) Run(state map[string]interface{}) multistep.StepAc
ui.Say("Typing the boot command over VNC...") ui.Say("Typing the boot command over VNC...")
for _, command := range config.BootCommand { for _, command := range config.BootCommand {
var buf bytes.Buffer command, err := config.tpl.Process(command, tplData)
t := template.Must(template.New("boot").Parse(command)) if err != nil {
t.Execute(&buf, tplData) err := fmt.Errorf("Error preparing boot command: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt
}
vncSendString(c, buf.String()) vncSendString(c, command)
} }
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -1,12 +1,10 @@
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" "os"
"text/template"
) )
type toolsUploadPathTemplate struct { type toolsUploadPathTemplate struct {
@ -34,11 +32,8 @@ func (*stepUploadTools) Run(state map[string]interface{}) multistep.StepAction {
defer f.Close() defer f.Close()
tplData := &toolsUploadPathTemplate{Flavor: config.ToolsUploadFlavor} tplData := &toolsUploadPathTemplate{Flavor: config.ToolsUploadFlavor}
var processedPath bytes.Buffer config.ToolsUploadPath, err = config.tpl.Process(config.ToolsUploadPath, tplData)
t := template.Must(template.New("path").Parse(config.ToolsUploadPath)) if err := comm.Upload(config.ToolsUploadPath, f); err != nil {
t.Execute(&processedPath, tplData)
if err := comm.Upload(processedPath.String(), f); err != nil {
state["error"] = fmt.Errorf("Error uploading VMware Tools: %s", err) state["error"] = fmt.Errorf("Error uploading VMware Tools: %s", err)
return multistep.ActionHalt return multistep.ActionHalt
} }