github.com/jbronn/packer@v0.1.6-0.20140120165540-8a1364dbd817/builder/virtualbox/ovf/config.go (about)

     1  package ovf
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
     8  	"github.com/mitchellh/packer/common"
     9  	"github.com/mitchellh/packer/packer"
    10  )
    11  
    12  // Config is the configuration structure for the builder.
    13  type Config struct {
    14  	common.PackerConfig          `mapstructure:",squash"`
    15  	vboxcommon.ExportConfig      `mapstructure:",squash"`
    16  	vboxcommon.FloppyConfig      `mapstructure:",squash"`
    17  	vboxcommon.OutputConfig      `mapstructure:",squash"`
    18  	vboxcommon.RunConfig         `mapstructure:",squash"`
    19  	vboxcommon.SSHConfig         `mapstructure:",squash"`
    20  	vboxcommon.ShutdownConfig    `mapstructure:",squash"`
    21  	vboxcommon.VBoxManageConfig  `mapstructure:",squash"`
    22  	vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
    23  
    24  	SourcePath string `mapstructure:"source_path"`
    25  	VMName     string `mapstructure:"vm_name"`
    26  
    27  	tpl *packer.ConfigTemplate
    28  }
    29  
    30  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    31  	c := new(Config)
    32  	md, err := common.DecodeConfig(c, raws...)
    33  	if err != nil {
    34  		return nil, nil, err
    35  	}
    36  
    37  	c.tpl, err = packer.NewConfigTemplate()
    38  	if err != nil {
    39  		return nil, nil, err
    40  	}
    41  	c.tpl.UserVars = c.PackerUserVars
    42  
    43  	// Defaults
    44  	if c.VMName == "" {
    45  		c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
    46  	}
    47  
    48  	// Prepare the errors
    49  	errs := common.CheckUnusedConfig(md)
    50  	errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(c.tpl)...)
    51  	errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(c.tpl)...)
    52  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
    53  	errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
    54  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
    55  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
    56  	errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(c.tpl)...)
    57  	errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(c.tpl)...)
    58  
    59  	templates := map[string]*string{
    60  		"source_path": &c.SourcePath,
    61  		"vm_name":     &c.VMName,
    62  	}
    63  
    64  	for n, ptr := range templates {
    65  		var err error
    66  		*ptr, err = c.tpl.Process(*ptr, nil)
    67  		if err != nil {
    68  			errs = packer.MultiErrorAppend(
    69  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    70  		}
    71  	}
    72  
    73  	if c.SourcePath == "" {
    74  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
    75  	} else {
    76  		if _, err := os.Stat(c.SourcePath); err != nil {
    77  			errs = packer.MultiErrorAppend(errs,
    78  				fmt.Errorf("source_path is invalid: %s", err))
    79  		}
    80  	}
    81  
    82  	// Warnings
    83  	var warnings []string
    84  	if c.ShutdownCommand == "" {
    85  		warnings = append(warnings,
    86  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
    87  				"will forcibly halt the virtual machine, which may result in data loss.")
    88  	}
    89  
    90  	// Check for any errors.
    91  	if errs != nil && len(errs.Errors) > 0 {
    92  		return nil, warnings, errs
    93  	}
    94  
    95  	return c, warnings, nil
    96  }