2017-05-09 10:23:57 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
2017-05-19 00:44:27 -04:00
|
|
|
"context"
|
|
|
|
"github.com/vmware/govmomi/object"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
2017-07-01 18:34:50 -04:00
|
|
|
type HardwareConfig struct {
|
2017-07-01 16:43:40 -04:00
|
|
|
CPUs int32 `mapstructure:"CPUs"`
|
|
|
|
CPUReservation int64 `mapstructure:"CPU_reservation"`
|
2017-07-01 16:52:35 -04:00
|
|
|
CPULimit int64 `mapstructure:"CPU_limit"`
|
2017-07-01 16:43:40 -04:00
|
|
|
RAM int64 `mapstructure:"RAM"`
|
2017-07-01 17:02:49 -04:00
|
|
|
RAMReservation int64 `mapstructure:"RAM_reservation"`
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
2017-07-01 18:34:50 -04:00
|
|
|
type StepConfigureHardware struct {
|
|
|
|
config *HardwareConfig
|
2017-05-19 00:44:27 -04:00
|
|
|
}
|
|
|
|
|
2017-07-01 18:34:50 -04:00
|
|
|
func (s *StepConfigureHardware) Run(state multistep.StateBag) multistep.StepAction {
|
2017-05-19 00:44:27 -04:00
|
|
|
vm := state.Get("vm").(*object.VirtualMachine)
|
|
|
|
ctx := state.Get("ctx").(context.Context)
|
2017-07-01 18:34:50 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-07-01 18:34:50 -04:00
|
|
|
if *s.config != (HardwareConfig{}) {
|
|
|
|
ui.Say("Customizing hardware parameters...")
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-07-01 18:34:50 -04:00
|
|
|
var confSpec types.VirtualMachineConfigSpec
|
|
|
|
confSpec.NumCPUs = s.config.CPUs
|
|
|
|
confSpec.MemoryMB = s.config.RAM
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-07-01 16:43:40 -04:00
|
|
|
var cpuSpec types.ResourceAllocationInfo
|
|
|
|
cpuSpec.Reservation = s.config.CPUReservation
|
2017-07-01 16:52:35 -04:00
|
|
|
cpuSpec.Limit = s.config.CPULimit
|
2017-07-01 16:43:40 -04:00
|
|
|
confSpec.CpuAllocation = &cpuSpec
|
|
|
|
|
2017-07-01 17:02:49 -04:00
|
|
|
var ramSpec types.ResourceAllocationInfo
|
|
|
|
ramSpec.Reservation = s.config.RAMReservation
|
|
|
|
confSpec.MemoryAllocation = &ramSpec
|
|
|
|
|
2017-05-19 00:44:27 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:34:50 -04:00
|
|
|
func (s *StepConfigureHardware) Cleanup(multistep.StateBag) {}
|