@@ -24,6 +24,7 @@ import (
24
24
"math/rand"
25
25
"net/http"
26
26
"os"
27
+ "os/exec"
27
28
"strings"
28
29
"sync"
29
30
"time"
@@ -36,6 +37,7 @@ import (
36
37
37
38
"github.com/golang/glog"
38
39
"golang.org/x/oauth2/google"
40
+ "google.golang.org/api/cloudresourcemanager/v1"
39
41
compute "google.golang.org/api/compute/v0.beta"
40
42
)
41
43
@@ -110,7 +112,9 @@ func main() {
110
112
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." )
111
113
}
112
114
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.
114
118
// Try get a Boskos Project
115
119
glog .Infof ("--project is missing, trying to fetch a project from boskos.\n " +
116
120
"(for local runs please set --project to your dev project)" )
@@ -126,6 +130,8 @@ func main() {
126
130
127
131
* project = p .GetName ()
128
132
133
+ glog .Infof ("Fetched project from Boskos: %v" , * project )
134
+
129
135
go func (c * client.Client , proj string ) {
130
136
for range time .Tick (time .Minute * 5 ) {
131
137
if err := c .UpdateOne (p .Name , "busy" , nil ); err != nil {
@@ -300,7 +306,7 @@ func createInstance(serviceAccount string) (string, error) {
300
306
myuuid := string (uuid .NewUUID ())
301
307
glog .V (2 ).Infof ("Creating instance: %v" , name )
302
308
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"
304
310
i := & compute.Instance {
305
311
Name : name ,
306
312
MachineType : machineType ("" ),
@@ -326,18 +332,49 @@ func createInstance(serviceAccount string) (string, error) {
326
332
},
327
333
}
328
334
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 )
333
345
}
334
- i .ServiceAccounts = []* compute.ServiceAccount {saObj }
346
+
347
+ serviceAccount = strings .TrimSpace (string (out ))
348
+ glog .Infof ("Got service account: %v" , serviceAccount )
349
+
335
350
}
336
351
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
+
338
375
if gotInstance , err := computeService .Instances .Get (* project , * zone , i .Name ).Do (); err != nil {
339
376
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 )
341
378
if err != nil {
342
379
ret := fmt .Sprintf ("could not create instance %s: API error: %v" , name , err )
343
380
if op != nil {
@@ -347,21 +384,37 @@ func createInstance(serviceAccount string) (string, error) {
347
384
} else if op .Error != nil {
348
385
return "" , fmt .Errorf ("could not create instance %s: %+v" , name , op .Error )
349
386
}
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
+
350
401
} 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 )
352
403
}
353
404
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")
361
416
}
362
- } else {
363
- glog .V (4 ).Infof ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE not set, not adding SSH public key to instance" )
364
- }
417
+ */
365
418
366
419
then := time .Now ()
367
420
err = wait .Poll (10 * time .Second , 5 * time .Minute , func () (bool , error ) {
@@ -521,3 +574,7 @@ func testsToGinkgoFocus(tests []string) string {
521
574
}
522
575
return focus + "\" "
523
576
}
577
+
578
+ func opIsDone (op * compute.Operation ) bool {
579
+ return op != nil && op .Status == "DONE"
580
+ }
0 commit comments