packer-cn/builder/vsphere/common/step_hardware.go

71 lines
2.0 KiB
Go
Raw Normal View History

2018-05-05 17:41:14 -04:00
package common
import (
"context"
"fmt"
2018-10-31 17:42:24 -04:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
2018-05-05 17:41:14 -04:00
)
type HardwareConfig struct {
2018-10-31 17:42:24 -04:00
CPUs int32 `mapstructure:"CPUs"`
2018-12-18 12:51:56 -05:00
CpuCores int32 `mapstructure:"cpu_cores"`
2018-10-31 17:42:24 -04:00
CPUReservation int64 `mapstructure:"CPU_reservation"`
CPULimit int64 `mapstructure:"CPU_limit"`
CpuHotAddEnabled bool `mapstructure:"CPU_hot_plug"`
2018-05-06 17:26:04 -04:00
2018-05-05 17:41:14 -04:00
RAM int64 `mapstructure:"RAM"`
RAMReservation int64 `mapstructure:"RAM_reservation"`
RAMReserveAll bool `mapstructure:"RAM_reserve_all"`
MemoryHotAddEnabled bool `mapstructure:"RAM_hot_plug"`
2018-05-06 17:26:04 -04:00
2018-12-17 07:49:04 -05:00
VideoRAM int64 `mapstructure:"video_ram"`
NestedHV bool `mapstructure:"NestedHV"`
2018-05-05 17:41:14 -04:00
}
func (c *HardwareConfig) Prepare() []error {
var errs []error
if c.RAMReservation > 0 && c.RAMReserveAll != false {
errs = append(errs, fmt.Errorf("'RAM_reservation' and 'RAM_reserve_all' cannot be used together"))
}
return errs
}
type StepConfigureHardware struct {
Config *HardwareConfig
}
func (s *StepConfigureHardware) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
if *s.Config != (HardwareConfig{}) {
2018-05-06 17:26:04 -04:00
ui.Say("Customizing hardware...")
2018-05-05 17:41:14 -04:00
err := vm.Configure(&driver.HardwareConfig{
CPUs: s.Config.CPUs,
2018-12-18 12:51:56 -05:00
CpuCores: s.Config.CpuCores,
2018-05-05 17:41:14 -04:00
CPUReservation: s.Config.CPUReservation,
CPULimit: s.Config.CPULimit,
RAM: s.Config.RAM,
RAMReservation: s.Config.RAMReservation,
RAMReserveAll: s.Config.RAMReserveAll,
NestedHV: s.Config.NestedHV,
CpuHotAddEnabled: s.Config.CpuHotAddEnabled,
MemoryHotAddEnabled: s.Config.MemoryHotAddEnabled,
2018-12-17 07:49:04 -05:00
VideoRAM: s.Config.VideoRAM,
2018-05-05 17:41:14 -04:00
})
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
func (s *StepConfigureHardware) Cleanup(multistep.StateBag) {}