fix vshere ToolsSyncTime and ToolsUpgradePolicy

This commit is contained in:
Joshua Foster 2020-07-01 12:53:56 -04:00
parent af9d2e6f22
commit 63f9282f5c
3 changed files with 31 additions and 20 deletions

View File

@ -7,6 +7,8 @@ import (
"context"
"fmt"
"github.com/vmware/govmomi/vim25/types"
"github.com/hashicorp/packer/builder/vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
@ -17,8 +19,7 @@ type ConfigParamsConfig struct {
// ConfigSpec: https://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html
ConfigParams map[string]string `mapstructure:"configuration_parameters"`
// Enables time synchronization with the host. If set to true will set `tools.syncTime` to `TRUE`.
// Defaults to FALSE.
// 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.
@ -39,17 +40,22 @@ func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) mult
configParams = s.Config.ConfigParams
}
if s.Config.ToolsSyncTime {
configParams["tools.syncTime"] = "TRUE"
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"
}
}
if s.Config.ToolsUpgradePolicy {
configParams["tools.upgrade.policy"] = "upgradeAtPowerCycle"
}
if len(configParams) > 0 {
if len(configParams) > 0 || info != nil {
ui.Say("Adding configuration parameters...")
if err := vm.AddConfigParams(configParams); err != nil {
if err := vm.AddConfigParams(configParams, info); err != nil {
state.Put("error", fmt.Errorf("error adding configuration parameters: %v", err))
return multistep.ActionHalt
}

View File

@ -800,18 +800,24 @@ func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error {
return err
}
func (vm *VirtualMachine) AddConfigParams(params map[string]string) error {
func (vm *VirtualMachine) AddConfigParams(params map[string]string, info *types.ToolsConfigInfo) error {
var confSpec types.VirtualMachineConfigSpec
var ov []types.BaseOptionValue
for k, v := range params {
o := types.OptionValue{
Key: k,
Value: v,
if len(params) > 0 {
var ov []types.BaseOptionValue
for k, v := range params {
o := types.OptionValue{
Key: k,
Value: v,
}
ov = append(ov, &o)
}
ov = append(ov, &o)
confSpec.ExtraConfig = ov
}
if info != nil {
confSpec.Tools = info
}
confSpec.ExtraConfig = ov
task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec)
if err != nil {

View File

@ -3,8 +3,7 @@
- `configuration_parameters` (map[string]string) - configuration_parameters is a direct passthrough to the VSphere API's
ConfigSpec: https://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html
- `tools_sync_time` (bool) - Enables time synchronization with the host. If set to true will set `tools.syncTime` to `TRUE`.
Defaults to FALSE.
- `tools_sync_time` (bool) - Enables time synchronization with the host. Defaults to false.
- `tools_upgrade_policy` (bool) - If sets to true, vSphere will automatically check and upgrade VMware Tools upon a system power cycle.
If not set, defaults to manual upgrade.