-
Notifications
You must be signed in to change notification settings - Fork 18
/
task.go
269 lines (225 loc) · 8.03 KB
/
task.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
package task
import (
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/internal/executor/environment"
"github.com/cirruslabs/cirrus-cli/pkg/parser/boolevator"
"github.com/cirruslabs/cirrus-cli/pkg/parser/instance"
"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/schema"
"github.com/cirruslabs/cirrus-cli/pkg/parser/task/command"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
jsschema "github.com/lestrrat-go/jsschema"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/anypb"
"strconv"
"strings"
)
type Task struct {
proto api.Task
alias string
dependsOn []string
onlyIfExpression string
missingInstancesAllowed bool
parseable.DefaultParser
}
// nolint:gocognit,nestif // it's a parser helper, there is a lot of boilerplate
func NewTask(
env map[string]string,
boolevator *boolevator.Boolevator,
additionalInstances map[string]protoreflect.MessageDescriptor,
additionalTaskProperties []*descriptor.FieldDescriptorProto,
missingInstancesAllowed bool,
) *Task {
task := &Task{
missingInstancesAllowed: missingInstancesAllowed,
}
// Don't force required fields in schema
task.SetCollectible(true)
// Make sure environment is parsed first and then all the possible instance
// because an instance can set CIRRUS_OS which can be used in some field (for example, "name: Tests ($CIRRUS_OS)").
AttachEnvironmentFields(&task.DefaultParser, &task.proto)
if _, ok := additionalInstances["container"]; !ok {
task.CollectibleField("container",
instance.NewCommunityContainer(environment.Merge(task.proto.Environment, env), boolevator).Schema(),
func(node *node.Node) error {
inst := instance.NewCommunityContainer(environment.Merge(task.proto.Environment, env), boolevator)
containerInstance, err := inst.Parse(node)
if err != nil {
return err
}
// Retrieve the platform to update the environment
task.proto.Environment = environment.Merge(
task.proto.Environment,
map[string]string{"CIRRUS_OS": strings.ToLower(containerInstance.Platform.String())},
)
anyInstance, err := anypb.New(containerInstance)
if err != nil {
return err
}
task.proto.Instance = anyInstance
return nil
})
}
if _, ok := additionalInstances["windows_container"]; !ok {
task.CollectibleField("windows_container",
instance.NewWindowsCommunityContainer(environment.Merge(task.proto.Environment, env), boolevator).Schema(),
func(node *node.Node) error {
inst := instance.NewWindowsCommunityContainer(environment.Merge(task.proto.Environment, env), boolevator)
containerInstance, err := inst.Parse(node)
if err != nil {
return err
}
// Retrieve the platform to update the environment
task.proto.Environment = environment.Merge(
task.proto.Environment,
map[string]string{"CIRRUS_OS": strings.ToLower(containerInstance.Platform.String())},
)
anyInstance, err := anypb.New(containerInstance)
if err != nil {
return err
}
task.proto.Instance = anyInstance
return nil
})
}
if _, ok := additionalInstances["persistent_worker"]; !ok {
task.CollectibleField("persistent_worker",
instance.NewPersistentWorker(environment.Merge(task.proto.Environment, env)).Schema(),
func(node *node.Node) error {
inst := instance.NewPersistentWorker(environment.Merge(task.proto.Environment, env))
persistentWorkerInstance, err := inst.Parse(node)
if err != nil {
return err
}
if isolation := persistentWorkerInstance.Isolation; isolation != nil {
if parallels, ok := isolation.Type.(*api.Isolation_Parallels_); ok {
if parallels.Parallels != nil {
task.proto.Environment = environment.Merge(
task.proto.Environment,
map[string]string{"CIRRUS_OS": strings.ToLower(parallels.Parallels.Platform.String())},
)
}
}
} else {
// Clear CIRRUS_OS since we don't know where we will be running
delete(task.proto.Environment, "CIRRUS_OS")
}
anyInstance, err := anypb.New(persistentWorkerInstance)
if err != nil {
return err
}
task.proto.Instance = anyInstance
return nil
},
)
}
for instanceName, descriptor := range additionalInstances {
scopedInstanceName := instanceName
scopedDescriptor := descriptor
instanceSchema := instance.NewProtoParser(scopedDescriptor, nil, nil).Schema()
task.CollectibleField(scopedInstanceName, instanceSchema, func(node *node.Node) error {
parser := instance.NewProtoParser(scopedDescriptor, environment.Merge(task.proto.Environment, env), boolevator)
parserInstance, err := parser.Parse(node)
if err != nil {
return err
}
anyInstance, err := anypb.New(parserInstance)
if err != nil {
return err
}
task.proto.Instance = anyInstance
platform := instance.GuessPlatform(anyInstance, scopedDescriptor)
if platform != "" {
task.proto.Environment = environment.Merge(
task.proto.Environment, map[string]string{
"CIRRUS_OS": platform,
},
)
}
return nil
})
}
// Only after environment and instances should we add all the rest fields.
AttachBaseTaskFields(&task.DefaultParser, &task.proto, env, boolevator, additionalTaskProperties)
AttachBaseTaskInstructions(&task.DefaultParser, &task.proto, env, boolevator)
task.OptionalField(nameable.NewSimpleNameable("alias"), schema.String(""), func(node *node.Node) error {
name, err := node.GetExpandedStringValue(environment.Merge(task.proto.Environment, env))
if err != nil {
return err
}
task.alias = name
return nil
})
dependsOnSchema := schema.StringOrListOfStrings("List of task names this task depends on.")
task.OptionalField(nameable.NewSimpleNameable("depends_on"), dependsOnSchema, func(node *node.Node) error {
dependsOn, err := node.GetSliceOfNonEmptyStrings()
if err != nil {
return err
}
task.dependsOn = dependsOn
return nil
})
task.CollectibleField("only_if", schema.Condition(""), func(node *node.Node) error {
onlyIfExpression, err := node.GetStringValue()
if err != nil {
return err
}
task.onlyIfExpression = onlyIfExpression
return nil
})
return task
}
func (task *Task) Parse(node *node.Node) error {
if err := task.DefaultParser.Parse(node); err != nil {
return err
}
if task.proto.Instance == nil && !task.missingInstancesAllowed {
return node.ParserError("task has no instance attached")
}
// Since the parsing is almost done and other commands are expected,
// we can safely append cache upload commands, if applicable
task.proto.Commands = append(task.proto.Commands, command.GenUploadCacheCmds(task.proto.Commands)...)
return nil
}
func (task *Task) Name() string {
return task.proto.Name
}
func (task *Task) SetName(name string) {
task.proto.Name = name
}
func (task *Task) Alias() string {
return task.alias
}
func (task *Task) DependsOnNames() []string {
return task.dependsOn
}
func (task *Task) ID() int64 { return task.proto.LocalGroupId }
func (task *Task) SetID(id int64) {
task.proto.LocalGroupId = id
}
func (task *Task) SetIndexWithinBuild(id int64) {
task.proto.Metadata.Properties["indexWithinBuild"] = strconv.FormatInt(id, 10)
}
func (task *Task) DependsOnIDs() []int64 { return task.proto.RequiredGroups }
func (task *Task) SetDependsOnIDs(ids []int64) { task.proto.RequiredGroups = ids }
func (task *Task) Proto() interface{} {
return &task.proto
}
func (task *Task) Enabled(env map[string]string, boolevator *boolevator.Boolevator) (bool, error) {
if task.onlyIfExpression == "" {
return true, nil
}
evaluation, err := boolevator.Eval(task.onlyIfExpression, environment.Merge(task.proto.Environment, env))
if err != nil {
return false, err
}
return evaluation, nil
}
func (task *Task) Schema() *jsschema.Schema {
modifiedSchema := task.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Cirrus CI task definition."
return modifiedSchema
}