diff --git a/builder.go b/builder.go index 00ede0f07..6dbb4b476 100644 --- a/builder.go +++ b/builder.go @@ -35,10 +35,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps. steps := []multistep.Step{ - &StepConfigureHW{ + &StepCloneVM{ config: b.config, }, - &StepCloneVM{ + &StepConfigureHW{ config: b.config, }, &StepRun{}, diff --git a/step_clone_vm.go b/step_clone_vm.go index 9f51972da..921b9e696 100644 --- a/step_clone_vm.go +++ b/step_clone_vm.go @@ -18,7 +18,6 @@ type CloneParameters struct { vmSrc *object.VirtualMachine ctx context.Context vmName string - confSpec *types.VirtualMachineConfigSpec } type StepCloneVM struct{ @@ -30,8 +29,6 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) ui.Say("start cloning...") - confSpec := state.Get("confSpec").(types.VirtualMachineConfigSpec) - // Prepare entities: client (authentification), finder, folder, virtual machine client, ctx, err := createClient(s.config.Url, s.config.Username, s.config.Password) if err != nil { @@ -60,7 +57,6 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction { vmSrc: vmSrc, ctx: ctx, vmName: s.config.VMName, - confSpec: &confSpec, } vm, err := cloneVM(&cloneParameters) @@ -111,7 +107,6 @@ func cloneVM(params *CloneParameters) (vm *object.VirtualMachine, err error) { var relocateSpec types.VirtualMachineRelocateSpec cloneSpec := types.VirtualMachineCloneSpec{ Location: relocateSpec, - Config: params.confSpec, PowerOn: false, } diff --git a/step_configure_hw.go b/step_configure_hw.go index d26c4ed11..2e6da31e5 100644 --- a/step_configure_hw.go +++ b/step_configure_hw.go @@ -5,17 +5,25 @@ import ( "github.com/hashicorp/packer/packer" "strconv" "github.com/vmware/govmomi/vim25/types" + "context" + "github.com/vmware/govmomi/object" ) type StepConfigureHW struct{ config *Config } +type ConfigParametersFlag struct { + NumCPUsPtr *int32 + MemoryMBPtr *int64 +} + func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction { - ui := state.Get("ui").(packer.Ui) - ui.Say("configuring virtual hardware...") + 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) @@ -25,6 +33,7 @@ func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction { } confSpec.NumCPUs = int32(cpus) + parametersFlag.NumCPUsPtr = &(confSpec.NumCPUs) } if s.config.Ram != "" { ram, err := strconv.Atoi(s.config.Ram) @@ -34,9 +43,25 @@ func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction { } confSpec.MemoryMB = int64(ram) + parametersFlag.MemoryMBPtr = &(confSpec.MemoryMB) } - state.Put("confSpec", confSpec) + ui := state.Get("ui").(packer.Ui) + if parametersFlag != (ConfigParametersFlag{}) { + ui.Say("configuring virtual hardware...") + task, err := vm.Reconfigure(ctx, confSpec) + if err != nil { + state.Put("error", err) + return multistep.ActionHalt + } + _, err = task.WaitForResult(ctx, nil) + if err != nil { + state.Put("error", err) + return multistep.ActionHalt + } + } else { + ui.Say("skipping the virtual hardware configration...") + } return multistep.ActionContinue }