-
Notifications
You must be signed in to change notification settings - Fork 18
/
parser.go
525 lines (439 loc) · 14.5 KB
/
parser.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
package parser
import (
"context"
"crypto/md5" // nolint:gosec // backwards compatibility
"errors"
"fmt"
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/internal/executor/environment"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/dummy"
"github.com/cirruslabs/cirrus-cli/pkg/parser/boolevator"
"github.com/cirruslabs/cirrus-cli/pkg/parser/modifier/matrix"
"github.com/cirruslabs/cirrus-cli/pkg/parser/nameable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/node"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parseable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/task"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/ptypes"
"github.com/lestrrat-go/jsschema"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
var (
ErrInternal = errors.New("internal error")
)
type Parser struct {
// Environment to take into account when expanding variables.
environment map[string]string
// Filesystem to reference when calculating file hashes.
//
// For example, Dockerfile contents are hashed to avoid duplicate builds.
fs fs.FileSystem
// A list of changed files useful when evaluating changesInclude() boolevator's function.
affectedFiles []string
boolevator *boolevator.Boolevator
parsers map[nameable.Nameable]parseable.Parseable
idNumbering int64
indexNumbering int64
additionalInstances map[string]protoreflect.MessageDescriptor
additionalTaskProperties []*descriptor.FieldDescriptorProto
tasksCountBeforeFiltering int64
}
type Result struct {
Errors []string
Tasks []*api.Task
TasksCountBeforeFiltering int64
}
func New(opts ...Option) *Parser {
parser := &Parser{
environment: make(map[string]string),
fs: dummy.New(),
}
// Apply options
for _, opt := range opts {
opt(parser)
}
// Initialize boolevator
parser.boolevator = boolevator.New(boolevator.WithFunctions(map[string]boolevator.Function{
"changesInclude": parser.bfuncChangesInclude(),
}))
// Register parsers
taskParser := task.NewTask(nil, nil, parser.additionalInstances, parser.additionalTaskProperties)
pipeParser := task.NewDockerPipe(nil, nil, parser.additionalTaskProperties)
builderParser := task.NewDockerBuilder(nil, nil, parser.additionalTaskProperties)
parser.parsers = map[nameable.Nameable]parseable.Parseable{
nameable.NewRegexNameable("^(.*)task$"): taskParser,
nameable.NewRegexNameable("^(.*)pipe$"): pipeParser,
nameable.NewRegexNameable("^(.*)docker_builder$"): builderParser,
}
return parser
}
func (p *Parser) parseTasks(tree *node.Node) ([]task.ParseableTaskLike, error) {
var tasks []task.ParseableTaskLike
for _, treeItem := range tree.Children {
for key, value := range p.parsers {
var taskLike task.ParseableTaskLike
switch value.(type) {
case *task.Task:
taskLike = task.NewTask(
environment.Copy(p.environment),
p.boolevator,
p.additionalInstances,
p.additionalTaskProperties,
)
case *task.DockerPipe:
taskLike = task.NewDockerPipe(environment.Copy(p.environment), p.boolevator, p.additionalTaskProperties)
case *task.DockerBuilder:
taskLike = task.NewDockerBuilder(environment.Copy(p.environment), p.boolevator, p.additionalTaskProperties)
default:
panic("unknown task-like object")
}
if !key.Matches(treeItem.Name) {
continue
}
err := taskLike.Parse(treeItem)
if err != nil {
return nil, err
}
taskLike.SetID(p.NextTaskID())
// Set task's name if not set in the definition
if taskLike.Name() == "" {
if rn, ok := key.(*nameable.RegexNameable); ok {
taskLike.SetName(rn.FirstGroupOrDefault(treeItem.Name, "main"))
}
}
// Filtering based on "only_if" expression evaluation
taskSpecificEnv := map[string]string{
"CIRRUS_TASK_NAME": taskLike.Name(),
}
p.tasksCountBeforeFiltering++
enabled, err := taskLike.Enabled(environment.Merge(taskSpecificEnv, p.environment), p.boolevator)
if err != nil {
return nil, err
}
if !enabled {
continue
}
taskLike.SetIndexWithinBuild(p.NextTaskLocalIndex())
tasks = append(tasks, taskLike)
}
}
return tasks, nil
}
func (p *Parser) Parse(ctx context.Context, config string) (*Result, error) {
var parsed yaml.MapSlice
// Unmarshal YAML
if err := yaml.Unmarshal([]byte(config), &parsed); err != nil {
return nil, err
}
// Convert the parsed and nested YAML structure into a tree
// to get the ability to walk parents
tree, err := node.NewFromSlice(parsed)
if err != nil {
return nil, err
}
// Run modifiers on it
if err := matrix.ExpandMatrices(tree); err != nil {
return nil, err
}
// Run parsers on the top-level nodes
tasks, err := p.parseTasks(tree)
if err != nil {
return &Result{
Errors: []string{err.Error()},
}, nil
}
resolveDependenciesShallow(tasks)
if len(tasks) == 0 {
return &Result{
Errors: []string{"configuration was parsed without errors, but no tasks were found"},
}, nil
}
if err := validateDependenciesDeep(tasks); err != nil {
return &Result{
Errors: []string{err.Error()},
}, nil
}
var protoTasks []*api.Task
for _, task := range tasks {
protoTask := task.Proto().(*api.Task)
if err := validateTask(protoTask); err != nil {
return &Result{
Errors: []string{err.Error()},
}, nil
}
protoTasks = append(protoTasks, protoTask)
}
// Create service tasks
serviceTasks, err := p.createServiceTasks(ctx, protoTasks)
if err != nil {
return &Result{
Errors: []string{err.Error()},
}, nil
}
protoTasks = append(protoTasks, serviceTasks...)
// Postprocess individual tasks
for _, protoTask := range protoTasks {
// Insert empty clone instruction if custom clone script wasn't provided by the user
ensureCloneInstruction(protoTask)
// Provide unique labels for identically named tasks
uniqueLabelsForTask, err := uniqueLabels(protoTask, protoTasks, p.additionalInstances)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInternal, err)
}
protoTask.Metadata.UniqueLabels = uniqueLabelsForTask
}
// Sort tasks by their IDs to make output consistent across runs
sort.Slice(protoTasks, func(i, j int) bool {
return protoTasks[i].LocalGroupId < protoTasks[j].LocalGroupId
})
return &Result{
Tasks: protoTasks,
TasksCountBeforeFiltering: p.tasksCountBeforeFiltering,
}, nil
}
func (p *Parser) ParseFromFile(ctx context.Context, path string) (*Result, error) {
config, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return p.Parse(ctx, string(config))
}
func (p *Parser) fileHash(ctx context.Context, path string, additionalBytes []byte) (string, error) {
// Note that this will be empty if we don't know anything about the file,
// so we'll return MD5(""), but that's OK since the purpose is caching
fileBytes, err := p.fs.Get(ctx, path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return "", err
}
// nolint:gosec // backwards compatibility
digest := md5.Sum(append(fileBytes, additionalBytes...))
return fmt.Sprintf("%x", digest), nil
}
func (p *Parser) NextTaskID() int64 {
defer func() {
p.idNumbering++
}()
return p.idNumbering
}
func (p *Parser) NextTaskLocalIndex() int64 {
defer func() {
p.indexNumbering++
}()
return p.indexNumbering
}
func (p *Parser) Schema() *schema.Schema {
schema := &schema.Schema{
Type: schema.PrimitiveTypes{schema.ObjectType},
ID: "https://cirrus-ci.org/",
Title: "JSON schema for Cirrus CI configuration files",
SchemaRef: "http://json-schema.org/draft-04/schema#",
Properties: make(map[string]*schema.Schema),
PatternProperties: make(map[*regexp.Regexp]*schema.Schema),
AdditionalProperties: &schema.AdditionalProperties{Schema: nil},
}
for parserName, parser := range p.parsers {
switch nameable := parserName.(type) {
case *nameable.SimpleNameable:
schema.Properties[nameable.Name()] = parser.Schema()
case *nameable.RegexNameable:
schema.PatternProperties[nameable.Regex()] = parser.Schema()
}
// Note: this is a simplification that doesn't return collectible fields recursively,
// because it assumes that we're only defining collectibles on the first depth level.
for _, collectibleFields := range parser.CollectibleFields() {
schema.Properties[collectibleFields.Name] = collectibleFields.Schema
}
}
return schema
}
func resolveDependenciesShallow(tasks []task.ParseableTaskLike) {
for _, task := range tasks {
var dependsOnIDs []int64
for _, dependsOnName := range task.DependsOnNames() {
for _, subTask := range tasks {
if subTask.Name() == dependsOnName || subTask.Alias() == dependsOnName {
dependsOnIDs = append(dependsOnIDs, subTask.ID())
}
}
}
sort.Slice(dependsOnIDs, func(i, j int) bool { return dependsOnIDs[i] < dependsOnIDs[j] })
task.SetDependsOnIDs(dependsOnIDs)
}
}
func (p *Parser) createServiceTask(
dockerfileHash string,
protoTask *api.Task,
taskContainer *api.ContainerInstance,
) (*api.Task, error) {
prebuiltInstance := &api.PrebuiltImageInstance{
Repository: fmt.Sprintf("cirrus-ci-community/%s", dockerfileHash),
Reference: "latest",
Platform: taskContainer.Platform,
Dockerfile: taskContainer.Dockerfile,
Arguments: taskContainer.DockerArguments,
}
anyInstance, err := ptypes.MarshalAny(prebuiltInstance)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInternal, err)
}
// Craft Docker build arguments: task name
var buildArgsSlice []string
for key, value := range taskContainer.DockerArguments {
buildArgsSlice = append(buildArgsSlice, fmt.Sprintf("%s=%s", key, value))
}
sort.Strings(buildArgsSlice)
var buildArgs string
for _, buildArg := range buildArgsSlice {
buildArgs += fmt.Sprintf(" %s", buildArg)
}
// Craft Docker build arguments: docker build command
var dockerBuildArgsSlice []string
for key, value := range taskContainer.DockerArguments {
dockerBuildArgsSlice = append(dockerBuildArgsSlice, fmt.Sprintf("%s=\"%s\"", key, value))
}
sort.Strings(dockerBuildArgsSlice)
var dockerBuildArgs string
for _, dockerBuildArg := range dockerBuildArgsSlice {
dockerBuildArgs += fmt.Sprintf(" --build-arg %s", dockerBuildArg)
}
script := fmt.Sprintf("docker build "+
"--tag gcr.io/%s:%s "+
"--file %s%s ",
prebuiltInstance.Repository, prebuiltInstance.Reference,
taskContainer.Dockerfile, dockerBuildArgs)
if taskContainer.Platform == api.Platform_WINDOWS {
script += "."
} else {
script += "${CIRRUS_DOCKER_CONTEXT:-$CIRRUS_WORKING_DIR}"
}
serviceTask := &api.Task{
Name: fmt.Sprintf("Prebuild %s%s", taskContainer.Dockerfile, buildArgs),
LocalGroupId: p.NextTaskID(),
Instance: anyInstance,
Commands: []*api.Command{
{
Name: "build",
Instruction: &api.Command_ScriptInstruction{
ScriptInstruction: &api.ScriptInstruction{
Scripts: []string{script},
},
},
},
{
Name: "push",
Instruction: &api.Command_ScriptInstruction{
ScriptInstruction: &api.ScriptInstruction{
Scripts: []string{fmt.Sprintf("gcloud docker -- push gcr.io/cirrus-ci-community/%s:latest",
dockerfileHash)},
},
},
},
},
Environment: protoTask.Environment,
Metadata: &api.Task_Metadata{
Properties: environment.Merge(
task.DefaultTaskProperties(),
map[string]string{
"skip_notifications": "true",
},
),
},
}
// Some metadata property fields duplicate other fields
serviceTask.Metadata.Properties["indexWithinBuild"] = strconv.FormatInt(p.NextTaskLocalIndex(), 10)
// Some metadata property fields are preserved from the original task
serviceTask.Metadata.Properties["timeout_in"] = protoTask.Metadata.Properties["timeout_in"]
return serviceTask, nil
}
func (p *Parser) createServiceTasks(ctx context.Context, protoTasks []*api.Task) ([]*api.Task, error) {
serviceTasks := make(map[string]*api.Task)
for _, protoTask := range protoTasks {
var dynamicInstance ptypes.DynamicAny
err := ptypes.UnmarshalAny(protoTask.Instance, &dynamicInstance)
if errors.Is(err, protoregistry.NotFound) {
continue
}
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInternal, err)
}
taskContainer, ok := dynamicInstance.Message.(*api.ContainerInstance)
if !ok {
continue
}
if taskContainer.Dockerfile == "" {
continue
}
// Craft Docker build arguments: arguments used in content hash calculation
var hashableArgsSlice []string
for key, value := range taskContainer.DockerArguments {
hashableArgsSlice = append(hashableArgsSlice, key+value)
}
sort.Strings(hashableArgsSlice)
hashableArgs := strings.Join(hashableArgsSlice, ", ")
dockerfileHash, err := p.fileHash(ctx, taskContainer.Dockerfile, []byte(hashableArgs))
if err != nil {
return nil, err
}
// Find or create service task
serviceTaskKey := taskContainer.Dockerfile + hashableArgs
serviceTask, ok := serviceTasks[serviceTaskKey]
if !ok {
serviceTask, err = p.createServiceTask(dockerfileHash, protoTask, taskContainer)
if err != nil {
return nil, err
}
serviceTasks[serviceTaskKey] = serviceTask
}
// Set dependency to the found or created service task
protoTask.RequiredGroups = append(protoTask.RequiredGroups, serviceTask.LocalGroupId)
// Ensure that the task will use our to-be-created image
taskContainer.Image = fmt.Sprintf("gcr.io/cirrus-ci-community/%s:latest", dockerfileHash)
updatedInstance, err := ptypes.MarshalAny(taskContainer)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInternal, err)
}
protoTask.Instance = updatedInstance
}
// Extract map values to a slice
var result []*api.Task
for _, serviceTask := range serviceTasks {
result = append(result, serviceTask)
}
return result, nil
}
func ensureCloneInstruction(task *api.Task) {
for _, command := range task.Commands {
if command.Name == "clone" {
return
}
}
// Inherit "image" property from the first task (if any),
// or otherwise we might break Docker Pipe
var properties map[string]string
if len(task.Commands) != 0 {
image, ok := task.Commands[0].Properties["image"]
if ok {
properties = map[string]string{
"image": image,
}
delete(task.Commands[0].Properties, "image")
}
}
cloneCommand := &api.Command{
Name: "clone",
Instruction: &api.Command_CloneInstruction{
CloneInstruction: &api.CloneInstruction{},
},
Properties: properties,
}
task.Commands = append([]*api.Command{cloneCommand}, task.Commands...)
}