github.com/tonnydourado/packer@v0.6.1-0.20140701134019-5d0cd9676a37/builder/parallels/pvm/config.go (about)

     1  package pvm
     2  
     3  import (
     4  	"fmt"
     5  	parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
     6  	"github.com/mitchellh/packer/common"
     7  	"github.com/mitchellh/packer/packer"
     8  	"os"
     9  )
    10  
    11  // Config is the configuration structure for the builder.
    12  type Config struct {
    13  	common.PackerConfig                 `mapstructure:",squash"`
    14  	parallelscommon.FloppyConfig        `mapstructure:",squash"`
    15  	parallelscommon.OutputConfig        `mapstructure:",squash"`
    16  	parallelscommon.RunConfig           `mapstructure:",squash"`
    17  	parallelscommon.SSHConfig           `mapstructure:",squash"`
    18  	parallelscommon.ShutdownConfig      `mapstructure:",squash"`
    19  	parallelscommon.PrlctlConfig        `mapstructure:",squash"`
    20  	parallelscommon.PrlctlVersionConfig `mapstructure:",squash"`
    21  
    22  	BootCommand             []string `mapstructure:"boot_command"`
    23  	ParallelsToolsMode      string   `mapstructure:"parallels_tools_mode"`
    24  	ParallelsToolsGuestPath string   `mapstructure:"parallels_tools_guest_path"`
    25  	ParallelsToolsHostPath  string   `mapstructure:"parallels_tools_host_path"`
    26  	SourcePath              string   `mapstructure:"source_path"`
    27  	VMName                  string   `mapstructure:"vm_name"`
    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  	// Defaults
    46  	if c.ParallelsToolsMode == "" {
    47  		c.ParallelsToolsMode = "disable"
    48  	}
    49  
    50  	if c.ParallelsToolsGuestPath == "" {
    51  		c.ParallelsToolsGuestPath = "prl-tools.iso"
    52  	}
    53  
    54  	if c.ParallelsToolsHostPath == "" {
    55  		c.ParallelsToolsHostPath = "/Applications/Parallels Desktop.app/Contents/Resources/Tools/prl-tools-other.iso"
    56  	}
    57  
    58  	if c.VMName == "" {
    59  		c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
    60  	}
    61  
    62  	// Prepare the errors
    63  	errs := common.CheckUnusedConfig(md)
    64  	errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(c.tpl)...)
    65  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
    66  	errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
    67  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
    68  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
    69  	errs = packer.MultiErrorAppend(errs, c.PrlctlConfig.Prepare(c.tpl)...)
    70  	errs = packer.MultiErrorAppend(errs, c.PrlctlVersionConfig.Prepare(c.tpl)...)
    71  
    72  	templates := map[string]*string{
    73  		"parallels_tools_mode":       &c.ParallelsToolsMode,
    74  		"parallels_tools_host_paht":  &c.ParallelsToolsHostPath,
    75  		"parallels_tools_guest_path": &c.ParallelsToolsGuestPath,
    76  		"source_path":                &c.SourcePath,
    77  		"vm_name":                    &c.VMName,
    78  	}
    79  
    80  	for n, ptr := range templates {
    81  		var err error
    82  		*ptr, err = c.tpl.Process(*ptr, nil)
    83  		if err != nil {
    84  			errs = packer.MultiErrorAppend(
    85  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    86  		}
    87  	}
    88  
    89  	for i, command := range c.BootCommand {
    90  		if err := c.tpl.Validate(command); err != nil {
    91  			errs = packer.MultiErrorAppend(errs,
    92  				fmt.Errorf("Error processing boot_command[%d]: %s", i, err))
    93  		}
    94  	}
    95  
    96  	validMode := false
    97  	validModes := []string{
    98  		parallelscommon.ParallelsToolsModeDisable,
    99  		parallelscommon.ParallelsToolsModeAttach,
   100  		parallelscommon.ParallelsToolsModeUpload,
   101  	}
   102  
   103  	for _, mode := range validModes {
   104  		if c.ParallelsToolsMode == mode {
   105  			validMode = true
   106  			break
   107  		}
   108  	}
   109  
   110  	if !validMode {
   111  		errs = packer.MultiErrorAppend(errs,
   112  			fmt.Errorf("parallels_tools_mode is invalid. Must be one of: %v", validModes))
   113  	}
   114  
   115  	if c.SourcePath == "" {
   116  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
   117  	} else {
   118  		if _, err := os.Stat(c.SourcePath); err != nil {
   119  			errs = packer.MultiErrorAppend(errs,
   120  				fmt.Errorf("source_path is invalid: %s", err))
   121  		}
   122  	}
   123  
   124  	// Warnings
   125  	var warnings []string
   126  	if c.ShutdownCommand == "" {
   127  		warnings = append(warnings,
   128  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
   129  				"will forcibly halt the virtual machine, which may result in data loss.")
   130  	}
   131  
   132  	// Check for any errors.
   133  	if errs != nil && len(errs.Errors) > 0 {
   134  		return nil, warnings, errs
   135  	}
   136  
   137  	return c, warnings, nil
   138  }