2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2013-12-25 17:52:40 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-12-26 10:34:27 -05:00
|
|
|
"os"
|
2013-12-25 17:52:40 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
2020-09-17 04:38:33 -04:00
|
|
|
"github.com/hashicorp/packer/common/bootcommand"
|
2019-06-13 11:23:21 -04:00
|
|
|
"github.com/hashicorp/packer/common/shutdowncommand"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-12-25 17:52:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure for the builder.
|
|
|
|
type Config struct {
|
2019-06-13 11:23:21 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
common.HTTPConfig `mapstructure:",squash"`
|
|
|
|
common.FloppyConfig `mapstructure:",squash"`
|
2020-09-17 04:38:33 -04:00
|
|
|
bootcommand.VNCConfig `mapstructure:",squash"`
|
2020-09-10 18:38:28 -04:00
|
|
|
common.CDConfig `mapstructure:",squash"`
|
2019-06-13 11:23:21 -04:00
|
|
|
vmwcommon.DriverConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.RunConfig `mapstructure:",squash"`
|
|
|
|
shutdowncommand.ShutdownConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.SSHConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.ToolsConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.VMXConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.ExportConfig `mapstructure:",squash"`
|
2020-08-26 04:13:11 -04:00
|
|
|
vmwcommon.DiskConfig `mapstructure:",squash"`
|
2019-08-26 10:56:57 -04:00
|
|
|
// By default Packer creates a 'full' clone of the virtual machine
|
|
|
|
// specified in source_path. The resultant virtual machine is fully
|
|
|
|
// independant from the parent it was cloned from.
|
|
|
|
//
|
|
|
|
// Setting linked to true instead causes Packer to create the virtual
|
|
|
|
// machine as a 'linked' clone. Linked clones use and require ongoing
|
|
|
|
// access to the disks of the parent virtual machine. The benefit of a
|
|
|
|
// linked clone is that the clones virtual disk is typically very much
|
|
|
|
// smaller than would be the case for a full clone. Additionally, the
|
|
|
|
// cloned virtual machine can also be created much faster. Creating a
|
|
|
|
// linked clone will typically only be of benefit in some advanced build
|
|
|
|
// scenarios. Most users will wish to create a full clone instead. Defaults
|
|
|
|
// to false.
|
2019-06-06 10:29:25 -04:00
|
|
|
Linked bool `mapstructure:"linked" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Path to the source VMX file to clone. If
|
2019-06-06 10:29:25 -04:00
|
|
|
// remote_type is enabled then this specifies a path on the remote_host.
|
2019-05-28 11:50:58 -04:00
|
|
|
SourcePath string `mapstructure:"source_path" required:"true"`
|
|
|
|
// This is the name of the VMX file for the new virtual
|
2019-06-06 10:29:25 -04:00
|
|
|
// machine, without the file extension. By default this is packer-BUILDNAME,
|
|
|
|
// where "BUILDNAME" is the name of the build.
|
|
|
|
VMName string `mapstructure:"vm_name" required:"false"`
|
2017-02-01 06:32:02 -05:00
|
|
|
|
2015-05-27 17:21:15 -04:00
|
|
|
ctx interpolate.Context
|
2013-12-25 17:52:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
2015-05-27 17:21:15 -04:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-05-27 17:21:15 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
"tools_upload_path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-12-25 17:52:40 -05:00
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2013-12-25 17:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
2013-12-26 10:34:27 -05:00
|
|
|
if c.VMName == "" {
|
2017-08-23 07:04:42 -04:00
|
|
|
c.VMName = fmt.Sprintf(
|
|
|
|
"packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix())
|
2013-12-26 10:34:27 -05:00
|
|
|
}
|
2013-12-25 17:52:40 -05:00
|
|
|
|
2020-09-11 10:46:33 -04:00
|
|
|
// Accumulate any errors and warnings
|
|
|
|
var warnings []string
|
2015-05-27 17:21:15 -04:00
|
|
|
var errs *packer.MultiError
|
2020-09-11 10:46:33 -04:00
|
|
|
|
2020-09-17 04:38:33 -04:00
|
|
|
runConfigWarnings, runConfigErrs := c.RunConfig.Prepare(&c.ctx, &c.DriverConfig)
|
2020-09-16 05:02:46 -04:00
|
|
|
warnings = append(warnings, runConfigWarnings...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, runConfigErrs...)
|
2015-05-27 17:21:15 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(&c.ctx)...)
|
2015-11-01 17:29:24 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
|
2015-05-27 17:21:15 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...)
|
2016-07-26 15:42:04 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
|
2020-09-10 18:38:28 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.CDConfig.Prepare(&c.ctx)...)
|
2018-04-18 17:10:28 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VNCConfig.Prepare(&c.ctx)...)
|
2020-09-17 04:38:33 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VNCConfig.Prepare(&c.ctx)...)
|
2017-03-07 02:03:04 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...)
|
2020-08-26 04:13:11 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.DiskConfig.Prepare(&c.ctx)...)
|
2014-05-12 22:02:30 -04:00
|
|
|
|
2018-11-01 19:37:54 -04:00
|
|
|
if c.RemoteType == "" {
|
2017-02-06 02:13:37 -05:00
|
|
|
if c.SourcePath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but is required"))
|
|
|
|
} else {
|
|
|
|
if _, err := os.Stat(c.SourcePath); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("source_path is invalid: %s", err))
|
|
|
|
}
|
2013-12-26 10:34:27 -05:00
|
|
|
}
|
2020-09-11 10:46:33 -04:00
|
|
|
if c.Headless && c.DisableVNC {
|
|
|
|
warnings = append(warnings,
|
|
|
|
"Headless mode uses VNC to retrieve output. Since VNC has been disabled,\n"+
|
|
|
|
"you won't be able to see any output.")
|
2018-11-01 19:37:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 04:13:11 -04:00
|
|
|
if c.DiskTypeId == "" {
|
|
|
|
// Default is growable virtual disk split in 2GB files.
|
|
|
|
c.DiskTypeId = "1"
|
|
|
|
|
|
|
|
if c.RemoteType == "esx5" {
|
|
|
|
c.DiskTypeId = "zeroedthick"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:18:38 -04:00
|
|
|
if c.Format == "" {
|
2020-08-26 16:23:19 -04:00
|
|
|
if c.RemoteType == "" {
|
2020-08-25 15:18:38 -04:00
|
|
|
c.Format = "vmx"
|
|
|
|
} else {
|
|
|
|
c.Format = "ovf"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 16:23:19 -04:00
|
|
|
if c.RemoteType == "" && c.Format == "vmx" {
|
2020-08-25 15:18:38 -04:00
|
|
|
// if we're building locally and want a vmx, there's nothing to export.
|
|
|
|
// Set skip export flag here to keep the export step from attempting
|
|
|
|
// an unneded export
|
|
|
|
c.SkipExport = true
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:42:03 -05:00
|
|
|
err = c.DriverConfig.Validate(c.SkipExport)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:31:23 -05:00
|
|
|
if c.ShutdownCommand == "" {
|
|
|
|
warnings = append(warnings,
|
|
|
|
"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
|
|
|
|
"will forcibly halt the virtual machine, which may result in data loss.")
|
|
|
|
}
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return warnings, errs
|
2013-12-25 17:52:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return warnings, nil
|
2013-12-25 17:52:40 -05:00
|
|
|
}
|