generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathstep_config_params.go
More file actions
132 lines (110 loc) · 4.6 KB
/
step_config_params.go
File metadata and controls
132 lines (110 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright IBM Corp. 2013, 2025
// SPDX-License-Identifier: MPL-2.0
//go:generate packer-sdc struct-markdown
//go:generate packer-sdc mapstructure-to-hcl2 -type ConfigParamsConfig
package common
import (
"context"
"fmt"
"log"
"github.com/vmware/govmomi/vim25/types"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/driver"
)
type ConfigParamsConfig struct {
// A map of key-value pairs to sent to the [`extraConfig`](https://dp-downloads.broadcom.com/api-content/apis/API_VWSA_001/8.0U3/html/ReferenceGuides/vim.vm.ConfigSpec.html#extraConfig).
// in the vSphere API's `VirtualMachineConfigSpec`.
//
// HCL Example:
//
// ```hcl
// configuration_parameters = {
// "disk.EnableUUID" = "TRUE"
// "svga.autodetect" = "TRUE"
// "log.keepOld" = "15"
// }
// ```
//
// JSON Example:
//
// ```json
// "configuration_parameters": {
// "disk.EnableUUID": "TRUE",
// "svga.autodetect": "TRUE",
// "log.keepOld": "15"
// }
// ```
//
// ~> **Note:** Configuration keys that would conflict with parameters that
// are explicitly configurable through other fields in the `ConfigSpec`` object
// 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)
// in the vSphere API documentation.
ConfigParams map[string]string `mapstructure:"configuration_parameters"`
// Enable or disable time synchronization between the guest operating system and the
// ESX host at startup and after VM operations that may introduce time drift (such
// as resume from suspend, vMotion, or snapshot restore). If set to `true`, time
// synchronization is explicitly enabled. If set to `false`, time synchronization is
// explicitly disabled. If omitted, the builder does not modify the virtual
// machine's time synchronization settings:
// - `vsphere-iso` builder uses the vSphere default for new virtual machines
// (`true`).
// - `vsphere-clone` builder inherits the setting from the source virtual machine.
ToolsSyncTime *bool `mapstructure:"tools_sync_time"`
// Enable or disable periodic time synchronization between the guest operating
// system and the ESX host. Use this setting only if the guest operating system does
// not have native time synchronization.
// - `vsphere-iso` builder uses the vSphere default for new virtual machines
// (`false`).
// - `vsphere-clone` builder inherits the setting from the source virtual machine.
ToolsSyncTimePeriodically *bool `mapstructure:"tools_sync_time_periodically"`
// Automatically check for and upgrade VMware Tools after a virtual machine
// power cycle. Defaults to `false`.
ToolsUpgradePolicy bool `mapstructure:"tools_upgrade_policy"`
}
type StepConfigParams struct {
Config *ConfigParamsConfig
}
func (c *ConfigParamsConfig) Prepare() []error {
var errs []error
if c.ToolsSyncTimePeriodically != nil && *c.ToolsSyncTimePeriodically {
if c.ToolsSyncTime == nil || !*c.ToolsSyncTime {
errs = append(errs, fmt.Errorf("'tools_sync_time_periodically' requires 'tools_sync_time' to be set to 'true'"))
}
}
return errs
}
func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
vm := state.Get("vm").(*driver.VirtualMachineDriver)
configParams := make(map[string]string)
if s.Config.ConfigParams != nil {
configParams = s.Config.ConfigParams
}
var info *types.ToolsConfigInfo
if s.Config.ToolsSyncTime != nil || s.Config.ToolsSyncTimePeriodically != nil || s.Config.ToolsUpgradePolicy {
info = &types.ToolsConfigInfo{}
// Gate: Whether time synchronization is allowed.
if s.Config.ToolsSyncTime != nil {
info.SyncTimeWithHostAllowed = s.Config.ToolsSyncTime
}
// Optional: Whether periodic time synchronization is allowed.
if s.Config.ToolsSyncTimePeriodically != nil {
info.SyncTimeWithHost = s.Config.ToolsSyncTimePeriodically
}
if s.Config.ToolsUpgradePolicy {
info.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
}
}
ui.Say("Adding configuration parameters...")
// Iterate over the map and log each key-value pair.
for key, value := range configParams {
log.Printf("[INFO] Adding: %s = %v", key, value)
}
if err := vm.AddConfigParams(configParams, info); 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) {}