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"
|
2018-04-18 17:53:59 -04:00
|
|
|
"github.com/hashicorp/packer/common/bootcommand"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-12-25 17:52:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure for the builder.
|
|
|
|
type Config struct {
|
2013-12-26 17:31:23 -05:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-11-01 17:29:24 -05:00
|
|
|
common.HTTPConfig `mapstructure:",squash"`
|
2016-07-26 15:42:04 -04:00
|
|
|
common.FloppyConfig `mapstructure:",squash"`
|
2018-04-18 17:10:28 -04:00
|
|
|
bootcommand.VNCConfig `mapstructure:",squash"`
|
2013-12-27 10:37:39 -05:00
|
|
|
vmwcommon.DriverConfig `mapstructure:",squash"`
|
2013-12-26 17:31:23 -05:00
|
|
|
vmwcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.RunConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.ShutdownConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.SSHConfig `mapstructure:",squash"`
|
2014-05-09 20:24:19 -04:00
|
|
|
vmwcommon.ToolsConfig `mapstructure:",squash"`
|
2013-12-26 17:31:23 -05:00
|
|
|
vmwcommon.VMXConfig `mapstructure:",squash"`
|
2017-03-07 02:03:04 -05:00
|
|
|
vmwcommon.ExportConfig `mapstructure:",squash"`
|
2013-12-25 17:52:40 -05:00
|
|
|
|
2018-10-29 14:24:26 -04:00
|
|
|
Linked bool `mapstructure:"linked"`
|
|
|
|
RemoteType string `mapstructure:"remote_type"`
|
|
|
|
SourcePath string `mapstructure:"source_path"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := new(Config)
|
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 {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Prepare the errors
|
2015-05-27 17:21:15 -04:00
|
|
|
var errs *packer.MultiError
|
|
|
|
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.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)...)
|
2016-02-02 15:41:42 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...)
|
2016-07-26 15:42:04 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
|
2018-04-18 17:10:28 -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)...)
|
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
|
|
|
}
|
2018-11-01 19:37:54 -04: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"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:42:03 -05:00
|
|
|
err = c.DriverConfig.Validate(c.SkipExport)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
|
2018-12-05 18:58:44 -05:00
|
|
|
if c.Format != "" {
|
|
|
|
if c.RemoteType != "esx5" {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("format is only valid when RemoteType=esx5"))
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-01 19:37:54 -04:00
|
|
|
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 10:34:27 -05:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:52:40 -05:00
|
|
|
// Warnings
|
|
|
|
var warnings []string
|
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
|
|
|
|
2017-10-09 20:12: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.")
|
|
|
|
}
|
|
|
|
|
2013-12-25 17:52:40 -05:00
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, warnings, nil
|
|
|
|
}
|