From 112638da01d214423ea5107130c125387c48a85b Mon Sep 17 00:00:00 2001 From: Moss Date: Fri, 5 Jun 2020 17:20:44 +0200 Subject: [PATCH] Add ToolSyncTime and ToolUpgradePolicy to vSphere --- builder/vsphere/clone/config.hcl2spec.go | 4 ++++ builder/vsphere/common/step_config_params.go | 23 ++++++++++++++++++- .../common/step_config_params.hcl2spec.go | 6 ++++- builder/vsphere/iso/config.hcl2spec.go | 4 ++++ .../ConfigParamsConfig-not-required.mdx | 6 +++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/builder/vsphere/clone/config.hcl2spec.go b/builder/vsphere/clone/config.hcl2spec.go index 4d3936e78..ee74b5156 100644 --- a/builder/vsphere/clone/config.hcl2spec.go +++ b/builder/vsphere/clone/config.hcl2spec.go @@ -48,6 +48,8 @@ type FlatConfig struct { NestedHV *bool `mapstructure:"NestedHV" cty:"NestedHV" hcl:"NestedHV"` Firmware *string `mapstructure:"firmware" cty:"firmware" hcl:"firmware"` ConfigParams map[string]string `mapstructure:"configuration_parameters" cty:"configuration_parameters" hcl:"configuration_parameters"` + ToolsSyncTime *bool `mapstructure:"tools_sync_time" cty:"tools_sync_time" hcl:"tools_sync_time"` + ToolsUpgradePolicy *bool `mapstructure:"tools_upgrade_policy" cty:"tools_upgrade_policy" hcl:"tools_upgrade_policy"` BootOrder *string `mapstructure:"boot_order" cty:"boot_order" hcl:"boot_order"` WaitTimeout *string `mapstructure:"ip_wait_timeout" cty:"ip_wait_timeout" hcl:"ip_wait_timeout"` SettleTimeout *string `mapstructure:"ip_settle_timeout" cty:"ip_settle_timeout" hcl:"ip_settle_timeout"` @@ -151,6 +153,8 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "NestedHV": &hcldec.AttrSpec{Name: "NestedHV", Type: cty.Bool, Required: false}, "firmware": &hcldec.AttrSpec{Name: "firmware", Type: cty.String, Required: false}, "configuration_parameters": &hcldec.AttrSpec{Name: "configuration_parameters", Type: cty.Map(cty.String), Required: false}, + "tools_sync_time": &hcldec.AttrSpec{Name: "tools_sync_time", Type: cty.Bool, Required: false}, + "tools_upgrade_policy": &hcldec.AttrSpec{Name: "tools_upgrade_policy", Type: cty.Bool, Required: false}, "boot_order": &hcldec.AttrSpec{Name: "boot_order", Type: cty.String, Required: false}, "ip_wait_timeout": &hcldec.AttrSpec{Name: "ip_wait_timeout", Type: cty.String, Required: false}, "ip_settle_timeout": &hcldec.AttrSpec{Name: "ip_settle_timeout", Type: cty.String, Required: false}, diff --git a/builder/vsphere/common/step_config_params.go b/builder/vsphere/common/step_config_params.go index 7cafdfbb0..7d194effc 100644 --- a/builder/vsphere/common/step_config_params.go +++ b/builder/vsphere/common/step_config_params.go @@ -16,6 +16,14 @@ 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 ConfigParams map[string]string `mapstructure:"configuration_parameters"` + + // Enables time synchronization with the host. If set to true will set `tools.syncTime` to `TRUE`. + // Defaults fo 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"` } type StepConfigParams struct { @@ -25,10 +33,23 @@ type StepConfigParams struct { func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) vm := state.Get("vm").(*driver.VirtualMachine) + configParams := make(map[string]string) if s.Config.ConfigParams != nil { + configParams = s.Config.ConfigParams + } + + if s.Config.ToolsSyncTime { + configParams["tools.syncTime"] = "TRUE" + } + + if s.Config.ToolsUpgradePolicy { + configParams["tools.upgrade.policy"] = "upgradeAtPowerCycle" + } + + if len(configParams) > 0 { ui.Say("Adding configuration parameters...") - if err := vm.AddConfigParams(s.Config.ConfigParams); err != nil { + if err := vm.AddConfigParams(configParams); err != nil { state.Put("error", fmt.Errorf("error adding configuration parameters: %v", err)) return multistep.ActionHalt } diff --git a/builder/vsphere/common/step_config_params.hcl2spec.go b/builder/vsphere/common/step_config_params.hcl2spec.go index fd68f2fbf..1339670f2 100644 --- a/builder/vsphere/common/step_config_params.hcl2spec.go +++ b/builder/vsphere/common/step_config_params.hcl2spec.go @@ -9,7 +9,9 @@ import ( // FlatConfigParamsConfig is an auto-generated flat version of ConfigParamsConfig. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. type FlatConfigParamsConfig struct { - ConfigParams map[string]string `mapstructure:"configuration_parameters" cty:"configuration_parameters" hcl:"configuration_parameters"` + ConfigParams map[string]string `mapstructure:"configuration_parameters" cty:"configuration_parameters" hcl:"configuration_parameters"` + ToolsSyncTime *bool `mapstructure:"tools_sync_time" cty:"tools_sync_time" hcl:"tools_sync_time"` + ToolsUpgradePolicy *bool `mapstructure:"tools_upgrade_policy" cty:"tools_upgrade_policy" hcl:"tools_upgrade_policy"` } // FlatMapstructure returns a new FlatConfigParamsConfig. @@ -25,6 +27,8 @@ func (*ConfigParamsConfig) FlatMapstructure() interface{ HCL2Spec() map[string]h func (*FlatConfigParamsConfig) HCL2Spec() map[string]hcldec.Spec { s := map[string]hcldec.Spec{ "configuration_parameters": &hcldec.AttrSpec{Name: "configuration_parameters", Type: cty.Map(cty.String), Required: false}, + "tools_sync_time": &hcldec.AttrSpec{Name: "tools_sync_time", Type: cty.Bool, Required: false}, + "tools_upgrade_policy": &hcldec.AttrSpec{Name: "tools_upgrade_policy", Type: cty.Bool, Required: false}, } return s } diff --git a/builder/vsphere/iso/config.hcl2spec.go b/builder/vsphere/iso/config.hcl2spec.go index d741e285b..2798a2562 100644 --- a/builder/vsphere/iso/config.hcl2spec.go +++ b/builder/vsphere/iso/config.hcl2spec.go @@ -54,6 +54,8 @@ type FlatConfig struct { VGPUProfile *string `mapstructure:"vgpu_profile" cty:"vgpu_profile" hcl:"vgpu_profile"` NestedHV *bool `mapstructure:"NestedHV" cty:"NestedHV" hcl:"NestedHV"` ConfigParams map[string]string `mapstructure:"configuration_parameters" cty:"configuration_parameters" hcl:"configuration_parameters"` + ToolsSyncTime *bool `mapstructure:"tools_sync_time" cty:"tools_sync_time" hcl:"tools_sync_time"` + ToolsUpgradePolicy *bool `mapstructure:"tools_upgrade_policy" cty:"tools_upgrade_policy" hcl:"tools_upgrade_policy"` ISOChecksum *string `mapstructure:"iso_checksum" required:"true" cty:"iso_checksum" hcl:"iso_checksum"` RawSingleISOUrl *string `mapstructure:"iso_url" required:"true" cty:"iso_url" hcl:"iso_url"` ISOUrls []string `mapstructure:"iso_urls" cty:"iso_urls" hcl:"iso_urls"` @@ -178,6 +180,8 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "vgpu_profile": &hcldec.AttrSpec{Name: "vgpu_profile", Type: cty.String, Required: false}, "NestedHV": &hcldec.AttrSpec{Name: "NestedHV", Type: cty.Bool, Required: false}, "configuration_parameters": &hcldec.AttrSpec{Name: "configuration_parameters", Type: cty.Map(cty.String), Required: false}, + "tools_sync_time": &hcldec.AttrSpec{Name: "tools_sync_time", Type: cty.Bool, Required: false}, + "tools_upgrade_policy": &hcldec.AttrSpec{Name: "tools_upgrade_policy", Type: cty.Bool, Required: false}, "iso_checksum": &hcldec.AttrSpec{Name: "iso_checksum", Type: cty.String, Required: false}, "iso_url": &hcldec.AttrSpec{Name: "iso_url", Type: cty.String, Required: false}, "iso_urls": &hcldec.AttrSpec{Name: "iso_urls", Type: cty.List(cty.String), Required: false}, diff --git a/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx b/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx index 5386e2cff..8b69dcce0 100644 --- a/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx @@ -2,4 +2,10 @@ - `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 fo 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. \ No newline at end of file