github.com/sneal/packer@v0.5.2/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.VMXConfig      `mapstructure:",squash"`
    21  
    22  	SkipCompaction bool   `mapstructure:"skip_compaction"`
    23  	SourcePath     string `mapstructure:"source_path"`
    24  	VMName         string `mapstructure:"vm_name"`
    25  
    26  	tpl *packer.ConfigTemplate
    27  }
    28  
    29  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    30  	c := new(Config)
    31  	md, err := common.DecodeConfig(c, raws...)
    32  	if err != nil {
    33  		return nil, nil, err
    34  	}
    35  
    36  	c.tpl, err = packer.NewConfigTemplate()
    37  	if err != nil {
    38  		return nil, nil, err
    39  	}
    40  	c.tpl.UserVars = c.PackerUserVars
    41  
    42  	// Defaults
    43  	if c.VMName == "" {
    44  		c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
    45  	}
    46  
    47  	// Prepare the errors
    48  	errs := common.CheckUnusedConfig(md)
    49  	errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(c.tpl)...)
    50  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
    51  	errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
    52  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
    53  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
    54  	errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(c.tpl)...)
    55  
    56  	templates := map[string]*string{
    57  		"source_path": &c.SourcePath,
    58  		"vm_name":     &c.VMName,
    59  	}
    60  
    61  	for n, ptr := range templates {
    62  		var err error
    63  		*ptr, err = c.tpl.Process(*ptr, nil)
    64  		if err != nil {
    65  			errs = packer.MultiErrorAppend(
    66  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    67  		}
    68  	}
    69  
    70  	if c.SourcePath == "" {
    71  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
    72  	} else {
    73  		if _, err := os.Stat(c.SourcePath); err != nil {
    74  			errs = packer.MultiErrorAppend(errs,
    75  				fmt.Errorf("source_path is invalid: %s", err))
    76  		}
    77  	}
    78  
    79  	// Warnings
    80  	var warnings []string
    81  	if c.ShutdownCommand == "" {
    82  		warnings = append(warnings,
    83  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
    84  				"will forcibly halt the virtual machine, which may result in data loss.")
    85  	}
    86  
    87  	// Check for any errors.
    88  	if errs != nil && len(errs.Errors) > 0 {
    89  		return nil, warnings, errs
    90  	}
    91  
    92  	return c, warnings, nil
    93  }