Add 'configuration_parameters' option

This commit is contained in:
Michael Kuzmin 2018-02-27 00:18:06 +03:00
parent 099215cebc
commit 39f1e4a394
4 changed files with 62 additions and 0 deletions

View File

@ -519,3 +519,25 @@ func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error {
func convertGiBToKiB(gib int64) int64 { func convertGiBToKiB(gib int64) int64 {
return gib * 1024 * 1024 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
}

View File

@ -50,6 +50,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Config: &b.config.FloppyConfig, Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore, Datastore: b.config.Datastore,
}, },
&StepConfigParams{
Config: &b.config.ConfigParamsConfig,
},
) )
if b.config.Comm.Type != "none" { if b.config.Comm.Type != "none" {

View File

@ -22,6 +22,7 @@ type Config struct {
CreateConfig `mapstructure:",squash"` CreateConfig `mapstructure:",squash"`
CDRomConfig `mapstructure:",squash"` CDRomConfig `mapstructure:",squash"`
FloppyConfig `mapstructure:",squash"` FloppyConfig `mapstructure:",squash"`
ConfigParamsConfig `mapstructure:",squash"`
ctx interpolate.Context ctx interpolate.Context
} }

36
iso/step_config_params.go Normal file
View File

@ -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) {}