@@ -78,7 +78,8 @@ func init() {
78
78
}
79
79
80
80
const (
81
- defaultMachine = "n1-standard-1"
81
+ defaultMachine = "n1-standard-1"
82
+ defaultFirewallRule = "default-allow-ssh"
82
83
)
83
84
84
85
var (
@@ -327,12 +328,42 @@ func test(tests []string) *TestResult {
327
328
return result
328
329
}
329
330
331
+ // Create default SSH filewall rule if it does not exist
332
+ func createDefaultFirewallRule () error {
333
+ var err error
334
+ if _ , err = computeService .Firewalls .Get (* project , defaultFirewallRule ).Do (); err != nil {
335
+ glog .Infof ("Default firewall rule %v does not exist, creating" , defaultFirewallRule )
336
+ f := & compute.Firewall {
337
+ Name : defaultFirewallRule ,
338
+ Allowed : []* compute.FirewallAllowed {
339
+ {
340
+ IPProtocol : "tcp" ,
341
+ Ports : []string {"22" },
342
+ },
343
+ },
344
+ }
345
+ _ , err = computeService .Firewalls .Insert (* project , f ).Do ()
346
+ if err != nil {
347
+ return fmt .Errorf ("Failed to insert required default SSH firewall Rule %v: %v" , defaultFirewallRule , err )
348
+ }
349
+ } else {
350
+ glog .Infof ("Default firewall rule %v already exists, skipping creation" , defaultFirewallRule )
351
+ }
352
+ return nil
353
+ }
354
+
330
355
// Provision a gce instance using image
331
356
func createInstance (serviceAccount string ) (string , error ) {
332
357
var err error
333
358
334
359
name := "gce-pd-csi-e2e"
335
360
myuuid := string (uuid .NewUUID ())
361
+
362
+ err = createDefaultFirewallRule ()
363
+ if err != nil {
364
+ return "" , fmt .Errorf ("Failed to create firewall rule: %v" , err )
365
+ }
366
+
336
367
glog .V (4 ).Infof ("Creating instance: %v" , name )
337
368
338
369
// TODO: Pick a better boot disk image
@@ -368,6 +399,15 @@ func createInstance(serviceAccount string) (string, error) {
368
399
}
369
400
i .ServiceAccounts = []* compute.ServiceAccount {saObj }
370
401
402
+ if pubkey , ok := os .LookupEnv ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE" ); ok {
403
+ glog .V (4 ).Infof ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE set to %v, adding public key to Instance" , pubkey )
404
+ meta , err := generateMetadataWithPublicKey (pubkey )
405
+ if err != nil {
406
+ return "" , err
407
+ }
408
+ i .Metadata = meta
409
+ }
410
+
371
411
if _ , err := computeService .Instances .Get (* project , * zone , i .Name ).Do (); err != nil {
372
412
op , err := computeService .Instances .Insert (* project , * zone , i ).Do ()
373
413
glog .V (4 ).Infof ("Inserted instance %v in project %v, zone %v" , i .Name , * project , * zone )
@@ -384,15 +424,6 @@ func createInstance(serviceAccount string) (string, error) {
384
424
glog .V (4 ).Infof ("Compute service GOT instance %v, skipping instance creation" , i .Name )
385
425
}
386
426
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
427
then := time .Now ()
397
428
err = wait .Poll (15 * time .Second , 5 * time .Minute , func () (bool , error ) {
398
429
glog .V (2 ).Infof ("Waiting for instance %v to come up. %v elapsed" , name , time .Since (then ))
@@ -418,7 +449,7 @@ func createInstance(serviceAccount string) (string, error) {
418
449
glog .Warningf ("SSH encountered an error: %v, output: %v" , err , sshOut )
419
450
return false , nil
420
451
}
421
-
452
+ glog . Infof ( "Instance %v in state RUNNING and vailable by SSH" , name )
422
453
return true , nil
423
454
})
424
455
@@ -431,52 +462,29 @@ func createInstance(serviceAccount string) (string, error) {
431
462
return name , nil
432
463
}
433
464
434
- func addPubKeyToInstance (project , zone , name , pubKeyFile string ) error {
435
- newKeys := ""
436
- i , err := computeService .Instances .Get (project , zone , name ).Do ()
437
- if err != nil {
438
- return err
439
- }
440
- fingerprint := i .Metadata .Fingerprint
441
- items := i .Metadata .Items
442
- for _ , item := range items {
443
- if item .Key == "ssh-keys" {
444
- glog .V (2 ).Infof ("Found existing ssh-keys, prepending to new key string" )
445
- newKeys += * item .Value
446
- break
447
- }
448
- }
465
+ func generateMetadataWithPublicKey (pubKeyFile string ) (* compute.Metadata , error ) {
449
466
publicKeyByte , err := ioutil .ReadFile (pubKeyFile )
450
467
if err != nil {
451
- return err
468
+ return nil , err
452
469
}
453
470
454
471
publicKey := string (publicKeyByte )
455
472
456
473
// Take username and prepend it to the public key
457
474
tokens := strings .Split (publicKey , " " )
458
475
if len (tokens ) != 3 {
459
- return fmt .Errorf ("Public key not comprised of 3 parts, instead was: %v" , publicKey )
476
+ return nil , fmt .Errorf ("Public key not comprised of 3 parts, instead was: %v" , publicKey )
460
477
}
461
478
publicKey = strings .TrimSpace (tokens [2 ]) + ":" + publicKey
462
-
463
- newKeys = newKeys + publicKey
464
- glog .V (4 ).Infof ("New ssh-keys for instance %v: %v" , name , newKeys )
465
479
newMeta := & compute.Metadata {
466
- Fingerprint : fingerprint ,
467
480
Items : []* compute.MetadataItems {
468
481
{
469
482
Key : "ssh-keys" ,
470
- Value : & newKeys ,
483
+ Value : & publicKey ,
471
484
},
472
485
},
473
486
}
474
- _ , err = computeService .Instances .SetMetadata (project , zone , name , newMeta ).Do ()
475
- if err != nil {
476
- return err
477
- }
478
- return nil
479
-
487
+ return newMeta , nil
480
488
}
481
489
482
490
func getexternalIP (instance * compute.Instance ) string {
0 commit comments