packer-cn/builder/vsphere/common/step_config_params.go

66 lines
1.9 KiB
Go
Raw Normal View History

//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type ConfigParamsConfig
package common
2018-02-26 16:18:06 -05:00
import (
2018-10-31 17:42:24 -04:00
"context"
"fmt"
"github.com/vmware/govmomi/vim25/types"
"github.com/hashicorp/packer/builder/vsphere/driver"
2018-10-31 17:42:24 -04:00
"github.com/hashicorp/packer/helper/multistep"
2018-02-26 16:18:06 -05:00
"github.com/hashicorp/packer/packer"
)
type ConfigParamsConfig struct {
// configuration_parameters is a direct passthrough to the VSphere API's
// ConfigSpec: https://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html
2018-02-26 16:18:06 -05:00
ConfigParams map[string]string `mapstructure:"configuration_parameters"`
// Enables time synchronization with the host. Defaults to false.
ToolsSyncTime bool `mapstructure:"tools_sync_time"`
// If sets to true, vSphere will automatically check and upgrade VMware Tools upon a system power cycle.
// If not set, defaults to manual upgrade.
ToolsUpgradePolicy bool `mapstructure:"tools_upgrade_policy"`
2018-02-26 16:18:06 -05:00
}
type StepConfigParams struct {
Config *ConfigParamsConfig
}
2018-04-25 07:22:38 -04:00
func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2018-02-26 16:18:06 -05:00
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
configParams := make(map[string]string)
2018-02-26 16:18:06 -05:00
2018-05-06 17:26:04 -04:00
if s.Config.ConfigParams != nil {
configParams = s.Config.ConfigParams
}
var info *types.ToolsConfigInfo
if s.Config.ToolsSyncTime || s.Config.ToolsUpgradePolicy {
info = &types.ToolsConfigInfo{}
if s.Config.ToolsSyncTime {
info.SyncTimeWithHost = &s.Config.ToolsSyncTime
}
if s.Config.ToolsUpgradePolicy {
info.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
}
2018-05-06 17:26:04 -04:00
ui.Say("Adding configuration parameters...")
if err := vm.AddConfigParams(configParams, info); err != nil {
2018-05-06 17:26:04 -04:00
state.Put("error", fmt.Errorf("error adding configuration parameters: %v", err))
return multistep.ActionHalt
}
2018-02-26 16:18:06 -05:00
}
return multistep.ActionContinue
}
func (s *StepConfigParams) Cleanup(state multistep.StateBag) {}