github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/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/helper/config"
    10  	"github.com/mitchellh/packer/packer"
    11  	"github.com/mitchellh/packer/template/interpolate"
    12  )
    13  
    14  // Config is the configuration structure for the builder.
    15  type Config struct {
    16  	common.PackerConfig      `mapstructure:",squash"`
    17  	vmwcommon.DriverConfig   `mapstructure:",squash"`
    18  	vmwcommon.OutputConfig   `mapstructure:",squash"`
    19  	vmwcommon.RunConfig      `mapstructure:",squash"`
    20  	vmwcommon.ShutdownConfig `mapstructure:",squash"`
    21  	vmwcommon.SSHConfig      `mapstructure:",squash"`
    22  	vmwcommon.ToolsConfig    `mapstructure:",squash"`
    23  	vmwcommon.VMXConfig      `mapstructure:",squash"`
    24  
    25  	BootCommand    []string `mapstructure:"boot_command"`
    26  	FloppyFiles    []string `mapstructure:"floppy_files"`
    27  	RemoteType     string   `mapstructure:"remote_type"`
    28  	SkipCompaction bool     `mapstructure:"skip_compaction"`
    29  	SourcePath     string   `mapstructure:"source_path"`
    30  	VMName         string   `mapstructure:"vm_name"`
    31  
    32  	ctx interpolate.Context
    33  }
    34  
    35  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    36  	c := new(Config)
    37  	err := config.Decode(c, &config.DecodeOpts{
    38  		Interpolate:        true,
    39  		InterpolateContext: &c.ctx,
    40  		InterpolateFilter: &interpolate.RenderFilter{
    41  			Exclude: []string{
    42  				"boot_command",
    43  				"tools_upload_path",
    44  			},
    45  		},
    46  	}, raws...)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  
    51  	// Defaults
    52  	if c.VMName == "" {
    53  		c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
    54  	}
    55  
    56  	// Prepare the errors
    57  	var errs *packer.MultiError
    58  	errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(&c.ctx)...)
    59  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...)
    60  	errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...)
    61  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
    62  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...)
    63  	errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...)
    64  	errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...)
    65  
    66  	if c.SourcePath == "" {
    67  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but is required"))
    68  	} else {
    69  		if _, err := os.Stat(c.SourcePath); err != nil {
    70  			errs = packer.MultiErrorAppend(errs,
    71  				fmt.Errorf("source_path is invalid: %s", err))
    72  		}
    73  	}
    74  
    75  	// Warnings
    76  	var warnings []string
    77  	if c.ShutdownCommand == "" {
    78  		warnings = append(warnings,
    79  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
    80  				"will forcibly halt the virtual machine, which may result in data loss.")
    81  	}
    82  
    83  	// Check for any errors.
    84  	if errs != nil && len(errs.Errors) > 0 {
    85  		return nil, warnings, errs
    86  	}
    87  
    88  	return c, warnings, nil
    89  }