Commit 3e76547b authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

bulder/qemu: convert interpolation

parent 4cae8764
...@@ -13,7 +13,9 @@ import ( ...@@ -13,7 +13,9 @@ import (
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
commonssh "github.com/mitchellh/packer/common/ssh" commonssh "github.com/mitchellh/packer/common/ssh"
"github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
) )
const BuilderId = "transcend.qemu" const BuilderId = "transcend.qemu"
...@@ -65,11 +67,11 @@ var diskCache = map[string]bool{ ...@@ -65,11 +67,11 @@ var diskCache = map[string]bool{
} }
type Builder struct { type Builder struct {
config config config Config
runner multistep.Runner runner multistep.Runner
} }
type config struct { type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
Accelerator string `mapstructure:"accelerator"` Accelerator string `mapstructure:"accelerator"`
...@@ -114,24 +116,25 @@ type config struct { ...@@ -114,24 +116,25 @@ type config struct {
bootWait time.Duration `` bootWait time.Duration ``
shutdownTimeout time.Duration `` shutdownTimeout time.Duration ``
sshWaitTimeout time.Duration `` sshWaitTimeout time.Duration ``
tpl *packer.ConfigTemplate ctx interpolate.Context
} }
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
md, err := common.DecodeConfig(&b.config, raws...) err := config.Decode(&b.config, &config.DecodeOpts{
if err != nil { Interpolate: true,
return nil, err InterpolateFilter: &interpolate.RenderFilter{
} Exclude: []string{
warnings := make([]string, 0) "boot_command",
"qemuargs",
b.config.tpl, err = packer.NewConfigTemplate() },
},
}, raws...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
b.config.tpl.UserVars = b.config.PackerUserVars
// Accumulate any errors var errs *packer.MultiError
errs := common.CheckUnusedConfig(md) warnings := make([]string, 0)
if b.config.DiskSize == 0 { if b.config.DiskSize == 0 {
b.config.DiskSize = 40000 b.config.DiskSize = 40000
...@@ -189,15 +192,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -189,15 +192,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
b.config.VNCPortMax = 6000 b.config.VNCPortMax = 6000
} }
for i, args := range b.config.QemuArgs {
for j, arg := range args {
if err := b.config.tpl.Validate(arg); err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Error processing qemu-system_x86-64[%d][%d]: %s", i, j, err))
}
}
}
if b.config.VMName == "" { if b.config.VMName == "" {
b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName) b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName)
} }
...@@ -218,63 +212,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { ...@@ -218,63 +212,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
b.config.DiskInterface = "virtio" b.config.DiskInterface = "virtio"
} }
// Errors
templates := map[string]*string{
"http_directory": &b.config.HTTPDir,
"iso_checksum": &b.config.ISOChecksum,
"iso_checksum_type": &b.config.ISOChecksumType,
"iso_url": &b.config.RawSingleISOUrl,
"output_directory": &b.config.OutputDir,
"shutdown_command": &b.config.ShutdownCommand,
"ssh_key_path": &b.config.SSHKeyPath,
"ssh_password": &b.config.SSHPassword,
"ssh_username": &b.config.SSHUser,
"vm_name": &b.config.VMName,
"format": &b.config.Format,
"boot_wait": &b.config.RawBootWait,
"shutdown_timeout": &b.config.RawShutdownTimeout,
"ssh_wait_timeout": &b.config.RawSSHWaitTimeout,
"accelerator": &b.config.Accelerator,
"machine_type": &b.config.MachineType,
"net_device": &b.config.NetDevice,
"disk_interface": &b.config.DiskInterface,
}
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, url := range b.config.ISOUrls {
var err error
b.config.ISOUrls[i], err = b.config.tpl.Process(url, nil)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing iso_urls[%d]: %s", i, 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.Format == "qcow2" || b.config.Format == "raw") { if !(b.config.Format == "qcow2" || b.config.Format == "raw") {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, errors.New("invalid format, only 'qcow2' or 'raw' are allowed")) errs, errors.New("invalid format, only 'qcow2' or 'raw' are allowed"))
......
...@@ -15,7 +15,7 @@ func sshAddress(state multistep.StateBag) (string, error) { ...@@ -15,7 +15,7 @@ func sshAddress(state multistep.StateBag) (string, error) {
} }
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) { func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
auth := []gossh.AuthMethod{ auth := []gossh.AuthMethod{
gossh.Password(config.SSHPassword), gossh.Password(config.SSHPassword),
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
type stepBootWait struct{} type stepBootWait struct{}
func (s *stepBootWait) Run(state multistep.StateBag) multistep.StepAction { func (s *stepBootWait) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if int64(config.bootWait) > 0 { if int64(config.bootWait) > 0 {
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
type stepConfigureVNC struct{} type stepConfigureVNC struct{}
func (stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { func (stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Find an open VNC port. Note that this can still fail later on // Find an open VNC port. Note that this can still fail later on
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
type stepCopyDisk struct{} type stepCopyDisk struct{}
func (s *stepCopyDisk) Run(state multistep.StateBag) multistep.StepAction { func (s *stepCopyDisk) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
isoPath := state.Get("iso_path").(string) isoPath := state.Get("iso_path").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
type stepCreateDisk struct{} type stepCreateDisk struct{}
func (s *stepCreateDisk) Run(state multistep.StateBag) multistep.StepAction { func (s *stepCreateDisk) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
name := config.VMName + "." + strings.ToLower(config.Format) name := config.VMName + "." + strings.ToLower(config.Format)
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
type stepForwardSSH struct{} type stepForwardSSH struct{}
func (s *stepForwardSSH) Run(state multistep.StateBag) multistep.StepAction { func (s *stepForwardSSH) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
log.Printf("Looking for available SSH port between %d and %d", config.SSHHostPortMin, config.SSHHostPortMax) log.Printf("Looking for available SSH port between %d and %d", config.SSHHostPortMin, config.SSHHostPortMax)
......
...@@ -25,7 +25,7 @@ type stepHTTPServer struct { ...@@ -25,7 +25,7 @@ type stepHTTPServer struct {
} }
func (s *stepHTTPServer) Run(state multistep.StateBag) multistep.StepAction { func (s *stepHTTPServer) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
var httpPort uint = 0 var httpPort uint = 0
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
type stepPrepareOutputDir struct{} type stepPrepareOutputDir struct{}
func (stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction { func (stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if _, err := os.Stat(config.OutputDir); err == nil && config.PackerForce { if _, err := os.Stat(config.OutputDir); err == nil && config.PackerForce {
...@@ -32,7 +32,7 @@ func (stepPrepareOutputDir) Cleanup(state multistep.StateBag) { ...@@ -32,7 +32,7 @@ func (stepPrepareOutputDir) Cleanup(state multistep.StateBag) {
_, halted := state.GetOk(multistep.StateHalted) _, halted := state.GetOk(multistep.StateHalted)
if cancelled || halted { if cancelled || halted {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting output directory...") ui.Say("Deleting output directory...")
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
type stepResizeDisk struct{} type stepResizeDisk struct{}
func (s *stepResizeDisk) Run(state multistep.StateBag) multistep.StepAction { func (s *stepResizeDisk) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName,
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
) )
// stepRun runs the virtual machine // stepRun runs the virtual machine
...@@ -56,7 +57,7 @@ func (s *stepRun) Cleanup(state multistep.StateBag) { ...@@ -56,7 +57,7 @@ func (s *stepRun) Cleanup(state multistep.StateBag) {
} }
func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error) { func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error) {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
isoPath := state.Get("iso_path").(string) isoPath := state.Get("iso_path").(string)
vncPort := state.Get("vnc_port").(uint) vncPort := state.Get("vnc_port").(uint)
sshHostPort := state.Get("sshHostPort").(uint) sshHostPort := state.Get("sshHostPort").(uint)
...@@ -109,14 +110,15 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error ...@@ -109,14 +110,15 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error
ui.Say("Overriding defaults Qemu arguments with QemuArgs...") ui.Say("Overriding defaults Qemu arguments with QemuArgs...")
httpPort := state.Get("http_port").(uint) httpPort := state.Get("http_port").(uint)
tplData := qemuArgsTemplateData{ ctx := config.ctx
ctx.Data = qemuArgsTemplateData{
"10.0.2.2", "10.0.2.2",
httpPort, httpPort,
config.HTTPDir, config.HTTPDir,
config.OutputDir, config.OutputDir,
config.VMName, config.VMName,
} }
newQemuArgs, err := processArgs(config.QemuArgs, config.tpl, &tplData) newQemuArgs, err := processArgs(config.QemuArgs, &ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -160,7 +162,7 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error ...@@ -160,7 +162,7 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error
return outArgs, nil return outArgs, nil
} }
func processArgs(args [][]string, tpl *packer.ConfigTemplate, tplData *qemuArgsTemplateData) ([][]string, error) { func processArgs(args [][]string, ctx *interpolate.Context) ([][]string, error) {
var err error var err error
if args == nil { if args == nil {
...@@ -172,7 +174,7 @@ func processArgs(args [][]string, tpl *packer.ConfigTemplate, tplData *qemuArgsT ...@@ -172,7 +174,7 @@ func processArgs(args [][]string, tpl *packer.ConfigTemplate, tplData *qemuArgsT
parms := make([]string, len(rowArgs)) parms := make([]string, len(rowArgs))
newArgs[argsIdx] = parms newArgs[argsIdx] = parms
for i, parm := range rowArgs { for i, parm := range rowArgs {
parms[i], err = tpl.Process(parm, &tplData) parms[i], err = interpolate.Render(parm, ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -24,7 +24,7 @@ type stepShutdown struct{} ...@@ -24,7 +24,7 @@ type stepShutdown struct{}
func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction { func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
comm := state.Get("communicator").(packer.Communicator) comm := state.Get("communicator").(packer.Communicator)
config := state.Get("config").(*config) config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
......
...@@ -2,15 +2,17 @@ package qemu ...@@ -2,15 +2,17 @@ package qemu
import ( import (
"fmt" "fmt"
"github.com/mitchellh/go-vnc"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log" "log"
"net" "net"
"strings" "strings"
"time" "time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/mitchellh/go-vnc"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
) )
const KeyLeftShift uint32 = 0xFFE1 const KeyLeftShift uint32 = 0xFFE1
...@@ -34,7 +36,7 @@ type bootCommandTemplateData struct { ...@@ -34,7 +36,7 @@ type bootCommandTemplateData struct {
type stepTypeBootCommand struct{} type stepTypeBootCommand struct{}
func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction { func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*Config)
httpPort := state.Get("http_port").(uint) httpPort := state.Get("http_port").(uint)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vncPort := state.Get("vnc_port").(uint) vncPort := state.Get("vnc_port").(uint)
...@@ -61,7 +63,8 @@ func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction ...@@ -61,7 +63,8 @@ func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction
log.Printf("Connected to VNC desktop: %s", c.DesktopName) log.Printf("Connected to VNC desktop: %s", c.DesktopName)
tplData := &bootCommandTemplateData{ ctx := config.ctx
ctx.Data = &bootCommandTemplateData{
"10.0.2.2", "10.0.2.2",
httpPort, httpPort,
config.VMName, config.VMName,
...@@ -69,7 +72,7 @@ func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction ...@@ -69,7 +72,7 @@ func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction
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 {
command, err := config.tpl.Process(command, tplData) command, err := interpolate.Render(command, &ctx)
if err != nil { if err != nil {
err := fmt.Errorf("Error preparing boot command: %s", err) err := fmt.Errorf("Error preparing boot command: %s", err)
state.Put("error", err) state.Put("error", err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment