2019-02-08 12:31:30 -05:00
|
|
|
package ovf
|
2013-12-21 20:38:06 -05:00
|
|
|
|
|
|
|
import (
|
2013-12-22 18:24:24 -05:00
|
|
|
"fmt"
|
2014-05-04 11:56:57 -04:00
|
|
|
"strings"
|
2013-12-22 20:02:22 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
2018-04-18 19:19:44 -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-21 20:38:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure for the builder.
|
|
|
|
type Config struct {
|
2014-03-12 19:14:44 -04: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 19:19:44 -04:00
|
|
|
bootcommand.BootConfig `mapstructure:",squash"`
|
2014-03-12 19:14:44 -04:00
|
|
|
vboxcommon.ExportConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.ExportOpts `mapstructure:",squash"`
|
|
|
|
vboxcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.RunConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.SSHConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.ShutdownConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxManageConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
|
2013-12-21 20:38:06 -05:00
|
|
|
|
2019-02-08 12:28:10 -05:00
|
|
|
Checksum string `mapstructure:"checksum"`
|
|
|
|
ChecksumType string `mapstructure:"checksum_type"`
|
|
|
|
GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
|
|
|
|
GuestAdditionsPath string `mapstructure:"guest_additions_path"`
|
|
|
|
GuestAdditionsInterface string `mapstructure:"guest_additions_interface"`
|
|
|
|
GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"`
|
|
|
|
GuestAdditionsURL string `mapstructure:"guest_additions_url"`
|
|
|
|
ImportFlags []string `mapstructure:"import_flags"`
|
|
|
|
ImportOpts string `mapstructure:"import_opts"`
|
|
|
|
SourcePath string `mapstructure:"source_path"`
|
|
|
|
TargetPath string `mapstructure:"target_path"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
|
|
|
KeepRegistered bool `mapstructure:"keep_registered"`
|
|
|
|
SkipExport bool `mapstructure:"skip_export"`
|
2013-12-22 18:19:19 -05:00
|
|
|
|
2015-05-27 17:03:56 -04:00
|
|
|
ctx interpolate.Context
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
2015-06-12 17:02:09 -04:00
|
|
|
c := new(Config)
|
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-05-27 17:03:56 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
"guest_additions_path",
|
|
|
|
"guest_additions_url",
|
|
|
|
"vboxmanage",
|
|
|
|
"vboxmanage_post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-12-21 20:38:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-22 18:24:24 -05:00
|
|
|
// Defaults
|
2014-05-04 11:56:57 -04:00
|
|
|
if c.GuestAdditionsMode == "" {
|
|
|
|
c.GuestAdditionsMode = "upload"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GuestAdditionsPath == "" {
|
|
|
|
c.GuestAdditionsPath = "VBoxGuestAdditions.iso"
|
|
|
|
}
|
2019-02-08 12:31:30 -05:00
|
|
|
if c.GuestAdditionsInterface == "" {
|
|
|
|
c.GuestAdditionsInterface = "ide"
|
2019-02-08 12:28:10 -05:00
|
|
|
}
|
2015-06-22 12:25:15 -04:00
|
|
|
|
2013-12-22 18:24:24 -05:00
|
|
|
if c.VMName == "" {
|
2015-06-22 12:25:15 -04:00
|
|
|
c.VMName = fmt.Sprintf(
|
|
|
|
"packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix())
|
2013-12-22 18:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-12-21 20:38:06 -05:00
|
|
|
// Prepare the errors
|
2015-05-27 17:03:56 -04:00
|
|
|
var errs *packer.MultiError
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.ExportOpts.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
|
2015-11-01 17:29:24 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
|
2015-05-27 17:03:56 -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.VBoxManageConfig.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(&c.ctx)...)
|
2018-04-18 19:19:44 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...)
|
2014-09-05 13:23:37 -04:00
|
|
|
|
2016-10-10 22:16:25 -04:00
|
|
|
c.ChecksumType = strings.ToLower(c.ChecksumType)
|
|
|
|
c.Checksum = strings.ToLower(c.Checksum)
|
|
|
|
|
2013-12-22 20:02:22 -05:00
|
|
|
if c.SourcePath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
|
|
|
|
} else {
|
2018-01-09 21:08:06 -05:00
|
|
|
c.SourcePath, err = common.ValidatedURL(c.SourcePath)
|
2016-10-10 22:16:25 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is invalid: %s", err))
|
2013-12-22 20:02:22 -05:00
|
|
|
}
|
2018-01-10 19:11:17 -05:00
|
|
|
fileOK := common.FileExistsLocally(c.SourcePath)
|
|
|
|
if !fileOK {
|
2018-01-03 19:53:47 -05:00
|
|
|
packer.MultiErrorAppend(errs,
|
2018-03-19 12:58:39 -04:00
|
|
|
fmt.Errorf("Source file '%s' needs to exist at time of config validation!", c.SourcePath))
|
2017-11-13 14:44:59 -05:00
|
|
|
}
|
2018-01-03 19:53:47 -05:00
|
|
|
|
2013-12-22 20:02:22 -05:00
|
|
|
}
|
|
|
|
|
2014-05-04 11:56:57 -04:00
|
|
|
validMode := false
|
|
|
|
validModes := []string{
|
|
|
|
vboxcommon.GuestAdditionsModeDisable,
|
|
|
|
vboxcommon.GuestAdditionsModeAttach,
|
|
|
|
vboxcommon.GuestAdditionsModeUpload,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mode := range validModes {
|
|
|
|
if c.GuestAdditionsMode == mode {
|
|
|
|
validMode = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validMode {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("guest_additions_mode is invalid. Must be one of: %v", validModes))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GuestAdditionsSHA256 != "" {
|
|
|
|
c.GuestAdditionsSHA256 = strings.ToLower(c.GuestAdditionsSHA256)
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:37:27 -05:00
|
|
|
// Warnings
|
|
|
|
var warnings []string
|
|
|
|
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-21 20:38:06 -05:00
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2013-12-22 12:37:27 -05:00
|
|
|
return nil, warnings, errs
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|
|
|
|
|
2014-09-05 13:23:37 -04:00
|
|
|
// TODO: Write a packer fix and just remove import_opts
|
|
|
|
if c.ImportOpts != "" {
|
|
|
|
c.ImportFlags = append(c.ImportFlags, "--options", c.ImportOpts)
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:02:09 -04:00
|
|
|
return c, warnings, nil
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|