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"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2013-12-21 20:38:06 -05:00
|
|
|
tpl *packer.ConfigTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := new(Config)
|
|
|
|
md, err := common.DecodeConfig(c, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.tpl, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
c.tpl.UserVars = c.PackerUserVars
|
|
|
|
|
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
|
|
|
|
errs := common.CheckUnusedConfig(md)
|
2013-12-22 13:40:39 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(c.tpl)...)
|
2014-03-12 09:12:20 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ExportOpts.Prepare(c.tpl)...)
|
2013-12-22 12:08:09 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(c.tpl)...)
|
2013-12-21 20:38:06 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
|
2013-12-22 13:30:12 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
|
2013-12-31 18:28:33 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
|
2013-12-22 12:08:09 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
|
2013-12-22 12:24:29 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(c.tpl)...)
|
2014-03-12 19:14:44 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(c.tpl)...)
|
2013-12-22 14:50:29 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(c.tpl)...)
|
2013-12-21 20:38:06 -05:00
|
|
|
|
2013-12-22 18:24:24 -05:00
|
|
|
templates := map[string]*string{
|
2014-05-04 11:56:57 -04:00
|
|
|
"guest_additions_mode": &c.GuestAdditionsMode,
|
|
|
|
"guest_additions_sha256": &c.GuestAdditionsSHA256,
|
|
|
|
"source_path": &c.SourcePath,
|
|
|
|
"vm_name": &c.VMName,
|
|
|
|
"import_opts": &c.ImportOpts,
|
2013-12-22 18:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = c.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 13:23:37 -04:00
|
|
|
sliceTemplates := map[string][]string{
|
|
|
|
"import_flags": c.ImportFlags,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, slice := range sliceTemplates {
|
|
|
|
for i, elem := range slice {
|
|
|
|
var err error
|
|
|
|
slice[i], err = c.tpl.Process(elem, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12 22:02:30 -04:00
|
|
|
for i, command := range c.BootCommand {
|
|
|
|
if err := c.tpl.Validate(command); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Error processing boot_command[%d]: %s", i, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 11:56:57 -04:00
|
|
|
validates := map[string]*string{
|
|
|
|
"guest_additions_path": &c.GuestAdditionsPath,
|
|
|
|
"guest_additions_url": &c.GuestAdditionsURL,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range validates {
|
|
|
|
if err := c.tpl.Validate(*ptr); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error parsing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:37:27 -05:00
|
|
|
return c, warnings, nil
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|