github.com/rothwerx/packer@v0.9.0/builder/virtualbox/ovf/config.go (about)

     1  package ovf
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
     9  	"github.com/mitchellh/packer/common"
    10  	"github.com/mitchellh/packer/helper/config"
    11  	"github.com/mitchellh/packer/packer"
    12  	"github.com/mitchellh/packer/template/interpolate"
    13  )
    14  
    15  // Config is the configuration structure for the builder.
    16  type Config struct {
    17  	common.PackerConfig             `mapstructure:",squash"`
    18  	common.HTTPConfig               `mapstructure:",squash"`
    19  	vboxcommon.ExportConfig         `mapstructure:",squash"`
    20  	vboxcommon.ExportOpts           `mapstructure:",squash"`
    21  	vboxcommon.FloppyConfig         `mapstructure:",squash"`
    22  	vboxcommon.OutputConfig         `mapstructure:",squash"`
    23  	vboxcommon.RunConfig            `mapstructure:",squash"`
    24  	vboxcommon.SSHConfig            `mapstructure:",squash"`
    25  	vboxcommon.ShutdownConfig       `mapstructure:",squash"`
    26  	vboxcommon.VBoxManageConfig     `mapstructure:",squash"`
    27  	vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
    28  	vboxcommon.VBoxVersionConfig    `mapstructure:",squash"`
    29  
    30  	BootCommand          []string `mapstructure:"boot_command"`
    31  	SourcePath           string   `mapstructure:"source_path"`
    32  	GuestAdditionsMode   string   `mapstructure:"guest_additions_mode"`
    33  	GuestAdditionsPath   string   `mapstructure:"guest_additions_path"`
    34  	GuestAdditionsURL    string   `mapstructure:"guest_additions_url"`
    35  	GuestAdditionsSHA256 string   `mapstructure:"guest_additions_sha256"`
    36  	VMName               string   `mapstructure:"vm_name"`
    37  	ImportOpts           string   `mapstructure:"import_opts"`
    38  	ImportFlags          []string `mapstructure:"import_flags"`
    39  
    40  	ctx interpolate.Context
    41  }
    42  
    43  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    44  	c := new(Config)
    45  	err := config.Decode(c, &config.DecodeOpts{
    46  		Interpolate:        true,
    47  		InterpolateContext: &c.ctx,
    48  		InterpolateFilter: &interpolate.RenderFilter{
    49  			Exclude: []string{
    50  				"boot_command",
    51  				"guest_additions_path",
    52  				"guest_additions_url",
    53  				"vboxmanage",
    54  				"vboxmanage_post",
    55  			},
    56  		},
    57  	}, raws...)
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  
    62  	// Defaults
    63  	if c.GuestAdditionsMode == "" {
    64  		c.GuestAdditionsMode = "upload"
    65  	}
    66  
    67  	if c.GuestAdditionsPath == "" {
    68  		c.GuestAdditionsPath = "VBoxGuestAdditions.iso"
    69  	}
    70  
    71  	if c.VMName == "" {
    72  		c.VMName = fmt.Sprintf(
    73  			"packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix())
    74  	}
    75  
    76  	// Prepare the errors
    77  	var errs *packer.MultiError
    78  	errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...)
    79  	errs = packer.MultiErrorAppend(errs, c.ExportOpts.Prepare(&c.ctx)...)
    80  	errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
    81  	errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
    82  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...)
    83  	errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...)
    84  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
    85  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...)
    86  	errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(&c.ctx)...)
    87  	errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(&c.ctx)...)
    88  	errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(&c.ctx)...)
    89  
    90  	if c.SourcePath == "" {
    91  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
    92  	} else {
    93  		if _, err := os.Stat(c.SourcePath); err != nil {
    94  			errs = packer.MultiErrorAppend(errs,
    95  				fmt.Errorf("source_path is invalid: %s", err))
    96  		}
    97  	}
    98  
    99  	validMode := false
   100  	validModes := []string{
   101  		vboxcommon.GuestAdditionsModeDisable,
   102  		vboxcommon.GuestAdditionsModeAttach,
   103  		vboxcommon.GuestAdditionsModeUpload,
   104  	}
   105  
   106  	for _, mode := range validModes {
   107  		if c.GuestAdditionsMode == mode {
   108  			validMode = true
   109  			break
   110  		}
   111  	}
   112  
   113  	if !validMode {
   114  		errs = packer.MultiErrorAppend(errs,
   115  			fmt.Errorf("guest_additions_mode is invalid. Must be one of: %v", validModes))
   116  	}
   117  
   118  	if c.GuestAdditionsSHA256 != "" {
   119  		c.GuestAdditionsSHA256 = strings.ToLower(c.GuestAdditionsSHA256)
   120  	}
   121  
   122  	// Warnings
   123  	var warnings []string
   124  	if c.ShutdownCommand == "" {
   125  		warnings = append(warnings,
   126  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
   127  				"will forcibly halt the virtual machine, which may result in data loss.")
   128  	}
   129  
   130  	// Check for any errors.
   131  	if errs != nil && len(errs.Errors) > 0 {
   132  		return nil, warnings, errs
   133  	}
   134  
   135  	// TODO: Write a packer fix and just remove import_opts
   136  	if c.ImportOpts != "" {
   137  		c.ImportFlags = append(c.ImportFlags, "--options", c.ImportOpts)
   138  	}
   139  
   140  	return c, warnings, nil
   141  }