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

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