This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
362 lines (281 loc) · 10.4 KB
/
config.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
package atc
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
const ConfigVersionHeader = "X-Concourse-Config-Version"
const DefaultPipelineName = "main"
const DefaultTeamName = "main"
type Tags []string
type ConfigResponse struct {
Config *Config `json:"config"`
Errors []string `json:"errors"`
RawConfig RawConfig `json:"raw_config"`
}
type Config struct {
Groups GroupConfigs `yaml:"groups" json:"groups" mapstructure:"groups"`
Resources ResourceConfigs `yaml:"resources" json:"resources" mapstructure:"resources"`
ResourceTypes ResourceTypes `yaml:"resource_types" json:"resource_types" mapstructure:"resource_types"`
Jobs JobConfigs `yaml:"jobs" json:"jobs" mapstructure:"jobs"`
}
type RawConfig string
func (r RawConfig) String() string {
return string(r)
}
type GroupConfig struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
Jobs []string `yaml:"jobs,omitempty" json:"jobs,omitempty" mapstructure:"jobs"`
Resources []string `yaml:"resources,omitempty" json:"resources,omitempty" mapstructure:"resources"`
}
type GroupConfigs []GroupConfig
func (groups GroupConfigs) Lookup(name string) (GroupConfig, bool) {
for _, group := range groups {
if group.Name == name {
return group, true
}
}
return GroupConfig{}, false
}
type ResourceConfig struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
WebhookToken string `yaml:"webhook_token,omitempty" json:"webhook_token" mapstructure:"webhook_token"`
Type string `yaml:"type" json:"type" mapstructure:"type"`
Source Source `yaml:"source" json:"source" mapstructure:"source"`
CheckEvery string `yaml:"check_every,omitempty" json:"check_every" mapstructure:"check_every"`
Tags Tags `yaml:"tags,omitempty" json:"tags" mapstructure:"tags"`
}
type ResourceType struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
Type string `yaml:"type" json:"type" mapstructure:"type"`
Source Source `yaml:"source" json:"source" mapstructure:"source"`
Privileged bool `yaml:"privileged,omitempty" json:"privileged" mapstructure:"privileged"`
Tags Tags `yaml:"tags,omitempty" json:"tags" mapstructure:"tags"`
Params Params `yaml:"params,omitempty" json:"params" mapstructure:"params"`
}
type ResourceTypes []ResourceType
func (types ResourceTypes) Lookup(name string) (ResourceType, bool) {
for _, t := range types {
if t.Name == name {
return t, true
}
}
return ResourceType{}, false
}
func (types ResourceTypes) Without(name string) ResourceTypes {
newTypes := ResourceTypes{}
for _, t := range types {
if t.Name != name {
newTypes = append(newTypes, t)
}
}
return newTypes
}
type Hooks struct {
Abort *PlanConfig
Failure *PlanConfig
Ensure *PlanConfig
Success *PlanConfig
}
// A PlanSequence corresponds to a chain of Compose plan, with an implicit
// `on: [success]` after every Task plan.
type PlanSequence []PlanConfig
// A VersionConfig represents the choice to include every version of a
// resource, the latest version of a resource, or a pinned (specific) one.
type VersionConfig struct {
Every bool `yaml:"every,omitempty" json:"every,omitempty"`
Latest bool `yaml:"latest,omitempty" json:"latest,omitempty"`
Pinned Version `yaml:"pinned,omitempty" json:"pinned,omitempty"`
}
func (c *VersionConfig) UnmarshalJSON(version []byte) error {
var data interface{}
err := json.Unmarshal(version, &data)
if err != nil {
return err
}
switch actual := data.(type) {
case string:
c.Every = actual == "every"
c.Latest = actual == "latest"
case map[string]interface{}:
version := Version{}
for k, v := range actual {
if s, ok := v.(string); ok {
version[k] = strings.TrimSpace(s)
}
}
c.Pinned = version
default:
return errors.New("unknown type for version")
}
return nil
}
func (c *VersionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
var data interface{}
err := unmarshal(&data)
if err != nil {
return err
}
switch actual := data.(type) {
case string:
c.Every = actual == "every"
c.Latest = actual == "latest"
case map[interface{}]interface{}:
version := Version{}
for k, v := range actual {
if ks, ok := k.(string); ok {
if vs, ok := v.(string); ok {
version[ks] = strings.TrimSpace(vs)
}
}
}
c.Pinned = version
default:
return errors.New("unknown type for version")
}
return nil
}
func (c *VersionConfig) MarshalYAML() (interface{}, error) {
if c.Latest {
return VersionLatest, nil
}
if c.Every {
return VersionEvery, nil
}
if c.Pinned != nil {
return c.Pinned, nil
}
return nil, nil
}
func (c *VersionConfig) MarshalJSON() ([]byte, error) {
if c.Latest {
return json.Marshal(VersionLatest)
}
if c.Every {
return json.Marshal(VersionEvery)
}
if c.Pinned != nil {
return json.Marshal(c.Pinned)
}
return json.Marshal("")
}
// A PlanConfig is a flattened set of configuration corresponding to
// a particular Plan, where Source and Version are populated lazily.
type PlanConfig struct {
// makes the Plan conditional
// conditions on which to perform a nested sequence
// compose a nested sequence of plans
// name of the nested 'do'
RawName string `yaml:"name,omitempty" json:"name,omitempty" mapstructure:"name"`
// a nested chain of steps to run
Do *PlanSequence `yaml:"do,omitempty" json:"do,omitempty" mapstructure:"do"`
// corresponds to an Aggregate plan, keyed by the name of each sub-plan
Aggregate *PlanSequence `yaml:"aggregate,omitempty" json:"aggregate,omitempty" mapstructure:"aggregate"`
// corresponds to Get and Put resource plans, respectively
// name of 'input', e.g. bosh-stemcell
Get string `yaml:"get,omitempty" json:"get,omitempty" mapstructure:"get"`
// jobs that this resource must have made it through
Passed []string `yaml:"passed,omitempty" json:"passed,omitempty" mapstructure:"passed"`
// whether to trigger based on this resource changing
Trigger bool `yaml:"trigger,omitempty" json:"trigger,omitempty" mapstructure:"trigger"`
// name of 'output', e.g. rootfs-tarball
Put string `yaml:"put,omitempty" json:"put,omitempty" mapstructure:"put"`
// corresponding resource config, e.g. aws-stemcell
Resource string `yaml:"resource,omitempty" json:"resource,omitempty" mapstructure:"resource"`
// corresponds to a Task plan
// name of 'task', e.g. unit, go1.3, go1.4
Task string `yaml:"task,omitempty" json:"task,omitempty" mapstructure:"task"`
// run task privileged
Privileged bool `yaml:"privileged,omitempty" json:"privileged,omitempty" mapstructure:"privileged"`
// task config path, e.g. foo/build.yml
TaskConfigPath string `yaml:"file,omitempty" json:"file,omitempty" mapstructure:"file"`
// inlined task config
TaskConfig *TaskConfig `yaml:"config,omitempty" json:"config,omitempty" mapstructure:"config"`
// used by Get and Put for specifying params to the resource
Params Params `yaml:"params,omitempty" json:"params,omitempty" mapstructure:"params"`
// used to pass specific inputs/outputs as generic inputs/outputs in task config
InputMapping map[string]string `yaml:"input_mapping,omitempty" json:"input_mapping,omitempty" mapstructure:"input_mapping"`
OutputMapping map[string]string `yaml:"output_mapping,omitempty" json:"output_mapping,omitempty" mapstructure:"output_mapping"`
// used to specify an image artifact from a previous build to be used as the image for a subsequent task container
ImageArtifactName string `yaml:"image,omitempty" json:"image,omitempty" mapstructure:"image"`
// used by Put to specify params for the subsequent Get
GetParams Params `yaml:"get_params,omitempty" json:"get_params,omitempty" mapstructure:"get_params"`
// used by any step to specify which workers are eligible to run the step
Tags Tags `yaml:"tags,omitempty" json:"tags,omitempty" mapstructure:"tags"`
// used by any step to run something when the build is aborted during execution of the step
Abort *PlanConfig `yaml:"on_abort,omitempty" json:"on_abort,omitempty" mapstructure:"on_abort"`
// used by any step to run something when the step reports a failure
Failure *PlanConfig `yaml:"on_failure,omitempty" json:"on_failure,omitempty" mapstructure:"on_failure"`
// used on any step to always execute regardless of the step's completed state
Ensure *PlanConfig `yaml:"ensure,omitempty" json:"ensure,omitempty" mapstructure:"ensure"`
// used on any step to execute on successful completion of the step
Success *PlanConfig `yaml:"on_success,omitempty" json:"on_success,omitempty" mapstructure:"on_success"`
// used on any step to swallow failures and errors
Try *PlanConfig `yaml:"try,omitempty" json:"try,omitempty" mapstructure:"try"`
// used on any step to interrupt the step after a given duration
Timeout string `yaml:"timeout,omitempty" json:"timeout,omitempty" mapstructure:"timeout"`
// not present in yaml
DependentGet string `yaml:"-" json:"-"`
// repeat the step up to N times, until it works
Attempts int `yaml:"attempts,omitempty" json:"attempts,omitempty" mapstructure:"attempts"`
Version *VersionConfig `yaml:"version,omitempty" json:"version,omitempty" mapstructure:"version"`
}
func (config PlanConfig) Name() string {
if config.RawName != "" {
return config.RawName
}
if config.Get != "" {
return config.Get
}
if config.Put != "" {
return config.Put
}
if config.Task != "" {
return config.Task
}
return ""
}
func (config PlanConfig) ResourceName() string {
resourceName := config.Resource
if resourceName != "" {
return resourceName
}
resourceName = config.Get
if resourceName != "" {
return resourceName
}
resourceName = config.Put
if resourceName != "" {
return resourceName
}
panic("no resource name!")
}
func (config PlanConfig) Hooks() Hooks {
return Hooks{Abort: config.Abort, Failure: config.Failure, Ensure: config.Ensure, Success: config.Success}
}
type ResourceConfigs []ResourceConfig
func (resources ResourceConfigs) Lookup(name string) (ResourceConfig, bool) {
for _, resource := range resources {
if resource.Name == name {
return resource, true
}
}
return ResourceConfig{}, false
}
type JobConfigs []JobConfig
func (jobs JobConfigs) Lookup(name string) (JobConfig, bool) {
for _, job := range jobs {
if job.Name == name {
return job, true
}
}
return JobConfig{}, false
}
func (config Config) JobIsPublic(jobName string) (bool, error) {
job, found := config.Jobs.Lookup(jobName)
if !found {
return false, fmt.Errorf("cannot find job with job name '%s'", jobName)
}
return job.Public, nil
}