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