@@ -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,8 @@ 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
+ // TODO: Pick a better boot disk image
310
+ imageURL := "projects/ml-images/global/images/family/tf-1-9"
304
311
i := & compute.Instance {
305
312
Name : name ,
306
313
MachineType : machineType ("" ),
@@ -326,18 +333,49 @@ func createInstance(serviceAccount string) (string, error) {
326
333
},
327
334
}
328
335
329
- if serviceAccount != "" {
330
- saObj := & compute.ServiceAccount {
331
- Email : serviceAccount ,
332
- Scopes : []string {"https://www.googleapis.com/auth/cloud-platform" },
336
+ // TODO: OVERWRITE SERVICEACCOUNT FOR TEST PURPOSES ONLY
337
+ if true /* serviceAccount == "" */ {
338
+ // TODO: Check if on CI
339
+ // Assume we are on CI and grab service account from gcloud
340
+ glog .Infof ("Running on CI, getting gcloud account" )
341
+ cmd := exec .Command ("gcloud" , "config" , "get-value" , "account" )
342
+ out , err := cmd .Output ()
343
+
344
+ if err != nil {
345
+ return "" , fmt .Errorf ("Failed to get current gclou account: %v" , err )
333
346
}
334
- i .ServiceAccounts = []* compute.ServiceAccount {saObj }
347
+
348
+ serviceAccount = strings .TrimSpace (string (out ))
349
+ glog .Infof ("Got service account: %v" , serviceAccount )
350
+
335
351
}
336
352
337
- var err error
353
+ c , err := google .DefaultClient (context .TODO (), cloudresourcemanager .CloudPlatformScope )
354
+ if err != nil {
355
+ glog .Fatal (err )
356
+ }
357
+
358
+ cloudresourcemanagerService , err := cloudresourcemanager .New (c )
359
+ if err != nil {
360
+ glog .Fatal (err )
361
+ }
362
+
363
+ resp , err := cloudresourcemanagerService .Projects .Get (* project ).Do ()
364
+ if err != nil {
365
+ glog .Fatal (err )
366
+ }
367
+
368
+ // TODO: Somehow need to get that one default Compute Engine service account
369
+ // [PROJECT_NUMBER][email protected]
370
+ saObj := & compute.ServiceAccount {
371
+ Email :
fmt .
Sprintf (
"%[email protected] " ,
resp .
ProjectNumber ),
372
+ Scopes : []string {"https://www.googleapis.com/auth/cloud-platform" },
373
+ }
374
+ i .ServiceAccounts = []* compute.ServiceAccount {saObj }
375
+
338
376
if gotInstance , err := computeService .Instances .Get (* project , * zone , i .Name ).Do (); err != nil {
339
377
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 )
378
+ glog .Infof ("Inserted instance in project %v, zone %v: %#v" , * project , * zone , i )
341
379
if err != nil {
342
380
ret := fmt .Sprintf ("could not create instance %s: API error: %v" , name , err )
343
381
if op != nil {
@@ -348,7 +386,7 @@ func createInstance(serviceAccount string) (string, error) {
348
386
return "" , fmt .Errorf ("could not create instance %s: %+v" , name , op .Error )
349
387
}
350
388
} else {
351
- glog .V ( 4 ). Infof ("Compute service GOT instance %v, skipping instance creation: %#v" , i .Name , gotInstance )
389
+ glog .Infof ("Compute service GOT instance %v, skipping instance creation: %#v" , i .Name , gotInstance )
352
390
}
353
391
354
392
pubkey , ok := os .LookupEnv ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE" )
@@ -360,7 +398,7 @@ func createInstance(serviceAccount string) (string, error) {
360
398
return "" , fmt .Errorf ("could not add Jenkins public key %v to instance %v: %v" , pubkey , i .Name , err )
361
399
}
362
400
} else {
363
- glog .V ( 4 ). Infof ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE not set, not adding SSH public key to instance" )
401
+ glog .Infof ("JENKINS_GCE_SSH_PUBLIC_KEY_FILE not set, not adding SSH public key to instance" )
364
402
}
365
403
366
404
then := time .Now ()
@@ -521,3 +559,7 @@ func testsToGinkgoFocus(tests []string) string {
521
559
}
522
560
return focus + "\" "
523
561
}
562
+
563
+ func opIsDone (op * compute.Operation ) bool {
564
+ return op != nil && op .Status == "DONE"
565
+ }
0 commit comments