github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/vmware/vmx/config.go (about) 1 package vmx 2 3 import ( 4 "fmt" 5 "os" 6 7 vmwcommon "github.com/hashicorp/packer/builder/vmware/common" 8 "github.com/hashicorp/packer/common" 9 "github.com/hashicorp/packer/helper/config" 10 "github.com/hashicorp/packer/packer" 11 "github.com/hashicorp/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( 55 "packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix()) 56 } 57 58 // Prepare the errors 59 var errs *packer.MultiError 60 errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(&c.ctx)...) 61 errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...) 62 errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...) 63 errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...) 64 errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...) 65 errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...) 66 errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...) 67 errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...) 68 errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...) 69 70 if c.SourcePath == "" { 71 errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but 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 }