github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/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  	FloppyFiles    []string `mapstructure:"floppy_files"`
    24  	RemoteType     string   `mapstructure:"remote_type"`
    25  	SkipCompaction bool     `mapstructure:"skip_compaction"`
    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.VMName == "" {
    47  		c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
    48  	}
    49  
    50  	// Prepare the errors
    51  	errs := common.CheckUnusedConfig(md)
    52  	errs = packer.MultiErrorAppend(errs, c.DriverConfig.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.ToolsConfig.Prepare(c.tpl)...)
    58  	errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(c.tpl)...)
    59  
    60  	templates := map[string]*string{
    61  		"remote_type": &c.RemoteType,
    62  		"source_path": &c.SourcePath,
    63  		"vm_name":     &c.VMName,
    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  }