-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
572 lines (512 loc) · 13.9 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package reco
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"time"
"github.com/ReconfigureIO/reco/logger"
"github.com/spf13/viper"
)
const (
// GlobalConfigDirKey is the key for reco configuration in viper.
GlobalConfigDirKey = "reco_global_config_dir"
// ConfigDirKey is the key for reco configuration for current directory.
ConfigDirKey = "reco_config_dir"
// waitInterval is the interval to wait before checking build status updates.
waitInterval = time.Second * 5
platformServerKey = "PLATFORM_SERVER"
platformServerAddress = "https://api.reconfigure.io"
platformAuthFile = "auth.json"
platformProjectFile = "project.json"
StatusWaiting = "WAITING"
JobTypeBuild = "build"
JobTypeDeployment = "deployment"
JobTypeSimulation = "simulation"
JobTypeGraph = "graph"
)
var (
alternativePlatformServer string
errUnsupported = errors.New("That command is not supported by the reconfigure.io platform")
errMissingServer = errors.New("PLATFORM_SERVER config or environment variable not set")
errAuthRequired = errors.New("Authentication required. Run 'reco auth' to authenticate")
errAuthFailed = errors.New("Authentication failed. Run 'reco auth' to try again")
errAuthFailedInvalidToken = errors.New("Authentication failed. The token you entered is invalid")
errAuthFileWriteFailed = errors.New("Authentication failed. Could not write token to disk")
errProjectNotSet = errors.New("Project not set. Run 'reco project set' to set one")
errProjectNotCreated = errors.New("No projects found. Run 'reco project create' to create one")
errProjectNotFound = errors.New("Project not found. Run 'reco project list' to view all your available projects")
errNetworkError = errors.New("Network error")
ErrNotFound = errors.New("Not found")
errInvalidToken = errors.New("The token is invalid")
errUnknownError = errors.New("Unknown error occurred")
errBadResponse = errors.New("Bad response from server")
errUnexpectedTermination = errors.New("Job ended without reaching desired state")
)
// Client is a reconfigure.io platform client.
type Client interface {
// Init initiates the client.
Init() error
// Auth authenticates the user.
Auth(token string) error
// Test handles simulation actions.
Test() Job
// Build handles build actions.
Build() Job
// Deployment handles deployment actions.
Deployment() Job
// Project handles project actions.
Project() ProjectConfig
// Graph handles graph actions.
Graph() Graph
}
var _ Client = &clientImpl{}
// clientImpl is the implementation of reconfigure.io client
type clientImpl struct {
platformServer string
Username string `json:"user_id,omitempty"`
Token string `json:"token,omitempty"`
ProjectID string `json:"project,omitempty"`
noCopy
}
// Make 'go vet' complain when copies of the clientImpl struct are taken.
// There should be exactly one of them during an invocation of reco.
// See https://github.com/golang/go/issues/8005#issuecomment-190753527
type noCopy struct{}
func (*noCopy) Lock() {}
// NewClient creates a new reconfigure.io client.
func NewClient() Client {
return &clientImpl{}
}
func (p *clientImpl) Build() Job {
return &buildJob{p}
}
func (p *clientImpl) Test() Job {
return &testJob{p}
}
func (p *clientImpl) Deployment() Job {
return &deploymentJob{p}
}
func (p *clientImpl) Project() ProjectConfig {
return &platformProject{p}
}
func (p *clientImpl) Graph() Graph {
return &platformGraph{p}
}
func (p *clientImpl) initAuth() {
if p.Username == "" && p.Token == "" {
p.loadAuth()
}
}
func (p *clientImpl) initProject() {
if p.ProjectID == "" {
p.loadProject()
}
}
func (p *clientImpl) authFileName() string {
return filepath.Join(viper.GetString(GlobalConfigDirKey), platformAuthFile)
}
func (p *clientImpl) projectFileName() string {
return filepath.Join(viper.GetString(ConfigDirKey), platformProjectFile)
}
func (p *clientImpl) saveAuth() error {
var prj clientImpl
if p.Username == "" || p.Token == "" {
return nil
}
prj.Username = p.Username
prj.Token = p.Token
authFile, err := os.OpenFile(p.authFileName(), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}
defer authFile.Close()
return json.NewEncoder(authFile).Encode(&prj)
}
func (p *clientImpl) saveProject() error {
var prj clientImpl
if p.ProjectID == "" {
return nil
}
prj.ProjectID = p.ProjectID
projectFile, err := os.OpenFile(p.projectFileName(), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}
defer projectFile.Close()
return json.NewEncoder(projectFile).Encode(&prj)
}
func (p *clientImpl) loadAuth() error {
f, err := os.Open(p.authFileName())
if err != nil {
return err
}
defer f.Close()
return json.NewDecoder(f).Decode(p)
}
func (p *clientImpl) loadProject() error {
f, err := os.Open(p.projectFileName())
if err != nil {
return err
}
defer f.Close()
return json.NewDecoder(f).Decode(p)
}
func (p *clientImpl) Init() error {
// is runtime env var set? Was build time env var set?
server := viper.GetString(platformServerKey)
if server == "" {
if alternativePlatformServer == "" {
server = platformServerAddress
} else {
server = alternativePlatformServer
fmt.Println("Using alternative platform server: ", server)
}
}
u, err := url.Parse(server)
if err != nil {
return err
}
if u.Scheme == "" {
u.Scheme = "http"
}
p.platformServer = u.String()
p.initAuth()
p.initProject()
return nil
}
func (p *clientImpl) Auth(token string) error {
authURL := fmt.Sprint("http://app.reconfigure.io/dashboard")
if token == "" {
fmt.Println("Visit your dashboard and copy your API key:", authURL)
fmt.Print("Enter your API key here: ")
if _, err := fmt.Scanln(&token); err != nil {
return err
}
}
str := strings.Split(token, "_")
if len(str) != 3 {
return errInvalidToken
}
p.Username = str[0] + "_" + str[1]
p.Token = str[2]
//test the token using an API call
req := p.apiRequest(endpoints.users.String())
_, err := req.Do("GET", nil)
if err != nil {
return err
} else {
err = p.saveAuth()
if err != nil {
return errAuthFileWriteFailed
}
logger.Info.Println("Authentication successful")
}
return nil
}
func jsonToReader(j interface{}) (io.Reader, error) {
var b bytes.Buffer
err := json.NewEncoder(&b).Encode(j)
return &b, err
}
func decodeJSON(r io.Reader, body interface{}) error {
if err := json.NewDecoder(r).Decode(body); err != nil {
return errBadResponse
}
// Check if decode struct has Error field.
// Return error if field is present and not empty.
if reflect.ValueOf(body).Kind() != reflect.Ptr {
return nil
}
errField := reflect.ValueOf(body).Elem().FieldByName("Error")
if e, ok := errField.Interface().(string); ok && e != "" {
return errors.New(e)
}
return nil
}
func (p *clientImpl) logJob(eventType string, id string) error {
return p.logs(eventType, id)
}
func (p *clientImpl) getStatus(jobType string, id string) string {
job, err := p.getJob(jobType, id)
if err == nil && job.Status != "" {
return job.Status
}
return StatusErrored
}
func (p *clientImpl) waitForStatus(jobType string, id string, targetStatus string) error {
status := StatusSubmitted
prevStatus := ""
for status != targetStatus {
status = strings.ToUpper(p.getStatus(jobType, id))
if status != prevStatus {
logger.Info.Println("status: ", status)
prevStatus = status
switch jobType {
case "deployment":
switch status {
case StatusQueued:
logger.Info.Println("Waiting for FPGA instance to be available")
case StatusSubmitted:
logger.Info.Println("Waiting for request to be queued")
default:
}
case "build":
switch status {
case StatusQueued:
logger.Info.Println("Waiting for job to start")
default:
}
case "simulation":
switch status {
case StatusQueued:
logger.Info.Println("Waiting for job to start")
default:
}
default:
}
}
if isCompleted(status) {
return errUnexpectedTermination
}
time.Sleep(10 * time.Second)
}
return nil
}
func (p *clientImpl) waitAndLog(jobType string, id string) error {
err := p.waitForStatus(jobType, id, StatusStarted)
if err != nil {
return err
}
return p.logJob(jobType, id)
}
func (p *clientImpl) logs(jobType string, id string) error {
_, err := p.waitForLog(jobType, id, false)
return err
}
func (p *clientImpl) getJob(jobType string, id string) (jobInfo, error) {
var apiResp struct {
Job jobInfo `json:"value"`
}
var endpoint string
switch jobType {
case JobTypeSimulation:
endpoint = endpoints.simulations.Item()
case JobTypeDeployment:
endpoint = endpoints.deployments.Item()
default:
endpoint = endpoints.builds.Item()
}
req := p.apiRequest(endpoint)
req.param("id", id)
resp, err := req.Do("GET", nil)
if err != nil {
return apiResp.Job, err
}
err = json.NewDecoder(resp.Body).Decode(&apiResp)
return apiResp.Job, err
}
// waitForLog attempts to stream logs. If peek is true, it ensures log streaming has started
// and returns the body for the caller to read remaining contents.
// Otherwise, logs are streamed to stderr.
func (p *clientImpl) waitForLog(jobType, id string, peek bool) (io.ReadCloser, error) {
var endpoint string
switch jobType {
case JobTypeSimulation:
endpoint = endpoints.simulations.Log()
case JobTypeDeployment:
endpoint = endpoints.deployments.Log()
default:
endpoint = endpoints.builds.Log()
}
req := p.apiRequest(endpoint)
req.param("id", id)
resp, err := req.Do("GET", nil)
if err != nil {
return nil, err
}
if peek {
// just verify that the server is streaming response
// and pass over the remaining body
var buf = make([]byte, 1)
var err error
for {
_, err = resp.Body.Read(buf)
if err != nil {
break
}
if buf[0] != 0 {
os.Stderr.Write(buf)
break
}
}
return resp.Body, err
} else {
defer resp.Body.Close()
_, err = io.Copy(os.Stderr, resp.Body)
return nil, err
}
}
func (p *clientImpl) uploadJob(jobType string, id string, srcArchive string) error {
var endpoint string
switch jobType {
case JobTypeSimulation:
endpoint = endpoints.simulations.Input()
case JobTypeGraph:
endpoint = endpoints.graphs.Input()
default:
endpoint = endpoints.builds.Input()
}
req := p.apiRequest(endpoint)
req.param("id", id)
req.jsonBody = false
f, err := os.Open(srcArchive)
if err != nil {
return err
}
resp, err := req.Do("PUT", f)
var respJSON struct {
Value apiResponse `json:"value"`
Error string `json:"error"`
}
decodeJSON(resp.Body, &respJSON)
if resp.StatusCode > 299 || len(respJSON.Value.Job.Events) == 0 {
return errors.New("unknown error occured")
}
return err
}
func (p *clientImpl) apiRequest(endpoint string) clientRequest {
return clientRequest{
endpoint: p.platformServer + endpoint,
username: p.Username,
password: p.Token,
jsonBody: true,
}
}
func (p *clientImpl) projectID() (string, error) {
if p.Username == "" || p.Token == "" {
return "", errAuthRequired
}
var prjs []ProjectInfo
prjs, err := p.Project().list()
switch err {
case errAuthFailed, errNetworkError:
return "", err
}
// check if project flag is set
if prjName := viper.GetString("project"); prjName != "" {
// extract project ID
for _, prj := range prjs {
if prj.Name == prjName {
return prj.ID, nil
}
}
return "", errProjectNotFound
}
if p.ProjectID == "" {
if len(prjs) == 0 {
return "", errProjectNotCreated
}
return "", errProjectNotSet
}
// return configured project
return p.ProjectID, nil
}
func (p *clientImpl) listJobs(jobType string, filters M) ([]jobInfo, error) {
limit := filters.Int("limit")
var endpoint string
switch jobType {
case JobTypeDeployment:
endpoint = endpoints.deployments.String()
case JobTypeSimulation, "test":
endpoint = endpoints.simulations.String()
case JobTypeGraph:
endpoint = endpoints.graphs.String()
default:
endpoint = endpoints.builds.String()
}
request := p.apiRequest(endpoint)
// if all-projects flag is not set,
// and public flag not set, use specific project.
if !filters.Bool("all") && !filters.Bool("public") {
projectID, err := p.projectID()
if err != nil {
return nil, err
}
request.queryParam("project", projectID)
}
if filters.Bool("public") {
request.queryParam("public", "true")
}
resp, err := request.Do("GET", nil)
if err != nil {
return nil, err
}
var respJSON struct {
Jobs []jobInfo `json:"value"`
Error string `json:"error"`
}
if err := decodeJSON(resp.Body, &respJSON); err != nil {
return nil, err
}
// handle status filter
status := filters.String("status")
if status != "" {
respJSON.Jobs = jobFilter(respJSON.Jobs).Filter("status", status)
}
sort.Sort(jobSorter(respJSON.Jobs))
if limit > 0 && limit < len(respJSON.Jobs) {
respJSON.Jobs = respJSON.Jobs[:limit]
}
return respJSON.Jobs, err
}
func (p *clientImpl) listBuilds(filters M) ([]jobInfo, error) {
return p.listJobs(JobTypeBuild, filters)
}
func (p *clientImpl) listDeployments(filters M) ([]jobInfo, error) {
return p.listJobs(JobTypeDeployment, filters)
}
func (p *clientImpl) listTests(filters M) ([]jobInfo, error) {
return p.listJobs(JobTypeSimulation, filters)
}
func (p *clientImpl) listGraphs(filters M) ([]jobInfo, error) {
return p.listJobs(JobTypeGraph, filters)
}
func (p *clientImpl) stopJob(eventType string, id string) error {
var endpoint string
switch eventType {
case JobTypeSimulation:
endpoint = endpoints.simulations.Events()
case JobTypeDeployment:
endpoint = endpoints.deployments.Events()
default:
endpoint = endpoints.builds.Events()
}
req := p.apiRequest(endpoint)
req.param("id", id)
reqBody := M{"status": StatusTerminating}
resp, err := req.Do("POST", reqBody)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errUnknownError
}
return nil
}
func inSlice(slice []string, val string) bool {
for _, v := range slice {
if val == v {
return true
}
}
return false
}