implement boot config struct for virtualbox

This commit is contained in:
Matthew Hooker 2018-04-18 16:19:44 -07:00
parent c8e76ce298
commit a0c9ddb9ae
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
6 changed files with 48 additions and 93 deletions

View File

@ -2,27 +2,19 @@ package common
import ( import (
"fmt" "fmt"
"time"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
) )
type RunConfig struct { type RunConfig struct {
Headless bool `mapstructure:"headless"` Headless bool `mapstructure:"headless"`
RawBootWait string `mapstructure:"boot_wait"`
VRDPBindAddress string `mapstructure:"vrdp_bind_address"` VRDPBindAddress string `mapstructure:"vrdp_bind_address"`
VRDPPortMin uint `mapstructure:"vrdp_port_min"` VRDPPortMin uint `mapstructure:"vrdp_port_min"`
VRDPPortMax uint `mapstructure:"vrdp_port_max"` VRDPPortMax uint `mapstructure:"vrdp_port_max"`
BootWait time.Duration ``
} }
func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { func (c *RunConfig) Prepare(ctx *interpolate.Context) (errs []error) {
if c.RawBootWait == "" {
c.RawBootWait = "10s"
}
if c.VRDPBindAddress == "" { if c.VRDPBindAddress == "" {
c.VRDPBindAddress = "127.0.0.1" c.VRDPBindAddress = "127.0.0.1"
} }
@ -35,17 +27,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
c.VRDPPortMax = 6000 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 { if c.VRDPPortMin > c.VRDPPortMax {
errs = append( errs = append(
errs, fmt.Errorf("vrdp_port_min must be less than vrdp_port_max")) errs, fmt.Errorf("vrdp_port_min must be less than vrdp_port_max"))
} }
return errs return
} }

View File

@ -4,38 +4,6 @@ import (
"testing" "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) { func TestRunConfigPrepare_VRDPBindAddress(t *testing.T) {
var c *RunConfig var c *RunConfig
var errs []error var errs []error

View File

@ -21,7 +21,7 @@ type bootCommandTemplateData struct {
} }
type StepTypeBootCommand struct { type StepTypeBootCommand struct {
BootCommand []string BootCommand string
BootWait time.Duration BootWait time.Duration
VMName string VMName string
Ctx interpolate.Context Ctx interpolate.Context
@ -67,33 +67,31 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag)
d := bootcommand.NewPCXTDriver(sendCodes, 25) d := bootcommand.NewPCXTDriver(sendCodes, 25)
ui.Say("Typing the boot command...") ui.Say("Typing the boot command...")
for i, command := range s.BootCommand { command, err := interpolate.Render(s.BootCommand, &s.Ctx)
command, err := interpolate.Render(command, &s.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) ui.Error(err.Error())
ui.Error(err.Error()) return multistep.ActionHalt
return multistep.ActionHalt }
}
seq, err := bootcommand.GenerateExpressionSequence(command) seq, err := bootcommand.GenerateExpressionSequence(command)
if err != nil { if err != nil {
err := fmt.Errorf("Error generating boot command: %s", err) err := fmt.Errorf("Error generating boot command: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
if err := seq.Do(ctx, d); err != nil { if err := seq.Do(ctx, d); err != nil {
err := fmt.Errorf("Error running boot command: %s", err) err := fmt.Errorf("Error running boot command: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
if pauseFn != nil { if pauseFn != nil {
pauseFn(multistep.DebugLocationAfterRun, fmt.Sprintf("boot_command[%d]: %s", i, command), state) pauseFn(multistep.DebugLocationAfterRun, fmt.Sprintf("boot_command: %s", command), state)
}
} }
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -8,6 +8,7 @@ import (
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/bootcommand"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -27,6 +28,7 @@ type Config struct {
common.HTTPConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"`
common.ISOConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"`
common.FloppyConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
vboxcommon.ExportConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"`
vboxcommon.ExportOpts `mapstructure:",squash"` vboxcommon.ExportOpts `mapstructure:",squash"`
vboxcommon.OutputConfig `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"`
@ -37,21 +39,20 @@ type Config struct {
vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
vboxcommon.VBoxVersionConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
BootCommand []string `mapstructure:"boot_command"` DiskSize uint `mapstructure:"disk_size"`
DiskSize uint `mapstructure:"disk_size"` GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
GuestAdditionsMode string `mapstructure:"guest_additions_mode"` GuestAdditionsPath string `mapstructure:"guest_additions_path"`
GuestAdditionsPath string `mapstructure:"guest_additions_path"` GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"`
GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"` GuestAdditionsURL string `mapstructure:"guest_additions_url"`
GuestAdditionsURL string `mapstructure:"guest_additions_url"` GuestOSType string `mapstructure:"guest_os_type"`
GuestOSType string `mapstructure:"guest_os_type"` HardDriveDiscard bool `mapstructure:"hard_drive_discard"`
HardDriveDiscard bool `mapstructure:"hard_drive_discard"` HardDriveInterface string `mapstructure:"hard_drive_interface"`
HardDriveInterface string `mapstructure:"hard_drive_interface"` SATAPortCount int `mapstructure:"sata_port_count"`
SATAPortCount int `mapstructure:"sata_port_count"` HardDriveNonrotational bool `mapstructure:"hard_drive_nonrotational"`
HardDriveNonrotational bool `mapstructure:"hard_drive_nonrotational"` ISOInterface string `mapstructure:"iso_interface"`
ISOInterface string `mapstructure:"iso_interface"` KeepRegistered bool `mapstructure:"keep_registered"`
KeepRegistered bool `mapstructure:"keep_registered"` SkipExport bool `mapstructure:"skip_export"`
SkipExport bool `mapstructure:"skip_export"` VMName string `mapstructure:"vm_name"`
VMName string `mapstructure:"vm_name"`
ctx interpolate.Context 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.VBoxManageConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.VBoxManagePostConfig.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.VBoxVersionConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...)
if b.config.DiskSize == 0 { if b.config.DiskSize == 0 {
b.config.DiskSize = 40000 b.config.DiskSize = 40000
@ -244,7 +246,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
&vboxcommon.StepTypeBootCommand{ &vboxcommon.StepTypeBootCommand{
BootWait: b.config.BootWait, BootWait: b.config.BootWait,
BootCommand: b.config.BootCommand, BootCommand: b.config.FlatBootCommand(),
VMName: b.config.VMName, VMName: b.config.VMName,
Ctx: b.config.ctx, Ctx: b.config.ctx,
}, },

View File

@ -107,7 +107,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
&vboxcommon.StepTypeBootCommand{ &vboxcommon.StepTypeBootCommand{
BootWait: b.config.BootWait, BootWait: b.config.BootWait,
BootCommand: b.config.BootCommand, BootCommand: b.config.FlatBootCommand(),
VMName: b.config.VMName, VMName: b.config.VMName,
Ctx: b.config.ctx, Ctx: b.config.ctx,
}, },

View File

@ -6,6 +6,7 @@ import (
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/bootcommand"
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
@ -16,6 +17,7 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
common.HTTPConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"`
common.FloppyConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
vboxcommon.ExportConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"`
vboxcommon.ExportOpts `mapstructure:",squash"` vboxcommon.ExportOpts `mapstructure:",squash"`
vboxcommon.OutputConfig `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"`
@ -26,7 +28,6 @@ type Config struct {
vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
vboxcommon.VBoxVersionConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
BootCommand []string `mapstructure:"boot_command"`
Checksum string `mapstructure:"checksum"` Checksum string `mapstructure:"checksum"`
ChecksumType string `mapstructure:"checksum_type"` ChecksumType string `mapstructure:"checksum_type"`
GuestAdditionsMode string `mapstructure:"guest_additions_mode"` 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.VBoxManageConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.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.VBoxVersionConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...)
c.ChecksumType = strings.ToLower(c.ChecksumType) c.ChecksumType = strings.ToLower(c.ChecksumType)
c.Checksum = strings.ToLower(c.Checksum) c.Checksum = strings.ToLower(c.Checksum)