diff --git a/builder/virtualbox/common/run_config.go b/builder/virtualbox/common/run_config.go index 6d3f58686..cf94c306b 100644 --- a/builder/virtualbox/common/run_config.go +++ b/builder/virtualbox/common/run_config.go @@ -2,27 +2,19 @@ package common import ( "fmt" - "time" "github.com/hashicorp/packer/template/interpolate" ) type RunConfig struct { - Headless bool `mapstructure:"headless"` - RawBootWait string `mapstructure:"boot_wait"` + Headless bool `mapstructure:"headless"` VRDPBindAddress string `mapstructure:"vrdp_bind_address"` VRDPPortMin uint `mapstructure:"vrdp_port_min"` VRDPPortMax uint `mapstructure:"vrdp_port_max"` - - BootWait time.Duration `` } -func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { - if c.RawBootWait == "" { - c.RawBootWait = "10s" - } - +func (c *RunConfig) Prepare(ctx *interpolate.Context) (errs []error) { if c.VRDPBindAddress == "" { c.VRDPBindAddress = "127.0.0.1" } @@ -35,17 +27,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { c.VRDPPortMax = 6000 } - var errs []error - var err error - c.BootWait, err = time.ParseDuration(c.RawBootWait) - if err != nil { - errs = append(errs, fmt.Errorf("Failed parsing boot_wait: %s", err)) - } - if c.VRDPPortMin > c.VRDPPortMax { errs = append( errs, fmt.Errorf("vrdp_port_min must be less than vrdp_port_max")) } - return errs + return } diff --git a/builder/virtualbox/common/run_config_test.go b/builder/virtualbox/common/run_config_test.go index 87b9b1b69..1caa7e3c8 100644 --- a/builder/virtualbox/common/run_config_test.go +++ b/builder/virtualbox/common/run_config_test.go @@ -4,38 +4,6 @@ import ( "testing" ) -func TestRunConfigPrepare_BootWait(t *testing.T) { - var c *RunConfig - var errs []error - - // Test a default boot_wait - c = new(RunConfig) - errs = c.Prepare(testConfigTemplate(t)) - if len(errs) > 0 { - t.Fatalf("should not have error: %s", errs) - } - - if c.RawBootWait != "10s" { - t.Fatalf("bad value: %s", c.RawBootWait) - } - - // Test with a bad boot_wait - c = new(RunConfig) - c.RawBootWait = "this is not good" - errs = c.Prepare(testConfigTemplate(t)) - if len(errs) == 0 { - t.Fatalf("bad: %#v", errs) - } - - // Test with a good one - c = new(RunConfig) - c.RawBootWait = "5s" - errs = c.Prepare(testConfigTemplate(t)) - if len(errs) > 0 { - t.Fatalf("should not have error: %s", errs) - } -} - func TestRunConfigPrepare_VRDPBindAddress(t *testing.T) { var c *RunConfig var errs []error diff --git a/builder/virtualbox/common/step_type_boot_command.go b/builder/virtualbox/common/step_type_boot_command.go index 2ae5957af..0effc3628 100644 --- a/builder/virtualbox/common/step_type_boot_command.go +++ b/builder/virtualbox/common/step_type_boot_command.go @@ -21,7 +21,7 @@ type bootCommandTemplateData struct { } type StepTypeBootCommand struct { - BootCommand []string + BootCommand string BootWait time.Duration VMName string Ctx interpolate.Context @@ -67,33 +67,31 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag) d := bootcommand.NewPCXTDriver(sendCodes, 25) ui.Say("Typing the boot command...") - for i, command := range s.BootCommand { - command, err := interpolate.Render(command, &s.Ctx) - if err != nil { - err := fmt.Errorf("Error preparing boot command: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + command, err := interpolate.Render(s.BootCommand, &s.Ctx) + if err != nil { + err := fmt.Errorf("Error preparing boot command: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } - seq, err := bootcommand.GenerateExpressionSequence(command) - if err != nil { - err := fmt.Errorf("Error generating boot command: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + seq, err := bootcommand.GenerateExpressionSequence(command) + if err != nil { + err := fmt.Errorf("Error generating boot command: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } - if err := seq.Do(ctx, d); err != nil { - err := fmt.Errorf("Error running boot command: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + if err := seq.Do(ctx, d); err != nil { + err := fmt.Errorf("Error running boot command: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } - if pauseFn != nil { - pauseFn(multistep.DebugLocationAfterRun, fmt.Sprintf("boot_command[%d]: %s", i, command), state) - } + if pauseFn != nil { + pauseFn(multistep.DebugLocationAfterRun, fmt.Sprintf("boot_command: %s", command), state) } return multistep.ActionContinue diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 5a53854ae..2ea19e70f 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -8,6 +8,7 @@ import ( vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/bootcommand" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/multistep" @@ -27,6 +28,7 @@ type Config struct { common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"` + bootcommand.BootConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"` vboxcommon.ExportOpts `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` @@ -37,21 +39,20 @@ type Config struct { vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"` - BootCommand []string `mapstructure:"boot_command"` - DiskSize uint `mapstructure:"disk_size"` - GuestAdditionsMode string `mapstructure:"guest_additions_mode"` - GuestAdditionsPath string `mapstructure:"guest_additions_path"` - GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"` - GuestAdditionsURL string `mapstructure:"guest_additions_url"` - GuestOSType string `mapstructure:"guest_os_type"` - HardDriveDiscard bool `mapstructure:"hard_drive_discard"` - HardDriveInterface string `mapstructure:"hard_drive_interface"` - SATAPortCount int `mapstructure:"sata_port_count"` - HardDriveNonrotational bool `mapstructure:"hard_drive_nonrotational"` - ISOInterface string `mapstructure:"iso_interface"` - KeepRegistered bool `mapstructure:"keep_registered"` - SkipExport bool `mapstructure:"skip_export"` - VMName string `mapstructure:"vm_name"` + DiskSize uint `mapstructure:"disk_size"` + GuestAdditionsMode string `mapstructure:"guest_additions_mode"` + GuestAdditionsPath string `mapstructure:"guest_additions_path"` + GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"` + GuestAdditionsURL string `mapstructure:"guest_additions_url"` + GuestOSType string `mapstructure:"guest_os_type"` + HardDriveDiscard bool `mapstructure:"hard_drive_discard"` + HardDriveInterface string `mapstructure:"hard_drive_interface"` + SATAPortCount int `mapstructure:"sata_port_count"` + HardDriveNonrotational bool `mapstructure:"hard_drive_nonrotational"` + ISOInterface string `mapstructure:"iso_interface"` + KeepRegistered bool `mapstructure:"keep_registered"` + SkipExport bool `mapstructure:"skip_export"` + VMName string `mapstructure:"vm_name"` ctx interpolate.Context } @@ -94,6 +95,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.VBoxManageConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VBoxManagePostConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VBoxVersionConfig.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...) if b.config.DiskSize == 0 { b.config.DiskSize = 40000 @@ -244,7 +246,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &vboxcommon.StepTypeBootCommand{ BootWait: b.config.BootWait, - BootCommand: b.config.BootCommand, + BootCommand: b.config.FlatBootCommand(), VMName: b.config.VMName, Ctx: b.config.ctx, }, diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 71921106e..09fd416a3 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -107,7 +107,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &vboxcommon.StepTypeBootCommand{ BootWait: b.config.BootWait, - BootCommand: b.config.BootCommand, + BootCommand: b.config.FlatBootCommand(), VMName: b.config.VMName, Ctx: b.config.ctx, }, diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 9a90b1a5b..bd60a4c4c 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -6,6 +6,7 @@ import ( vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/bootcommand" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" @@ -16,6 +17,7 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"` + bootcommand.BootConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"` vboxcommon.ExportOpts `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` @@ -26,7 +28,6 @@ type Config struct { vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"` - BootCommand []string `mapstructure:"boot_command"` Checksum string `mapstructure:"checksum"` ChecksumType string `mapstructure:"checksum_type"` GuestAdditionsMode string `mapstructure:"guest_additions_mode"` @@ -90,6 +91,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { 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)...) + errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...) c.ChecksumType = strings.ToLower(c.ChecksumType) c.Checksum = strings.ToLower(c.Checksum)