github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/builder/virtualbox/ovf/config.go (about) 1 package ovf 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" 9 "github.com/mitchellh/packer/common" 10 "github.com/mitchellh/packer/helper/config" 11 "github.com/mitchellh/packer/packer" 12 "github.com/mitchellh/packer/template/interpolate" 13 ) 14 15 // Config is the configuration structure for the builder. 16 type Config struct { 17 common.PackerConfig `mapstructure:",squash"` 18 vboxcommon.ExportConfig `mapstructure:",squash"` 19 vboxcommon.ExportOpts `mapstructure:",squash"` 20 vboxcommon.FloppyConfig `mapstructure:",squash"` 21 vboxcommon.OutputConfig `mapstructure:",squash"` 22 vboxcommon.RunConfig `mapstructure:",squash"` 23 vboxcommon.SSHConfig `mapstructure:",squash"` 24 vboxcommon.ShutdownConfig `mapstructure:",squash"` 25 vboxcommon.VBoxManageConfig `mapstructure:",squash"` 26 vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` 27 vboxcommon.VBoxVersionConfig `mapstructure:",squash"` 28 29 BootCommand []string `mapstructure:"boot_command"` 30 SourcePath string `mapstructure:"source_path"` 31 GuestAdditionsMode string `mapstructure:"guest_additions_mode"` 32 GuestAdditionsPath string `mapstructure:"guest_additions_path"` 33 GuestAdditionsURL string `mapstructure:"guest_additions_url"` 34 GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"` 35 VMName string `mapstructure:"vm_name"` 36 ImportOpts string `mapstructure:"import_opts"` 37 ImportFlags []string `mapstructure:"import_flags"` 38 39 ctx interpolate.Context 40 } 41 42 func NewConfig(raws ...interface{}) (*Config, []string, error) { 43 c := new(Config) 44 err := config.Decode(c, &config.DecodeOpts{ 45 Interpolate: true, 46 InterpolateContext: &c.ctx, 47 InterpolateFilter: &interpolate.RenderFilter{ 48 Exclude: []string{ 49 "boot_command", 50 "guest_additions_path", 51 "guest_additions_url", 52 "vboxmanage", 53 "vboxmanage_post", 54 }, 55 }, 56 }, raws...) 57 if err != nil { 58 return nil, nil, err 59 } 60 61 // Defaults 62 if c.GuestAdditionsMode == "" { 63 c.GuestAdditionsMode = "upload" 64 } 65 66 if c.GuestAdditionsPath == "" { 67 c.GuestAdditionsPath = "VBoxGuestAdditions.iso" 68 } 69 70 if c.VMName == "" { 71 c.VMName = fmt.Sprintf( 72 "packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix()) 73 } 74 75 // Prepare the errors 76 var errs *packer.MultiError 77 errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...) 78 errs = packer.MultiErrorAppend(errs, c.ExportOpts.Prepare(&c.ctx)...) 79 errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...) 80 errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...) 81 errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...) 82 errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...) 83 errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...) 84 errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(&c.ctx)...) 85 errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(&c.ctx)...) 86 errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(&c.ctx)...) 87 88 if c.SourcePath == "" { 89 errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required")) 90 } else { 91 if _, err := os.Stat(c.SourcePath); err != nil { 92 errs = packer.MultiErrorAppend(errs, 93 fmt.Errorf("source_path is invalid: %s", err)) 94 } 95 } 96 97 validMode := false 98 validModes := []string{ 99 vboxcommon.GuestAdditionsModeDisable, 100 vboxcommon.GuestAdditionsModeAttach, 101 vboxcommon.GuestAdditionsModeUpload, 102 } 103 104 for _, mode := range validModes { 105 if c.GuestAdditionsMode == mode { 106 validMode = true 107 break 108 } 109 } 110 111 if !validMode { 112 errs = packer.MultiErrorAppend(errs, 113 fmt.Errorf("guest_additions_mode is invalid. Must be one of: %v", validModes)) 114 } 115 116 if c.GuestAdditionsSHA256 != "" { 117 c.GuestAdditionsSHA256 = strings.ToLower(c.GuestAdditionsSHA256) 118 } 119 120 // Warnings 121 var warnings []string 122 if c.ShutdownCommand == "" { 123 warnings = append(warnings, 124 "A shutdown_command was not specified. Without a shutdown command, Packer\n"+ 125 "will forcibly halt the virtual machine, which may result in data loss.") 126 } 127 128 // Check for any errors. 129 if errs != nil && len(errs.Errors) > 0 { 130 return nil, warnings, errs 131 } 132 133 // TODO: Write a packer fix and just remove import_opts 134 if c.ImportOpts != "" { 135 c.ImportFlags = append(c.ImportFlags, "--options", c.ImportOpts) 136 } 137 138 return c, warnings, nil 139 }