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. // Build the steps.
steps := []multistep.Step{ steps := []multistep.Step{
&StepConfigureHW{ &StepCloneVM{
config: b.config, config: b.config,
}, },
&StepCloneVM{ &StepConfigureHW{
config: b.config, config: b.config,
}, },
&StepRun{}, &StepRun{},

View File

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

View File

@ -5,17 +5,25 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"strconv" "strconv"
"github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/vim25/types"
"context"
"github.com/vmware/govmomi/object"
) )
type StepConfigureHW struct{ type StepConfigureHW struct{
config *Config config *Config
} }
type ConfigParametersFlag struct {
NumCPUsPtr *int32
MemoryMBPtr *int64
}
func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction { func (s *StepConfigureHW) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) vm := state.Get("vm").(*object.VirtualMachine)
ui.Say("configuring virtual hardware...") ctx := state.Get("ctx").(context.Context)
var confSpec types.VirtualMachineConfigSpec var confSpec types.VirtualMachineConfigSpec
parametersFlag := ConfigParametersFlag{}
// configure HW // configure HW
if s.config.Cpus != "" { if s.config.Cpus != "" {
cpus, err := strconv.Atoi(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) confSpec.NumCPUs = int32(cpus)
parametersFlag.NumCPUsPtr = &(confSpec.NumCPUs)
} }
if s.config.Ram != "" { if s.config.Ram != "" {
ram, err := strconv.Atoi(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) 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 return multistep.ActionContinue
} }