github.phpd.cn/hashicorp/packer@v1.3.2/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/common/bootcommand" 10 "github.com/hashicorp/packer/helper/config" 11 "github.com/hashicorp/packer/packer" 12 "github.com/hashicorp/packer/template/interpolate" 13 ) 14 15 // Config is the configuration structure for the builder. 16 type Config struct { 17 common.PackerConfig `mapstructure:",squash"` 18 common.HTTPConfig `mapstructure:",squash"` 19 common.FloppyConfig `mapstructure:",squash"` 20 bootcommand.VNCConfig `mapstructure:",squash"` 21 vmwcommon.DriverConfig `mapstructure:",squash"` 22 vmwcommon.OutputConfig `mapstructure:",squash"` 23 vmwcommon.RunConfig `mapstructure:",squash"` 24 vmwcommon.ShutdownConfig `mapstructure:",squash"` 25 vmwcommon.SSHConfig `mapstructure:",squash"` 26 vmwcommon.ToolsConfig `mapstructure:",squash"` 27 vmwcommon.VMXConfig `mapstructure:",squash"` 28 29 Linked bool `mapstructure:"linked"` 30 RemoteType string `mapstructure:"remote_type"` 31 SkipCompaction bool `mapstructure:"skip_compaction"` 32 SourcePath string `mapstructure:"source_path"` 33 VMName string `mapstructure:"vm_name"` 34 35 ctx interpolate.Context 36 } 37 38 func NewConfig(raws ...interface{}) (*Config, []string, error) { 39 c := new(Config) 40 err := config.Decode(c, &config.DecodeOpts{ 41 Interpolate: true, 42 InterpolateContext: &c.ctx, 43 InterpolateFilter: &interpolate.RenderFilter{ 44 Exclude: []string{ 45 "boot_command", 46 "tools_upload_path", 47 }, 48 }, 49 }, raws...) 50 if err != nil { 51 return nil, nil, err 52 } 53 54 // Defaults 55 if c.VMName == "" { 56 c.VMName = fmt.Sprintf( 57 "packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix()) 58 } 59 60 // Prepare the errors 61 var errs *packer.MultiError 62 errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(&c.ctx)...) 63 errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...) 64 errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...) 65 errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...) 66 errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...) 67 errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...) 68 errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...) 69 errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...) 70 errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...) 71 errs = packer.MultiErrorAppend(errs, c.VNCConfig.Prepare(&c.ctx)...) 72 73 if c.SourcePath == "" { 74 errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but is required")) 75 } else { 76 if _, err := os.Stat(c.SourcePath); err != nil { 77 errs = packer.MultiErrorAppend(errs, 78 fmt.Errorf("source_path is invalid: %s", err)) 79 } 80 } 81 82 // Warnings 83 var warnings []string 84 if c.ShutdownCommand == "" { 85 warnings = append(warnings, 86 "A shutdown_command was not specified. Without a shutdown command, Packer\n"+ 87 "will forcibly halt the virtual machine, which may result in data loss.") 88 } 89 90 if c.Headless && c.DisableVNC { 91 warnings = append(warnings, 92 "Headless mode uses VNC to retrieve output. Since VNC has been disabled,\n"+ 93 "you won't be able to see any output.") 94 } 95 96 // Check for any errors. 97 if errs != nil && len(errs.Errors) > 0 { 98 return nil, warnings, errs 99 } 100 101 return c, warnings, nil 102 }