2013-12-21 20:38:06 -05:00
|
|
|
package ovf
|
|
|
|
|
|
|
|
import (
|
2013-12-22 18:24:24 -05:00
|
|
|
"fmt"
|
2013-12-22 20:02:22 -05:00
|
|
|
"os"
|
2014-05-04 11:56:57 -04:00
|
|
|
"strings"
|
2013-12-22 20:02:22 -05:00
|
|
|
|
2013-12-21 20:38:06 -05:00
|
|
|
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
|
|
|
"github.com/mitchellh/packer/common"
|
2015-05-27 17:03:56 -04:00
|
|
|
"github.com/mitchellh/packer/helper/config"
|
2013-12-21 20:38:06 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-05-27 17:03:56 -04:00
|
|
|
"github.com/mitchellh/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"`
|
|
|
|
vboxcommon.ExportConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.ExportOpts `mapstructure:",squash"`
|
|
|
|
vboxcommon.FloppyConfig `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
|
|
|
|
2014-05-12 22:02:30 -04:00
|
|
|
BootCommand []string `mapstructure:"boot_command"`
|
2014-09-05 13:23:37 -04:00
|
|
|
SourcePath string `mapstructure:"source_path"`
|
|
|
|
GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
|
|
|
|
GuestAdditionsPath string `mapstructure:"guest_additions_path"`
|
|
|
|
GuestAdditionsURL string `mapstructure:"guest_additions_url"`
|
|
|
|
GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
|
|
|
ImportOpts string `mapstructure:"import_opts"`
|
|
|
|
ImportFlags []string `mapstructure:"import_flags"`
|
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-05-27 17:03:56 -04:00
|
|
|
var c Config
|
|
|
|
err := config.Decode(&c, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
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"
|
|
|
|
}
|
2013-12-22 18:24:24 -05:00
|
|
|
if c.VMName == "" {
|
|
|
|
c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
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)...)
|
|
|
|
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)...)
|
2014-09-05 13:23:37 -04:00
|
|
|
|
2013-12-22 20:02:22 -05:00
|
|
|
if c.SourcePath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
|
|
|
|
} else {
|
|
|
|
if _, err := os.Stat(c.SourcePath); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("source_path is invalid: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-27 17:03:56 -04:00
|
|
|
return &c, warnings, nil
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|