forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
376 lines (284 loc) · 9.49 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
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
package atc
import (
"fmt"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
"github.com/mitchellh/mapstructure"
)
type TaskConfig struct {
// The platform the task must run on (e.g. linux, windows).
Platform string `json:"platform,omitempty" yaml:"platform,omitempty" mapstructure:"platform"`
// Optional string specifying an image to use for the build. Depending on the
// platform, this may or may not be required (e.g. Windows/OS X vs. Linux).
RootfsURI string `json:"rootfs_uri,omitempty" yaml:"rootfs_uri,omitempty" mapstructure:"rootfs_uri"`
ImageResource *ImageResource `json:"image_resource,omitempty" yaml:"image_resource,omitempty" mapstructure:"image_resource"`
// Parameters to pass to the task via environment variables.
Params map[string]string `json:"params,omitempty" yaml:"params,omitempty" mapstructure:"params"`
// Script to execute.
Run TaskRunConfig `json:"run,omitempty" yaml:"run,omitempty" mapstructure:"run"`
// The set of (logical, name-only) inputs required by the task.
Inputs []TaskInputConfig `json:"inputs,omitempty" yaml:"inputs,omitempty" mapstructure:"inputs"`
// The set of (logical, name-only) outputs provided by the task.
Outputs []TaskOutputConfig `json:"outputs,omitempty" yaml:"outputs,omitempty" mapstructure:"outputs"`
// Path to cached directory that will be shared between builds for the same task.
Caches []CacheConfig `json:"caches,omitempty" yaml:"caches,omitempty" mapstructure:"caches"`
}
type ImageResource struct {
Type string `yaml:"type" json:"type" mapstructure:"type"`
Source Source `yaml:"source" json:"source" mapstructure:"source"`
Params *Params `yaml:"params,omitempty" json:"params,omitempty" mapstructure:"params"`
Version *Version `yaml:"version,omitempty" json:"version,omitempty" mapstructure:"version"`
}
func NewTaskConfig(configBytes []byte) (TaskConfig, error) {
var untypedInput map[string]interface{}
if err := yaml.Unmarshal(configBytes, &untypedInput); err != nil {
return TaskConfig{}, err
}
var config TaskConfig
var metadata mapstructure.Metadata
msConfig := &mapstructure.DecoderConfig{
Metadata: &metadata,
Result: &config,
WeaklyTypedInput: true,
DecodeHook: SanitizeDecodeHook,
}
decoder, err := mapstructure.NewDecoder(msConfig)
if err != nil {
return TaskConfig{}, err
}
if err := decoder.Decode(untypedInput); err != nil {
return TaskConfig{}, err
}
if len(metadata.Unused) > 0 {
keys := strings.Join(metadata.Unused, ", ")
return TaskConfig{}, fmt.Errorf("extra keys in the task configuration: %s", keys)
}
err = config.Validate()
if err != nil {
return TaskConfig{}, err
}
return config, nil
}
func (config TaskConfig) Merge(other TaskConfig) TaskConfig {
if other.Platform != "" {
config.Platform = other.Platform
}
if other.RootfsURI != "" {
config.RootfsURI = other.RootfsURI
}
if len(config.Params) > 0 {
newParams := map[string]string{}
for k, v := range config.Params {
newParams[k] = v
}
for k, v := range other.Params {
newParams[k] = v
}
config.Params = newParams
} else {
config.Params = other.Params
}
if len(other.Inputs) != 0 {
config.Inputs = other.Inputs
}
if other.Run.Path != "" {
config.Run = other.Run
}
return config
}
func (config TaskConfig) Validate() error {
messages := []string{}
if config.Platform == "" {
messages = append(messages, " missing 'platform'")
}
if config.Run.Path == "" {
messages = append(messages, " missing path to executable to run")
}
messages = append(messages, config.validateInputsAndOutputs()...)
if len(messages) > 0 {
return fmt.Errorf("invalid task configuration:\n%s", strings.Join(messages, "\n"))
}
return nil
}
func (config TaskConfig) validateInputsAndOutputs() []string {
messages := []string{}
messages = append(messages, config.validateInputContainsNames()...)
messages = append(messages, config.validateOutputContainsNames()...)
messages = append(messages, config.validateDotPath()...)
messages = append(messages, config.validateOverlappingPaths()...)
return messages
}
func (config TaskConfig) validateDotPath() []string {
messages := []string{}
pathCount := 0
dotPath := false
for _, input := range config.Inputs {
path := strings.TrimPrefix(input.resolvePath(), "./")
if path == "." {
dotPath = true
}
pathCount++
}
for _, output := range config.Outputs {
path := strings.TrimPrefix(output.resolvePath(), "./")
if path == "." {
dotPath = true
}
pathCount++
}
if pathCount > 1 && dotPath {
messages = append(messages, " you may not have more than one input or output when one of them has a path of '.'")
}
return messages
}
type pathCounter struct {
inputCount map[string]int
outputCount map[string]int
}
func (counter *pathCounter) foundInBoth(path string) bool {
_, inputFound := counter.inputCount[path]
_, outputFound := counter.outputCount[path]
return inputFound && outputFound
}
func (counter *pathCounter) registerInput(input TaskInputConfig) {
path := strings.TrimPrefix(input.resolvePath(), "./")
if val, found := counter.inputCount[path]; !found {
counter.inputCount[path] = 1
} else {
counter.inputCount[path] = val + 1
}
}
func (counter *pathCounter) registerOutput(output TaskOutputConfig) {
path := strings.TrimPrefix(output.resolvePath(), "./")
if val, found := counter.outputCount[path]; !found {
counter.outputCount[path] = 1
} else {
counter.outputCount[path] = val + 1
}
}
const duplicateErrorMessage = " cannot have more than one %s using the same path '%s'"
func pathContains(child string, parent string) bool {
if child == parent {
return false
}
childParts := strings.Split(child, string(filepath.Separator))
parentParts := strings.Split(parent, string(filepath.Separator))
if len(childParts) < len(parentParts) {
return false
}
for i, part := range parentParts {
if childParts[i] == part {
continue
} else {
return false
}
}
return true
}
func (counter pathCounter) getErrorMessages() []string {
messages := []string{}
for path, numOccurrences := range counter.inputCount {
if numOccurrences > 1 {
messages = append(messages, fmt.Sprintf(duplicateErrorMessage, "input", path))
}
if counter.foundInBoth(path) {
messages = append(messages, fmt.Sprintf(" cannot have an input and output using the same path '%s'", path))
}
for candidateParentPath := range counter.inputCount {
if pathContains(path, candidateParentPath) {
messages = append(messages, fmt.Sprintf(" cannot nest inputs: '%s' is nested under input directory '%s'", path, candidateParentPath))
}
}
for candidateParentPath := range counter.outputCount {
if pathContains(path, candidateParentPath) {
messages = append(messages, fmt.Sprintf(" cannot nest inputs within outputs: '%s' is nested under output directory '%s'", path, candidateParentPath))
}
}
}
for path, numOccurrences := range counter.outputCount {
if numOccurrences > 1 {
messages = append(messages, fmt.Sprintf(duplicateErrorMessage, "output", path))
}
for candidateParentPath := range counter.outputCount {
if pathContains(path, candidateParentPath) {
messages = append(messages, fmt.Sprintf(" cannot nest outputs: '%s' is nested under output directory '%s'", path, candidateParentPath))
}
}
for candidateParentPath := range counter.inputCount {
if pathContains(path, candidateParentPath) {
messages = append(messages, fmt.Sprintf(" cannot nest outputs within inputs: '%s' is nested under input directory '%s'", path, candidateParentPath))
}
}
}
return messages
}
func (config TaskConfig) countInputOutputPaths() pathCounter {
counter := &pathCounter{
inputCount: make(map[string]int),
outputCount: make(map[string]int),
}
for _, input := range config.Inputs {
counter.registerInput(input)
}
for _, output := range config.Outputs {
counter.registerOutput(output)
}
return *counter
}
func (config TaskConfig) validateOverlappingPaths() []string {
counter := config.countInputOutputPaths()
return counter.getErrorMessages()
}
func (config TaskConfig) validateOutputContainsNames() []string {
messages := []string{}
for i, output := range config.Outputs {
if output.Name == "" {
messages = append(messages, fmt.Sprintf(" output in position %d is missing a name", i))
}
}
return messages
}
func (config TaskConfig) validateInputContainsNames() []string {
messages := []string{}
for i, input := range config.Inputs {
if input.Name == "" {
messages = append(messages, fmt.Sprintf(" input in position %d is missing a name", i))
}
}
return messages
}
type TaskRunConfig struct {
Path string `json:"path" yaml:"path"`
Args []string `json:"args,omitempty" yaml:"args"`
Dir string `json:"dir,omitempty" yaml:"dir"`
// The user that the task will run as (defaults to whatever the docker image specifies)
User string `json:"user,omitempty" yaml:"user,omitempty" mapstructure:"user"`
}
type TaskInputConfig struct {
Name string `json:"name" yaml:"name"`
Path string `json:"path,omitempty" yaml:"path"`
}
func (input TaskInputConfig) resolvePath() string {
if input.Path != "" {
return input.Path
}
return input.Name
}
type TaskOutputConfig struct {
Name string `json:"name" yaml:"name"`
Path string `json:"path,omitempty" yaml:"path"`
}
func (output TaskOutputConfig) resolvePath() string {
if output.Path != "" {
return output.Path
}
return output.Name
}
type MetadataField struct {
Name string `json:"name"`
Value string `json:"value"`
}
type CacheConfig struct {
Path string `json:"path,omitempty" yaml:"path,omitempty" mapstructure:"path"`
}