forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.go
737 lines (671 loc) · 22.4 KB
/
cmds.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
package cmds
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/user"
"sort"
"strings"
"text/tabwriter"
"github.com/fsouza/go-dockerclient"
"github.com/gogo/protobuf/jsonpb"
pach "github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/pkg/uuid"
ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
"github.com/pachyderm/pachyderm/src/server/pkg/cmdutil"
"github.com/pachyderm/pachyderm/src/server/pps/example"
"github.com/pachyderm/pachyderm/src/server/pps/pretty"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
codestart = "```sh"
codeend = "```"
)
// ByCreationTime is an implementation of sort.Interface which
// sorts pps job info by creation time, ascending.
type ByCreationTime []*ppsclient.JobInfo
func (arr ByCreationTime) Len() int { return len(arr) }
func (arr ByCreationTime) Swap(i, j int) { arr[i], arr[j] = arr[j], arr[i] }
func (arr ByCreationTime) Less(i, j int) bool {
if arr[i].Started == nil || arr[j].Started == nil {
return false
}
if arr[i].Started.Seconds < arr[j].Started.Seconds {
return true
} else if arr[i].Started.Seconds == arr[j].Started.Seconds {
return arr[i].Started.Nanos < arr[j].Started.Nanos
}
return false
}
// pipelineManifestReader helps with unmarshalling pipeline configs from JSON. It's used by
// create-pipeline and update-pipeline
type pipelineManifestReader struct {
buf bytes.Buffer
decoder *json.Decoder
}
func newPipelineManifestReader(path string) (result *pipelineManifestReader, retErr error) {
result = new(pipelineManifestReader)
var pipelineReader io.Reader
if path == "-" {
pipelineReader = io.TeeReader(os.Stdin, &result.buf)
fmt.Print("Reading from stdin.\n")
} else if url, err := url.Parse(path); err == nil && url.Scheme != "" {
resp, err := http.Get(url.String())
if err != nil {
return nil, sanitizeErr(err)
}
defer func() {
if err := resp.Body.Close(); err != nil && retErr == nil {
retErr = sanitizeErr(err)
}
}()
rawBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
pipelineReader = io.TeeReader(strings.NewReader(string(rawBytes)), &result.buf)
} else {
rawBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
pipelineReader = io.TeeReader(strings.NewReader(string(rawBytes)), &result.buf)
}
result.decoder = json.NewDecoder(pipelineReader)
return result, nil
}
func (r *pipelineManifestReader) nextCreatePipelineRequest() (*ppsclient.CreatePipelineRequest, error) {
var result ppsclient.CreatePipelineRequest
if err := jsonpb.UnmarshalNext(r.decoder, &result); err != nil {
return nil, err
}
return &result, nil
}
// Cmds returns a slice containing pps commands.
func Cmds(address string, noMetrics *bool) ([]*cobra.Command, error) {
metrics := !*noMetrics
marshaller := &jsonpb.Marshaler{Indent: " "}
job := &cobra.Command{
Use: "job",
Short: "Docs for jobs.",
Long: `Jobs are the basic unit of computation in Pachyderm.
Jobs run a containerized workload over a set of finished input commits.
Creating a job will also create a new repo and a commit in that repo which
contains the output of the job. Unless the job is created with another job as a
parent. If the job is created with a parent it will use the same repo as its
parent job and the commit it creates will use the parent job's commit as a
parent.
If the job fails the commit it creates will not be finished.
The increase the throughput of a job increase the Shard paremeter.
`,
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
return nil
}),
}
exampleCreateJobRequest, err := marshaller.MarshalToString(example.CreateJobRequest)
if err != nil {
return nil, err
}
exampleRunPipelineSpec, err := marshaller.MarshalToString(example.RunPipelineSpec)
if err != nil {
return nil, err
}
pipelineSpec := "[Pipeline Specification](../reference/pipeline_spec.html)"
var jobPath string
var pushImages bool
var registry string
var username string
var password string
createJob := &cobra.Command{
Use: "create-job -f job.json",
Short: "Create a new job. Returns the id of the created job.",
Long: fmt.Sprintf("Create a new job from a spec, the spec looks like this\n%s", exampleCreateJobRequest),
Run: cmdutil.RunFixedArgs(0, func(args []string) (retErr error) {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
var buf bytes.Buffer
var jobReader io.Reader
if jobPath == "-" {
jobReader = io.TeeReader(os.Stdin, &buf)
fmt.Print("Reading from stdin.\n")
} else if url, err := url.Parse(jobPath); err == nil && url.Scheme != "" {
resp, err := http.Get(url.String())
if err != nil {
return sanitizeErr(err)
}
defer func() {
if err := resp.Body.Close(); err != nil && retErr == nil {
retErr = sanitizeErr(err)
}
}()
jobReader = resp.Body
} else {
jobFile, err := os.Open(jobPath)
if err != nil {
return sanitizeErr(err)
}
defer func() {
if err := jobFile.Close(); err != nil && retErr == nil {
retErr = sanitizeErr(err)
}
}()
jobReader = io.TeeReader(jobFile, &buf)
}
var request ppsclient.CreateJobRequest
decoder := json.NewDecoder(jobReader)
if err := jsonpb.UnmarshalNext(decoder, &request); err != nil {
return sanitizeErr(err)
}
if pushImages {
pushedImage, err := pushImage(registry, username, password, request.Transform.Image)
if err != nil {
return err
}
request.Transform.Image = pushedImage
}
job, err := client.PpsAPIClient.CreateJob(
context.Background(),
&request,
)
if err != nil {
return sanitizeErr(err)
}
fmt.Println(job.ID)
return nil
}),
}
createJob.Flags().StringVarP(&jobPath, "file", "f", "-", "The file containing the job, it can be a url or local file. - reads from stdin.")
createJob.Flags().BoolVarP(&pushImages, "push-images", "p", false, "If true, push local docker images into the cluster registry.")
createJob.Flags().StringVarP(®istry, "registry", "r", "docker.io", "The registry to push images to.")
createJob.Flags().StringVarP(&username, "username", "u", "", "The username to push images as, defaults to your OS username.")
createJob.Flags().StringVarP(&password, "password", "", "", "Your password for the registry being pushed to.")
var block bool
inspectJob := &cobra.Command{
Use: "inspect-job job-id",
Short: "Return info about a job.",
Long: "Return info about a job.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
jobInfo, err := client.InspectJob(args[0], block)
if err != nil {
cmdutil.ErrorAndExit("error from InspectJob: %s", err.Error())
}
if jobInfo == nil {
cmdutil.ErrorAndExit("job %s not found.", args[0])
}
return pretty.PrintDetailedJobInfo(jobInfo)
}),
}
inspectJob.Flags().BoolVarP(&block, "block", "b", false, "block until the job has either succeeded or failed")
var pipelineName string
listJob := &cobra.Command{
Use: "list-job [-p pipeline-name] [commits]",
Short: "Return info about jobs.",
Long: `Return info about jobs.
Examples:
` + codestart + `# return all jobs
$ pachctl list-job
# return all jobs in pipeline foo
$ pachctl list-job -p foo
# return all jobs whose input commits include foo/XXX and bar/YYY
$ pachctl list-job foo/XXX bar/YYY
# return all jobs in pipeline foo and whose input commits include bar/YYY
$ pachctl list-job -p foo bar/YYY
` + codeend,
Run: func(cmd *cobra.Command, args []string) {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
cmdutil.ErrorAndExit("error from InspectJob: %v", sanitizeErr(err))
}
commits, err := cmdutil.ParseCommits(args)
if err != nil {
cmd.Usage()
cmdutil.ErrorAndExit("error from InspectJob: %v", sanitizeErr(err))
}
jobInfos, err := client.ListJob(pipelineName, commits)
if err != nil {
cmdutil.ErrorAndExit("error from InspectJob: %v", sanitizeErr(err))
}
// Display newest jobs first
sort.Sort(sort.Reverse(ByCreationTime(jobInfos)))
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
pretty.PrintJobHeader(writer)
for _, jobInfo := range jobInfos {
pretty.PrintJobInfo(writer, jobInfo)
}
if err := writer.Flush(); err != nil {
cmdutil.ErrorAndExit("error from InspectJob: %v", sanitizeErr(err))
}
},
}
listJob.Flags().StringVarP(&pipelineName, "pipeline", "p", "", "Limit to jobs made by pipeline.")
deleteJob := &cobra.Command{
Use: "delete-job job-id",
Short: "Delete a job.",
Long: "Delete a job.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
if err := client.DeleteJob(args[0]); err != nil {
cmdutil.ErrorAndExit("error from DeleteJob: %s", err.Error())
}
return nil
}),
}
var (
jobID string
commaInputs string // comma-separated list of input files of interest
raw bool
)
getLogs := &cobra.Command{
Use: "get-logs [--pipeline=<pipeline>|--job=<job id>]",
Short: "Return logs from a job.",
Long: `Return logs from a job.
Examples:
` + codestart + `# return logs emitted by recent jobs in the "filter" pipeline
$ pachctl get-logs --pipeline=filter
# return logs emitted by the job aedfa12aedf
$ pachctl get-logs --job=aedfa12aedf
# return logs emitted by the pipeline \"filter\" while processing /apple.txt and a file with the hash 123aef
$ pachctl get-logs --pipeline=filter --inputs=/apple.txt,123aef
` + codeend,
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return fmt.Errorf("error from GetLogs: %v", sanitizeErr(err))
}
// Validate flags
if len(jobID) == 0 && len(pipelineName) == 0 {
return fmt.Errorf("must set either --pipeline or --job (or both)")
}
// Break up comma-separated input paths, and filter out empty entries
data := strings.Split(commaInputs, ",")
for i := 0; i < len(data); {
if len(data[i]) == 0 {
if i+1 < len(data) {
copy(data[i:], data[i+1:])
}
data = data[:len(data)-1]
} else {
i++
}
}
// Issue RPC
marshaler := &jsonpb.Marshaler{}
iter := client.GetLogs(pipelineName, jobID, data)
for iter.Next() {
var messageStr string
if raw {
var err error
messageStr, err = marshaler.MarshalToString(iter.Message())
if err != nil {
fmt.Fprintf(os.Stderr, "error marshalling \"%v\": %s\n", iter.Message(), err)
}
} else {
if iter.Message().User {
messageStr = iter.Message().Message
}
}
if messageStr != "" {
fmt.Println(messageStr)
}
}
return iter.Err()
}),
}
getLogs.Flags().StringVar(&pipelineName, "pipeline", "", "Filter the log "+
"for lines from this pipeline (accepts pipeline name)")
getLogs.Flags().StringVar(&jobID, "job", "", "Filter for log lines from "+
"this job (accepts job ID)")
getLogs.Flags().StringVar(&commaInputs, "inputs", "", "Filter for log lines "+
"generated while processing these files (accepts PFS paths or file hashes)")
getLogs.Flags().BoolVar(&raw, "raw", false, "Return log messages verbatim from server.")
pipeline := &cobra.Command{
Use: "pipeline",
Short: "Docs for pipelines.",
Long: `Pipelines are a powerful abstraction for automating jobs.
Pipelines take a set of repos as inputs, rather than the set of commits that
jobs take. Pipelines then subscribe to commits on those repos and launches a job
to process each incoming commit.
Creating a pipeline will also create a repo of the same name.
All jobs created by a pipeline will create commits in the pipeline's repo.
`,
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
return nil
}),
}
var pipelinePath string
createPipeline := &cobra.Command{
Use: "create-pipeline -f pipeline.json",
Short: "Create a new pipeline.",
Long: fmt.Sprintf("Create a new pipeline from a %s", pipelineSpec),
Run: cmdutil.RunFixedArgs(0, func(args []string) (retErr error) {
cfgReader, err := newPipelineManifestReader(pipelinePath)
if err != nil {
return err
}
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return sanitizeErr(err)
}
for {
request, err := cfgReader.nextCreatePipelineRequest()
if err == io.EOF {
break
} else if err != nil {
return err
}
if pushImages {
pushedImage, err := pushImage(registry, username, password, request.Transform.Image)
if err != nil {
return err
}
request.Transform.Image = pushedImage
}
if _, err := client.PpsAPIClient.CreatePipeline(
context.Background(),
request,
); err != nil {
return sanitizeErr(err)
}
}
return nil
}),
}
createPipeline.Flags().StringVarP(&pipelinePath, "file", "f", "-", "The file containing the pipeline, it can be a url or local file. - reads from stdin.")
createPipeline.Flags().BoolVarP(&pushImages, "push-images", "p", false, "If true, push local docker images into the cluster registry.")
createPipeline.Flags().StringVarP(®istry, "registry", "r", "docker.io", "The registry to push images to.")
createPipeline.Flags().StringVarP(&username, "username", "u", "", "The username to push images as, defaults to your OS username.")
createPipeline.Flags().StringVarP(&password, "password", "", "", "Your password for the registry being pushed to.")
updatePipeline := &cobra.Command{
Use: "update-pipeline -f pipeline.json",
Short: "Update an existing Pachyderm pipeline.",
Long: fmt.Sprintf("Update a Pachyderm pipeline with a new %s", pipelineSpec),
Run: cmdutil.RunFixedArgs(0, func(args []string) (retErr error) {
cfgReader, err := newPipelineManifestReader(pipelinePath)
if err != nil {
return err
}
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return sanitizeErr(err)
}
for {
request, err := cfgReader.nextCreatePipelineRequest()
if err == io.EOF {
break
} else if err != nil {
return err
}
request.Update = true
if pushImages {
pushedImage, err := pushImage(registry, username, password, request.Transform.Image)
if err != nil {
return err
}
request.Transform.Image = pushedImage
}
if _, err := client.PpsAPIClient.CreatePipeline(
context.Background(),
request,
); err != nil {
return sanitizeErr(err)
}
}
return nil
}),
}
updatePipeline.Flags().StringVarP(&pipelinePath, "file", "f", "-", "The file containing the pipeline, it can be a url or local file. - reads from stdin.")
updatePipeline.Flags().BoolVarP(&pushImages, "push-images", "p", false, "If true, push local docker images into the cluster registry.")
updatePipeline.Flags().StringVarP(®istry, "registry", "r", "docker.io", "The registry to push images to.")
updatePipeline.Flags().StringVarP(&username, "username", "u", "", "The username to push images as, defaults to your OS username.")
updatePipeline.Flags().StringVarP(&password, "password", "", "", "Your password for the registry being pushed to.")
inspectPipeline := &cobra.Command{
Use: "inspect-pipeline pipeline-name",
Short: "Return info about a pipeline.",
Long: "Return info about a pipeline.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
pipelineInfo, err := client.InspectPipeline(args[0])
if err != nil {
cmdutil.ErrorAndExit("error from InspectPipeline: %s", err.Error())
}
if pipelineInfo == nil {
cmdutil.ErrorAndExit("pipeline %s not found.", args[0])
}
return pretty.PrintDetailedPipelineInfo(pipelineInfo)
}),
}
listPipeline := &cobra.Command{
Use: "list-pipeline",
Short: "Return info about all pipelines.",
Long: "Return info about all pipelines.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
pipelineInfos, err := client.ListPipeline()
if err != nil {
cmdutil.ErrorAndExit("error from ListPipeline: %s", err.Error())
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
pretty.PrintPipelineHeader(writer)
for _, pipelineInfo := range pipelineInfos {
pretty.PrintPipelineInfo(writer, pipelineInfo)
}
return writer.Flush()
}),
}
deletePipeline := &cobra.Command{
Use: "delete-pipeline pipeline-name",
Short: "Delete a pipeline.",
Long: "Delete a pipeline.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
if err := client.DeletePipeline(args[0]); err != nil {
cmdutil.ErrorAndExit("error from DeletePipeline: %s", err.Error())
}
return nil
}),
}
startPipeline := &cobra.Command{
Use: "start-pipeline pipeline-name",
Short: "Restart a stopped pipeline.",
Long: "Restart a stopped pipeline.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
if err := client.StartPipeline(args[0]); err != nil {
cmdutil.ErrorAndExit("error from StartPipeline: %s", err.Error())
}
return nil
}),
}
stopPipeline := &cobra.Command{
Use: "stop-pipeline pipeline-name",
Short: "Stop a running pipeline.",
Long: "Stop a running pipeline.",
Run: cmdutil.RunFixedArgs(1, func(args []string) error {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
if err := client.StopPipeline(args[0]); err != nil {
cmdutil.ErrorAndExit("error from StopPipeline: %s", err.Error())
}
return nil
}),
}
var specPath string
runPipeline := &cobra.Command{
Use: "run-pipeline pipeline-name [-f job.json]",
Short: "Run a pipeline once.",
Long: fmt.Sprintf("Run a pipeline once, optionally overriding some pipeline options by providing a spec. The spec looks like this:\n%s", exampleRunPipelineSpec),
Run: cmdutil.RunFixedArgs(1, func(args []string) (retErr error) {
client, err := pach.NewMetricsClientFromAddress(address, metrics, "user")
if err != nil {
return err
}
request := &ppsclient.CreateJobRequest{
Pipeline: &ppsclient.Pipeline{
Name: args[0],
},
}
var buf bytes.Buffer
var specReader io.Reader
if specPath == "-" {
specReader = io.TeeReader(os.Stdin, &buf)
fmt.Print("Reading from stdin.\n")
} else if specPath != "" {
specFile, err := os.Open(specPath)
if err != nil {
return err
}
defer func() {
if err := specFile.Close(); err != nil && retErr == nil {
retErr = err
}
}()
specReader = io.TeeReader(specFile, &buf)
decoder := json.NewDecoder(specReader)
if err := jsonpb.UnmarshalNext(decoder, request); err != nil {
return err
}
}
job, err := client.PpsAPIClient.CreateJob(
context.Background(),
request,
)
if err != nil {
return sanitizeErr(err)
}
fmt.Println(job.ID)
return nil
}),
}
runPipeline.Flags().StringVarP(&specPath, "file", "f", "", "The file containing the run-pipeline spec, - reads from stdin.")
var result []*cobra.Command
result = append(result, job)
result = append(result, createJob)
result = append(result, inspectJob)
result = append(result, listJob)
result = append(result, deleteJob)
result = append(result, getLogs)
result = append(result, pipeline)
result = append(result, createPipeline)
result = append(result, updatePipeline)
result = append(result, inspectPipeline)
result = append(result, listPipeline)
result = append(result, deletePipeline)
result = append(result, startPipeline)
result = append(result, stopPipeline)
result = append(result, runPipeline)
return result, nil
}
func describeSyntaxError(originalErr error, parsedBuffer bytes.Buffer) error {
sErr, ok := originalErr.(*json.SyntaxError)
if !ok {
return originalErr
}
buffer := make([]byte, sErr.Offset)
parsedBuffer.Read(buffer)
lineOffset := strings.LastIndex(string(buffer[:len(buffer)-1]), "\n")
if lineOffset == -1 {
lineOffset = 0
}
lines := strings.Split(string(buffer[:len(buffer)-1]), "\n")
lineNumber := len(lines)
descriptiveErrorString := fmt.Sprintf("Syntax Error on line %v:\n%v\n%v^\n%v\n",
lineNumber,
string(buffer[lineOffset:]),
strings.Repeat(" ", int(sErr.Offset)-2-lineOffset),
originalErr,
)
return errors.New(descriptiveErrorString)
}
func sanitizeErr(err error) error {
if err == nil {
return nil
}
return errors.New(grpc.ErrorDesc(err))
}
// pushImage pushes an image as registry/user/image. Registry and user can be
// left empty.
func pushImage(registry string, username string, password string, image string) (string, error) {
client, err := docker.NewClientFromEnv()
if err != nil {
return "", err
}
repo, _ := docker.ParseRepositoryTag(image)
components := strings.Split(repo, "/")
name := components[len(components)-1]
if username == "" {
user, err := user.Current()
if err != nil {
return "", err
}
username = user.Username
}
pushRepo := fmt.Sprintf("%s/%s/%s", registry, username, name)
pushTag := uuid.NewWithoutDashes()
if err := client.TagImage(image, docker.TagImageOptions{
Repo: pushRepo,
Tag: pushTag,
Context: context.Background(),
}); err != nil {
return "", err
}
var authConfig docker.AuthConfiguration
if password != "" {
authConfig = docker.AuthConfiguration{ServerAddress: registry}
authConfig.Username = username
authConfig.Password = password
} else {
authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
return "", fmt.Errorf("error parsing auth: %s, try running `docker login`", err.Error())
}
for _, _authConfig := range authConfigs.Configs {
serverAddress := _authConfig.ServerAddress
if strings.Contains(serverAddress, registry) {
authConfig = _authConfig
break
}
}
}
fmt.Printf("Pushing %s:%s, this may take a while.\n", pushRepo, pushTag)
if err := client.PushImage(
docker.PushImageOptions{
Name: pushRepo,
Tag: pushTag,
},
authConfig,
); err != nil {
return "", err
}
return fmt.Sprintf("%s:%s", pushRepo, pushTag), nil
}