diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 9e022b398..1634bd5ad 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -70,7 +70,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe GuestAdditionsMode: b.config.GuestAdditionsMode, GuestAdditionsURL: b.config.GuestAdditionsURL, GuestAdditionsSHA256: b.config.GuestAdditionsSHA256, - Tpl: b.config.tpl, + Ctx: b.config.ctx, }, &StepImport{ Name: b.config.VMName, @@ -88,7 +88,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &vboxcommon.StepVBoxManage{ Commands: b.config.VBoxManage, - Tpl: b.config.tpl, + Ctx: b.config.ctx, }, &vboxcommon.StepRun{ BootWait: b.config.BootWait, @@ -97,7 +97,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vboxcommon.StepTypeBootCommand{ BootCommand: b.config.BootCommand, VMName: b.config.VMName, - Tpl: b.config.tpl, + Ctx: b.config.ctx, }, &common.StepConnectSSH{ SSHAddress: vboxcommon.SSHAddress, @@ -110,7 +110,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vboxcommon.StepUploadGuestAdditions{ GuestAdditionsMode: b.config.GuestAdditionsMode, GuestAdditionsPath: b.config.GuestAdditionsPath, - Tpl: b.config.tpl, + Ctx: b.config.ctx, }, new(common.StepProvision), &vboxcommon.StepShutdown{ @@ -120,7 +120,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe new(vboxcommon.StepRemoveDevices), &vboxcommon.StepVBoxManage{ Commands: b.config.VBoxManagePost, - Tpl: b.config.tpl, + Ctx: b.config.ctx, }, &vboxcommon.StepExport{ Format: b.config.Format, diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 885687b67..de60bcc7b 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -7,7 +7,9 @@ import ( vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/template/interpolate" ) // Config is the configuration structure for the builder. @@ -34,22 +36,27 @@ type Config struct { ImportOpts string `mapstructure:"import_opts"` ImportFlags []string `mapstructure:"import_flags"` - tpl *packer.ConfigTemplate + ctx interpolate.Context } func NewConfig(raws ...interface{}) (*Config, []string, error) { - c := new(Config) - md, err := common.DecodeConfig(c, raws...) + var c Config + err := config.Decode(&c, &config.DecodeOpts{ + Interpolate: true, + InterpolateFilter: &interpolate.RenderFilter{ + Exclude: []string{ + "boot_command", + "guest_additions_path", + "guest_additions_url", + "vboxmanage", + "vboxmanage_post", + }, + }, + }, raws...) if err != nil { return nil, nil, err } - c.tpl, err = packer.NewConfigTemplate() - if err != nil { - return nil, nil, err - } - c.tpl.UserVars = c.PackerUserVars - // Defaults if c.GuestAdditionsMode == "" { c.GuestAdditionsMode = "upload" @@ -63,49 +70,17 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } // Prepare the errors - errs := common.CheckUnusedConfig(md) - errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.ExportOpts.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...) - errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(c.tpl)...) - errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(c.tpl)...) - - templates := map[string]*string{ - "guest_additions_mode": &c.GuestAdditionsMode, - "guest_additions_sha256": &c.GuestAdditionsSHA256, - "source_path": &c.SourcePath, - "vm_name": &c.VMName, - "import_opts": &c.ImportOpts, - } - - for n, ptr := range templates { - var err error - *ptr, err = c.tpl.Process(*ptr, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing %s: %s", n, err)) - } - } - - sliceTemplates := map[string][]string{ - "import_flags": c.ImportFlags, - } - - for n, slice := range sliceTemplates { - for i, elem := range slice { - var err error - slice[i], err = c.tpl.Process(elem, nil) - if err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err)) - } - } - } + var errs *packer.MultiError + errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.ExportOpts.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...) + errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(&c.ctx)...) if c.SourcePath == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required")) @@ -116,25 +91,6 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } } - for i, command := range c.BootCommand { - if err := c.tpl.Validate(command); err != nil { - errs = packer.MultiErrorAppend(errs, - fmt.Errorf("Error processing boot_command[%d]: %s", i, err)) - } - } - - validates := map[string]*string{ - "guest_additions_path": &c.GuestAdditionsPath, - "guest_additions_url": &c.GuestAdditionsURL, - } - - for n, ptr := range validates { - if err := c.tpl.Validate(*ptr); err != nil { - errs = packer.MultiErrorAppend( - errs, fmt.Errorf("Error parsing %s: %s", n, err)) - } - } - validMode := false validModes := []string{ vboxcommon.GuestAdditionsModeDisable, @@ -176,5 +132,5 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { c.ImportFlags = append(c.ImportFlags, "--options", c.ImportOpts) } - return c, warnings, nil + return &c, warnings, nil }