github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/builder/parallels/pvm/config.go (about) 1 package pvm 2 3 import ( 4 "fmt" 5 parallelscommon "github.com/mitchellh/packer/builder/parallels/common" 6 "github.com/mitchellh/packer/common" 7 "github.com/mitchellh/packer/packer" 8 "os" 9 ) 10 11 // Config is the configuration structure for the builder. 12 type Config struct { 13 common.PackerConfig `mapstructure:",squash"` 14 parallelscommon.FloppyConfig `mapstructure:",squash"` 15 parallelscommon.OutputConfig `mapstructure:",squash"` 16 parallelscommon.RunConfig `mapstructure:",squash"` 17 parallelscommon.SSHConfig `mapstructure:",squash"` 18 parallelscommon.ShutdownConfig `mapstructure:",squash"` 19 parallelscommon.PrlctlConfig `mapstructure:",squash"` 20 parallelscommon.PrlctlVersionConfig `mapstructure:",squash"` 21 22 ParallelsToolsMode string `mapstructure:"parallels_tools_mode"` 23 ParallelsToolsGuestPath string `mapstructure:"parallels_tools_guest_path"` 24 ParallelsToolsHostPath string `mapstructure:"parallels_tools_host_path"` 25 26 SourcePath string `mapstructure:"source_path"` 27 VMName string `mapstructure:"vm_name"` 28 29 tpl *packer.ConfigTemplate 30 } 31 32 func NewConfig(raws ...interface{}) (*Config, []string, error) { 33 c := new(Config) 34 md, err := common.DecodeConfig(c, raws...) 35 if err != nil { 36 return nil, nil, err 37 } 38 39 c.tpl, err = packer.NewConfigTemplate() 40 if err != nil { 41 return nil, nil, err 42 } 43 c.tpl.UserVars = c.PackerUserVars 44 45 // Defaults 46 if c.ParallelsToolsMode == "" { 47 c.ParallelsToolsMode = "disable" 48 } 49 50 if c.ParallelsToolsGuestPath == "" { 51 c.ParallelsToolsGuestPath = "prl-tools.iso" 52 } 53 54 if c.ParallelsToolsHostPath == "" { 55 c.ParallelsToolsHostPath = "/Applications/Parallels Desktop.app/Contents/Resources/Tools/prl-tools-other.iso" 56 } 57 58 if c.VMName == "" { 59 c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName) 60 } 61 62 // Prepare the errors 63 errs := common.CheckUnusedConfig(md) 64 errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(c.tpl)...) 65 errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...) 66 errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...) 67 errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...) 68 errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...) 69 errs = packer.MultiErrorAppend(errs, c.PrlctlConfig.Prepare(c.tpl)...) 70 errs = packer.MultiErrorAppend(errs, c.PrlctlVersionConfig.Prepare(c.tpl)...) 71 72 templates := map[string]*string{ 73 "parallels_tools_mode": &c.ParallelsToolsMode, 74 "parallels_tools_host_paht": &c.ParallelsToolsHostPath, 75 "parallels_tools_guest_path": &c.ParallelsToolsGuestPath, 76 "source_path": &c.SourcePath, 77 "vm_name": &c.VMName, 78 } 79 80 for n, ptr := range templates { 81 var err error 82 *ptr, err = c.tpl.Process(*ptr, nil) 83 if err != nil { 84 errs = packer.MultiErrorAppend( 85 errs, fmt.Errorf("Error processing %s: %s", n, err)) 86 } 87 } 88 89 validMode := false 90 validModes := []string{ 91 parallelscommon.ParallelsToolsModeDisable, 92 parallelscommon.ParallelsToolsModeAttach, 93 parallelscommon.ParallelsToolsModeUpload, 94 } 95 96 for _, mode := range validModes { 97 if c.ParallelsToolsMode == mode { 98 validMode = true 99 break 100 } 101 } 102 103 if !validMode { 104 errs = packer.MultiErrorAppend(errs, 105 fmt.Errorf("parallels_tools_mode is invalid. Must be one of: %v", validModes)) 106 } 107 108 if c.SourcePath == "" { 109 errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required")) 110 } else { 111 if _, err := os.Stat(c.SourcePath); err != nil { 112 errs = packer.MultiErrorAppend(errs, 113 fmt.Errorf("source_path is invalid: %s", err)) 114 } 115 } 116 117 // Warnings 118 var warnings []string 119 if c.ShutdownCommand == "" { 120 warnings = append(warnings, 121 "A shutdown_command was not specified. Without a shutdown command, Packer\n"+ 122 "will forcibly halt the virtual machine, which may result in data loss.") 123 } 124 125 // Check for any errors. 126 if errs != nil && len(errs.Errors) > 0 { 127 return nil, warnings, errs 128 } 129 130 return c, warnings, nil 131 }