github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/parallels/pvm/config.go (about)

     1  package pvm
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	parallelscommon "github.com/mitchellh/packer/builder/parallels/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  	parallelscommon.FloppyConfig        `mapstructure:",squash"`
    16  	parallelscommon.OutputConfig        `mapstructure:",squash"`
    17  	parallelscommon.PrlctlConfig        `mapstructure:",squash"`
    18  	parallelscommon.PrlctlVersionConfig `mapstructure:",squash"`
    19  	parallelscommon.RunConfig           `mapstructure:",squash"`
    20  	parallelscommon.SSHConfig           `mapstructure:",squash"`
    21  	parallelscommon.ShutdownConfig      `mapstructure:",squash"`
    22  	parallelscommon.ToolsConfig         `mapstructure:",squash"`
    23  
    24  	BootCommand []string `mapstructure:"boot_command"`
    25  	SourcePath  string   `mapstructure:"source_path"`
    26  	VMName      string   `mapstructure:"vm_name"`
    27  	ReassignMac bool     `mapstructure:"reassign_mac"`
    28  
    29  	tpl *packer.ConfigTemplate
    30  }
    31  
    32  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    33  	c := new(Config)
    34  	md, err := common.DecodeConfig(c, raws...)
    35  	if err != nil {
    36  		return nil, nil, err
    37  	}
    38  
    39  	c.tpl, err = packer.NewConfigTemplate()
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  	c.tpl.UserVars = c.PackerUserVars
    44  
    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.FloppyConfig.Prepare(c.tpl)...)
    52  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
    53  	errs = packer.MultiErrorAppend(errs, c.PrlctlConfig.Prepare(c.tpl)...)
    54  	errs = packer.MultiErrorAppend(errs, c.PrlctlVersionConfig.Prepare(c.tpl)...)
    55  	errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
    56  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
    57  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
    58  	errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(c.tpl)...)
    59  
    60  	templates := map[string]*string{
    61  		"source_path": &c.SourcePath,
    62  		"vm_name":     &c.VMName,
    63  	}
    64  
    65  	for n, ptr := range templates {
    66  		var err error
    67  		*ptr, err = c.tpl.Process(*ptr, nil)
    68  		if err != nil {
    69  			errs = packer.MultiErrorAppend(
    70  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    71  		}
    72  	}
    73  
    74  	for i, command := range c.BootCommand {
    75  		if err := c.tpl.Validate(command); err != nil {
    76  			errs = packer.MultiErrorAppend(errs,
    77  				fmt.Errorf("Error processing boot_command[%d]: %s", i, err))
    78  		}
    79  	}
    80  
    81  	if c.SourcePath == "" {
    82  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
    83  	} else {
    84  		if _, err := os.Stat(c.SourcePath); err != nil {
    85  			errs = packer.MultiErrorAppend(errs,
    86  				fmt.Errorf("source_path is invalid: %s", err))
    87  		}
    88  	}
    89  
    90  	// Warnings
    91  	var warnings []string
    92  	if c.ShutdownCommand == "" {
    93  		warnings = append(warnings,
    94  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
    95  				"will forcibly halt the virtual machine, which may result in data loss.")
    96  	}
    97  
    98  	// Check for any errors.
    99  	if errs != nil && len(errs.Errors) > 0 {
   100  		return nil, warnings, errs
   101  	}
   102  
   103  	return c, warnings, nil
   104  }