Skip to content

Commit 28708b5

Browse files
authored
Merge pull request #581 from hashicorp/refactor/time-sync-setting
refactor: time synchronization settings
2 parents 42c302d + 14d4e05 commit 28708b5

File tree

9 files changed

+101
-18
lines changed

9 files changed

+101
-18
lines changed

.web-docs/components/builder/vsphere-clone/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,22 @@ JSON Example:
319319
are silently ignored. Refer to the [`VirtualMachineConfigSpec`](https://dp-downloads.broadcom.com/api-content/apis/API_VWSA_001/8.0U3/html/ReferenceGuides/vim.vm.ConfigSpec.html)
320320
in the vSphere API documentation.
321321

322-
- `tools_sync_time` (bool) - Enable time synchronization with the ESX host where the virtual machine
323-
is running. Defaults to `false`.
322+
- `tools_sync_time` (\*bool) - Enable or disable time synchronization between the guest operating system and the
323+
ESX host at startup and after VM operations that may introduce time drift (such
324+
as resume from suspend, vMotion, or snapshot restore). If set to `true`, time
325+
synchronization is explicitly enabled. If set to `false`, time synchronization is
326+
explicitly disabled. If omitted, the builder does not modify the virtual
327+
machine's time synchronization settings:
328+
- `vsphere-iso` builder uses the vSphere default for new virtual machines
329+
(`true`).
330+
- `vsphere-clone` builder inherits the setting from the source virtual machine.
331+
332+
- `tools_sync_time_periodically` (\*bool) - Enable or disable periodic time synchronization between the guest operating
333+
system and the ESX host. Use this setting only if the guest operating system does
334+
not have native time synchronization.
335+
- `vsphere-iso` builder uses the vSphere default for new virtual machines
336+
(`false`).
337+
- `vsphere-clone` builder inherits the setting from the source virtual machine.
324338

325339
- `tools_upgrade_policy` (bool) - Automatically check for and upgrade VMware Tools after a virtual machine
326340
power cycle. Defaults to `false`.

.web-docs/components/builder/vsphere-iso/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,8 +1641,22 @@ JSON Example:
16411641
are silently ignored. Refer to the [`VirtualMachineConfigSpec`](https://dp-downloads.broadcom.com/api-content/apis/API_VWSA_001/8.0U3/html/ReferenceGuides/vim.vm.ConfigSpec.html)
16421642
in the vSphere API documentation.
16431643

1644-
- `tools_sync_time` (bool) - Enable time synchronization with the ESX host where the virtual machine
1645-
is running. Defaults to `false`.
1644+
- `tools_sync_time` (\*bool) - Enable or disable time synchronization between the guest operating system and the
1645+
ESX host at startup and after VM operations that may introduce time drift (such
1646+
as resume from suspend, vMotion, or snapshot restore). If set to `true`, time
1647+
synchronization is explicitly enabled. If set to `false`, time synchronization is
1648+
explicitly disabled. If omitted, the builder does not modify the virtual
1649+
machine's time synchronization settings:
1650+
- `vsphere-iso` builder uses the vSphere default for new virtual machines
1651+
(`true`).
1652+
- `vsphere-clone` builder inherits the setting from the source virtual machine.
1653+
1654+
- `tools_sync_time_periodically` (\*bool) - Enable or disable periodic time synchronization between the guest operating
1655+
system and the ESX host. Use this setting only if the guest operating system does
1656+
not have native time synchronization.
1657+
- `vsphere-iso` builder uses the vSphere default for new virtual machines
1658+
(`false`).
1659+
- `vsphere-clone` builder inherits the setting from the source virtual machine.
16461660

16471661
- `tools_upgrade_policy` (bool) - Automatically check for and upgrade VMware Tools after a virtual machine
16481662
power cycle. Defaults to `false`.

builder/vsphere/clone/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
8888
errs = packersdk.MultiErrorAppend(errs, c.CloneConfig.Prepare()...)
8989
errs = packersdk.MultiErrorAppend(errs, c.LocationConfig.Prepare()...)
9090
errs = packersdk.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
91+
errs = packersdk.MultiErrorAppend(errs, c.ConfigParamsConfig.Prepare()...)
9192
errs = packersdk.MultiErrorAppend(errs, c.FlagConfig.Prepare(&c.HardwareConfig)...)
9293
errs = packersdk.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
9394
errs = packersdk.MultiErrorAppend(errs, c.CDRomConfig.Prepare(&c.ReattachCDRomConfig)...)

builder/vsphere/clone/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/vsphere/common/step_config_params.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,23 @@ type ConfigParamsConfig struct {
4747
// are silently ignored. Refer to the [`VirtualMachineConfigSpec`](https://dp-downloads.broadcom.com/api-content/apis/API_VWSA_001/8.0U3/html/ReferenceGuides/vim.vm.ConfigSpec.html)
4848
// in the vSphere API documentation.
4949
ConfigParams map[string]string `mapstructure:"configuration_parameters"`
50-
// Enable time synchronization with the ESX host where the virtual machine
51-
// is running. Defaults to `false`.
52-
ToolsSyncTime bool `mapstructure:"tools_sync_time"`
50+
// Enable or disable time synchronization between the guest operating system and the
51+
// ESX host at startup and after VM operations that may introduce time drift (such
52+
// as resume from suspend, vMotion, or snapshot restore). If set to `true`, time
53+
// synchronization is explicitly enabled. If set to `false`, time synchronization is
54+
// explicitly disabled. If omitted, the builder does not modify the virtual
55+
// machine's time synchronization settings:
56+
// - `vsphere-iso` builder uses the vSphere default for new virtual machines
57+
// (`true`).
58+
// - `vsphere-clone` builder inherits the setting from the source virtual machine.
59+
ToolsSyncTime *bool `mapstructure:"tools_sync_time"`
60+
// Enable or disable periodic time synchronization between the guest operating
61+
// system and the ESX host. Use this setting only if the guest operating system does
62+
// not have native time synchronization.
63+
// - `vsphere-iso` builder uses the vSphere default for new virtual machines
64+
// (`false`).
65+
// - `vsphere-clone` builder inherits the setting from the source virtual machine.
66+
ToolsSyncTimePeriodically *bool `mapstructure:"tools_sync_time_periodically"`
5367
// Automatically check for and upgrade VMware Tools after a virtual machine
5468
// power cycle. Defaults to `false`.
5569
ToolsUpgradePolicy bool `mapstructure:"tools_upgrade_policy"`
@@ -59,6 +73,18 @@ type StepConfigParams struct {
5973
Config *ConfigParamsConfig
6074
}
6175

76+
func (c *ConfigParamsConfig) Prepare() []error {
77+
var errs []error
78+
79+
if c.ToolsSyncTimePeriodically != nil && *c.ToolsSyncTimePeriodically {
80+
if c.ToolsSyncTime == nil || !*c.ToolsSyncTime {
81+
errs = append(errs, fmt.Errorf("'tools_sync_time_periodically' requires 'tools_sync_time' to be set to 'true'"))
82+
}
83+
}
84+
85+
return errs
86+
}
87+
6288
func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
6389
ui := state.Get("ui").(packersdk.Ui)
6490
vm := state.Get("vm").(*driver.VirtualMachineDriver)
@@ -69,11 +95,18 @@ func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) mult
6995
}
7096

7197
var info *types.ToolsConfigInfo
72-
if s.Config.ToolsSyncTime || s.Config.ToolsUpgradePolicy {
98+
99+
if s.Config.ToolsSyncTime != nil || s.Config.ToolsSyncTimePeriodically != nil || s.Config.ToolsUpgradePolicy {
73100
info = &types.ToolsConfigInfo{}
74101

75-
if s.Config.ToolsSyncTime {
76-
info.SyncTimeWithHost = &s.Config.ToolsSyncTime
102+
// Gate: Whether time synchronization is allowed.
103+
if s.Config.ToolsSyncTime != nil {
104+
info.SyncTimeWithHostAllowed = s.Config.ToolsSyncTime
105+
}
106+
107+
// Optional: Whether periodic time synchronization is allowed.
108+
if s.Config.ToolsSyncTimePeriodically != nil {
109+
info.SyncTimeWithHost = s.Config.ToolsSyncTimePeriodically
77110
}
78111

79112
if s.Config.ToolsUpgradePolicy {

builder/vsphere/common/step_config_params.hcl2spec.go

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/vsphere/iso/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
110110
errs = packersdk.MultiErrorAppend(errs, c.CreateConfig.Prepare()...)
111111
errs = packersdk.MultiErrorAppend(errs, c.LocationConfig.Prepare()...)
112112
errs = packersdk.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
113+
errs = packersdk.MultiErrorAppend(errs, c.ConfigParamsConfig.Prepare()...)
113114
errs = packersdk.MultiErrorAppend(errs, c.FlagConfig.Prepare(&c.HardwareConfig)...)
114115
errs = packersdk.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
115116
errs = packersdk.MultiErrorAppend(errs, c.CDRomConfig.Prepare(&c.ReattachCDRomConfig)...)

builder/vsphere/iso/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,22 @@
2828
are silently ignored. Refer to the [`VirtualMachineConfigSpec`](https://dp-downloads.broadcom.com/api-content/apis/API_VWSA_001/8.0U3/html/ReferenceGuides/vim.vm.ConfigSpec.html)
2929
in the vSphere API documentation.
3030

31-
- `tools_sync_time` (bool) - Enable time synchronization with the ESX host where the virtual machine
32-
is running. Defaults to `false`.
31+
- `tools_sync_time` (\*bool) - Enable or disable time synchronization between the guest operating system and the
32+
ESX host at startup and after VM operations that may introduce time drift (such
33+
as resume from suspend, vMotion, or snapshot restore). If set to `true`, time
34+
synchronization is explicitly enabled. If set to `false`, time synchronization is
35+
explicitly disabled. If omitted, the builder does not modify the virtual
36+
machine's time synchronization settings:
37+
- `vsphere-iso` builder uses the vSphere default for new virtual machines
38+
(`true`).
39+
- `vsphere-clone` builder inherits the setting from the source virtual machine.
40+
41+
- `tools_sync_time_periodically` (\*bool) - Enable or disable periodic time synchronization between the guest operating
42+
system and the ESX host. Use this setting only if the guest operating system does
43+
not have native time synchronization.
44+
- `vsphere-iso` builder uses the vSphere default for new virtual machines
45+
(`false`).
46+
- `vsphere-clone` builder inherits the setting from the source virtual machine.
3347

3448
- `tools_upgrade_policy` (bool) - Automatically check for and upgrade VMware Tools after a virtual machine
3549
power cycle. Defaults to `false`.

0 commit comments

Comments
 (0)