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