github.com/rothwerx/packer@v0.9.0/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 Subnetwork string `mapstructure:"subnetwork"` 35 Address string `mapstructure:"address"` 36 Preemptible bool `mapstructure:"preemptible"` 37 SourceImage string `mapstructure:"source_image"` 38 SourceImageProjectId string `mapstructure:"source_image_project_id"` 39 RawStateTimeout string `mapstructure:"state_timeout"` 40 Tags []string `mapstructure:"tags"` 41 UseInternalIP bool `mapstructure:"use_internal_ip"` 42 Region string `mapstructure:"region"` 43 Zone string `mapstructure:"zone"` 44 45 account accountFile 46 privateKeyBytes []byte 47 stateTimeout time.Duration 48 ctx interpolate.Context 49 } 50 51 func NewConfig(raws ...interface{}) (*Config, []string, error) { 52 c := new(Config) 53 err := config.Decode(c, &config.DecodeOpts{ 54 Interpolate: true, 55 InterpolateContext: &c.ctx, 56 InterpolateFilter: &interpolate.RenderFilter{ 57 Exclude: []string{ 58 "run_command", 59 }, 60 }, 61 }, raws...) 62 if err != nil { 63 return nil, nil, err 64 } 65 66 var errs *packer.MultiError 67 68 // Set defaults. 69 if c.Network == "" { 70 c.Network = "default" 71 } 72 73 if c.DiskSizeGb == 0 { 74 c.DiskSizeGb = 10 75 } 76 77 if c.ImageDescription == "" { 78 c.ImageDescription = "Created by Packer" 79 } 80 81 if c.ImageName == "" { 82 img, err := interpolate.Render("packer-{{timestamp}}", nil) 83 if err != nil { 84 errs = packer.MultiErrorAppend(errs, 85 fmt.Errorf("Unable to parse image name: %s ", err)) 86 } else { 87 c.ImageName = img 88 } 89 } 90 91 if c.InstanceName == "" { 92 c.InstanceName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) 93 } 94 95 if c.DiskName == "" { 96 c.DiskName = c.InstanceName 97 } 98 99 if c.MachineType == "" { 100 c.MachineType = "n1-standard-1" 101 } 102 103 if c.RawStateTimeout == "" { 104 c.RawStateTimeout = "5m" 105 } 106 107 if c.Comm.SSHUsername == "" { 108 c.Comm.SSHUsername = "root" 109 } 110 111 if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { 112 errs = packer.MultiErrorAppend(errs, es...) 113 } 114 115 // Process required parameters. 116 if c.ProjectId == "" { 117 errs = packer.MultiErrorAppend( 118 errs, errors.New("a project_id must be specified")) 119 } 120 121 if c.SourceImage == "" { 122 errs = packer.MultiErrorAppend( 123 errs, errors.New("a source_image must be specified")) 124 } 125 126 if c.Zone == "" { 127 errs = packer.MultiErrorAppend( 128 errs, errors.New("a zone must be specified")) 129 } 130 if c.Region == "" && len(c.Zone) > 2 { 131 // get region from Zone 132 region := c.Zone[:len(c.Zone)-2] 133 c.Region = region 134 } 135 136 stateTimeout, err := time.ParseDuration(c.RawStateTimeout) 137 if err != nil { 138 errs = packer.MultiErrorAppend( 139 errs, fmt.Errorf("Failed parsing state_timeout: %s", err)) 140 } 141 c.stateTimeout = stateTimeout 142 143 if c.AccountFile != "" { 144 if err := processAccountFile(&c.account, c.AccountFile); err != nil { 145 errs = packer.MultiErrorAppend(errs, err) 146 } 147 } 148 149 // Check for any errors. 150 if errs != nil && len(errs.Errors) > 0 { 151 return nil, nil, errs 152 } 153 154 return c, nil, nil 155 }