builder/virtualbox: use new template processing stuff

This commit is contained in:
Mitchell Hashimoto 2013-08-08 16:00:47 -07:00
parent fde7910e85
commit 8fad60b1b4
4 changed files with 86 additions and 18 deletions

View File

@ -57,6 +57,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 {
@ -65,6 +66,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)
@ -124,6 +130,53 @@ func (b *Builder) Prepare(raws ...interface{}) error {
b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName)
} }
// Errors
templates := map[string]*string{
"guest_additions_path": &b.config.GuestAdditionsPath,
"guest_additions_url": &b.config.GuestAdditionsURL,
"guest_additions_sha256": &b.config.GuestAdditionsSHA256,
"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,
"virtualbox_version_file": &b.config.VBoxVersionFile,
"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))
}
}
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"))
@ -215,6 +268,15 @@ func (b *Builder) Prepare(raws ...interface{}) error {
errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err)) errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err))
} }
for i, args := range b.config.VBoxManage {
for j, arg := range args {
if err := b.config.tpl.Validate(arg); err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing vboxmanage[%d][%d]: %s", i, j, err))
}
}
}
b.driver, err = b.newDriver() b.driver, err = b.newDriver()
if err != nil { if err != nil {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(

View File

@ -1,13 +1,11 @@
package virtualbox package virtualbox
import ( import (
"bytes"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"strings" "strings"
"text/template"
"time" "time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -49,11 +47,15 @@ func (s *stepTypeBootCommand) Run(state map[string]interface{}) multistep.StepAc
ui.Say("Typing the boot command...") ui.Say("Typing the boot command...")
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
}
for _, code := range scancodes(buf.String()) { for _, code := range scancodes(command) {
if code == "wait" { if code == "wait" {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
continue continue

View File

@ -1,12 +1,10 @@
package virtualbox package virtualbox
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 guestAdditionsPathTemplate struct { type guestAdditionsPathTemplate struct {
@ -39,12 +37,16 @@ func (s *stepUploadGuestAdditions) Run(state map[string]interface{}) multistep.S
Version: version, Version: version,
} }
var processedPath bytes.Buffer config.GuestAdditionsPath, err = config.tpl.Process(config.GuestAdditionsPath, tplData)
t := template.Must(template.New("path").Parse(config.GuestAdditionsPath)) if err != nil {
t.Execute(&processedPath, tplData) err := fmt.Errorf("Error preparing guest additions path: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say("Uploading VirtualBox guest additions ISO...") ui.Say("Uploading VirtualBox guest additions ISO...")
if err := comm.Upload(processedPath.String(), f); err != nil { if err := comm.Upload(config.GuestAdditionsPath, f); err != nil {
state["error"] = fmt.Errorf("Error uploading guest additions: %s", err) state["error"] = fmt.Errorf("Error uploading guest additions: %s", err)
return multistep.ActionHalt return multistep.ActionHalt
} }

View File

@ -1,12 +1,10 @@
package virtualbox package virtualbox
import ( import (
"bytes"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"strings" "strings"
"text/template"
) )
type commandTemplate struct { type commandTemplate struct {
@ -40,10 +38,14 @@ func (s *stepVBoxManage) Run(state map[string]interface{}) multistep.StepAction
copy(command, originalCommand) copy(command, originalCommand)
for i, arg := range command { for i, arg := range command {
var buf bytes.Buffer var err error
t := template.Must(template.New("arg").Parse(arg)) command[i], err = config.tpl.Process(arg, tplData)
t.Execute(&buf, tplData) if err != nil {
command[i] = buf.String() err := fmt.Errorf("Error preparing vboxmanage command: %s", err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt
}
} }
ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " "))) ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " ")))