forked from vmware-archive/atc
/
garden_factory.go
226 lines (183 loc) · 6.65 KB
/
garden_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
package exec
import (
"crypto/sha1"
"fmt"
"path/filepath"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/creds"
"github.com/concourse/atc/db"
"github.com/concourse/atc/resource"
"github.com/concourse/atc/worker"
)
type gardenFactory struct {
workerClient worker.Client
resourceFetcher resource.Fetcher
resourceFactory resource.ResourceFactory
dbResourceCacheFactory db.ResourceCacheFactory
variablesFactory creds.VariablesFactory
putActions map[atc.PlanID]*PutAction
}
func NewGardenFactory(
workerClient worker.Client,
resourceFetcher resource.Fetcher,
resourceFactory resource.ResourceFactory,
dbResourceCacheFactory db.ResourceCacheFactory,
variablesFactory creds.VariablesFactory,
) Factory {
return &gardenFactory{
workerClient: workerClient,
resourceFetcher: resourceFetcher,
resourceFactory: resourceFactory,
dbResourceCacheFactory: dbResourceCacheFactory,
variablesFactory: variablesFactory,
putActions: map[atc.PlanID]*PutAction{},
}
}
type GetStepFactory struct {
ActionsStep
}
type GetStep Step
func (s GetStepFactory) Using(repository *worker.ArtifactRepository) Step {
return GetStep(s.ActionsStep.Using(repository))
}
type PutStepFactory struct {
ActionsStep
}
type PutStep Step
func (s PutStepFactory) Using(repository *worker.ArtifactRepository) Step {
return PutStep(s.ActionsStep.Using(repository))
}
type TaskStepFactory struct {
ActionsStep
}
type TaskStep Step
func (s TaskStepFactory) Using(repository *worker.ArtifactRepository) Step {
return TaskStep(s.ActionsStep.Using(repository))
}
func (factory *gardenFactory) Get(
logger lager.Logger,
plan atc.Plan,
build db.Build,
stepMetadata StepMetadata,
workerMetadata db.ContainerMetadata,
buildEventsDelegate ActionsBuildEventsDelegate,
imageFetchingDelegate ImageFetchingDelegate,
) StepFactory {
workerMetadata.WorkingDirectory = resource.ResourcesDir("get")
variables := factory.variablesFactory.NewVariables(build.TeamName(), build.PipelineName())
getAction := &GetAction{
Type: plan.Get.Type,
Name: plan.Get.Name,
Resource: plan.Get.Resource,
Source: creds.NewSource(variables, plan.Get.Source),
Params: plan.Get.Params,
VersionSource: NewVersionSourceFromPlan(plan.Get, factory.putActions),
Tags: plan.Get.Tags,
Outputs: []string{plan.Get.Name},
imageFetchingDelegate: imageFetchingDelegate,
resourceFetcher: factory.resourceFetcher,
teamID: build.TeamID(),
buildID: build.ID(),
planID: plan.ID,
containerMetadata: workerMetadata,
dbResourceCacheFactory: factory.dbResourceCacheFactory,
stepMetadata: stepMetadata,
// TODO: remove after all actions are introduced
resourceTypes: creds.NewVersionedResourceTypes(variables, plan.Get.VersionedResourceTypes),
}
actions := []Action{getAction}
return GetStepFactory{NewActionsStep(logger, actions, buildEventsDelegate)}
}
func (factory *gardenFactory) Put(
logger lager.Logger,
plan atc.Plan,
build db.Build,
stepMetadata StepMetadata,
workerMetadata db.ContainerMetadata,
buildEventsDelegate ActionsBuildEventsDelegate,
imageFetchingDelegate ImageFetchingDelegate,
) StepFactory {
workerMetadata.WorkingDirectory = resource.ResourcesDir("put")
variables := factory.variablesFactory.NewVariables(build.TeamName(), build.PipelineName())
putAction := &PutAction{
Type: plan.Put.Type,
Name: plan.Put.Name,
Resource: plan.Put.Resource,
Source: creds.NewSource(variables, plan.Put.Source),
Params: plan.Put.Params,
Tags: plan.Put.Tags,
imageFetchingDelegate: imageFetchingDelegate,
resourceFactory: factory.resourceFactory,
teamID: build.TeamID(),
buildID: build.ID(),
planID: plan.ID,
containerMetadata: workerMetadata,
stepMetadata: stepMetadata,
resourceTypes: creds.NewVersionedResourceTypes(variables, plan.Put.VersionedResourceTypes),
}
factory.putActions[plan.ID] = putAction
actions := []Action{putAction}
return PutStepFactory{NewActionsStep(logger, actions, buildEventsDelegate)}
}
func (factory *gardenFactory) Task(
logger lager.Logger,
plan atc.Plan,
build db.Build,
containerMetadata db.ContainerMetadata,
taskBuildEventsDelegate TaskBuildEventsDelegate,
buildEventsDelegate ActionsBuildEventsDelegate,
imageFetchingDelegate ImageFetchingDelegate,
) StepFactory {
workingDirectory := factory.taskWorkingDirectory(worker.ArtifactName(plan.Task.Name))
containerMetadata.WorkingDirectory = workingDirectory
var taskConfigFetcher TaskConfigFetcher
if plan.Task.ConfigPath != "" && (plan.Task.Config != nil || plan.Task.Params != nil) {
taskConfigFetcher = MergedConfigFetcher{
A: FileConfigFetcher{plan.Task.ConfigPath},
B: StaticConfigFetcher{Plan: *plan.Task},
}
} else if plan.Task.Config != nil {
taskConfigFetcher = StaticConfigFetcher{Plan: *plan.Task}
} else if plan.Task.ConfigPath != "" {
taskConfigFetcher = FileConfigFetcher{plan.Task.ConfigPath}
}
taskConfigFetcher = ValidatingConfigFetcher{ConfigFetcher: taskConfigFetcher}
taskConfigFetcher = DeprecationConfigFetcher{
Delegate: taskConfigFetcher,
Stderr: imageFetchingDelegate.Stderr(),
}
fetchConfigAction := &FetchConfigAction{
configFetcher: taskConfigFetcher,
}
configSource := &FetchConfigActionTaskConfigSource{
Action: fetchConfigAction,
}
variables := factory.variablesFactory.NewVariables(build.TeamName(), build.PipelineName())
taskAction := &TaskAction{
privileged: Privileged(plan.Task.Privileged),
configSource: configSource,
tags: plan.Task.Tags,
inputMapping: plan.Task.InputMapping,
outputMapping: plan.Task.OutputMapping,
artifactsRoot: workingDirectory,
imageArtifactName: plan.Task.ImageArtifactName,
buildEventsDelegate: taskBuildEventsDelegate,
imageFetchingDelegate: imageFetchingDelegate,
workerPool: factory.workerClient,
teamID: build.TeamID(),
buildID: build.ID(),
jobID: build.JobID(),
stepName: plan.Task.Name,
planID: plan.ID,
containerMetadata: containerMetadata,
resourceTypes: creds.NewVersionedResourceTypes(variables, plan.Task.VersionedResourceTypes),
variables: variables,
}
actions := []Action{fetchConfigAction, taskAction}
return TaskStepFactory{NewActionsStep(logger, actions, buildEventsDelegate)}
}
func (factory *gardenFactory) taskWorkingDirectory(sourceName worker.ArtifactName) string {
sum := sha1.Sum([]byte(sourceName))
return filepath.Join("/tmp", "build", fmt.Sprintf("%x", sum[:4]))
}