160 lines
5.7 KiB
Go
Raw Normal View History

//go:generate struct-markdown
2013-12-25 15:52:40 -07:00
package vmx
import (
"fmt"
2013-12-26 08:34:27 -07:00
"os"
2013-12-25 15:52:40 -07:00
2017-04-04 13:39:01 -07:00
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
"github.com/hashicorp/packer/common"
2018-04-18 14:53:59 -07:00
"github.com/hashicorp/packer/common/bootcommand"
"github.com/hashicorp/packer/common/shutdowncommand"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
2013-12-25 15:52:40 -07:00
)
// Config is the configuration structure for the builder.
type Config struct {
common.PackerConfig `mapstructure:",squash"`
common.HTTPConfig `mapstructure:",squash"`
common.FloppyConfig `mapstructure:",squash"`
bootcommand.VNCConfig `mapstructure:",squash"`
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"`
// 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 16:29:25 +02:00
Linked bool `mapstructure:"linked" required:"false"`
// The type of remote machine that will be used to
2019-06-06 16:29:25 +02:00
// build this VM rather than a local desktop product. The only value accepted
// for this currently is esx5. If this is not set, a desktop product will
// be used. By default, this is not set.
RemoteType string `mapstructure:"remote_type" required:"false"`
// Path to the source VMX file to clone. If
2019-06-06 16:29:25 +02:00
// remote_type is enabled then this specifies a path on the remote_host.
SourcePath string `mapstructure:"source_path" required:"true"`
// This is the name of the VMX file for the new virtual
2019-06-06 16:29:25 +02: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"`
2015-05-27 14:21:15 -07:00
ctx interpolate.Context
2013-12-25 15:52:40 -07:00
}
func NewConfig(raws ...interface{}) (*Config, []string, error) {
c := new(Config)
2015-05-27 14:21:15 -07:00
err := config.Decode(c, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &c.ctx,
2015-05-27 14:21:15 -07:00
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"boot_command",
"tools_upload_path",
},
},
}, raws...)
2013-12-25 15:52:40 -07:00
if err != nil {
return nil, nil, err
}
// Defaults
2013-12-26 08:34:27 -07:00
if c.VMName == "" {
c.VMName = fmt.Sprintf(
"packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix())
2013-12-26 08:34:27 -07:00
}
2013-12-25 15:52:40 -07:00
// Prepare the errors
2015-05-27 14:21:15 -07:00
var errs *packer.MultiError
errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
2015-05-27 14:21:15 -07:00
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...)
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)...)
errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...)
2016-07-26 20:42:04 +01:00
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.VNCConfig.Prepare(&c.ctx)...)
2017-03-07 09:03:04 +02:00
errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...)
if c.RemoteType == "" {
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 08:34:27 -07:00
}
} else {
// Remote configuration validation
if c.RemoteHost == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("remote_host must be specified"))
}
if c.RemoteType != "esx5" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Only 'esx5' value is accepted for remote_type"))
}
}
err = c.DriverConfig.Validate(c.SkipExport)
if err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
2018-12-05 15:58:44 -08:00
if c.Format != "" {
if c.RemoteType != "esx5" {
errs = packer.MultiErrorAppend(errs,
2019-05-10 18:49:42 -04:00
fmt.Errorf("format is only valid when remote_type=esx5"))
2018-12-05 15:58:44 -08:00
}
} else {
c.Format = "ovf"
}
if !(c.Format == "ova" || c.Format == "ovf" || c.Format == "vmx") {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("format must be one of ova, ovf, or vmx"))
2013-12-26 08:34:27 -07:00
}
2013-12-25 15:52:40 -07:00
// Warnings
var warnings []string
2013-12-26 15:31:23 -07: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 15:52:40 -07:00
2017-10-09 17:12:33 -07: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.")
}
2013-12-25 15:52:40 -07:00
// Check for any errors.
if errs != nil && len(errs.Errors) > 0 {
return nil, warnings, errs
}
return c, warnings, nil
}