-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
137 lines (117 loc) · 4.17 KB
/
main.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
// Uploader for optimized upload of a zip archive to RBE CAS.
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"flag"
log "github.com/golang/glog"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/digest"
"github.com/google/device-infra/src/devtools/rbe/casuploader/uploader"
"github.com/google/device-infra/src/devtools/rbe/rbeclient"
)
const version = "1.1"
// multiStringFlag is a slice of strings for parsing command flags into a string list.
type multiStringFlag []string
func (f *multiStringFlag) String() string {
return fmt.Sprintf("%v", *f)
}
func (f *multiStringFlag) Set(val string) error {
*f = append(*f, val)
return nil
}
func (f *multiStringFlag) Get() any {
return []string(*f)
}
var (
printVersion = flag.Bool("version", false, "Print version information")
zipPath = flag.String("zip-path", "", "Path to a .zip file to upload")
dirPath = flag.String("dir-path", "", "Path to a directory to upload")
casInstance = flag.String("cas-instance", "", "RBE instance")
casAddr = flag.String("cas-addr", "remotebuildexecution.googleapis.com:443", "RBE server addr")
serviceAccount = flag.String("service-account-json", "", "Path to JSON file with service account credentials to use.")
useADC = flag.Bool("use-adc", false, "True to use Application Default Credentials (ADC).")
dumpDigest = flag.String("dump-digest", "", "Output the digest to file")
dumpFileDetails = flag.String("dump-file-details", "", "Export information of all uploaded files to a file")
excludeFilters multiStringFlag
)
func checkFlags() error {
if *casInstance == "" {
return errors.New("-cas-instance must be specified")
}
if *casAddr == "" {
return errors.New("-cas-addr must be specified")
}
if *zipPath == "" && *dirPath == "" {
return errors.New("-zip-path or -dir-path must be specified")
}
if *dirPath != "" && *zipPath != "" {
return errors.New("-dir-path and -zip-path cannot both be specified")
}
if *serviceAccount == "" && *useADC == false {
return errors.New("Either -use-adc must be true or -service-account-json must be specified")
}
if *serviceAccount != "" && *useADC == true {
return errors.New("-use-adc and -service-account-json must not be set together")
}
return nil
}
func main() {
flag.Var(&excludeFilters, "exclude-filters",
"Regular expression of paths to be excluded from uploading. The regex will implicitly "+
"append the root directory path to the beginning, so DO NOT use \"^\" in the regex.")
flag.Set("silent_init", "true")
flag.Set("logtostderr", "true")
flag.Set("stderrthreshold", "INFO")
flag.Parse()
if *printVersion == true {
fmt.Printf("version: %s\n", version)
os.Exit(0)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle interruption and SIGTERM to cancel read operations, so that the FUSE directory can be unmounted safely.
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.Infoln("Interrupted, exiting...")
cancel()
time.Sleep(3 * time.Second)
os.Exit(1)
}()
if err := checkFlags(); err != nil {
log.Exit(err)
}
start := time.Now()
// Create a new RBE client.
client, err := rbeclient.New(ctx, rbeclient.Opts{*casInstance, *casAddr, *serviceAccount, *useADC})
if err != nil {
log.Exit(err)
}
defer client.Close()
var rootDigest digest.Digest
uploaderConfig := uploader.NewCommonConfig(ctx, client, excludeFilters, *dumpFileDetails)
if *zipPath != "" {
zipUploader := uploader.NewZipUploader(uploaderConfig, *zipPath)
rootDigest, err = zipUploader.DoUpload()
if err != nil {
log.Exitf("Failed to upload the zip archive to CAS: %v", err)
}
} else if *dirPath != "" {
dirUploader := uploader.NewDirUploader(uploaderConfig, *dirPath, nil)
rootDigest, err = dirUploader.DoUpload()
if err != nil {
log.Exitf("Failed to upload the directory to CAS: %v", err)
}
}
output := fmt.Sprintf("%s/%d", rootDigest.Hash, rootDigest.Size)
if *dumpDigest != "" {
os.WriteFile(*dumpDigest, []byte(output), 0644)
}
log.Infof("Uploaded %s to RBE instance %s, root digest: %s. E2E time: %v\n", *zipPath, *casInstance, output, time.Since(start))
}