Refactor hardware customization

This commit is contained in:
Michael Kuzmin 2017-07-02 01:34:50 +03:00
parent 22d4b7e733
commit a03c91e73a
4 changed files with 18 additions and 67 deletions

View File

@ -70,8 +70,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&StepCloneVM{
config: b.config,
},
&StepConfigureHW{
config: b.config,
&StepConfigureHardware{
config: &b.config.HardwareConfig,
},
&StepRun{},
&communicator.StepConnect{

View File

@ -8,7 +8,6 @@ import (
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"strconv"
"time"
)
@ -32,8 +31,7 @@ type Config struct {
LinkedClone bool `mapstructure:"linked_clone"`
// Customization
CPUs string `mapstructure:"CPUs"`
RAM string `mapstructure:"RAM"`
HardwareConfig `mapstructure:",squash"`
// Provisioning
communicator.Config `mapstructure:",squash"`
@ -83,16 +81,6 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("vSphere host is required"))
}
if c.CPUs != "" {
if _, err := strconv.Atoi(c.CPUs); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid number of CPU sockets"))
}
}
if c.RAM != "" {
if _, err := strconv.Atoi(c.RAM); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid number for RAM"))
}
}
if c.RawShutdownTimeout != "" {
timeout, err := time.ParseDuration(c.RawShutdownTimeout)
if err != nil {

View File

@ -11,20 +11,6 @@ func TestMinimalConfig(t *testing.T) {
testConfigOk(t, warns, errs)
}
func TestInvalidCpu(t *testing.T) {
raw := minimalConfig()
raw["CPUs"] = "string"
_, warns, errs := NewConfig(raw)
testConfigErr(t, warns, errs)
}
func TestInvalidRam(t *testing.T) {
raw := minimalConfig()
raw["RAM"] = "string"
_, warns, errs := NewConfig(raw)
testConfigErr(t, warns, errs)
}
func TestTimeout(t *testing.T) {
raw := minimalConfig()
raw["shutdown_timeout"] = "3m"

View File

@ -3,53 +3,32 @@ package main
import (
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/packer"
"strconv"
"github.com/vmware/govmomi/vim25/types"
"context"
"github.com/vmware/govmomi/object"
)
type StepConfigureHW struct{
config *Config
type HardwareConfig struct {
CPUs int32 `mapstructure:"CPUs"`
RAM int64 `mapstructure:"RAM"`
}
type ConfigParametersFlag struct {
NumCPUsPtr *int32
MemoryMBPtr *int64
type StepConfigureHardware struct {
config *HardwareConfig
}
func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction {
func (s *StepConfigureHardware) Run(state multistep.StateBag) multistep.StepAction {
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)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
confSpec.NumCPUs = int32(CPUs)
parametersFlag.NumCPUsPtr = &(confSpec.NumCPUs)
}
if s.config.RAM != "" {
ram, err := strconv.Atoi(s.config.RAM)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
confSpec.MemoryMB = int64(ram)
parametersFlag.MemoryMBPtr = &(confSpec.MemoryMB)
}
ui := state.Get("ui").(packer.Ui)
if parametersFlag != (ConfigParametersFlag{}) {
ui.Say("configuring virtual hardware...")
// Reconfigure hardware
if *s.config != (HardwareConfig{}) {
ui.Say("Customizing hardware parameters...")
var confSpec types.VirtualMachineConfigSpec
confSpec.NumCPUs = s.config.CPUs
confSpec.MemoryMB = s.config.RAM
task, err := vm.Reconfigure(ctx, confSpec)
if err != nil {
state.Put("error", err)
@ -60,11 +39,9 @@ func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction {
state.Put("error", err)
return multistep.ActionHalt
}
} else {
ui.Say("skipping the virtual hardware configration...")
}
return multistep.ActionContinue
}
func (s *StepConfigureHW) Cleanup(multistep.StateBag) {}
func (s *StepConfigureHardware) Cleanup(multistep.StateBag) {}