From e662927623ec9607690154308a449dba334d396d Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Sun, 15 Apr 2018 21:54:02 -0700 Subject: [PATCH] Implement boot config struct for parallels --- builder/parallels/common/run_config.go | 30 ------------ builder/parallels/common/run_config_test.go | 37 -------------- .../common/step_type_boot_command.go | 48 +++++++++---------- builder/parallels/iso/builder.go | 8 ++-- builder/parallels/pvm/builder.go | 2 +- builder/parallels/pvm/config.go | 12 ++--- 6 files changed, 34 insertions(+), 103 deletions(-) delete mode 100644 builder/parallels/common/run_config.go delete mode 100644 builder/parallels/common/run_config_test.go diff --git a/builder/parallels/common/run_config.go b/builder/parallels/common/run_config.go deleted file mode 100644 index 53fb8757b..000000000 --- a/builder/parallels/common/run_config.go +++ /dev/null @@ -1,30 +0,0 @@ -package common - -import ( - "fmt" - "time" - - "github.com/hashicorp/packer/template/interpolate" -) - -// RunConfig contains the configuration for VM run. -type RunConfig struct { - RawBootWait string `mapstructure:"boot_wait"` - - BootWait time.Duration `` -} - -// Prepare sets the configuration for VM run. -func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { - if c.RawBootWait == "" { - c.RawBootWait = "10s" - } - - var err error - c.BootWait, err = time.ParseDuration(c.RawBootWait) - if err != nil { - return []error{fmt.Errorf("Failed parsing boot_wait: %s", err)} - } - - return nil -} diff --git a/builder/parallels/common/run_config_test.go b/builder/parallels/common/run_config_test.go deleted file mode 100644 index 8068fe625..000000000 --- a/builder/parallels/common/run_config_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package common - -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) - } -} diff --git a/builder/parallels/common/step_type_boot_command.go b/builder/parallels/common/step_type_boot_command.go index 899462f15..cedc10b8a 100644 --- a/builder/parallels/common/step_type_boot_command.go +++ b/builder/parallels/common/step_type_boot_command.go @@ -22,7 +22,7 @@ type bootCommandTemplateData struct { // StepTypeBootCommand is a step that "types" the boot command into the VM via // the prltype script, built on the Parallels Virtualization SDK - Python API. type StepTypeBootCommand struct { - BootCommand []string + BootCommand string BootWait time.Duration HostInterfaces []string VMName string @@ -84,33 +84,31 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag) d := bootcommand.NewPCXTDriver(sendCodes, -1) 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/parallels/iso/builder.go b/builder/parallels/iso/builder.go index e4710b5a6..0bb8b4209 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -7,6 +7,7 @@ import ( parallelscommon "github.com/hashicorp/packer/builder/parallels/common" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/boot_command" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/multistep" @@ -26,16 +27,15 @@ type Config struct { common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` common.FloppyConfig `mapstructure:",squash"` + bootcommand.Config `mapstructure:",squash"` parallelscommon.OutputConfig `mapstructure:",squash"` parallelscommon.PrlctlConfig `mapstructure:",squash"` parallelscommon.PrlctlPostConfig `mapstructure:",squash"` parallelscommon.PrlctlVersionConfig `mapstructure:",squash"` - parallelscommon.RunConfig `mapstructure:",squash"` parallelscommon.ShutdownConfig `mapstructure:",squash"` parallelscommon.SSHConfig `mapstructure:",squash"` parallelscommon.ToolsConfig `mapstructure:",squash"` - BootCommand []string `mapstructure:"boot_command"` DiskSize uint `mapstructure:"disk_size"` DiskType string `mapstructure:"disk_type"` GuestOSType string `mapstructure:"guest_os_type"` @@ -76,13 +76,13 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend( errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) - errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.PrlctlConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.PrlctlPostConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.PrlctlVersionConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ToolsConfig.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.Config.Prepare(&b.config.ctx)...) if b.config.DiskSize == 0 { b.config.DiskSize = 40000 @@ -188,7 +188,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ¶llelscommon.StepRun{}, ¶llelscommon.StepTypeBootCommand{ BootWait: b.config.BootWait, - BootCommand: b.config.BootCommand, + BootCommand: b.config.FlatBootCommand(), HostInterfaces: b.config.HostInterfaces, VMName: b.config.VMName, Ctx: b.config.ctx, diff --git a/builder/parallels/pvm/builder.go b/builder/parallels/pvm/builder.go index 4fd5e73d9..d21388eff 100644 --- a/builder/parallels/pvm/builder.go +++ b/builder/parallels/pvm/builder.go @@ -76,7 +76,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, ¶llelscommon.StepRun{}, ¶llelscommon.StepTypeBootCommand{ - BootCommand: b.config.BootCommand, + BootCommand: b.config.FlatBootCommand(), BootWait: b.config.BootWait, HostInterfaces: []string{}, VMName: b.config.VMName, diff --git a/builder/parallels/pvm/config.go b/builder/parallels/pvm/config.go index 7641e0c9d..0ece6e11e 100644 --- a/builder/parallels/pvm/config.go +++ b/builder/parallels/pvm/config.go @@ -6,6 +6,7 @@ import ( parallelscommon "github.com/hashicorp/packer/builder/parallels/common" "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/common/boot_command" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" @@ -19,15 +20,14 @@ type Config struct { parallelscommon.PrlctlConfig `mapstructure:",squash"` parallelscommon.PrlctlPostConfig `mapstructure:",squash"` parallelscommon.PrlctlVersionConfig `mapstructure:",squash"` - parallelscommon.RunConfig `mapstructure:",squash"` parallelscommon.SSHConfig `mapstructure:",squash"` parallelscommon.ShutdownConfig `mapstructure:",squash"` + bootcommand.Config `mapstructure:",squash"` parallelscommon.ToolsConfig `mapstructure:",squash"` - BootCommand []string `mapstructure:"boot_command"` - SourcePath string `mapstructure:"source_path"` - VMName string `mapstructure:"vm_name"` - ReassignMAC bool `mapstructure:"reassign_mac"` + SourcePath string `mapstructure:"source_path"` + VMName string `mapstructure:"vm_name"` + ReassignMAC bool `mapstructure:"reassign_mac"` ctx interpolate.Context } @@ -61,7 +61,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, c.PrlctlConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.PrlctlPostConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.PrlctlVersionConfig.Prepare(&c.ctx)...) - errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.Config.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.ToolsConfig.Prepare(&c.ctx)...)