forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
26_convert_job_build_config_to_job_plans.go
110 lines (90 loc) · 2.61 KB
/
26_convert_job_build_config_to_job_plans.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
package migrations
import (
"database/sql"
"encoding/json"
"github.com/concourse/atc/db/migration"
internal "github.com/concourse/atc/db/migrations/internal/26"
)
func ConvertJobBuildConfigToJobPlans(tx migration.LimitedTx) error {
var configPayload []byte
err := tx.QueryRow(`
SELECT config
FROM config
`).Scan(&configPayload)
if err != nil {
if err == sql.ErrNoRows {
return nil
}
return err
}
var config internal.Config
err = json.Unmarshal(configPayload, &config)
if err != nil {
return err
}
for ji, job := range config.Jobs {
if len(job.Plan) > 0 { // skip jobs already converted to plans
continue
}
convertedSequence := internal.PlanSequence{}
inputAggregates := make(internal.PlanSequence, len(job.InputConfigs))
for ii, input := range job.InputConfigs {
name := input.RawName
resource := input.Resource
if name == "" {
name = input.Resource
resource = ""
}
inputAggregates[ii] = internal.PlanConfig{
Get: name,
Resource: resource,
RawTrigger: input.RawTrigger,
Passed: input.Passed,
Params: input.Params,
}
}
if len(inputAggregates) > 0 {
convertedSequence = append(convertedSequence, internal.PlanConfig{Aggregate: &inputAggregates})
}
if job.TaskConfig != nil || job.TaskConfigPath != "" {
convertedSequence = append(convertedSequence, internal.PlanConfig{
Task: "build", // default name
TaskConfigPath: job.TaskConfigPath,
TaskConfig: job.TaskConfig,
Privileged: job.Privileged,
})
}
outputAggregates := make(internal.PlanSequence, len(job.OutputConfigs))
for oi, output := range job.OutputConfigs {
var conditions *internal.Conditions
if output.RawPerformOn != nil { // NOT len(0)
conditionsCasted := internal.Conditions(output.RawPerformOn)
conditions = &conditionsCasted
}
outputAggregates[oi] = internal.PlanConfig{
Put: output.Resource,
Conditions: conditions,
Params: output.Params,
}
}
if len(outputAggregates) > 0 {
convertedSequence = append(convertedSequence, internal.PlanConfig{Aggregate: &outputAggregates})
}
// zero-out old-style config so they're omitted from new payload
config.Jobs[ji].InputConfigs = nil
config.Jobs[ji].OutputConfigs = nil
config.Jobs[ji].TaskConfigPath = ""
config.Jobs[ji].TaskConfig = nil
config.Jobs[ji].Privileged = false
config.Jobs[ji].Plan = convertedSequence
}
migratedConfig, err := json.Marshal(config)
if err != nil {
return err
}
_, err = tx.Exec(`
UPDATE config
SET config = $1, id = nextval('config_id_seq')
`, migratedConfig)
return err
}