From b05ba0a9a21860e05352029eb7aa3d1d91ea5eaa Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Sun, 2 Jul 2017 07:49:39 +0300 Subject: [PATCH] hardware step uses vSphere driver --- driver.go | 28 ++++++++++++++++++++++++++++ step_hardware.go | 23 +---------------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/driver.go b/driver.go index ce5f26eda..79a692750 100644 --- a/driver.go +++ b/driver.go @@ -129,3 +129,31 @@ func (d *Driver) destroyVM(vm *object.VirtualMachine) error { } return nil } + +func (d *Driver) configureVM(vm *object.VirtualMachine, config *HardwareConfig) error { + var confSpec types.VirtualMachineConfigSpec + confSpec.NumCPUs = config.CPUs + confSpec.MemoryMB = config.RAM + + var cpuSpec types.ResourceAllocationInfo + cpuSpec.Reservation = config.CPUReservation + cpuSpec.Limit = config.CPULimit + confSpec.CpuAllocation = &cpuSpec + + var ramSpec types.ResourceAllocationInfo + ramSpec.Reservation = config.RAMReservation + confSpec.MemoryAllocation = &ramSpec + + confSpec.MemoryReservationLockedToMax = &config.RAMReserveAll + + task, err := vm.Reconfigure(d.ctx, confSpec) + if err != nil { + return err + } + _, err = task.WaitForResult(d.ctx, nil) + if err != nil { + return err + } + + return nil +} diff --git a/step_hardware.go b/step_hardware.go index 9da092ef6..11b986c78 100644 --- a/step_hardware.go +++ b/step_hardware.go @@ -3,7 +3,6 @@ package main import ( "github.com/mitchellh/multistep" "github.com/hashicorp/packer/packer" - "github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/object" "fmt" ) @@ -39,27 +38,7 @@ func (s *StepConfigureHardware) Run(state multistep.StateBag) multistep.StepActi if *s.config != (HardwareConfig{}) { ui.Say("Customizing hardware parameters...") - var confSpec types.VirtualMachineConfigSpec - confSpec.NumCPUs = s.config.CPUs - confSpec.MemoryMB = s.config.RAM - - var cpuSpec types.ResourceAllocationInfo - cpuSpec.Reservation = s.config.CPUReservation - cpuSpec.Limit = s.config.CPULimit - confSpec.CpuAllocation = &cpuSpec - - var ramSpec types.ResourceAllocationInfo - ramSpec.Reservation = s.config.RAMReservation - confSpec.MemoryAllocation = &ramSpec - - confSpec.MemoryReservationLockedToMax = &s.config.RAMReserveAll - - task, err := vm.Reconfigure(d.ctx, confSpec) - if err != nil { - state.Put("error", err) - return multistep.ActionHalt - } - _, err = task.WaitForResult(d.ctx, nil) + err := d.configureVM(vm, s.config) if err != nil { state.Put("error", err) return multistep.ActionHalt