forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_factory.go
377 lines (316 loc) · 8.13 KB
/
build_factory.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
package factory
import (
"errors"
"github.com/concourse/atc"
"github.com/concourse/atc/db"
)
var ErrResourceNotFound = errors.New("resource not found")
//go:generate counterfeiter . BuildFactory
type BuildFactory interface {
Create(atc.JobConfig, atc.ResourceConfigs, atc.VersionedResourceTypes, []db.BuildInput) (atc.Plan, error)
}
type buildFactory struct {
PipelineID int
planFactory atc.PlanFactory
}
func NewBuildFactory(pipelineID int, planFactory atc.PlanFactory) BuildFactory {
return &buildFactory{
PipelineID: pipelineID,
planFactory: planFactory,
}
}
func (factory *buildFactory) Create(
job atc.JobConfig,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
inputs []db.BuildInput,
) (atc.Plan, error) {
plan, err := factory.constructPlanFromJob(job, resources, resourceTypes, inputs)
if err != nil {
return atc.Plan{}, err
}
return factory.applyHooks(constructionParams{
plan: plan,
hooks: job.Hooks(),
resources: resources,
resourceTypes: resourceTypes,
inputs: inputs,
})
}
func (factory *buildFactory) constructPlanFromJob(
job atc.JobConfig,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
inputs []db.BuildInput,
) (atc.Plan, error) {
planSequence := job.Plan
if len(planSequence) == 1 {
return factory.constructPlanFromConfig(
planSequence[0],
resources,
resourceTypes,
inputs,
)
}
return factory.do(planSequence, resources, resourceTypes, inputs)
}
func (factory *buildFactory) do(
planSequence atc.PlanSequence,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
inputs []db.BuildInput,
) (atc.Plan, error) {
do := atc.DoPlan{}
var err error
for _, planConfig := range planSequence {
nextStep, err := factory.constructPlanFromConfig(
planConfig,
resources,
resourceTypes,
inputs,
)
if err != nil {
return atc.Plan{}, err
}
do = append(do, nextStep)
}
return factory.planFactory.NewPlan(do), err
}
func (factory *buildFactory) constructPlanFromConfig(
planConfig atc.PlanConfig,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
inputs []db.BuildInput,
) (atc.Plan, error) {
var plan atc.Plan
var err error
if planConfig.Attempts == 0 {
plan, err = factory.constructUnhookedPlan(planConfig, resources, resourceTypes, inputs)
if err != nil {
return atc.Plan{}, err
}
} else {
retryStep := make(atc.RetryPlan, planConfig.Attempts)
for i := 0; i < planConfig.Attempts; i++ {
attempt, err := factory.constructUnhookedPlan(planConfig, resources, resourceTypes, inputs)
if err != nil {
return atc.Plan{}, err
}
retryStep[i] = attempt
}
plan = factory.planFactory.NewPlan(retryStep)
}
return factory.applyHooks(constructionParams{
plan: plan,
hooks: planConfig.Hooks(),
resources: resources,
resourceTypes: resourceTypes,
inputs: inputs,
})
}
func (factory *buildFactory) constructUnhookedPlan(
planConfig atc.PlanConfig,
resources atc.ResourceConfigs,
resourceTypes atc.VersionedResourceTypes,
inputs []db.BuildInput,
) (atc.Plan, error) {
var plan atc.Plan
var err error
switch {
case planConfig.Do != nil:
plan, err = factory.do(
*planConfig.Do,
resources,
resourceTypes,
inputs,
)
if err != nil {
return atc.Plan{}, err
}
case planConfig.Put != "":
logicalName := planConfig.Put
resourceName := planConfig.Resource
if resourceName == "" {
resourceName = logicalName
}
resource, found := resources.Lookup(resourceName)
if !found {
return atc.Plan{}, ErrResourceNotFound
}
putPlan := factory.planFactory.NewPlan(atc.PutPlan{
Type: resource.Type,
Name: logicalName,
Resource: resourceName,
Source: resource.Source,
Params: planConfig.Params,
Tags: planConfig.Tags,
VersionedResourceTypes: resourceTypes,
})
dependentGetPlan := factory.planFactory.NewPlan(atc.GetPlan{
Type: resource.Type,
Name: logicalName,
Resource: resourceName,
VersionFrom: &putPlan.ID,
Params: planConfig.GetParams,
Tags: planConfig.Tags,
Source: resource.Source,
VersionedResourceTypes: resourceTypes,
})
plan = factory.planFactory.NewPlan(atc.OnSuccessPlan{
Step: putPlan,
Next: dependentGetPlan,
})
case planConfig.Get != "":
resourceName := planConfig.Resource
if resourceName == "" {
resourceName = planConfig.Get
}
resource, found := resources.Lookup(resourceName)
if !found {
return atc.Plan{}, ErrResourceNotFound
}
name := planConfig.Get
var version atc.Version
for _, input := range inputs {
if input.Name == name {
version = atc.Version(input.Version)
break
}
}
plan = factory.planFactory.NewPlan(atc.GetPlan{
Type: resource.Type,
Name: name,
Resource: resourceName,
Source: resource.Source,
Params: planConfig.Params,
Version: &version,
Tags: planConfig.Tags,
VersionedResourceTypes: resourceTypes,
})
case planConfig.Task != "":
plan = factory.planFactory.NewPlan(atc.TaskPlan{
Name: planConfig.Task,
Privileged: planConfig.Privileged,
Config: planConfig.TaskConfig,
ConfigPath: planConfig.TaskConfigPath,
Tags: planConfig.Tags,
Params: planConfig.Params,
InputMapping: planConfig.InputMapping,
OutputMapping: planConfig.OutputMapping,
ImageArtifactName: planConfig.ImageArtifactName,
VersionedResourceTypes: resourceTypes,
})
case planConfig.Try != nil:
nextStep, err := factory.constructPlanFromConfig(
*planConfig.Try,
resources,
resourceTypes,
inputs,
)
if err != nil {
return atc.Plan{}, err
}
plan = factory.planFactory.NewPlan(atc.TryPlan{
Step: nextStep,
})
case planConfig.Aggregate != nil:
aggregate := atc.AggregatePlan{}
for _, planConfig := range *planConfig.Aggregate {
nextStep, err := factory.constructPlanFromConfig(
planConfig,
resources,
resourceTypes,
inputs,
)
if err != nil {
return atc.Plan{}, err
}
aggregate = append(aggregate, nextStep)
}
plan = factory.planFactory.NewPlan(aggregate)
}
if planConfig.Timeout != "" {
plan = factory.planFactory.NewPlan(atc.TimeoutPlan{
Duration: planConfig.Timeout,
Step: plan,
})
}
return plan, nil
}
type constructionParams struct {
plan atc.Plan
hooks atc.Hooks
resources atc.ResourceConfigs
resourceTypes atc.VersionedResourceTypes
inputs []db.BuildInput
}
func (factory *buildFactory) applyHooks(cp constructionParams) (atc.Plan, error) {
var err error
cp, err = factory.failureIfPresent(cp)
if err != nil {
return atc.Plan{}, err
}
cp, err = factory.successIfPresent(cp)
if err != nil {
return atc.Plan{}, err
}
cp, err = factory.ensureIfPresent(cp)
if err != nil {
return atc.Plan{}, err
}
return cp.plan, nil
}
func (factory *buildFactory) successIfPresent(cp constructionParams) (constructionParams, error) {
if cp.hooks.Success != nil {
nextPlan, err := factory.constructPlanFromConfig(
*cp.hooks.Success,
cp.resources,
cp.resourceTypes,
cp.inputs,
)
if err != nil {
return constructionParams{}, err
}
cp.plan = factory.planFactory.NewPlan(atc.OnSuccessPlan{
Step: cp.plan,
Next: nextPlan,
})
}
return cp, nil
}
func (factory *buildFactory) failureIfPresent(cp constructionParams) (constructionParams, error) {
if cp.hooks.Failure != nil {
nextPlan, err := factory.constructPlanFromConfig(
*cp.hooks.Failure,
cp.resources,
cp.resourceTypes,
cp.inputs,
)
if err != nil {
return constructionParams{}, err
}
cp.plan = factory.planFactory.NewPlan(atc.OnFailurePlan{
Step: cp.plan,
Next: nextPlan,
})
}
return cp, nil
}
func (factory *buildFactory) ensureIfPresent(cp constructionParams) (constructionParams, error) {
if cp.hooks.Ensure != nil {
nextPlan, err := factory.constructPlanFromConfig(
*cp.hooks.Ensure,
cp.resources,
cp.resourceTypes,
cp.inputs,
)
if err != nil {
return constructionParams{}, err
}
cp.plan = factory.planFactory.NewPlan(atc.EnsurePlan{
Step: cp.plan,
Next: nextPlan,
})
}
return cp, nil
}