-
Notifications
You must be signed in to change notification settings - Fork 327
/
build.go
377 lines (345 loc) · 10.7 KB
/
build.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
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"errors"
"flag"
"os"
"os/user"
"strings"
"github.com/golang/protobuf/proto"
"github.com/google/gapid/core/app"
"github.com/google/gapid/core/git"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/net/grpcutil"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/core/os/device/host"
"github.com/google/gapid/core/os/file"
"github.com/google/gapid/test/robot/build"
"github.com/google/gapid/test/robot/search/script"
"google.golang.org/grpc"
)
type UploadOptions struct {
RobotOptions
CL string `help:"The build CL, will be guessed if not set"`
Description string `help:"An optional build description"`
Tag string `help:"The optional build tag"`
Track string `help:"The package's track, will be guessed if not set"`
Uploader string `help:"The uploading entity, will be guessed if not set"`
BuilderAbi string `help:"The abi of the builder device, will assume this device if not set"`
BuildBot bool `help:"Whether this package was built by a build bot"`
}
func init() {
uploadVerb.Add(&app.Verb{
Name: "build",
ShortHelp: "Upload a build to the server",
ShortUsage: "<zip file|directory>",
Action: &buildUploadVerb{UploadOptions: UploadOptions{RobotOptions: defaultRobotOptions}},
})
searchVerb.Add(&app.Verb{
Name: "artifact",
ShortHelp: "List build artifacts in the server",
ShortUsage: "<query>",
Action: &artifactSearchVerb{RobotOptions: defaultRobotOptions},
})
searchVerb.Add(&app.Verb{
Name: "package",
ShortHelp: "List build packages in the server",
ShortUsage: "<query>",
Action: &packageSearchVerb{RobotOptions: defaultRobotOptions},
})
searchVerb.Add(&app.Verb{
Name: "track",
ShortHelp: "List build tracks in the server",
ShortUsage: "<query>",
Action: &trackSearchVerb{RobotOptions: defaultRobotOptions},
})
setVerb.Add(&app.Verb{
Name: "track",
ShortHelp: "Sets values on a track",
ShortUsage: "<id or name>",
Action: &trackUpdateVerb{RobotOptions: defaultRobotOptions},
})
setVerb.Add(&app.Verb{
Name: "package",
ShortHelp: "Sets values on a package",
ShortUsage: "<id>",
Action: &packageUpdateVerb{RobotOptions: defaultRobotOptions},
})
}
type buildUploadVerb struct {
UploadOptions
store build.Store
info *build.Information
}
func (v *buildUploadVerb) Run(ctx context.Context, flags flag.FlagSet) (err error) {
if flags.NArg() != 1 {
err = errors.New("Missing expected argument")
return log.Err(ctx, err, "build upload expects a single filepath as argument")
}
p := file.Abs(flags.Arg(0))
u := make([]uploadable, 1)
if p.IsDir() {
b, err := zip(p)
if err != nil {
return log.Err(ctx, err, "failed to create artifact zip")
}
u[0] = data(b.Bytes(), p.Basename()+".zip", false)
} else {
u[0] = path(p.System())
}
return upload(ctx, u, v.ServerAddress, v)
}
func (v *buildUploadVerb) prepare(ctx context.Context, conn *grpc.ClientConn) (err error) {
v.store = build.NewRemote(ctx, conn)
v.info, err = v.UploadOptions.createBuildInfo(ctx)
return
}
func (v *buildUploadVerb) process(ctx context.Context, id string) error {
return storeBuild(ctx, v.store, v.info, id)
}
func storeBuild(ctx context.Context, store build.Store, info *build.Information, id string) error {
id, merged, err := store.Add(ctx, id, info)
if err != nil {
return log.Err(ctx, err, "Failed processing build")
}
if merged {
log.I(ctx, "Merged with build set %s", id)
} else {
log.I(ctx, "New build set %s", id)
}
return nil
}
func (o *UploadOptions) createBuildInfo(ctx context.Context) (*build.Information, error) {
// see if we can find a git cl in the cwd
var typ build.Type
if o.BuildBot {
typ = build.BuildBot
if o.CL == "" || o.Description == "" || o.BuilderAbi == "" {
err := errors.New("Missing expected argument")
return nil, log.Err(ctx, err, "build bot packages require the CL, desciption and builder ABI")
}
if o.Track == "" {
o.Track = "master"
}
} else {
typ = o.initFromGit(ctx)
}
if o.Uploader == "" {
// guess uploader from environment
if user, err := user.Current(); err == nil {
o.Uploader = user.Username
log.I(ctx, "Detected uploader %s", o.Uploader)
}
}
log.I(ctx, "Detected build type %s", typ)
builder := &device.Instance{Configuration: &device.Configuration{}}
if o.BuilderAbi != "" {
if typ == build.BuildBot {
builder.Name = "BuildBot"
}
builder.Configuration.ABIs = []*device.ABI{device.ABIByName(o.BuilderAbi)}
}
if len(builder.Configuration.ABIs) == 0 {
builder = host.Instance(ctx)
}
return &build.Information{
Type: typ,
Branch: o.Track,
Cl: o.CL,
Tag: o.Tag,
Description: o.Description,
Builder: builder,
Uploader: o.Uploader,
}, nil
}
func (o *UploadOptions) initFromGit(ctx context.Context) (typ build.Type) {
// Assume not clean, until we can verify.
typ = build.Local
g, err := git.New(".")
if err != nil {
log.E(ctx, "Git failed. Error: %v", err)
return
}
if cl, err := g.HeadCL(ctx, o.CL); err != nil {
log.E(ctx, "CL failed. Error: %v", err)
} else {
if o.CL == "" {
// guess cl from git
o.CL = cl.SHA.String()
log.I(ctx, "Detected CL %s", o.CL)
}
if o.Description == "" {
// guess description from git
o.Description = cl.Subject
log.I(ctx, "Detected description %s", o.Description)
}
}
if status, err := g.Status(ctx); err != nil {
log.E(ctx, "Status failed. Error: %v", err)
} else if status.Clean() {
typ = build.User
}
if o.Track == "" {
// guess branch from git
if branch, err := g.CurrentBranch(ctx); err != nil {
log.E(ctx, "Branch failed. Error: %v", err)
o.Track = "auto"
} else {
o.Track = branch
log.I(ctx, "Detected track %s", o.Track)
}
}
return
}
func zip(in file.Path) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
if err := file.ZIP(buf, in); err != nil {
return nil, err
}
return buf, nil
}
type artifactSearchVerb struct {
RobotOptions
}
func (v *artifactSearchVerb) Run(ctx context.Context, flags flag.FlagSet) error {
return grpcutil.Client(ctx, v.ServerAddress, func(ctx context.Context, conn *grpc.ClientConn) error {
b := build.NewRemote(ctx, conn)
expression := strings.Join(flags.Args(), " ")
out := os.Stdout
expr, err := script.Parse(ctx, expression)
if err != nil {
return log.Err(ctx, err, "Malformed search query")
}
return b.SearchArtifacts(ctx, expr.Query(), func(ctx context.Context, entry *build.Artifact) error {
proto.MarshalText(out, entry)
return nil
})
}, grpc.WithInsecure())
}
type packageSearchVerb struct {
RobotOptions
}
func (v *packageSearchVerb) Run(ctx context.Context, flags flag.FlagSet) error {
return grpcutil.Client(ctx, v.ServerAddress, func(ctx context.Context, conn *grpc.ClientConn) error {
b := build.NewRemote(ctx, conn)
expression := strings.Join(flags.Args(), " ")
out := os.Stdout
expr, err := script.Parse(ctx, expression)
if err != nil {
return log.Err(ctx, err, "Malformed search query")
}
return b.SearchPackages(ctx, expr.Query(), func(ctx context.Context, entry *build.Package) error {
proto.MarshalText(out, entry)
return nil
})
}, grpc.WithInsecure())
}
type trackSearchVerb struct {
RobotOptions
}
func (v *trackSearchVerb) Run(ctx context.Context, flags flag.FlagSet) error {
return grpcutil.Client(ctx, v.ServerAddress, func(ctx context.Context, conn *grpc.ClientConn) error {
b := build.NewRemote(ctx, conn)
expression := strings.Join(flags.Args(), " ")
out := os.Stdout
expr, err := script.Parse(ctx, expression)
if err != nil {
return log.Err(ctx, err, "Malformed search query")
}
return b.SearchTracks(ctx, expr.Query(), func(ctx context.Context, entry *build.Track) error {
proto.MarshalText(out, entry)
return nil
})
}, grpc.WithInsecure())
}
var idOrName = script.MustParse("Id == $ or Name == $").Using("$")
type trackUpdateVerb struct {
RobotOptions
Name string `help:"The new name for the track"`
Description string `help:"A description of the track"`
Pkg string `help:"The id of the package at the head of the track"`
}
func (v *trackUpdateVerb) Run(ctx context.Context, flags flag.FlagSet) error {
return grpcutil.Client(ctx, v.ServerAddress, func(ctx context.Context, conn *grpc.ClientConn) error {
b := build.NewRemote(ctx, conn)
args := flags.Args()
track := &build.Track{
Name: v.Name,
Description: v.Description,
Head: v.Pkg,
}
if len(args) != 0 {
// Updating an existing track, find it first
err := b.SearchTracks(ctx, idOrName(args[0]).Query(), func(ctx context.Context, entry *build.Track) error {
if track.Id != "" {
return log.Err(ctx, nil, "Multiple tracks matched")
}
track.Id = entry.Id
return nil
})
if err != nil {
return err
}
if track.Id == "" {
return log.Err(ctx, nil, "No tracks matched")
}
}
track, err := b.UpdateTrack(ctx, track)
if err != nil {
return err
}
log.I(ctx, track.String())
return nil
}, grpc.WithInsecure())
}
type packageUpdateVerb struct {
RobotOptions
Description string `help:"A description of the track"`
Parent string `help:"The id of the package that will be the new parent"`
}
func (v *packageUpdateVerb) Run(ctx context.Context, flags flag.FlagSet) error {
return grpcutil.Client(ctx, v.ServerAddress, func(ctx context.Context, conn *grpc.ClientConn) error {
b := build.NewRemote(ctx, conn)
args := flags.Args()
pkg := &build.Package{
Information: &build.Information{Description: v.Description},
Parent: v.Parent,
}
if len(args) == 0 {
return log.Err(ctx, nil, "Missing argument, must specify a package to update")
}
err := b.SearchPackages(ctx, script.MustParse("Id == $").Using("$")(args[0]).Query(), func(ctx context.Context, entry *build.Package) error {
if pkg.Id != "" {
return log.Err(ctx, nil, "Multiple packages matched")
}
pkg.Id = entry.Id
return nil
})
if err != nil {
return err
}
if pkg.Id == "" {
return log.Err(ctx, nil, "No packages matched")
}
pkg, err = b.UpdatePackage(ctx, pkg)
if err != nil {
return err
}
log.I(ctx, pkg.String())
return nil
}, grpc.WithInsecure())
}