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{ &StepCloneVM{
config: b.config, config: b.config,
}, },
&StepConfigureHW{ &StepConfigureHardware{
config: b.config, config: &b.config.HardwareConfig,
}, },
&StepRun{}, &StepRun{},
&communicator.StepConnect{ &communicator.StepConnect{

View File

@ -8,7 +8,6 @@ import (
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"strconv"
"time" "time"
) )
@ -32,8 +31,7 @@ type Config struct {
LinkedClone bool `mapstructure:"linked_clone"` LinkedClone bool `mapstructure:"linked_clone"`
// Customization // Customization
CPUs string `mapstructure:"CPUs"` HardwareConfig `mapstructure:",squash"`
RAM string `mapstructure:"RAM"`
// Provisioning // Provisioning
communicator.Config `mapstructure:",squash"` 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")) 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 != "" { if c.RawShutdownTimeout != "" {
timeout, err := time.ParseDuration(c.RawShutdownTimeout) timeout, err := time.ParseDuration(c.RawShutdownTimeout)
if err != nil { if err != nil {

View File

@ -11,20 +11,6 @@ func TestMinimalConfig(t *testing.T) {
testConfigOk(t, warns, errs) 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) { func TestTimeout(t *testing.T) {
raw := minimalConfig() raw := minimalConfig()
raw["shutdown_timeout"] = "3m" raw["shutdown_timeout"] = "3m"

View File

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