Skip to content

Commit 1d4d6fb

Browse files
committed
Add metadata with public ssh key at instance creation time
1 parent 6dcdaaf commit 1d4d6fb

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

test/remote/run_remote/run_remote.go

+42-9
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,22 @@ func createInstance(serviceAccount string) (string, error) {
368368
}
369369
i.ServiceAccounts = []*compute.ServiceAccount{saObj}
370370

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+
371387
if _, err := computeService.Instances.Get(*project, *zone, i.Name).Do(); err != nil {
372388
op, err := computeService.Instances.Insert(*project, *zone, i).Do()
373389
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) {
384400
glog.V(4).Infof("Compute service GOT instance %v, skipping instance creation", i.Name)
385401
}
386402

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-
396403
then := time.Now()
397404
err = wait.Poll(15*time.Second, 5*time.Minute, func() (bool, error) {
398405
glog.V(2).Infof("Waiting for instance %v to come up. %v elapsed", name, time.Since(then))
@@ -431,6 +438,32 @@ func createInstance(serviceAccount string) (string, error) {
431438
return name, nil
432439
}
433440

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+
glog.V(4).Infof("New ssh-keys for instance %v: %v", name, newKeys)
456+
newMeta := &compute.Metadata{
457+
Items: []*compute.MetadataItems{
458+
{
459+
Key: "ssh-keys",
460+
Value: &publicKey,
461+
},
462+
},
463+
}
464+
return newMeta, nil
465+
}
466+
434467
func addPubKeyToInstance(project, zone, name, pubKeyFile string) error {
435468
newKeys := ""
436469
i, err := computeService.Instances.Get(project, zone, name).Do()

0 commit comments

Comments
 (0)