diff --git a/driver/vm.go b/driver/vm.go index 3b43069e9..118bdfb77 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -519,3 +519,25 @@ func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error { func convertGiBToKiB(gib int64) int64 { return gib * 1024 * 1024 } + +func (vm *VirtualMachine) AddConfigParams(params map[string]string) error { + var confSpec types.VirtualMachineConfigSpec + + var ov []types.BaseOptionValue + for k, v := range params { + o := types.OptionValue{ + Key: k, + Value: v, + } + ov = append(ov, &o) + } + confSpec.ExtraConfig = ov + + task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec) + if err != nil { + return err + } + + _, err = task.WaitForResult(vm.driver.ctx, nil) + return err +} diff --git a/iso/builder.go b/iso/builder.go index 822506149..6730ec969 100644 --- a/iso/builder.go +++ b/iso/builder.go @@ -50,6 +50,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Config: &b.config.FloppyConfig, Datastore: b.config.Datastore, }, + &StepConfigParams{ + Config: &b.config.ConfigParamsConfig, + }, ) if b.config.Comm.Type != "none" { diff --git a/iso/config.go b/iso/config.go index 516a6d279..73777d571 100644 --- a/iso/config.go +++ b/iso/config.go @@ -22,6 +22,7 @@ type Config struct { CreateConfig `mapstructure:",squash"` CDRomConfig `mapstructure:",squash"` FloppyConfig `mapstructure:",squash"` + ConfigParamsConfig `mapstructure:",squash"` ctx interpolate.Context } diff --git a/iso/step_config_params.go b/iso/step_config_params.go new file mode 100644 index 000000000..15449fa70 --- /dev/null +++ b/iso/step_config_params.go @@ -0,0 +1,36 @@ +package iso + +import ( + "github.com/hashicorp/packer/packer" + "github.com/jetbrains-infra/packer-builder-vsphere/driver" + "github.com/mitchellh/multistep" + "fmt" +) + +type ConfigParamsConfig struct { + ConfigParams map[string]string `mapstructure:"configuration_parameters"` +} + +func (c *ConfigParamsConfig) Prepare() []error { + return nil +} + +type StepConfigParams struct { + Config *ConfigParamsConfig +} + +func (s *StepConfigParams) Run(state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packer.Ui) + vm := state.Get("vm").(*driver.VirtualMachine) + + ui.Say("Adding configuration parameters...") + + if err := vm.AddConfigParams(s.Config.ConfigParams); err != nil { + state.Put("error", fmt.Errorf("error adding configuration parameters: %v", err)) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *StepConfigParams) Cleanup(state multistep.StateBag) {}