-
Notifications
You must be signed in to change notification settings - Fork 49
/
tool.go
867 lines (806 loc) · 27.7 KB
/
tool.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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
// Package tool provides implementation of the debugging related operations
// supported by go/cmd/remotetool package.
package tool
import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"errors"
log "github.com/golang/glog"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/cas"
rc "github.com/bazelbuild/remote-apis-sdks/go/pkg/client"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/command"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/digest"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/filemetadata"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/outerr"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/rexec"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/uploadinfo"
cpb "github.com/bazelbuild/remote-apis-sdks/go/api/command"
repb "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
)
const (
stdoutFile = "stdout"
stderrFile = "stderr"
)
// testOnlyStartDeterminismExec
var testOnlyStartDeterminismExec = func() {}
// Client is a remote execution client.
type Client struct {
GrpcClient *rc.Client
}
// CheckDeterminism executes the action the given number of times and compares
// output digests, reporting failure if a mismatch is detected.
func (c *Client) CheckDeterminism(ctx context.Context, actionDigest, actionRoot string, attempts int) error {
oe := outerr.SystemOutErr
firstMd, firstRes := c.ExecuteAction(ctx, actionDigest, actionRoot, "", oe)
for i := 1; i < attempts; i++ {
testOnlyStartDeterminismExec()
md, res := c.ExecuteAction(ctx, actionDigest, actionRoot, "", oe)
gotErr := false
if (firstRes == nil) != (res == nil) {
log.Errorf("action does not produce a consistent result, got %v and %v from consecutive executions", res, firstRes)
gotErr = true
}
if len(md.OutputFileDigests) != len(firstMd.OutputFileDigests) {
log.Errorf("action does not produce a consistent number of outputs, got %v and %v from consecutive executions", len(md.OutputFileDigests), len(firstMd.OutputFileDigests))
gotErr = true
}
for p, d := range md.OutputFileDigests {
firstD, ok := firstMd.OutputFileDigests[p]
if !ok {
log.Errorf("action does not produce %v consistently", p)
gotErr = true
continue
}
if d != firstD {
log.Errorf("action does not produce a consistent digest for %v, got %v and %v", p, d, firstD)
gotErr = true
continue
}
}
if gotErr {
return fmt.Errorf("action is not deterministic, check error log for more details")
}
}
return nil
}
func (c *Client) prepCommand(ctx context.Context, client *rexec.Client, actionDigest, actionRoot string) (*command.Command, error) {
acDg, err := digest.NewFromString(actionDigest)
if err != nil {
return nil, err
}
actionProto := &repb.Action{}
if _, err := c.GrpcClient.ReadProto(ctx, acDg, actionProto); err != nil {
return nil, err
}
commandProto := &repb.Command{}
cmdDg, err := digest.NewFromProto(actionProto.GetCommandDigest())
if err != nil {
return nil, err
}
log.Infof("Reading command from action digest..")
if _, err := c.GrpcClient.ReadProto(ctx, cmdDg, commandProto); err != nil {
return nil, err
}
fetchInputs := actionRoot == ""
if fetchInputs {
curTime := time.Now().Format(time.RFC3339)
actionRoot = filepath.Join(os.TempDir(), acDg.Hash+"_"+curTime)
}
inputRoot := filepath.Join(actionRoot, "input")
var nodeProperties map[string]*cpb.NodeProperties
if fetchInputs {
dg, err := digest.NewFromProto(actionProto.GetInputRootDigest())
if err != nil {
return nil, err
}
log.Infof("Fetching input tree from input root digest %s into %s", dg, inputRoot)
ts, _, err := c.GrpcClient.DownloadDirectory(ctx, dg, inputRoot, client.FileMetadataCache)
if err != nil {
return nil, err
}
nodeProperties = make(map[string]*cpb.NodeProperties)
for path, t := range ts {
if t.NodeProperties != nil {
nodeProperties[path] = command.NodePropertiesFromAPI(t.NodeProperties)
}
}
} else if nodeProperties, err = readNodePropertiesFromFile(filepath.Join(actionRoot, "input_node_properties.textproto")); err != nil {
return nil, err
}
contents, err := os.ReadDir(inputRoot)
if err != nil {
return nil, err
}
inputPaths := []string{}
for _, f := range contents {
inputPaths = append(inputPaths, f.Name())
}
// Construct Command object.
cmd := command.FromREProto(commandProto)
cmd.InputSpec.Inputs = inputPaths
cmd.InputSpec.InputNodeProperties = nodeProperties
cmd.InputSpec.SymlinkBehavior = command.PreserveSymlink
cmd.ExecRoot = inputRoot
if actionProto.Timeout != nil {
cmd.Timeout = actionProto.Timeout.AsDuration()
}
return cmd, nil
}
func readNodePropertiesFromFile(path string) (nps map[string]*cpb.NodeProperties, err error) {
if _, err = os.Stat(path); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("error accessing input node properties file: %v", err)
}
return nil, nil
}
inTxt, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading input node properties from file: %v", err)
}
ipb := &cpb.InputSpec{}
if err := prototext.Unmarshal(inTxt, ipb); err != nil {
return nil, fmt.Errorf("error unmarshalling input node properties from file %s: %v", path, err)
}
return ipb.InputNodeProperties, nil
}
// DownloadActionResult downloads the action result of the given action digest
// if it exists in the remote cache.
func (c *Client) DownloadActionResult(ctx context.Context, actionDigest, pathPrefix string) error {
acDg, err := digest.NewFromString(actionDigest)
if err != nil {
return err
}
actionProto := &repb.Action{}
if _, err := c.GrpcClient.ReadProto(ctx, acDg, actionProto); err != nil {
return err
}
commandProto := &repb.Command{}
cmdDg, err := digest.NewFromProto(actionProto.GetCommandDigest())
if err != nil {
return err
}
log.Infof("Reading command from action digest..")
if _, err := c.GrpcClient.ReadProto(ctx, cmdDg, commandProto); err != nil {
return err
}
// Construct Command object.
cmd := command.FromREProto(commandProto)
resPb, err := c.getActionResult(ctx, actionDigest)
if err != nil {
return err
}
if resPb == nil {
return fmt.Errorf("action digest %v not found in cache", actionDigest)
}
log.Infof("Cleaning contents of %v.", pathPrefix)
os.RemoveAll(pathPrefix)
os.Mkdir(pathPrefix, 0755)
log.Infof("Downloading action results of %v to %v.", actionDigest, pathPrefix)
// We don't really need an in-memory filemetadata cache for debugging operations.
noopCache := filemetadata.NewNoopCache()
if _, err := c.GrpcClient.DownloadActionOutputs(ctx, resPb, filepath.Join(pathPrefix, cmd.WorkingDir), noopCache); err != nil {
log.Errorf("Failed downloading action outputs: %v.", err)
}
// We have not requested for stdout/stderr to be inlined in GetActionResult, so the server
// should be returning the digest instead of sending raw data.
outMsgs := map[string]*repb.Digest{
filepath.Join(pathPrefix, stdoutFile): resPb.StdoutDigest,
filepath.Join(pathPrefix, stderrFile): resPb.StderrDigest,
}
for path, reDg := range outMsgs {
if reDg == nil {
continue
}
dg := &digest.Digest{
Hash: reDg.GetHash(),
Size: reDg.GetSizeBytes(),
}
log.Infof("Downloading stdout/stderr to %v.", path)
bytes, _, err := c.GrpcClient.ReadBlob(ctx, *dg)
if err != nil {
log.Errorf("Unable to read blob for %v with digest %v.", path, dg)
}
if err := os.WriteFile(path, bytes, 0644); err != nil {
log.Errorf("Unable to write output of digest %v to file %v.", dg, path)
}
}
log.Infof("Successfully downloaded results of %v to %v.", actionDigest, pathPrefix)
return nil
}
// DownloadBlob downloads a blob from the remote cache into the specified path.
// If the path is empty, it writes the contents to stdout instead.
func (c *Client) DownloadBlob(ctx context.Context, blobDigest, path string) (string, error) {
outputToStdout := false
if path == "" {
outputToStdout = true
// Create a temp file.
tmpFile, err := os.CreateTemp(os.TempDir(), "")
if err != nil {
return "", err
}
if err := tmpFile.Close(); err != nil {
return "", err
}
path = tmpFile.Name()
defer os.Remove(path)
}
dg, err := digest.NewFromString(blobDigest)
if err != nil {
return "", err
}
log.Infof("Downloading blob of %v to %v.", dg, path)
if _, err := c.GrpcClient.ReadBlobToFile(ctx, dg, path); err != nil {
return "", err
}
if !outputToStdout {
return "", nil
}
contents, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(contents), nil
}
// UploadBlob uploads a blob from the specified path into the remote cache.
func (c *Client) UploadBlob(ctx context.Context, path string) error {
dg, err := digest.NewFromFile(path)
if err != nil {
return err
}
log.Infof("Uploading blob of %v from %v.", dg, path)
ue := uploadinfo.EntryFromFile(dg, path)
if _, _, err := c.GrpcClient.UploadIfMissing(ctx, ue); err != nil {
return err
}
return nil
}
// UploadBlobV2 uploads a blob from the specified path into the remote cache using newer cas implementation.
func (c *Client) UploadBlobV2(ctx context.Context, path string) error {
casC, err := cas.NewClient(ctx, c.GrpcClient.Connection(), c.GrpcClient.InstanceName)
if err != nil {
return err
}
inputC := make(chan *cas.UploadInput)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
inputC <- &cas.UploadInput{
Path: path,
}
close(inputC)
return nil
})
eg.Go(func() error {
_, err := casC.Upload(ctx, cas.UploadOptions{}, inputC)
return err
})
return eg.Wait()
}
// DownloadDirectory downloads a an input root from the remote cache into the specified path.
func (c *Client) DownloadDirectory(ctx context.Context, rootDigest, path string) error {
log.Infof("Cleaning contents of %v.", path)
os.RemoveAll(path)
os.Mkdir(path, 0755)
dg, err := digest.NewFromString(rootDigest)
if err != nil {
return err
}
log.Infof("Downloading input root %v to %v.", dg, path)
_, _, err = c.GrpcClient.DownloadDirectory(ctx, dg, path, filemetadata.NewNoopCache())
return err
}
// UploadStats contains various metadata of a directory upload.
type UploadStats struct {
rc.TreeStats
RootDigest digest.Digest
CountBlobs int64
CountCacheMisses int64
BytesTransferred int64
BytesCacheMisses int64
Error string
}
// UploadDirectory uploads a directory from the specified path as a Merkle-tree to the remote cache.
func (c *Client) UploadDirectory(ctx context.Context, path string) (*UploadStats, error) {
log.Infof("Computing Merkle tree rooted at %s", path)
root, blobs, stats, err := c.GrpcClient.ComputeMerkleTree(ctx, path, "", "", &command.InputSpec{Inputs: []string{"."}}, filemetadata.NewNoopCache())
if err != nil {
return &UploadStats{Error: err.Error()}, err
}
us := &UploadStats{
TreeStats: *stats,
RootDigest: root,
CountBlobs: int64(len(blobs)),
}
log.Infof("Directory root digest: %v", root)
log.Infof("Directory stats: %d files, %d directories, %d symlinks, %d total bytes", stats.InputFiles, stats.InputDirectories, stats.InputSymlinks, stats.TotalInputBytes)
log.Infof("Uploading directory %v rooted at %s to CAS.", root, path)
missing, n, err := c.GrpcClient.UploadIfMissing(ctx, blobs...)
if err != nil {
us.Error = err.Error()
return us, err
}
var sumMissingBytes int64
for _, d := range missing {
sumMissingBytes += d.Size
}
us.CountCacheMisses = int64(len(missing))
us.BytesTransferred = n
us.BytesCacheMisses = sumMissingBytes
return us, nil
}
func (c *Client) writeProto(m proto.Message, baseName string) error {
f, err := os.Create(baseName)
if err != nil {
return err
}
defer f.Close()
f.WriteString(prototext.Format(m))
return nil
}
// DownloadAction parses and downloads an action to the given directory.
// The output directory will have the following:
// 1. ac.textproto: the action proto file in text format.
// 2. cmd.textproto: the command proto file in text format.
// 3. input/: the input tree root directory with all files under it.
// 4. input_node_properties.txtproto: all the NodeProperties defined on the
// input tree, as an InputSpec proto file in text format. Will be omitted
// if no NodeProperties are defined.
func (c *Client) DownloadAction(ctx context.Context, actionDigest, outputPath string, overwrite bool) error {
resPb, err := c.getActionResult(ctx, actionDigest)
if err != nil {
return err
}
acDg, err := digest.NewFromString(actionDigest)
if err != nil {
return err
}
actionProto := &repb.Action{}
log.Infof("Reading action..")
if _, err := c.GrpcClient.ReadProto(ctx, acDg, actionProto); err != nil {
return err
}
// Directory already exists, ask the user for confirmation before overwrite it.
if _, err := os.Stat(outputPath); !os.IsNotExist(err) {
fmt.Printf("Directory '%s' already exists. Do you want to overwrite it? (yes/no): ", outputPath)
if !overwrite {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("error reading user input: %v", err)
}
input = strings.TrimSpace(input)
input = strings.ToLower(input)
if !(input == "yes" || input == "y") {
return errors.New("operation aborted.")
}
}
// If the user confirms, remove the existing directory and create a new one
err = os.RemoveAll(outputPath)
if err != nil {
return fmt.Errorf("error removing existing directory: %v", err)
}
}
// Directory doesn't exist, create it.
err = os.MkdirAll(outputPath, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating the directory: %v", err)
}
log.Infof("Directory created: %v", outputPath)
if err := c.writeProto(actionProto, filepath.Join(outputPath, "ac.textproto")); err != nil {
return err
}
cmdDg, err := digest.NewFromProto(actionProto.GetCommandDigest())
if err != nil {
return err
}
log.Infof("Reading command from action..")
commandProto := &repb.Command{}
if _, err := c.GrpcClient.ReadProto(ctx, cmdDg, commandProto); err != nil {
return err
}
if err := c.writeProto(commandProto, filepath.Join(outputPath, "cmd.textproto")); err != nil {
return err
}
if err := c.writeExecScript(ctx, commandProto, filepath.Join(outputPath, "run_locally.sh")); err != nil {
return err
}
log.Infof("Fetching input tree from input root digest.. %v", actionProto.GetInputRootDigest())
rootPath := filepath.Join(outputPath, "input")
os.RemoveAll(rootPath)
os.Mkdir(rootPath, 0755)
rDg, err := digest.NewFromProto(actionProto.GetInputRootDigest())
if err != nil {
return err
}
ts, _, err := c.GrpcClient.DownloadDirectory(ctx, rDg, rootPath, filemetadata.NewNoopCache())
if err != nil {
return fmt.Errorf("error fetching input tree: %v", err)
}
is := &cpb.InputSpec{InputNodeProperties: make(map[string]*cpb.NodeProperties)}
for path, t := range ts {
if t.NodeProperties != nil {
is.InputNodeProperties[path] = command.NodePropertiesFromAPI(t.NodeProperties)
}
}
if len(is.InputNodeProperties) != 0 {
err = c.writeProto(is, filepath.Join(outputPath, "input_node_properties.textproto"))
if err != nil {
return err
}
}
res, err := c.formatAction(ctx, actionProto, resPb, commandProto, cmdDg)
if err != nil {
return fmt.Errorf("error formatting action %v", err)
}
err = os.WriteFile(filepath.Join(outputPath, "action.txt"), []byte(res), 0644)
if err != nil {
return fmt.Errorf("error dumping to action.txt] %v: %v", outputPath, err)
}
return nil
}
// shellSprintf is intended to add args sanitization before using them.
func shellSprintf(format string, args ...any) string {
// TODO: check args for flag injection
return fmt.Sprintf(format, args...)
}
func (c *Client) writeExecScript(ctx context.Context, cmd *repb.Command, filename string) error {
if cmd == nil {
return fmt.Errorf("invalid comment (nil)")
}
var runActionScript bytes.Buffer
runActionFilename := filepath.Join(filepath.Dir(filename), "run_command.sh")
wd := cmd.WorkingDirectory
cmdArgs := make([]string, len(cmd.GetArguments()))
for i, arg := range cmd.GetArguments() {
if strings.Contains(arg, " ") {
cmdArgs[i] = fmt.Sprintf("'%s'", arg)
continue
}
cmdArgs[i] = arg
}
execCmd := strings.Join(cmdArgs, " ")
runActionScript.WriteString(shellSprintf("#!/bin/bash\n\n"))
runActionScript.WriteString(shellSprintf("# This script is meant to be called by %v.\n", filename))
if wd != "" {
runActionScript.WriteString(shellSprintf("cd %v\n", wd))
}
for _, od := range cmd.GetOutputDirectories() {
runActionScript.WriteString(shellSprintf("mkdir -p %v\n", od))
}
for _, of := range cmd.GetOutputFiles() {
runActionScript.WriteString(shellSprintf("mkdir -p %v\n", filepath.Dir(of)))
}
for _, e := range cmd.GetEnvironmentVariables() {
runActionScript.WriteString(shellSprintf("export %v=%v\n", e.GetName(), e.GetValue()))
}
runActionScript.WriteString(execCmd)
runActionScript.WriteRune('\n')
runActionScript.WriteString(shellSprintf("bash\n"))
if err := os.WriteFile(runActionFilename, runActionScript.Bytes(), 0755); err != nil {
return err
}
var container string
var dockerParams string
for _, property := range cmd.Platform.GetProperties() {
if property.Name == "container-image" {
container = strings.TrimPrefix(property.Value, "docker://")
continue
}
if property.Name == "dockerPrivileged" {
dockerParams = "--privileged"
}
}
if container == "" {
return fmt.Errorf("container-image platform property missing from command proto: %v", cmd)
}
var execScript bytes.Buffer
execScript.WriteString(shellSprintf("#!/bin/bash\n\n"))
execScript.WriteString(shellSprintf("# This script can be used to run the action locally on\n"))
execScript.WriteString(shellSprintf("# this machine.\n"))
execScript.WriteString(shellSprintf("echo \"WARNING: The results from executing the action through this script may differ from results from RBE.\"\n"))
execScript.WriteString(shellSprintf("set -x\n"))
execScript.WriteString("docker run -i -t -w /b/f/w -v `pwd`/input:/b/f/w -v `pwd`/run_command.sh:/b/f/w/run_command.sh ")
execScript.WriteString(dockerParams)
execScript.WriteString(" ")
execScript.WriteString(container)
execScript.WriteString(" ./run_command.sh\n")
return os.WriteFile(filename, execScript.Bytes(), 0755)
}
func (c *Client) prepProtos(ctx context.Context, actionRoot string) (string, error) {
cmdTxt, err := os.ReadFile(filepath.Join(actionRoot, "cmd.textproto"))
if err != nil {
return "", err
}
cmdPb := &repb.Command{}
if err := prototext.Unmarshal(cmdTxt, cmdPb); err != nil {
return "", err
}
ue, err := uploadinfo.EntryFromProto(cmdPb)
if err != nil {
return "", err
}
if _, _, err := c.GrpcClient.UploadIfMissing(ctx, ue); err != nil {
return "", err
}
ac, err := os.ReadFile(filepath.Join(actionRoot, "ac.textproto"))
if err != nil {
return "", err
}
acPb := &repb.Action{}
if err := prototext.Unmarshal(ac, acPb); err != nil {
return "", err
}
dg, err := digest.NewFromMessage(cmdPb)
if err != nil {
return "", err
}
acPb.CommandDigest = dg.ToProto()
ue, err = uploadinfo.EntryFromProto(acPb)
if err != nil {
return "", err
}
if _, _, err := c.GrpcClient.UploadIfMissing(ctx, ue); err != nil {
return "", err
}
dg, err = digest.NewFromMessage(acPb)
if err != nil {
return "", err
}
return dg.String(), nil
}
// ExecuteAction executes an action in a canonical structure remotely.
// The structure is the same as that produced by DownloadAction.
// top level >
//
// > ac.textproto (Action text proto)
// > cmd.textproto (Command text proto)
// > input_node_properties.textproto (InputSpec text proto, optional)
// > input (Input root)
// > inputs...
func (c *Client) ExecuteAction(ctx context.Context, actionDigest, actionRoot, outDir string, oe outerr.OutErr) (*command.Metadata, error) {
fmc := filemetadata.NewNoopCache()
client := &rexec.Client{
FileMetadataCache: fmc,
GrpcClient: c.GrpcClient,
}
if actionRoot != "" {
var err error
if actionDigest, err = c.prepProtos(ctx, actionRoot); err != nil {
return nil, err
}
}
cmd, err := c.prepCommand(ctx, client, actionDigest, actionRoot)
if err != nil {
return nil, err
}
opt := &command.ExecutionOptions{AcceptCached: false, DownloadOutputs: false, DownloadOutErr: true}
ec, err := client.NewContext(ctx, cmd, opt, oe)
if err != nil {
return nil, err
}
ec.ExecuteRemotely()
fmt.Printf("Action complete\n")
fmt.Printf("---------------\n")
fmt.Printf("Action digest: %v\n", ec.Metadata.ActionDigest.String())
fmt.Printf("Command digest: %v\n", ec.Metadata.CommandDigest.String())
fmt.Printf("Stdout digest: %v\n", ec.Metadata.StdoutDigest.String())
fmt.Printf("Stderr digest: %v\n", ec.Metadata.StderrDigest.String())
fmt.Printf("Number of Input Files: %v\n", ec.Metadata.InputFiles)
fmt.Printf("Number of Input Dirs: %v\n", ec.Metadata.InputDirectories)
if len(cmd.InputSpec.InputNodeProperties) != 0 {
fmt.Printf("Number of Input Node Properties: %d\n", len(cmd.InputSpec.InputNodeProperties))
}
fmt.Printf("Number of Output Files: %v\n", ec.Metadata.OutputFiles)
fmt.Printf("Number of Output Directories: %v\n", ec.Metadata.OutputDirectories)
switch ec.Result.Status {
case command.NonZeroExitResultStatus:
oe.WriteErr([]byte(fmt.Sprintf("Remote action FAILED with exit code %d.\n", ec.Result.ExitCode)))
case command.TimeoutResultStatus:
oe.WriteErr([]byte(fmt.Sprintf("Remote action TIMED OUT after %0f seconds.\n", cmd.Timeout.Seconds())))
case command.InterruptedResultStatus:
oe.WriteErr([]byte(fmt.Sprintf("Remote execution was interrupted.\n")))
case command.RemoteErrorResultStatus:
oe.WriteErr([]byte(fmt.Sprintf("Remote execution error: %v.\n", ec.Result.Err)))
case command.LocalErrorResultStatus:
oe.WriteErr([]byte(fmt.Sprintf("Local error: %v.\n", ec.Result.Err)))
}
if ec.Result.Err == nil && outDir != "" {
ec.DownloadOutputs(outDir)
fmt.Printf("Output written to %v\n", outDir)
}
return ec.Metadata, ec.Result.Err
}
// ShowAction parses and displays an action with its corresponding command.
func (c *Client) ShowAction(ctx context.Context, actionDigest string) (string, error) {
resPb, err := c.getActionResult(ctx, actionDigest)
if err != nil {
return "", err
}
acDg, err := digest.NewFromString(actionDigest)
if err != nil {
return "", err
}
actionProto := &repb.Action{}
if _, err := c.GrpcClient.ReadProto(ctx, acDg, actionProto); err != nil {
return "", err
}
commandProto := &repb.Command{}
cmdDg, err := digest.NewFromProto(actionProto.GetCommandDigest())
if err != nil {
return "", err
}
log.Infof("Reading command from action digest..")
if _, err := c.GrpcClient.ReadProto(ctx, cmdDg, commandProto); err != nil {
return "", err
}
return c.formatAction(ctx, actionProto, resPb, commandProto, cmdDg)
}
func (c *Client) formatAction(ctx context.Context, actionProto *repb.Action, resPb *repb.ActionResult, commandProto *repb.Command, cmdDg digest.Digest) (string, error) {
var showActionRes bytes.Buffer
if actionProto.Timeout != nil {
timeout := actionProto.Timeout.AsDuration()
showActionRes.WriteString(fmt.Sprintf("Timeout: %s\n", timeout.String()))
}
showActionRes.WriteString("Command\n=======\n")
showActionRes.WriteString(fmt.Sprintf("Command Digest: %v\n", cmdDg))
for _, ev := range commandProto.GetEnvironmentVariables() {
showActionRes.WriteString(fmt.Sprintf("\t%s=%s\n", ev.Name, ev.Value))
}
cmdStr := strings.Join(commandProto.GetArguments(), " ")
showActionRes.WriteString(fmt.Sprintf("\t%v\n", cmdStr))
showActionRes.WriteString("\nPlatform\n========\n")
for _, property := range commandProto.GetPlatform().GetProperties() {
showActionRes.WriteString(fmt.Sprintf("\t%s=%s\n", property.Name, property.Value))
}
showActionRes.WriteString("\nInputs\n======\n")
log.Infof("Fetching input tree from input root digest..")
inpTree, _, err := c.getInputTree(ctx, actionProto.GetInputRootDigest())
if err != nil {
showActionRes.WriteString("Failed to fetch input tree:\n")
showActionRes.WriteString(err.Error())
showActionRes.WriteString("\n")
} else {
showActionRes.WriteString(inpTree)
}
if resPb == nil {
showActionRes.WriteString("\nNo action result in cache.\n")
} else {
log.Infof("Fetching output tree from action result..")
outs, err := c.getOutputs(ctx, resPb)
if err != nil {
return "", err
}
showActionRes.WriteString("\n")
showActionRes.WriteString(outs)
}
return showActionRes.String(), nil
}
func (c *Client) getOutputs(ctx context.Context, actionRes *repb.ActionResult) (string, error) {
var res bytes.Buffer
res.WriteString("------------------------------------------------------------------------\n")
res.WriteString("Action Result\n\n")
res.WriteString(fmt.Sprintf("Exit code: %d\n", actionRes.ExitCode))
if actionRes.StdoutDigest != nil {
dg, err := digest.NewFromProto(actionRes.StdoutDigest)
if err != nil {
return "", err
}
res.WriteString(fmt.Sprintf("stdout digest: %v\n", dg))
}
if actionRes.StderrDigest != nil {
dg, err := digest.NewFromProto(actionRes.StderrDigest)
if err != nil {
return "", err
}
res.WriteString(fmt.Sprintf("stderr digest: %v\n", dg))
}
res.WriteString("\nOutput Files\n============\n")
for _, of := range actionRes.GetOutputFiles() {
dg, err := digest.NewFromProto(of.GetDigest())
if err != nil {
return "", err
}
res.WriteString(fmt.Sprintf("%v, digest: %v\n", of.GetPath(), dg))
}
res.WriteString("\nOutput Files From Directories\n=============================\n")
for _, od := range actionRes.GetOutputDirectories() {
treeDigest := od.GetTreeDigest()
dg, err := digest.NewFromProto(treeDigest)
if err != nil {
return "", err
}
outDirTree := &repb.Tree{}
if _, err := c.GrpcClient.ReadProto(ctx, dg, outDirTree); err != nil {
return "", err
}
outputs, _, err := c.flattenTree(ctx, outDirTree)
if err != nil {
return "", err
}
res.WriteString("\n")
res.WriteString(outputs)
}
return res.String(), nil
}
func (c *Client) getInputTree(ctx context.Context, root *repb.Digest) (string, []string, error) {
var res bytes.Buffer
dg, err := digest.NewFromProto(root)
if err != nil {
return "", nil, err
}
res.WriteString(fmt.Sprintf("[Root directory digest: %v]", dg))
dirs, err := c.GrpcClient.GetDirectoryTree(ctx, root)
if err != nil {
return "", nil, err
}
if len(dirs) == 0 {
return "", nil, fmt.Errorf("Empty directories returned by GetTree for %v", dg)
}
t := &repb.Tree{
Root: dirs[0],
Children: dirs,
}
inputs, paths, err := c.flattenTree(ctx, t)
if err != nil {
return "", nil, err
}
res.WriteString("\n")
res.WriteString(inputs)
return res.String(), paths, nil
}
func (c *Client) flattenTree(ctx context.Context, t *repb.Tree) (string, []string, error) {
var res bytes.Buffer
outputs, err := c.GrpcClient.FlattenTree(t, "")
if err != nil {
return "", nil, err
}
// Sort the values by path.
paths := make([]string, 0, len(outputs))
for path := range outputs {
if path == "" {
path = "."
outputs[path] = outputs[""]
}
paths = append(paths, path)
}
sort.Strings(paths)
for _, path := range paths {
output := outputs[path]
var np string
if output.NodeProperties != nil {
np = fmt.Sprintf(" [Node properties: %v]", prototext.MarshalOptions{Multiline: false}.Format(output.NodeProperties))
}
if output.IsEmptyDirectory {
res.WriteString(fmt.Sprintf("%v: [Directory digest: %v]%s\n", path, output.Digest, np))
} else if output.SymlinkTarget != "" {
res.WriteString(fmt.Sprintf("%v: [Symlink digest: %v, Symlink Target: %v]%s\n", path, output.Digest, output.SymlinkTarget, np))
} else {
res.WriteString(fmt.Sprintf("%v: [File digest: %v]%s\n", path, output.Digest, np))
}
}
return res.String(), paths, nil
}
func (c *Client) getActionResult(ctx context.Context, actionDigest string) (*repb.ActionResult, error) {
acDg, err := digest.NewFromString(actionDigest)
if err != nil {
return nil, err
}
d := &repb.Digest{
Hash: acDg.Hash,
SizeBytes: acDg.Size,
}
resPb, err := c.GrpcClient.CheckActionCache(ctx, d)
if err != nil {
return nil, err
}
return resPb, nil
}