packer-cn/step_hardware.go

60 lines
1.6 KiB
Go
Raw Normal View History

package main
import (
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/packer"
"github.com/vmware/govmomi/object"
2017-07-02 00:50:01 +03:00
"fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
)
2017-07-02 01:34:50 +03:00
type HardwareConfig struct {
2017-07-01 23:43:40 +03:00
CPUs int32 `mapstructure:"CPUs"`
CPUReservation int64 `mapstructure:"CPU_reservation"`
2017-07-01 23:52:35 +03:00
CPULimit int64 `mapstructure:"CPU_limit"`
2017-07-01 23:43:40 +03:00
RAM int64 `mapstructure:"RAM"`
2017-07-02 00:02:49 +03:00
RAMReservation int64 `mapstructure:"RAM_reservation"`
2017-07-02 00:50:01 +03:00
RAMReserveAll bool `mapstructure:"RAM_reserve_all"`
}
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
}
2017-07-02 01:34:50 +03:00
type StepConfigureHardware struct {
config *HardwareConfig
}
2017-07-02 01:34:50 +03:00
func (s *StepConfigureHardware) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver)
2017-07-02 23:29:50 +03:00
vm := state.Get("vm").(*object.VirtualMachine)
2017-07-02 01:34:50 +03:00
if *s.config != (HardwareConfig{}) {
ui.Say("Customizing hardware parameters...")
err := d.ConfigureVM(vm, &driver.HardwareConfig{
CPUs: s.config.CPUs,
CPUReservation: s.config.CPUReservation,
CPULimit: s.config.CPULimit,
RAM: s.config.RAM,
RAMReservation: s.config.RAMReservation,
RAMReserveAll: s.config.RAMReserveAll,
})
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
2017-07-02 01:34:50 +03:00
func (s *StepConfigureHardware) Cleanup(multistep.StateBag) {}