github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/virtualbox/ovf/config.go (about)

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