github.com/sneal/packer@v0.5.2/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/packer" 10 ) 11 12 // Config is the configuration structure for the GCE builder. It stores 13 // both the publicly settable state as well as the privately generated 14 // state of the config object. 15 type Config struct { 16 common.PackerConfig `mapstructure:",squash"` 17 18 BucketName string `mapstructure:"bucket_name"` 19 ClientSecretsFile string `mapstructure:"client_secrets_file"` 20 ImageName string `mapstructure:"image_name"` 21 ImageDescription string `mapstructure:"image_description"` 22 MachineType string `mapstructure:"machine_type"` 23 Metadata map[string]string `mapstructure:"metadata"` 24 Network string `mapstructure:"network"` 25 Passphrase string `mapstructure:"passphrase"` 26 PrivateKeyFile string `mapstructure:"private_key_file"` 27 ProjectId string `mapstructure:"project_id"` 28 SourceImage string `mapstructure:"source_image"` 29 SSHUsername string `mapstructure:"ssh_username"` 30 SSHPort uint `mapstructure:"ssh_port"` 31 RawSSHTimeout string `mapstructure:"ssh_timeout"` 32 RawStateTimeout string `mapstructure:"state_timeout"` 33 Tags []string `mapstructure:"tags"` 34 Zone string `mapstructure:"zone"` 35 36 clientSecrets *clientSecrets 37 instanceName string 38 privateKeyBytes []byte 39 sshTimeout time.Duration 40 stateTimeout time.Duration 41 tpl *packer.ConfigTemplate 42 } 43 44 func NewConfig(raws ...interface{}) (*Config, []string, error) { 45 c := new(Config) 46 md, err := common.DecodeConfig(c, raws...) 47 if err != nil { 48 return nil, nil, err 49 } 50 51 c.tpl, err = packer.NewConfigTemplate() 52 if err != nil { 53 return nil, nil, err 54 } 55 c.tpl.UserVars = c.PackerUserVars 56 57 // Prepare the errors 58 errs := common.CheckUnusedConfig(md) 59 60 // Set defaults. 61 if c.Network == "" { 62 c.Network = "default" 63 } 64 65 if c.ImageDescription == "" { 66 c.ImageDescription = "Created by Packer" 67 } 68 69 if c.ImageName == "" { 70 c.ImageName = "packer-{{timestamp}}" 71 } 72 73 if c.MachineType == "" { 74 c.MachineType = "n1-standard-1" 75 } 76 77 if c.RawSSHTimeout == "" { 78 c.RawSSHTimeout = "5m" 79 } 80 81 if c.RawStateTimeout == "" { 82 c.RawStateTimeout = "5m" 83 } 84 85 if c.SSHUsername == "" { 86 c.SSHUsername = "root" 87 } 88 89 if c.SSHPort == 0 { 90 c.SSHPort = 22 91 } 92 93 // Process Templates 94 templates := map[string]*string{ 95 "bucket_name": &c.BucketName, 96 "client_secrets_file": &c.ClientSecretsFile, 97 "image_name": &c.ImageName, 98 "image_description": &c.ImageDescription, 99 "machine_type": &c.MachineType, 100 "network": &c.Network, 101 "passphrase": &c.Passphrase, 102 "private_key_file": &c.PrivateKeyFile, 103 "project_id": &c.ProjectId, 104 "source_image": &c.SourceImage, 105 "ssh_username": &c.SSHUsername, 106 "ssh_timeout": &c.RawSSHTimeout, 107 "state_timeout": &c.RawStateTimeout, 108 "zone": &c.Zone, 109 } 110 111 for n, ptr := range templates { 112 var err error 113 *ptr, err = c.tpl.Process(*ptr, nil) 114 if err != nil { 115 errs = packer.MultiErrorAppend( 116 errs, fmt.Errorf("Error processing %s: %s", n, err)) 117 } 118 } 119 120 // Process required parameters. 121 if c.BucketName == "" { 122 errs = packer.MultiErrorAppend( 123 errs, errors.New("a bucket_name must be specified")) 124 } 125 126 if c.ClientSecretsFile == "" { 127 errs = packer.MultiErrorAppend( 128 errs, errors.New("a client_secrets_file must be specified")) 129 } 130 131 if c.PrivateKeyFile == "" { 132 errs = packer.MultiErrorAppend( 133 errs, errors.New("a private_key_file must be specified")) 134 } 135 136 if c.ProjectId == "" { 137 errs = packer.MultiErrorAppend( 138 errs, errors.New("a project_id must be specified")) 139 } 140 141 if c.SourceImage == "" { 142 errs = packer.MultiErrorAppend( 143 errs, errors.New("a source_image must be specified")) 144 } 145 146 if c.Zone == "" { 147 errs = packer.MultiErrorAppend( 148 errs, errors.New("a zone must be specified")) 149 } 150 151 // Process timeout settings. 152 sshTimeout, err := time.ParseDuration(c.RawSSHTimeout) 153 if err != nil { 154 errs = packer.MultiErrorAppend( 155 errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err)) 156 } 157 c.sshTimeout = sshTimeout 158 159 stateTimeout, err := time.ParseDuration(c.RawStateTimeout) 160 if err != nil { 161 errs = packer.MultiErrorAppend( 162 errs, fmt.Errorf("Failed parsing state_timeout: %s", err)) 163 } 164 c.stateTimeout = stateTimeout 165 166 if c.ClientSecretsFile != "" { 167 // Load the client secrets file. 168 cs, err := loadClientSecrets(c.ClientSecretsFile) 169 if err != nil { 170 errs = packer.MultiErrorAppend( 171 errs, fmt.Errorf("Failed parsing client secrets file: %s", err)) 172 } 173 c.clientSecrets = cs 174 } 175 176 if c.PrivateKeyFile != "" { 177 // Load the private key. 178 c.privateKeyBytes, err = processPrivateKeyFile(c.PrivateKeyFile, c.Passphrase) 179 if err != nil { 180 errs = packer.MultiErrorAppend( 181 errs, fmt.Errorf("Failed loading private key file: %s", err)) 182 } 183 } 184 185 // Check for any errors. 186 if errs != nil && len(errs.Errors) > 0 { 187 return nil, nil, errs 188 } 189 190 return c, nil, nil 191 }