forked from vmware-archive/atc
/
task_step.go
559 lines (453 loc) · 13.8 KB
/
task_step.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package exec
import (
"archive/tar"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"path"
"path/filepath"
"strconv"
"strings"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/lager/lagerctx"
"github.com/concourse/atc"
"github.com/concourse/atc/creds"
"github.com/concourse/atc/db"
"github.com/concourse/atc/worker"
)
const taskProcessID = "task"
const taskProcessPropertyName = "concourse:task-process"
const taskExitStatusPropertyName = "concourse:exit-status"
// MissingInputsError is returned when any of the task's required inputs are
// missing.
type MissingInputsError struct {
Inputs []string
}
// Error prints a human-friendly message listing the inputs that were missing.
func (err MissingInputsError) Error() string {
return fmt.Sprintf("missing inputs: %s", strings.Join(err.Inputs, ", "))
}
type MissingTaskImageSourceError struct {
SourceName string
}
func (err MissingTaskImageSourceError) Error() string {
return fmt.Sprintf(`missing image artifact source: %s
make sure there's a corresponding 'get' step, or a task that produces it as an output`, err.SourceName)
}
type TaskImageSourceParametersError struct {
Err error
}
func (err TaskImageSourceParametersError) Error() string {
return fmt.Sprintf("failed to evaluate image resource parameters: %s", err.Err)
}
//go:generate counterfeiter . TaskDelegate
type TaskDelegate interface {
BuildStepDelegate
Initializing(lager.Logger, atc.TaskConfig)
Starting(lager.Logger, atc.TaskConfig)
Finished(lager.Logger, ExitStatus)
}
// TaskStep executes a TaskConfig, whose inputs will be fetched from the
// worker.ArtifactRepository and outputs will be added to the worker.ArtifactRepository.
type TaskStep struct {
privileged Privileged
configSource TaskConfigSource
tags atc.Tags
inputMapping map[string]string
outputMapping map[string]string
artifactsRoot string
imageArtifactName string
delegate TaskDelegate
workerPool worker.Client
teamID int
buildID int
jobID int
stepName string
planID atc.PlanID
containerMetadata db.ContainerMetadata
resourceTypes creds.VersionedResourceTypes
variables creds.Variables
succeeded bool
}
func NewTaskStep(
privileged Privileged,
configSource TaskConfigSource,
tags atc.Tags,
inputMapping map[string]string,
outputMapping map[string]string,
artifactsRoot string,
imageArtifactName string,
delegate TaskDelegate,
workerPool worker.Client,
teamID int,
buildID int,
jobID int,
stepName string,
planID atc.PlanID,
containerMetadata db.ContainerMetadata,
resourceTypes creds.VersionedResourceTypes,
variables creds.Variables,
) Step {
return &TaskStep{
privileged: privileged,
configSource: configSource,
tags: tags,
inputMapping: inputMapping,
outputMapping: outputMapping,
artifactsRoot: artifactsRoot,
imageArtifactName: imageArtifactName,
delegate: delegate,
workerPool: workerPool,
teamID: teamID,
buildID: buildID,
jobID: jobID,
stepName: stepName,
planID: planID,
containerMetadata: containerMetadata,
resourceTypes: resourceTypes,
variables: variables,
}
}
// Run will first selects the worker based on the TaskConfig's platform, the
// TaskStep's tags, and prioritized by availability of volumes for the TaskConfig's
// inputs. Inputs that did not have volumes available on the worker will be streamed
// in to the container.
//
// If any inputs are not available in the worker.ArtifactRepository, MissingInputsError
// is returned.
//
// Once all the inputs are satisfied, the task's script will be executed. If
// the task is canceled via the context, the script will be interrupted.
//
// If the script exits successfully, the outputs specified in the TaskConfig
// are registered with the worker.ArtifactRepository. If no outputs are specified, the
// task's entire working directory is registered as an ArtifactSource under the
// name of the task.
func (action *TaskStep) Run(ctx context.Context, state RunState) error {
logger := lagerctx.FromContext(ctx)
repository := state.Artifacts()
config, err := action.configSource.FetchConfig(repository)
if err != nil {
return err
}
action.delegate.Initializing(logger, config)
containerSpec, err := action.containerSpec(logger, repository, config)
if err != nil {
return err
}
container, err := action.workerPool.FindOrCreateContainer(
ctx,
logger,
action.delegate,
db.NewBuildStepContainerOwner(action.buildID, action.planID),
action.containerMetadata,
containerSpec,
action.resourceTypes,
)
if err != nil {
return err
}
exitStatusProp, err := container.Property(taskExitStatusPropertyName)
if err == nil {
logger.Info("already-exited", lager.Data{"status": exitStatusProp})
status, err := strconv.Atoi(exitStatusProp)
if err != nil {
return err
}
action.succeeded = status == 0
err = action.registerOutputs(logger, repository, config, container)
if err != nil {
return err
}
return nil
}
// for backwards compatibility with containers
// that had their task process name set as property
var processID string
processID, err = container.Property(taskProcessPropertyName)
if err != nil {
processID = taskProcessID
}
processIO := garden.ProcessIO{
Stdout: action.delegate.Stdout(),
Stderr: action.delegate.Stderr(),
}
process, err := container.Attach(processID, processIO)
if err == nil {
logger.Info("already-running")
} else {
logger.Info("spawning")
action.delegate.Starting(logger, config)
process, err = container.Run(garden.ProcessSpec{
ID: taskProcessID,
Path: config.Run.Path,
Args: config.Run.Args,
Dir: path.Join(action.artifactsRoot, config.Run.Dir),
// Guardian sets the default TTY window size to width: 80, height: 24,
// which creates ANSI control sequences that do not work with other window sizes
TTY: &garden.TTYSpec{WindowSize: &garden.WindowSize{Columns: 500, Rows: 500}},
}, processIO)
}
if err != nil {
return err
}
logger.Info("attached")
exited := make(chan struct{})
var processStatus int
var processErr error
go func() {
processStatus, processErr = process.Wait()
close(exited)
}()
select {
case <-ctx.Done():
err = action.registerOutputs(logger, repository, config, container)
if err != nil {
return err
}
err = container.Stop(false)
if err != nil {
logger.Error("stopping-container", err)
}
<-exited
return ctx.Err()
case <-exited:
if processErr != nil {
return processErr
}
err = action.registerOutputs(logger, repository, config, container)
if err != nil {
return err
}
action.delegate.Finished(logger, ExitStatus(processStatus))
err = container.SetProperty(taskExitStatusPropertyName, fmt.Sprintf("%d", processStatus))
if err != nil {
return err
}
action.succeeded = processStatus == 0
return nil
}
}
func (action *TaskStep) Succeeded() bool {
return action.succeeded
}
func (action *TaskStep) containerSpec(logger lager.Logger, repository *worker.ArtifactRepository, config atc.TaskConfig) (worker.ContainerSpec, error) {
imageSpec := worker.ImageSpec{
Privileged: bool(action.privileged),
}
if action.imageArtifactName != "" {
source, found := repository.SourceFor(worker.ArtifactName(action.imageArtifactName))
if !found {
return worker.ContainerSpec{}, MissingTaskImageSourceError{action.imageArtifactName}
}
imageSpec.ImageArtifactSource = source
imageSpec.ImageArtifactName = worker.ArtifactName(action.imageArtifactName)
} else if config.ImageResource != nil {
imageSpec.ImageResource = &worker.ImageResource{
Type: config.ImageResource.Type,
Source: creds.NewSource(action.variables, config.ImageResource.Source),
Params: config.ImageResource.Params,
Version: config.ImageResource.Version,
}
} else if config.RootfsURI != "" {
imageSpec.ImageURL = config.RootfsURI
}
params, err := creds.NewTaskParams(action.variables, config.Params).Evaluate()
if err != nil {
return worker.ContainerSpec{}, err
}
containerSpec := worker.ContainerSpec{
Platform: config.Platform,
Tags: action.tags,
TeamID: action.teamID,
ImageSpec: imageSpec,
User: config.Run.User,
Dir: action.artifactsRoot,
Env: action.envForParams(params),
Inputs: []worker.InputSource{},
Outputs: worker.OutputPaths{},
}
var missingRequiredInputs []string
for _, input := range config.Inputs {
inputName := input.Name
if sourceName, ok := action.inputMapping[inputName]; ok {
inputName = sourceName
}
source, found := repository.SourceFor(worker.ArtifactName(inputName))
if !found {
if !input.Optional {
missingRequiredInputs = append(missingRequiredInputs, inputName)
}
continue
}
containerSpec.Inputs = append(containerSpec.Inputs, &taskInputSource{
config: input,
source: source,
artifactsRoot: action.artifactsRoot,
})
}
if len(missingRequiredInputs) > 0 {
return worker.ContainerSpec{}, MissingInputsError{missingRequiredInputs}
}
for _, cacheConfig := range config.Caches {
source := newTaskCacheSource(logger, action.teamID, action.jobID, action.stepName, cacheConfig.Path)
containerSpec.Inputs = append(containerSpec.Inputs, &taskCacheInputSource{
source: source,
artifactsRoot: action.artifactsRoot,
cachePath: cacheConfig.Path,
})
}
for _, output := range config.Outputs {
path := artifactsPath(output, action.artifactsRoot)
containerSpec.Outputs[output.Name] = path
}
return containerSpec, nil
}
func (action *TaskStep) registerOutputs(logger lager.Logger, repository *worker.ArtifactRepository, config atc.TaskConfig, container worker.Container) error {
volumeMounts := container.VolumeMounts()
logger.Debug("registering-outputs", lager.Data{"outputs": config.Outputs})
for _, output := range config.Outputs {
outputName := output.Name
if destinationName, ok := action.outputMapping[output.Name]; ok {
outputName = destinationName
}
outputPath := artifactsPath(output, action.artifactsRoot)
for _, mount := range volumeMounts {
if mount.MountPath == outputPath {
source := newTaskArtifactSource(logger, mount.Volume)
repository.RegisterSource(worker.ArtifactName(outputName), source)
}
}
}
// Do not initialize caches for one-off builds
if action.jobID != 0 {
logger.Debug("initializing-caches", lager.Data{"caches": config.Caches})
for _, cacheConfig := range config.Caches {
for _, volumeMount := range volumeMounts {
if volumeMount.MountPath == filepath.Join(action.artifactsRoot, cacheConfig.Path) {
logger.Debug("initializing-cache", lager.Data{"path": volumeMount.MountPath})
err := volumeMount.Volume.InitializeTaskCache(logger, action.jobID, action.stepName, cacheConfig.Path, bool(action.privileged))
if err != nil {
return err
}
continue
}
}
}
}
return nil
}
func (TaskStep) envForParams(params map[string]string) []string {
env := make([]string, 0, len(params))
for k, v := range params {
env = append(env, k+"="+v)
}
return env
}
type taskArtifactSource struct {
logger lager.Logger
volume worker.Volume
}
func newTaskArtifactSource(
logger lager.Logger,
volume worker.Volume,
) *taskArtifactSource {
return &taskArtifactSource{
logger: logger,
volume: volume,
}
}
func (src *taskArtifactSource) StreamTo(destination worker.ArtifactDestination) error {
out, err := src.volume.StreamOut(".")
if err != nil {
return err
}
defer out.Close()
return destination.StreamIn(".", out)
}
func (src *taskArtifactSource) StreamFile(filename string) (io.ReadCloser, error) {
out, err := src.volume.StreamOut(filename)
if err != nil {
return nil, err
}
gzReader, err := gzip.NewReader(out)
if err != nil {
return nil, FileNotFoundError{Path: filename}
}
tarReader := tar.NewReader(gzReader)
_, err = tarReader.Next()
if err != nil {
return nil, FileNotFoundError{Path: filename}
}
return fileReadCloser{
Reader: tarReader,
Closer: out,
}, nil
}
func (src *taskArtifactSource) VolumeOn(w worker.Worker) (worker.Volume, bool, error) {
return w.LookupVolume(src.logger, src.volume.Handle())
}
type taskInputSource struct {
config atc.TaskInputConfig
source worker.ArtifactSource
artifactsRoot string
}
func (s *taskInputSource) Source() worker.ArtifactSource { return s.source }
func (s *taskInputSource) DestinationPath() string {
subdir := s.config.Path
if s.config.Path == "" {
subdir = s.config.Name
}
return filepath.Join(s.artifactsRoot, subdir)
}
func artifactsPath(outputConfig atc.TaskOutputConfig, artifactsRoot string) string {
outputSrc := outputConfig.Path
if len(outputSrc) == 0 {
outputSrc = outputConfig.Name
}
return path.Join(artifactsRoot, outputSrc) + "/"
}
type taskCacheInputSource struct {
source worker.ArtifactSource
artifactsRoot string
cachePath string
}
func (s *taskCacheInputSource) Source() worker.ArtifactSource { return s.source }
func (s *taskCacheInputSource) DestinationPath() string {
return filepath.Join(s.artifactsRoot, s.cachePath)
}
type taskCacheSource struct {
logger lager.Logger
teamID int
jobID int
stepName string
path string
}
func newTaskCacheSource(
logger lager.Logger,
teamID int,
jobID int,
stepName string,
path string,
) *taskCacheSource {
return &taskCacheSource{
logger: logger,
teamID: teamID,
jobID: jobID,
stepName: stepName,
path: path,
}
}
func (src *taskCacheSource) StreamTo(destination worker.ArtifactDestination) error {
// cache will be initialized every time on a new worker
return nil
}
func (src *taskCacheSource) StreamFile(filename string) (io.ReadCloser, error) {
return nil, errors.New("taskCacheSource.StreamFile not implemented")
}
func (src *taskCacheSource) VolumeOn(w worker.Worker) (worker.Volume, bool, error) {
return w.FindVolumeForTaskCache(src.logger, src.teamID, src.jobID, src.stepName, src.path)
}