Skip to content

Commit 33fc4fc

Browse files
committed
Testing commit
1 parent bf8f83f commit 33fc4fc

File tree

4 files changed

+9947
-21
lines changed

4 files changed

+9947
-21
lines changed

Gopkg.lock

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/remote/run_remote/run_remote.go

+77-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"math/rand"
2525
"net/http"
2626
"os"
27+
"os/exec"
2728
"strings"
2829
"sync"
2930
"time"
@@ -36,6 +37,7 @@ import (
3637

3738
"github.com/golang/glog"
3839
"golang.org/x/oauth2/google"
40+
"google.golang.org/api/cloudresourcemanager/v1"
3941
compute "google.golang.org/api/compute/v0.beta"
4042
)
4143

@@ -110,7 +112,9 @@ func main() {
110112
glog.Fatal("You must specify a service account to create an instance under that has at least OWNERS permissions on disks and READER on instances.")
111113
}
112114

113-
if *project == "" {
115+
// TODO: OVERWRITE PROJECT FOR TEST PURPOSES ONLY
116+
if true /* *project == "" */ {
117+
// TODO: Check if we're on CI and just overwrite project anyway if we are.
114118
// Try get a Boskos Project
115119
glog.Infof("--project is missing, trying to fetch a project from boskos.\n" +
116120
"(for local runs please set --project to your dev project)")
@@ -126,6 +130,8 @@ func main() {
126130

127131
*project = p.GetName()
128132

133+
glog.Infof("Fetched project from Boskos: %v", *project)
134+
129135
go func(c *client.Client, proj string) {
130136
for range time.Tick(time.Minute * 5) {
131137
if err := c.UpdateOne(p.Name, "busy", nil); err != nil {
@@ -300,7 +306,7 @@ func createInstance(serviceAccount string) (string, error) {
300306
myuuid := string(uuid.NewUUID())
301307
glog.V(2).Infof("Creating instance: %v", name)
302308

303-
imageURL := "https://www.googleapis.com/compute/v1/projects/ml-images/global/images/debian-9-tf-1-9-v20180626"
309+
imageURL := "projects/ml-images/global/images/family/tf-1-9"
304310
i := &compute.Instance{
305311
Name: name,
306312
MachineType: machineType(""),
@@ -326,18 +332,49 @@ func createInstance(serviceAccount string) (string, error) {
326332
},
327333
}
328334

329-
if serviceAccount != "" {
330-
saObj := &compute.ServiceAccount{
331-
Email: serviceAccount,
332-
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
335+
// TODO: OVERWRITE SERVICEACCOUNT FOR TEST PURPOSES ONLY
336+
if true /* serviceAccount == "" */ {
337+
// TODO: Check if on CI
338+
// Assume we are on CI and grab service account from gcloud
339+
glog.Infof("Running on CI, getting gcloud account")
340+
cmd := exec.Command("gcloud", "config", "get-value", "account")
341+
out, err := cmd.Output()
342+
343+
if err != nil {
344+
return "", fmt.Errorf("Failed to get current gclou account: %v", err)
333345
}
334-
i.ServiceAccounts = []*compute.ServiceAccount{saObj}
346+
347+
serviceAccount = strings.TrimSpace(string(out))
348+
glog.Infof("Got service account: %v", serviceAccount)
349+
335350
}
336351

337-
var err error
352+
c, err := google.DefaultClient(context.TODO(), cloudresourcemanager.CloudPlatformScope)
353+
if err != nil {
354+
glog.Fatal(err)
355+
}
356+
357+
cloudresourcemanagerService, err := cloudresourcemanager.New(c)
358+
if err != nil {
359+
glog.Fatal(err)
360+
}
361+
362+
resp, err := cloudresourcemanagerService.Projects.Get(*project).Do()
363+
if err != nil {
364+
glog.Fatal(err)
365+
}
366+
367+
// TODO: Somehow need to get that one default Compute Engine service account
368+
// [PROJECT_NUMBER][email protected]
369+
saObj := &compute.ServiceAccount{
370+
Email: fmt.Sprintf("%[email protected]", resp.ProjectNumber),
371+
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
372+
}
373+
i.ServiceAccounts = []*compute.ServiceAccount{saObj}
374+
338375
if gotInstance, err := computeService.Instances.Get(*project, *zone, i.Name).Do(); err != nil {
339376
op, err := computeService.Instances.Insert(*project, *zone, i).Do()
340-
glog.V(4).Infof("Inserted instance in project %v, zone %v: %#v", *project, *zone, i)
377+
glog.Infof("Inserted instance in project %v, zone %v: %#v", *project, *zone, i)
341378
if err != nil {
342379
ret := fmt.Sprintf("could not create instance %s: API error: %v", name, err)
343380
if op != nil {
@@ -347,21 +384,37 @@ func createInstance(serviceAccount string) (string, error) {
347384
} else if op.Error != nil {
348385
return "", fmt.Errorf("could not create instance %s: %+v", name, op.Error)
349386
}
387+
388+
// WAIT FOR OP
389+
wait.Poll(3*time.Second, 5*time.Minute, func() (bool, error) {
390+
pollOp, err := computeService.ZoneOperations.Get(*project, *zone, op.Name).Do()
391+
if err != nil {
392+
glog.Errorf("WaitForOp(op: %#v, zone: %#v) failed to poll the operation: %v", op, zone, err)
393+
return false, err
394+
}
395+
glog.Infof("Insert Op data: %#v", pollOp)
396+
glog.Infof("Op Error: %v", pollOp.Error)
397+
done := opIsDone(pollOp)
398+
return done, err
399+
})
400+
350401
} else {
351-
glog.V(4).Infof("Compute service GOT instance %v, skipping instance creation: %#v", i.Name, gotInstance)
402+
glog.Infof("Compute service GOT instance %v, skipping instance creation: %#v", i.Name, gotInstance)
352403
}
353404

354-
pubkey, ok := os.LookupEnv("JENKINS_GCE_SSH_PUBLIC_KEY_FILE")
355-
if ok {
356-
glog.Infof("Running on Jenkins and JENKINS_GCE_SSH_PUBLIC_KEY_FILE set")
357-
// If we're on CI add public SSH keys to the instance
358-
err = addPubKeyToInstance(*project, *zone, i.Name, pubkey)
359-
if err != nil {
360-
return "", fmt.Errorf("could not add Jenkins public key %v to instance %v: %v", pubkey, i.Name, err)
405+
/*
406+
pubkey, ok := os.LookupEnv("JENKINS_GCE_SSH_PUBLIC_KEY_FILE")
407+
if ok {
408+
glog.Infof("Running on Jenkins and JENKINS_GCE_SSH_PUBLIC_KEY_FILE set")
409+
// If we're on CI add public SSH keys to the instance
410+
err = addPubKeyToInstance(*project, *zone, i.Name, pubkey)
411+
if err != nil {
412+
return "", fmt.Errorf("could not add Jenkins public key %v to instance %v: %v", pubkey, i.Name, err)
413+
}
414+
} else {
415+
glog.Infof("JENKINS_GCE_SSH_PUBLIC_KEY_FILE not set, not adding SSH public key to instance")
361416
}
362-
} else {
363-
glog.V(4).Infof("JENKINS_GCE_SSH_PUBLIC_KEY_FILE not set, not adding SSH public key to instance")
364-
}
417+
*/
365418

366419
then := time.Now()
367420
err = wait.Poll(10*time.Second, 5*time.Minute, func() (bool, error) {
@@ -521,3 +574,7 @@ func testsToGinkgoFocus(tests []string) string {
521574
}
522575
return focus + "\""
523576
}
577+
578+
func opIsDone(op *compute.Operation) bool {
579+
return op != nil && op.Status == "DONE"
580+
}

0 commit comments

Comments
 (0)