github.com/rothwerx/packer@v0.9.0/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  	vmwcommon.DriverConfig   `mapstructure:",squash"`
    19  	vmwcommon.OutputConfig   `mapstructure:",squash"`
    20  	vmwcommon.RunConfig      `mapstructure:",squash"`
    21  	vmwcommon.ShutdownConfig `mapstructure:",squash"`
    22  	vmwcommon.SSHConfig      `mapstructure:",squash"`
    23  	vmwcommon.ToolsConfig    `mapstructure:",squash"`
    24  	vmwcommon.VMXConfig      `mapstructure:",squash"`
    25  
    26  	BootCommand    []string `mapstructure:"boot_command"`
    27  	FloppyFiles    []string `mapstructure:"floppy_files"`
    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  
    68  	if c.SourcePath == "" {
    69  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but is required"))
    70  	} else {
    71  		if _, err := os.Stat(c.SourcePath); err != nil {
    72  			errs = packer.MultiErrorAppend(errs,
    73  				fmt.Errorf("source_path is invalid: %s", err))
    74  		}
    75  	}
    76  
    77  	// Warnings
    78  	var warnings []string
    79  	if c.ShutdownCommand == "" {
    80  		warnings = append(warnings,
    81  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
    82  				"will forcibly halt the virtual machine, which may result in data loss.")
    83  	}
    84  
    85  	// Check for any errors.
    86  	if errs != nil && len(errs.Errors) > 0 {
    87  		return nil, warnings, errs
    88  	}
    89  
    90  	return c, warnings, nil
    91  }