Hw customization (#13)

moved all configuration after cloning
This commit is contained in:
Elizaveta Tretyakova 2017-05-19 07:44:27 +03:00 committed by GitHub
parent 29593bb509
commit b3dcaab8b3
3 changed files with 30 additions and 10 deletions

View File

@ -35,10 +35,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps.
steps := []multistep.Step{
&StepConfigureHW{
&StepCloneVM{
config: b.config,
},
&StepCloneVM{
&StepConfigureHW{
config: b.config,
},
&StepRun{},

View File

@ -18,7 +18,6 @@ type CloneParameters struct {
vmSrc *object.VirtualMachine
ctx context.Context
vmName string
confSpec *types.VirtualMachineConfigSpec
}
type StepCloneVM struct{
@ -30,8 +29,6 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
ui.Say("start cloning...")
confSpec := state.Get("confSpec").(types.VirtualMachineConfigSpec)
// Prepare entities: client (authentification), finder, folder, virtual machine
client, ctx, err := createClient(s.config.Url, s.config.Username, s.config.Password)
if err != nil {
@ -60,7 +57,6 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
vmSrc: vmSrc,
ctx: ctx,
vmName: s.config.VMName,
confSpec: &confSpec,
}
vm, err := cloneVM(&cloneParameters)
@ -111,7 +107,6 @@ func cloneVM(params *CloneParameters) (vm *object.VirtualMachine, err error) {
var relocateSpec types.VirtualMachineRelocateSpec
cloneSpec := types.VirtualMachineCloneSpec{
Location: relocateSpec,
Config: params.confSpec,
PowerOn: false,
}

View File

@ -5,17 +5,25 @@ import (
"github.com/hashicorp/packer/packer"
"strconv"
"github.com/vmware/govmomi/vim25/types"
"context"
"github.com/vmware/govmomi/object"
)
type StepConfigureHW struct{
config *Config
}
type ConfigParametersFlag struct {
NumCPUsPtr *int32
MemoryMBPtr *int64
}
func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
ui.Say("configuring virtual hardware...")
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)
@ -25,6 +33,7 @@ func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction {
}
confSpec.NumCPUs = int32(cpus)
parametersFlag.NumCPUsPtr = &(confSpec.NumCPUs)
}
if s.config.Ram != "" {
ram, err := strconv.Atoi(s.config.Ram)
@ -34,9 +43,25 @@ func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction {
}
confSpec.MemoryMB = int64(ram)
parametersFlag.MemoryMBPtr = &(confSpec.MemoryMB)
}
state.Put("confSpec", confSpec)
ui := state.Get("ui").(packer.Ui)
if parametersFlag != (ConfigParametersFlag{}) {
ui.Say("configuring virtual hardware...")
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
}
} else {
ui.Say("skipping the virtual hardware configration...")
}
return multistep.ActionContinue
}