@@ -78,7 +78,8 @@ func init() {
7878}
7979
8080const (
81- defaultMachine = "n1-standard-1"
81+ defaultMachine = "n1-standard-1"
82+ defaultFirewallRule = "default-allow-ssh"
8283)
8384
8485var (
@@ -327,12 +328,42 @@ func test(tests []string) *TestResult {
327328 return result
328329}
329330
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+
330355// Provision a gce instance using image
331356func createInstance (serviceAccount string ) (string , error ) {
332357 var err error
333358
334359 name := "gce-pd-csi-e2e"
335360 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+
336367 glog .V (4 ).Infof ("Creating instance: %v" , name )
337368
338369 // TODO: Pick a better boot disk image
@@ -368,6 +399,15 @@ func createInstance(serviceAccount string) (string, error) {
368399 }
369400 i .ServiceAccounts = []* compute.ServiceAccount {saObj }
370401
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+
371411 if _ , err := computeService .Instances .Get (* project , * zone , i .Name ).Do (); err != nil {
372412 op , err := computeService .Instances .Insert (* project , * zone , i ).Do ()
373413 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) {
384424 glog .V (4 ).Infof ("Compute service GOT instance %v, skipping instance creation" , i .Name )
385425 }
386426
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-
396427 then := time .Now ()
397428 err = wait .Poll (15 * time .Second , 5 * time .Minute , func () (bool , error ) {
398429 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) {
418449 glog .Warningf ("SSH encountered an error: %v, output: %v" , err , sshOut )
419450 return false , nil
420451 }
421-
452+ glog . Infof ( "Instance %v in state RUNNING and vailable by SSH" , name )
422453 return true , nil
423454 })
424455
@@ -431,52 +462,29 @@ func createInstance(serviceAccount string) (string, error) {
431462 return name , nil
432463}
433464
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 ) {
449466 publicKeyByte , err := ioutil .ReadFile (pubKeyFile )
450467 if err != nil {
451- return err
468+ return nil , err
452469 }
453470
454471 publicKey := string (publicKeyByte )
455472
456473 // Take username and prepend it to the public key
457474 tokens := strings .Split (publicKey , " " )
458475 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 )
460477 }
461478 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 )
465479 newMeta := & compute.Metadata {
466- Fingerprint : fingerprint ,
467480 Items : []* compute.MetadataItems {
468481 {
469482 Key : "ssh-keys" ,
470- Value : & newKeys ,
483+ Value : & publicKey ,
471484 },
472485 },
473486 }
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
480488}
481489
482490func getexternalIP (instance * compute.Instance ) string {
0 commit comments