github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/profitbricks/config.go (about) 1 package profitbricks 2 3 import ( 4 "errors" 5 "github.com/mitchellh/mapstructure" 6 "github.com/mitchellh/packer/common" 7 "github.com/mitchellh/packer/helper/communicator" 8 "github.com/mitchellh/packer/helper/config" 9 "github.com/mitchellh/packer/packer" 10 "github.com/mitchellh/packer/template/interpolate" 11 "os" 12 ) 13 14 type Config struct { 15 common.PackerConfig `mapstructure:",squash"` 16 Comm communicator.Config `mapstructure:",squash"` 17 18 PBUsername string `mapstructure:"username"` 19 PBPassword string `mapstructure:"password"` 20 PBUrl string `mapstructure:"url"` 21 22 Region string `mapstructure:"location"` 23 Image string `mapstructure:"image"` 24 SSHKey string 25 SnapshotName string `mapstructure:"snapshot_name"` 26 DiskSize int `mapstructure:"disk_size"` 27 DiskType string `mapstructure:"disk_type"` 28 Cores int `mapstructure:"cores"` 29 Ram int `mapstructure:"ram"` 30 Retries int `mapstructure:"retries"` 31 CommConfig communicator.Config `mapstructure:",squash"` 32 ctx interpolate.Context 33 } 34 35 func NewConfig(raws ...interface{}) (*Config, []string, error) { 36 var c Config 37 38 var md mapstructure.Metadata 39 err := config.Decode(&c, &config.DecodeOpts{ 40 Metadata: &md, 41 Interpolate: true, 42 InterpolateContext: &c.ctx, 43 InterpolateFilter: &interpolate.RenderFilter{ 44 Exclude: []string{ 45 "run_command", 46 }, 47 }, 48 }, raws...) 49 if err != nil { 50 return nil, nil, err 51 } 52 53 var errs *packer.MultiError 54 55 if c.Comm.SSHPassword == "" && c.Comm.SSHPrivateKey == "" { 56 errs = packer.MultiErrorAppend( 57 errs, errors.New("Either ssh private key path or ssh password must be set.")) 58 } 59 60 if c.SnapshotName == "" { 61 def, err := interpolate.Render("packer-{{timestamp}}", nil) 62 if err != nil { 63 panic(err) 64 } 65 66 // Default to packer-{{ unix timestamp (utc) }} 67 c.SnapshotName = def 68 } 69 70 if c.PBUsername == "" { 71 c.PBUsername = os.Getenv("PROFITBRICKS_USERNAME") 72 } 73 74 if c.PBPassword == "" { 75 c.PBPassword = os.Getenv("PROFITBRICKS_PASSWORD") 76 } 77 78 if c.PBUrl == "" { 79 c.PBUrl = "https://api.profitbricks.com/rest/v2" 80 } 81 82 if c.Cores == 0 { 83 c.Cores = 4 84 } 85 86 if c.Ram == 0 { 87 c.Ram = 2048 88 } 89 90 if c.DiskSize == 0 { 91 c.DiskSize = 50 92 } 93 94 if c.Region == "" { 95 c.Region = "us/las" 96 } 97 98 if c.DiskType == "" { 99 c.DiskType = "HDD" 100 } 101 102 if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { 103 errs = packer.MultiErrorAppend(errs, es...) 104 } 105 106 if c.Image == "" { 107 errs = packer.MultiErrorAppend( 108 errs, errors.New("ProfitBricks 'image' is required")) 109 } 110 111 if c.PBUsername == "" { 112 errs = packer.MultiErrorAppend( 113 errs, errors.New("ProfitBricks username is required")) 114 } 115 116 if c.PBPassword == "" { 117 errs = packer.MultiErrorAppend( 118 errs, errors.New("ProfitBricks password is required")) 119 } 120 121 if errs != nil && len(errs.Errors) > 0 { 122 return nil, nil, errs 123 } 124 common.ScrubConfig(c, c.PBUsername) 125 126 return &c, nil, nil 127 }