2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type ConfigParamsConfig
|
|
|
|
|
2018-05-04 18:48:16 -04:00
|
|
|
package common
|
2018-02-26 16:18:06 -05:00
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-07-01 12:53:56 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"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 {
|
2020-05-29 18:07:34 -04:00
|
|
|
// 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"`
|
2020-06-05 11:20:44 -04:00
|
|
|
|
2020-07-01 12:53:56 -04:00
|
|
|
// Enables time synchronization with the host. Defaults to false.
|
2020-06-05 11:20:44 -04:00
|
|
|
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)
|
2020-06-05 11:20:44 -04:00
|
|
|
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 {
|
2020-06-05 11:20:44 -04:00
|
|
|
configParams = s.Config.ConfigParams
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:53:56 -04:00
|
|
|
var info *types.ToolsConfigInfo
|
|
|
|
if s.Config.ToolsSyncTime || s.Config.ToolsUpgradePolicy {
|
|
|
|
info = &types.ToolsConfigInfo{}
|
2020-06-05 11:20:44 -04:00
|
|
|
|
2020-07-01 12:53:56 -04:00
|
|
|
if s.Config.ToolsSyncTime {
|
|
|
|
info.SyncTimeWithHost = &s.Config.ToolsSyncTime
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Config.ToolsUpgradePolicy {
|
|
|
|
info.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
|
|
|
|
}
|
2020-08-05 10:57:12 -04:00
|
|
|
}
|
2020-06-05 11:20:44 -04:00
|
|
|
|
2020-08-05 10:57:12 -04:00
|
|
|
ui.Say("Adding configuration parameters...")
|
|
|
|
if err := vm.AddConfigParams(configParams, info); err != nil {
|
|
|
|
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) {}
|