github.com/sneal/packer@v0.5.2/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  	ImportOpts string `mapstructure:"import_opts"`
    27  
    28  	tpl *packer.ConfigTemplate
    29  }
    30  
    31  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    32  	c := new(Config)
    33  	md, err := common.DecodeConfig(c, raws...)
    34  	if err != nil {
    35  		return nil, nil, err
    36  	}
    37  
    38  	c.tpl, err = packer.NewConfigTemplate()
    39  	if err != nil {
    40  		return nil, nil, err
    41  	}
    42  	c.tpl.UserVars = c.PackerUserVars
    43  
    44  	// Defaults
    45  	if c.VMName == "" {
    46  		c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
    47  	}
    48  
    49  	// Prepare the errors
    50  	errs := common.CheckUnusedConfig(md)
    51  	errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(c.tpl)...)
    52  	errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(c.tpl)...)
    53  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
    54  	errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
    55  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
    56  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
    57  	errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(c.tpl)...)
    58  	errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(c.tpl)...)
    59  
    60  	templates := map[string]*string{
    61  		"source_path": &c.SourcePath,
    62  		"vm_name":     &c.VMName,
    63  		"import_opts": &c.ImportOpts,
    64  	}
    65  
    66  	for n, ptr := range templates {
    67  		var err error
    68  		*ptr, err = c.tpl.Process(*ptr, nil)
    69  		if err != nil {
    70  			errs = packer.MultiErrorAppend(
    71  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    72  		}
    73  	}
    74  
    75  	if c.SourcePath == "" {
    76  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
    77  	} else {
    78  		if _, err := os.Stat(c.SourcePath); err != nil {
    79  			errs = packer.MultiErrorAppend(errs,
    80  				fmt.Errorf("source_path is invalid: %s", err))
    81  		}
    82  	}
    83  
    84  	// Warnings
    85  	var warnings []string
    86  	if c.ShutdownCommand == "" {
    87  		warnings = append(warnings,
    88  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
    89  				"will forcibly halt the virtual machine, which may result in data loss.")
    90  	}
    91  
    92  	// Check for any errors.
    93  	if errs != nil && len(errs.Errors) > 0 {
    94  		return nil, warnings, errs
    95  	}
    96  
    97  	return c, warnings, nil
    98  }