-
Notifications
You must be signed in to change notification settings - Fork 116
/
runconfig.go
498 lines (427 loc) · 13 KB
/
runconfig.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
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package runconfig
import (
"fmt"
"strings"
"github.com/sorintlab/errors"
"agola.io/agola/internal/config"
itypes "agola.io/agola/internal/services/types"
"agola.io/agola/internal/util"
rstypes "agola.io/agola/services/runservice/types"
"agola.io/agola/services/types"
)
const (
defaultShell = "/bin/sh -e"
)
func genRuntime(c *config.Config, ce *config.Runtime, variables map[string]string) *rstypes.Runtime {
containers := []*rstypes.Container{}
for _, cc := range ce.Containers {
env := genEnv(cc.Environment, variables)
container := &rstypes.Container{
Image: cc.Image,
Environment: env,
User: cc.User,
Privileged: cc.Privileged,
Entrypoint: cc.Entrypoint,
Volumes: make([]rstypes.Volume, len(cc.Volumes)),
}
for i, ccVol := range cc.Volumes {
container.Volumes[i] = rstypes.Volume{
Path: ccVol.Path,
}
if ccVol.TmpFS != nil {
var size int64
if ccVol.TmpFS.Size != nil {
size = ccVol.TmpFS.Size.Value()
}
container.Volumes[i].TmpFS = &rstypes.VolumeTmpFS{
Size: size,
}
}
}
containers = append(containers, container)
}
return &rstypes.Runtime{
Type: rstypes.RuntimeType(ce.Type),
Arch: ce.Arch,
Containers: containers,
}
}
func stepFromConfigStep(csi interface{}, variables map[string]string) interface{} {
switch cs := csi.(type) {
case *config.CloneStep:
// transform a "clone" step in a "run" step command
rs := &config.RunStep{}
rs.Type = "run"
rs.Name = "Clone repository and checkout code"
rs.Command = fmt.Sprintf(`
set -x
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
touch ~/.ssh/known_hosts
chmod 600 ~/.ssh/known_hosts
# Add public ssh host key
if [ -n "$AGOLA_SSHHOSTKEY" ]; then
echo "$AGOLA_SSHHOSTKEY" >> ~/.ssh/known_hosts
fi
# Add repository deploy key
(cat <<EOF > ~/.ssh/id_rsa
$AGOLA_SSHPRIVKEY
EOF
)
STRICT_HOST_KEY_CHECKING="yes"
if [ -n "$AGOLA_SKIPSSHHOSTKEYCHECK" ]; then
# Disable git host key verification
STRICT_HOST_KEY_CHECKING="no"
fi
(cat <<EOF > ~/.ssh/config
Host $AGOLA_GIT_HOST
HostName $AGOLA_GIT_HOST
Port $AGOLA_GIT_PORT
StrictHostKeyChecking ${STRICT_HOST_KEY_CHECKING}
PasswordAuthentication no
IgnoreUnknown PubkeyAcceptedKeyTypes,PubkeyAcceptedAlgorithms
PubkeyAcceptedKeyTypes +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa
EOF
)
git clone %s $AGOLA_REPOSITORY_URL .
git fetch origin $AGOLA_GIT_REF
if [ -n "$AGOLA_GIT_COMMITSHA" ]; then
git checkout $AGOLA_GIT_COMMITSHA
else
git checkout FETCH_HEAD
fi
`, genCloneOptions(cs))
return rs
case *config.RunStep:
rs := &rstypes.RunStep{}
env := genEnv(cs.Environment, variables)
rs.Type = cs.Type
rs.Name = cs.Name
rs.Command = cs.Command
rs.Environment = env
rs.WorkingDir = cs.WorkingDir
rs.Shell = cs.Shell
rs.Tty = cs.Tty
return rs
case *config.SaveToWorkspaceStep:
sws := &rstypes.SaveToWorkspaceStep{}
sws.Type = cs.Type
sws.Name = cs.Name
sws.Contents = make([]rstypes.SaveContent, len(cs.Contents))
for i, csc := range cs.Contents {
sc := rstypes.SaveContent{}
sc.SourceDir = csc.SourceDir
sc.DestDir = csc.DestDir
sc.Paths = csc.Paths
sws.Contents[i] = sc
}
return sws
case *config.RestoreWorkspaceStep:
rws := &rstypes.RestoreWorkspaceStep{}
rws.Name = cs.Name
rws.Type = cs.Type
rws.DestDir = cs.DestDir
return rws
case *config.SaveCacheStep:
sws := &rstypes.SaveCacheStep{}
sws.Type = cs.Type
sws.Name = cs.Name
sws.Key = cs.Key
sws.Contents = make([]rstypes.SaveContent, len(cs.Contents))
for i, csc := range cs.Contents {
sc := rstypes.SaveContent{}
sc.SourceDir = csc.SourceDir
sc.DestDir = csc.DestDir
sc.Paths = csc.Paths
sws.Contents[i] = sc
}
return sws
case *config.RestoreCacheStep:
rws := &rstypes.RestoreCacheStep{}
rws.Name = cs.Name
rws.Type = cs.Type
rws.Keys = cs.Keys
rws.DestDir = cs.DestDir
return rws
default:
panic(errors.Errorf("unknown config step type: %s", util.Dump(cs)))
}
}
// GenRunConfigTasks generates a run config tasks from a run in the config, expanding all the references to tasks
// this functions assumes that the config is already checked for possible errors (i.e referenced task must exits)
func GenRunConfigTasks(uuid util.UUIDGenerator, c *config.Config, runName string, variables map[string]string, refType itypes.RunRefType, branch, tag, ref string) map[string]*rstypes.RunConfigTask {
cr := c.Run(runName)
rcts := map[string]*rstypes.RunConfigTask{}
for _, ct := range cr.Tasks {
include := types.MatchWhen(ct.When.ToWhen(), refType, branch, tag, ref)
steps := make(rstypes.Steps, len(ct.Steps))
for i, cpts := range ct.Steps {
steps[i] = stepFromConfigStep(cpts, variables)
}
tEnv := genEnv(ct.Environment, variables)
t := &rstypes.RunConfigTask{
ID: uuid.New(ct.Name).String(),
Name: ct.Name,
Runtime: genRuntime(c, ct.Runtime, variables),
Environment: tEnv,
WorkingDir: ct.WorkingDir,
Shell: ct.Shell,
User: ct.User,
Steps: steps,
IgnoreFailure: ct.IgnoreFailure,
Skip: !include,
NeedsApproval: ct.Approval,
DockerRegistriesAuth: make(map[string]rstypes.DockerRegistryAuth),
}
if t.Shell == "" {
t.Shell = defaultShell
}
if c.DockerRegistriesAuth != nil {
for regname, auth := range c.DockerRegistriesAuth {
t.DockerRegistriesAuth[regname] = rstypes.DockerRegistryAuth{
Type: rstypes.DockerRegistryAuthType(auth.Type),
Username: genValue(auth.Username, variables),
Password: genValue(auth.Password, variables),
Auth: genValue(auth.Auth, variables),
}
}
}
// override with per run docker registry auth
if cr.DockerRegistriesAuth != nil {
for regname, auth := range cr.DockerRegistriesAuth {
t.DockerRegistriesAuth[regname] = rstypes.DockerRegistryAuth{
Type: rstypes.DockerRegistryAuthType(auth.Type),
Username: genValue(auth.Username, variables),
Password: genValue(auth.Password, variables),
Auth: genValue(auth.Auth, variables),
}
}
}
// override with per task docker registry auth
if ct.DockerRegistriesAuth != nil {
for regname, auth := range ct.DockerRegistriesAuth {
t.DockerRegistriesAuth[regname] = rstypes.DockerRegistryAuth{
Type: rstypes.DockerRegistryAuthType(auth.Type),
Username: genValue(auth.Username, variables),
Password: genValue(auth.Password, variables),
Auth: genValue(auth.Auth, variables),
}
}
}
if c.TaskTimeoutInterval != nil {
t.TaskTimeoutInterval = c.TaskTimeoutInterval.Duration
}
// override with per run task timeout
if cr.TaskTimeoutInterval != nil {
t.TaskTimeoutInterval = cr.TaskTimeoutInterval.Duration
}
// override with per task timeout
if ct.TaskTimeoutInterval != nil {
t.TaskTimeoutInterval = ct.TaskTimeoutInterval.Duration
}
rcts[t.ID] = t
}
// populate depends, needs to be done after having created all the tasks so we can resolve their id
for _, rct := range rcts {
ct := cr.Task(rct.Name)
depends := make(map[string]*rstypes.RunConfigTaskDepend, len(ct.Depends))
for _, d := range ct.Depends {
conditions := make([]rstypes.RunConfigTaskDependCondition, len(d.Conditions))
// when no conditions are defined default to on_success
if len(d.Conditions) == 0 {
conditions = append(conditions, rstypes.RunConfigTaskDependConditionOnSuccess)
} else {
for ic, c := range d.Conditions {
var condition rstypes.RunConfigTaskDependCondition
switch c {
case config.DependConditionOnSuccess:
condition = rstypes.RunConfigTaskDependConditionOnSuccess
case config.DependConditionOnFailure:
condition = rstypes.RunConfigTaskDependConditionOnFailure
case config.DependConditionOnSkipped:
condition = rstypes.RunConfigTaskDependConditionOnSkipped
}
conditions[ic] = condition
}
}
drct := getRunConfigTaskByName(rcts, d.TaskName)
depends[drct.ID] = &rstypes.RunConfigTaskDepend{
TaskID: drct.ID,
Conditions: conditions,
}
}
rct.Depends = depends
}
return rcts
}
func getRunConfigTaskByName(rcts map[string]*rstypes.RunConfigTask, name string) *rstypes.RunConfigTask {
for _, rct := range rcts {
if rct.Name == name {
return rct
}
}
return nil
}
func CheckRunConfigTasks(rcts map[string]*rstypes.RunConfigTask) error {
// check circular dependencies
cerrs := &util.Errors{}
for _, t := range rcts {
allParents := GetAllParents(rcts, t)
for _, parent := range allParents {
if parent.ID == t.ID {
// TODO(sgotti) get the parent that depends on task to report it
dep := []string{}
for _, parent := range allParents {
pparents := GetParents(rcts, parent)
for _, pparent := range pparents {
if pparent.ID == t.ID {
dep = append(dep, fmt.Sprintf("%q", parent.Name))
}
}
}
cerrs.Append(errors.Errorf("circular dependency between task %q and tasks %s", t.Name, strings.Join(dep, " ")))
}
}
}
if cerrs.IsErr() {
return cerrs
}
// check that the task and its parent don't have a common dependency
for _, t := range rcts {
parents := GetParents(rcts, t)
for _, parent := range parents {
allParentParents := GetAllParents(rcts, parent)
for _, p := range parents {
for _, pp := range allParentParents {
if p.ID == pp.ID {
return errors.Errorf("task %q and its parent %q have both a dependency on task %q", t.Name, parent.Name, p.Name)
}
}
}
}
}
return nil
}
func GenTasksLevels(rcts map[string]*rstypes.RunConfigTask) error {
// reset all task level
for _, t := range rcts {
t.Level = -1
}
level := 0
for {
c := 0
for _, t := range rcts {
// skip tasks with the level already set
if t.Level != -1 {
continue
}
parents := GetParents(rcts, t)
ok := true
for _, p := range parents {
// * skip if the parent doesn't have a level yet
// * skip if the parent has a level equal than the current one (this happens when
// we have just set a level to a task in this same level loop)
if p.Level == -1 || p.Level >= level {
ok = false
}
}
if ok {
t.Level = level
c++
}
}
// if no tasks were updated in this level we can stop here
if c == 0 {
break
}
level++
}
for _, t := range rcts {
if t.Level == -1 {
return errors.Errorf("circular dependency detected")
}
}
return nil
}
// GetParents returns direct parents of task.
func GetParents(rcts map[string]*rstypes.RunConfigTask, task *rstypes.RunConfigTask) []*rstypes.RunConfigTask {
parents := []*rstypes.RunConfigTask{}
for _, t := range rcts {
if _, ok := task.Depends[t.ID]; ok {
parents = append(parents, t)
}
}
return parents
}
// GetAllParents returns all the parents (both direct and ancestors) of task.
// In case of circular dependency it won't loop forever but will also return
// task as parent of itself
func GetAllParents(rcts map[string]*rstypes.RunConfigTask, task *rstypes.RunConfigTask) []*rstypes.RunConfigTask {
pMap := map[string]*rstypes.RunConfigTask{}
nextParents := GetParents(rcts, task)
for len(nextParents) > 0 {
parents := nextParents
nextParents = []*rstypes.RunConfigTask{}
for _, parent := range parents {
if _, ok := pMap[parent.ID]; ok {
continue
}
pMap[parent.ID] = parent
nextParents = append(nextParents, GetParents(rcts, parent)...)
}
}
parents := make([]*rstypes.RunConfigTask, 0, len(pMap))
for _, v := range pMap {
parents = append(parents, v)
}
return parents
}
func GetParentDependConditions(t, pt *rstypes.RunConfigTask) []rstypes.RunConfigTaskDependCondition {
if dt, ok := t.Depends[pt.ID]; ok {
return dt.Conditions
}
return nil
}
func genEnv(cenv map[string]config.Value, variables map[string]string) map[string]string {
env := map[string]string{}
for envName, envVar := range cenv {
env[envName] = genValue(envVar, variables)
}
return env
}
func genValue(val config.Value, variables map[string]string) string {
switch val.Type {
case config.ValueTypeString:
return val.Value
case config.ValueTypeFromVariable:
return variables[val.Value]
default:
panic(errors.Errorf("wrong value type: %q", val.Value))
}
}
func genCloneOptions(c *config.CloneStep) string {
cloneoptions := []string{}
if c.Depth != nil {
cloneoptions = append(cloneoptions, fmt.Sprintf("--depth %d", *c.Depth))
}
if c.RecurseSubmodules {
cloneoptions = append(cloneoptions, "--recurse-submodules")
}
return strings.Join(cloneoptions, " ")
}