From a03c91e73a7b3449a10203037eedeb952ace7963 Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Sun, 2 Jul 2017 01:34:50 +0300 Subject: [PATCH] Refactor hardware customization --- builder.go | 4 ++-- config.go | 14 +------------ config_test.go | 14 ------------- step_hardware.go | 53 ++++++++++++++---------------------------------- 4 files changed, 18 insertions(+), 67 deletions(-) diff --git a/builder.go b/builder.go index 479c4c2f0..f3404f1ba 100644 --- a/builder.go +++ b/builder.go @@ -70,8 +70,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepCloneVM{ config: b.config, }, - &StepConfigureHW{ - config: b.config, + &StepConfigureHardware{ + config: &b.config.HardwareConfig, }, &StepRun{}, &communicator.StepConnect{ diff --git a/config.go b/config.go index 246447d30..80405a5c5 100644 --- a/config.go +++ b/config.go @@ -8,7 +8,6 @@ import ( "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" - "strconv" "time" ) @@ -32,8 +31,7 @@ type Config struct { LinkedClone bool `mapstructure:"linked_clone"` // Customization - CPUs string `mapstructure:"CPUs"` - RAM string `mapstructure:"RAM"` + HardwareConfig `mapstructure:",squash"` // Provisioning communicator.Config `mapstructure:",squash"` @@ -83,16 +81,6 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, fmt.Errorf("vSphere host is required")) } - if c.CPUs != "" { - if _, err := strconv.Atoi(c.CPUs); err != nil { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid number of CPU sockets")) - } - } - if c.RAM != "" { - if _, err := strconv.Atoi(c.RAM); err != nil { - errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid number for RAM")) - } - } if c.RawShutdownTimeout != "" { timeout, err := time.ParseDuration(c.RawShutdownTimeout) if err != nil { diff --git a/config_test.go b/config_test.go index 3188dfadf..46f80662b 100644 --- a/config_test.go +++ b/config_test.go @@ -11,20 +11,6 @@ func TestMinimalConfig(t *testing.T) { testConfigOk(t, warns, errs) } -func TestInvalidCpu(t *testing.T) { - raw := minimalConfig() - raw["CPUs"] = "string" - _, warns, errs := NewConfig(raw) - testConfigErr(t, warns, errs) -} - -func TestInvalidRam(t *testing.T) { - raw := minimalConfig() - raw["RAM"] = "string" - _, warns, errs := NewConfig(raw) - testConfigErr(t, warns, errs) -} - func TestTimeout(t *testing.T) { raw := minimalConfig() raw["shutdown_timeout"] = "3m" diff --git a/step_hardware.go b/step_hardware.go index 4b5f7d2e2..38771c332 100644 --- a/step_hardware.go +++ b/step_hardware.go @@ -3,53 +3,32 @@ package main import ( "github.com/mitchellh/multistep" "github.com/hashicorp/packer/packer" - "strconv" "github.com/vmware/govmomi/vim25/types" "context" "github.com/vmware/govmomi/object" ) -type StepConfigureHW struct{ - config *Config +type HardwareConfig struct { + CPUs int32 `mapstructure:"CPUs"` + RAM int64 `mapstructure:"RAM"` } -type ConfigParametersFlag struct { - NumCPUsPtr *int32 - MemoryMBPtr *int64 +type StepConfigureHardware struct { + config *HardwareConfig } -func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction { +func (s *StepConfigureHardware) Run(state multistep.StateBag) multistep.StepAction { vm := state.Get("vm").(*object.VirtualMachine) ctx := state.Get("ctx").(context.Context) - - var confSpec types.VirtualMachineConfigSpec - parametersFlag := ConfigParametersFlag{} - // configure HW - if s.config.CPUs != "" { - CPUs, err := strconv.Atoi(s.config.CPUs) - if err != nil { - state.Put("error", err) - return multistep.ActionHalt - } - - confSpec.NumCPUs = int32(CPUs) - parametersFlag.NumCPUsPtr = &(confSpec.NumCPUs) - } - if s.config.RAM != "" { - ram, err := strconv.Atoi(s.config.RAM) - if err != nil { - state.Put("error", err) - return multistep.ActionHalt - } - - confSpec.MemoryMB = int64(ram) - parametersFlag.MemoryMBPtr = &(confSpec.MemoryMB) - } - ui := state.Get("ui").(packer.Ui) - if parametersFlag != (ConfigParametersFlag{}) { - ui.Say("configuring virtual hardware...") - // Reconfigure hardware + + if *s.config != (HardwareConfig{}) { + ui.Say("Customizing hardware parameters...") + + var confSpec types.VirtualMachineConfigSpec + confSpec.NumCPUs = s.config.CPUs + confSpec.MemoryMB = s.config.RAM + task, err := vm.Reconfigure(ctx, confSpec) if err != nil { state.Put("error", err) @@ -60,11 +39,9 @@ func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction { state.Put("error", err) return multistep.ActionHalt } - } else { - ui.Say("skipping the virtual hardware configration...") } return multistep.ActionContinue } -func (s *StepConfigureHW) Cleanup(multistep.StateBag) {} +func (s *StepConfigureHardware) Cleanup(multistep.StateBag) {}