github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/builder/googlecompute/config.go (about) 1 package googlecompute 2 3 import ( 4 "errors" 5 "fmt" 6 "time" 7 8 "github.com/mitchellh/packer/common" 9 "github.com/mitchellh/packer/common/uuid" 10 "github.com/mitchellh/packer/helper/communicator" 11 "github.com/mitchellh/packer/helper/config" 12 "github.com/mitchellh/packer/packer" 13 "github.com/mitchellh/packer/template/interpolate" 14 ) 15 16 // Config is the configuration structure for the GCE builder. It stores 17 // both the publicly settable state as well as the privately generated 18 // state of the config object. 19 type Config struct { 20 common.PackerConfig `mapstructure:",squash"` 21 Comm communicator.Config `mapstructure:",squash"` 22 23 AccountFile string `mapstructure:"account_file"` 24 ProjectId string `mapstructure:"project_id"` 25 26 DiskName string `mapstructure:"disk_name"` 27 DiskSizeGb int64 `mapstructure:"disk_size"` 28 ImageName string `mapstructure:"image_name"` 29 ImageDescription string `mapstructure:"image_description"` 30 InstanceName string `mapstructure:"instance_name"` 31 MachineType string `mapstructure:"machine_type"` 32 Metadata map[string]string `mapstructure:"metadata"` 33 Network string `mapstructure:"network"` 34 SourceImage string `mapstructure:"source_image"` 35 SourceImageProjectId string `mapstructure:"source_image_project_id"` 36 RawStateTimeout string `mapstructure:"state_timeout"` 37 Tags []string `mapstructure:"tags"` 38 UseInternalIP bool `mapstructure:"use_internal_ip"` 39 Zone string `mapstructure:"zone"` 40 41 account accountFile 42 privateKeyBytes []byte 43 stateTimeout time.Duration 44 ctx interpolate.Context 45 } 46 47 func NewConfig(raws ...interface{}) (*Config, []string, error) { 48 c := new(Config) 49 err := config.Decode(c, &config.DecodeOpts{ 50 Interpolate: true, 51 InterpolateContext: &c.ctx, 52 InterpolateFilter: &interpolate.RenderFilter{ 53 Exclude: []string{ 54 "run_command", 55 }, 56 }, 57 }, raws...) 58 if err != nil { 59 return nil, nil, err 60 } 61 62 // Set defaults. 63 if c.Network == "" { 64 c.Network = "default" 65 } 66 67 if c.DiskSizeGb == 0 { 68 c.DiskSizeGb = 10 69 } 70 71 if c.ImageDescription == "" { 72 c.ImageDescription = "Created by Packer" 73 } 74 75 if c.ImageName == "" { 76 c.ImageName = "packer-{{timestamp}}" 77 } 78 79 if c.InstanceName == "" { 80 c.InstanceName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) 81 } 82 83 if c.DiskName == "" { 84 c.DiskName = c.InstanceName 85 } 86 87 if c.MachineType == "" { 88 c.MachineType = "n1-standard-1" 89 } 90 91 if c.RawStateTimeout == "" { 92 c.RawStateTimeout = "5m" 93 } 94 95 if c.Comm.SSHUsername == "" { 96 c.Comm.SSHUsername = "root" 97 } 98 99 var errs *packer.MultiError 100 101 // Process required parameters. 102 if c.ProjectId == "" { 103 errs = packer.MultiErrorAppend( 104 errs, errors.New("a project_id must be specified")) 105 } 106 107 if c.SourceImage == "" { 108 errs = packer.MultiErrorAppend( 109 errs, errors.New("a source_image must be specified")) 110 } 111 112 if c.Zone == "" { 113 errs = packer.MultiErrorAppend( 114 errs, errors.New("a zone must be specified")) 115 } 116 117 stateTimeout, err := time.ParseDuration(c.RawStateTimeout) 118 if err != nil { 119 errs = packer.MultiErrorAppend( 120 errs, fmt.Errorf("Failed parsing state_timeout: %s", err)) 121 } 122 c.stateTimeout = stateTimeout 123 124 if c.AccountFile != "" { 125 if err := loadJSON(&c.account, c.AccountFile); err != nil { 126 errs = packer.MultiErrorAppend( 127 errs, fmt.Errorf("Failed parsing account file: %s", err)) 128 } 129 } 130 131 // Check for any errors. 132 if errs != nil && len(errs.Errors) > 0 { 133 return nil, nil, errs 134 } 135 136 return c, nil, nil 137 }