@@ -368,6 +368,22 @@ func createInstance(serviceAccount string) (string, error) {
368
368
}
369
369
i .ServiceAccounts = []* compute.ServiceAccount {saObj }
370
370
371
+ if pubkey , ok := os .LookupEnv ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE" ); ok {
372
+ glog .V (4 ).Infof ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE set to %v, adding public key to Instance" , pubkey )
373
+ meta , err := generateMetadataWithPublicKey (pubkey )
374
+ if err != nil {
375
+ return "" , err
376
+ }
377
+ i .Metadata = meta
378
+ /*glog.V(4).Infof("JENKINS_GCE_SSH_PUBLIC_KEY_FILE set to %v, adding public key to Instance", pubkey)
379
+ // If we're on CI add public SSH keys to the instance
380
+ i.Metadata =
381
+ err = addPubKeyToInstance(*project, *zone, i.Name, pubkey)
382
+ if err != nil {
383
+ return "", fmt.Errorf("could not add Jenkins public key %v to instance %v: %v", pubkey, i.Name, err)
384
+ }*/
385
+ }
386
+
371
387
if _ , err := computeService .Instances .Get (* project , * zone , i .Name ).Do (); err != nil {
372
388
op , err := computeService .Instances .Insert (* project , * zone , i ).Do ()
373
389
glog .V (4 ).Infof ("Inserted instance %v in project %v, zone %v" , i .Name , * project , * zone )
@@ -384,15 +400,6 @@ func createInstance(serviceAccount string) (string, error) {
384
400
glog .V (4 ).Infof ("Compute service GOT instance %v, skipping instance creation" , i .Name )
385
401
}
386
402
387
- if pubkey , ok := os .LookupEnv ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE" ); ok {
388
- glog .V (4 ).Infof ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE set to %v, adding public key to Instance" , pubkey )
389
- // If we're on CI add public SSH keys to the instance
390
- err = addPubKeyToInstance (* project , * zone , i .Name , pubkey )
391
- if err != nil {
392
- return "" , fmt .Errorf ("could not add Jenkins public key %v to instance %v: %v" , pubkey , i .Name , err )
393
- }
394
- }
395
-
396
403
then := time .Now ()
397
404
err = wait .Poll (15 * time .Second , 5 * time .Minute , func () (bool , error ) {
398
405
glog .V (2 ).Infof ("Waiting for instance %v to come up. %v elapsed" , name , time .Since (then ))
@@ -418,7 +425,7 @@ func createInstance(serviceAccount string) (string, error) {
418
425
glog .Warningf ("SSH encountered an error: %v, output: %v" , err , sshOut )
419
426
return false , nil
420
427
}
421
-
428
+ glog . Infof ( "Instance %v in state RUNNING and vailable by SSH" , name )
422
429
return true , nil
423
430
})
424
431
@@ -431,6 +438,31 @@ func createInstance(serviceAccount string) (string, error) {
431
438
return name , nil
432
439
}
433
440
441
+ func generateMetadataWithPublicKey (pubKeyFile string ) (* compute.Metadata , error ) {
442
+ publicKeyByte , err := ioutil .ReadFile (pubKeyFile )
443
+ if err != nil {
444
+ return nil , err
445
+ }
446
+
447
+ publicKey := string (publicKeyByte )
448
+
449
+ // Take username and prepend it to the public key
450
+ tokens := strings .Split (publicKey , " " )
451
+ if len (tokens ) != 3 {
452
+ return nil , fmt .Errorf ("Public key not comprised of 3 parts, instead was: %v" , publicKey )
453
+ }
454
+ publicKey = strings .TrimSpace (tokens [2 ]) + ":" + publicKey
455
+ newMeta := & compute.Metadata {
456
+ Items : []* compute.MetadataItems {
457
+ {
458
+ Key : "ssh-keys" ,
459
+ Value : & publicKey ,
460
+ },
461
+ },
462
+ }
463
+ return newMeta , nil
464
+ }
465
+
434
466
func addPubKeyToInstance (project , zone , name , pubKeyFile string ) error {
435
467
newKeys := ""
436
468
i , err := computeService .Instances .Get (project , zone , name ).Do ()
0 commit comments