forked from vmware-archive/atc
/
sqldb_containers.go
566 lines (479 loc) · 12.9 KB
/
sqldb_containers.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package db
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/concourse/atc"
)
const containerColumns = "worker_name, resource_id, check_type, check_source, build_id, plan_id, stage, handle, b.name as build_name, r.name as resource_name, p.id as pipeline_id, p.name as pipeline_name, j.name as job_name, step_name, type, working_directory, env_variables, attempts, process_user, ttl, EXTRACT(epoch FROM expires_at - NOW()), c.id, resource_type_version, c.team_id"
const containerJoins = `
LEFT JOIN pipelines p
ON p.id = c.pipeline_id
LEFT JOIN resources r
ON r.id = c.resource_id
LEFT JOIN builds b
ON b.id = c.build_id
LEFT JOIN jobs j
ON j.id = b.job_id`
var ErrInvalidIdentifier = errors.New("invalid container identifier")
func scanRows(rows *sql.Rows) ([]SavedContainer, error) {
var containers []SavedContainer
for rows.Next() {
container, err := scanContainer(rows)
if err != nil {
return nil, nil
}
containers = append(containers, container)
}
return containers, nil
}
func (db *SQLDB) FindJobContainersFromUnsuccessfulBuilds() ([]SavedContainer, error) {
rows, err := db.conn.Query(
`SELECT ` + containerColumns + `
FROM containers c ` + containerJoins + `
WHERE (b.status = 'failed' OR b.status = 'errored')
AND b.job_id is not null`)
if err != nil {
if err == sql.ErrNoRows {
return []SavedContainer{}, nil
}
return nil, err
}
return scanRows(rows)
}
func (db *SQLDB) FindContainerByIdentifier(id ContainerIdentifier) (SavedContainer, bool, error) {
conditions := []string{"(expires_at IS NULL OR expires_at > NOW())"}
params := []interface{}{}
addParam := func(column string, param interface{}) {
conditions = append(conditions, fmt.Sprintf("%s = $%d", column, len(params)+1))
params = append(params, param)
}
switch {
case isValidCheckID(id):
checkSourceBlob, err := json.Marshal(id.CheckSource)
if err != nil {
return SavedContainer{}, false, err
}
if id.ResourceID > 0 {
addParam("resource_id", id.ResourceID)
}
addParam("check_type", id.CheckType)
addParam("check_source", checkSourceBlob)
addParam("stage", string(id.Stage))
conditions = append(conditions, "(best_if_used_by IS NULL OR best_if_used_by > NOW())")
case isValidStepID(id):
addParam("build_id", id.BuildID)
addParam("plan_id", string(id.PlanID))
addParam("stage", string(id.Stage))
default:
return SavedContainer{}, false, ErrInvalidIdentifier
}
if id.ImageResourceSource != nil && id.ImageResourceType != "" {
marshaled, err := json.Marshal(id.ImageResourceSource)
if err != nil {
return SavedContainer{}, false, err
}
addParam("image_resource_source", string(marshaled))
addParam("image_resource_type", id.ImageResourceType)
} else {
conditions = append(conditions, []string{
"image_resource_source IS NULL",
"image_resource_type IS NULL",
}...)
}
selectQuery := `
SELECT ` + containerColumns + `
FROM containers c ` + containerJoins + `
WHERE ` + strings.Join(conditions, " AND ")
rows, err := db.conn.Query(selectQuery, params...)
if err != nil {
return SavedContainer{}, false, err
}
var containers []SavedContainer
for rows.Next() {
container, err := scanContainer(rows)
if err != nil {
return SavedContainer{}, false, nil
}
containers = append(containers, container)
}
switch len(containers) {
case 0:
return SavedContainer{}, false, nil
case 1:
return containers[0], true, nil
default:
return SavedContainer{}, false, ErrMultipleContainersFound
}
}
func (db *SQLDB) GetContainer(handle string) (SavedContainer, bool, error) {
container, err := scanContainer(db.conn.QueryRow(`
SELECT `+containerColumns+`
FROM containers c `+containerJoins+`
WHERE c.handle = $1
AND (expires_at IS NULL OR expires_at > NOW())
`, handle))
if err != nil {
if err == sql.ErrNoRows {
return SavedContainer{}, false, nil
}
return SavedContainer{}, false, err
}
return container, true, nil
}
func (db *SQLDB) CreateContainer(
container Container,
ttl time.Duration,
maxLifetime time.Duration,
volumeHandles []string,
) (SavedContainer, error) {
if !(isValidCheckID(container.ContainerIdentifier) || isValidStepID(container.ContainerIdentifier)) {
return SavedContainer{}, ErrInvalidIdentifier
}
tx, err := db.conn.Begin()
if err != nil {
return SavedContainer{}, err
}
defer tx.Rollback()
checkSource, err := json.Marshal(container.CheckSource)
if err != nil {
return SavedContainer{}, err
}
envVariables, err := json.Marshal(container.EnvironmentVariables)
if err != nil {
return SavedContainer{}, err
}
user := container.User
interval := fmt.Sprintf("%d second", int(ttl.Seconds()))
if container.PipelineName != "" && container.PipelineID == 0 {
// containers that belong to some pipeline must be identified by pipeline ID not name
return SavedContainer{}, errors.New("container metadata must include pipeline ID")
}
var pipelineID sql.NullInt64
if container.PipelineID != 0 {
pipelineID.Int64 = int64(container.PipelineID)
pipelineID.Valid = true
}
var resourceID sql.NullInt64
if container.ResourceID != 0 {
resourceID.Int64 = int64(container.ResourceID)
resourceID.Valid = true
}
var resourceTypeVersion string
if container.ResourceTypeVersion != nil {
resourceTypeVersionBytes, err := json.Marshal(container.ResourceTypeVersion)
if err != nil {
return SavedContainer{}, err
}
resourceTypeVersion = string(resourceTypeVersionBytes)
}
var buildID sql.NullInt64
if container.BuildID != 0 {
buildID.Int64 = int64(container.BuildID)
buildID.Valid = true
}
var attempts sql.NullString
if len(container.Attempts) > 0 {
attemptsBlob, err := json.Marshal(container.Attempts)
if err != nil {
return SavedContainer{}, err
}
attempts.Valid = true
attempts.String = string(attemptsBlob)
}
var imageResourceSource sql.NullString
if container.ImageResourceSource != nil {
marshaled, err := json.Marshal(container.ImageResourceSource)
if err != nil {
return SavedContainer{}, err
}
imageResourceSource.String = string(marshaled)
imageResourceSource.Valid = true
}
var imageResourceType sql.NullString
if container.ImageResourceType != "" {
imageResourceType.String = container.ImageResourceType
imageResourceType.Valid = true
}
maxLifetimeValue := "NULL"
if maxLifetime > 0 {
maxLifetimeValue = fmt.Sprintf(`NOW() + '%d second'::INTERVAL`, int(maxLifetime.Seconds()))
}
var id int
err = tx.QueryRow(`
INSERT INTO containers (handle, resource_id, step_name, pipeline_id, build_id, type, worker_name,
expires_at, ttl, best_if_used_by, check_type, check_source, plan_id, working_directory,
env_variables, attempts, stage, image_resource_type, image_resource_source,
process_user, resource_type_version, team_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW() + $8::INTERVAL, $9,`+maxLifetimeValue+`, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)
RETURNING id`,
container.Handle,
resourceID,
container.StepName,
pipelineID,
buildID,
container.Type.String(),
container.WorkerName,
interval,
ttl,
container.CheckType,
checkSource,
string(container.PlanID),
container.WorkingDirectory,
envVariables,
attempts,
string(container.Stage),
imageResourceType,
imageResourceSource,
user,
resourceTypeVersion,
container.TeamID,
).Scan(&id)
if err != nil {
return SavedContainer{}, err
}
newContainer, err := scanContainer(tx.QueryRow(`
SELECT `+containerColumns+`
FROM containers c `+containerJoins+`
WHERE c.id = $1
`, id))
if err != nil {
return SavedContainer{}, err
}
for _, volumeHandle := range volumeHandles {
_, err = tx.Exec(`
UPDATE volumes
SET container_id = $1
WHERE handle = $2
`, id, volumeHandle)
if err != nil {
return SavedContainer{}, err
}
}
err = tx.Commit()
if err != nil {
return SavedContainer{}, err
}
return newContainer, nil
}
func (db *SQLDB) UpdateExpiresAtOnContainer(handle string, ttl time.Duration) error {
tx, err := db.conn.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if ttl == 0 {
_, err = tx.Exec(`
UPDATE containers SET expires_at = NULL, ttl = 0
WHERE handle = $1
`, handle)
if err != nil {
return err
}
} else {
interval := fmt.Sprintf("%d second", int(ttl.Seconds()))
_, err = tx.Exec(`
UPDATE containers SET expires_at = NOW() + $2::INTERVAL, ttl = $3
WHERE handle = $1
`,
handle,
interval,
ttl,
)
if err != nil {
return err
}
}
return tx.Commit()
}
func (db *SQLDB) ReapContainer(handle string) error {
rows, err := db.conn.Exec(`
DELETE FROM containers WHERE handle = $1
`, handle)
if err != nil {
return err
}
affected, err := rows.RowsAffected()
if err != nil {
return err
}
// just to be explicit: reaping 0 containers is fine;
// it may have already expired
if affected == 0 {
return nil
}
return nil
}
func (db *SQLDB) DeleteContainer(handle string) error {
tx, err := db.conn.Begin()
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.Exec(`
DELETE FROM containers WHERE handle = $1
`, handle)
if err != nil {
return err
}
return tx.Commit()
}
func isValidCheckID(id ContainerIdentifier) bool {
if !((id.ResourceID > 0 || id.ResourceTypeVersion != nil) &&
id.CheckType != "" &&
id.CheckSource != nil &&
id.BuildID == 0 &&
id.PlanID == "") {
return false
}
switch id.Stage {
case ContainerStageCheck, ContainerStageGet:
return id.ImageResourceType != "" && id.ImageResourceSource != nil
case ContainerStageRun:
return id.ImageResourceType == "" && id.ImageResourceSource == nil
default:
return false
}
}
func isValidStepID(id ContainerIdentifier) bool {
if !(id.ResourceID == 0 &&
id.CheckType == "" &&
id.CheckSource == nil &&
id.BuildID > 0 &&
id.PlanID != "") {
return false
}
switch id.Stage {
case ContainerStageCheck, ContainerStageGet:
return id.ImageResourceType != "" && id.ImageResourceSource != nil
case ContainerStageRun:
return id.ImageResourceType == "" && id.ImageResourceSource == nil
default:
return false
}
}
func scanContainer(row scannable) (SavedContainer, error) {
var (
teamID sql.NullInt64
resourceID sql.NullInt64
checkSourceBlob []byte
buildID sql.NullInt64
planID sql.NullString
stage string
buildName sql.NullString
resourceName sql.NullString
pipelineID sql.NullInt64
pipelineName sql.NullString
jobName sql.NullString
infoType string
envVariablesBlob []byte
attempts sql.NullString
ttlInSeconds *float64
resourceTypeVersion []byte
)
container := SavedContainer{}
err := row.Scan(
&container.WorkerName,
&resourceID,
&container.CheckType,
&checkSourceBlob,
&buildID,
&planID,
&stage,
&container.Handle,
&buildName,
&resourceName,
&pipelineID,
&pipelineName,
&jobName,
&container.StepName,
&infoType,
&container.WorkingDirectory,
&envVariablesBlob,
&attempts,
&container.User,
&container.TTL,
&ttlInSeconds,
&container.ID,
&resourceTypeVersion,
&teamID,
)
if err != nil {
return SavedContainer{}, err
}
if resourceID.Valid {
container.ResourceID = int(resourceID.Int64)
}
if buildID.Valid {
container.ContainerIdentifier.BuildID = int(buildID.Int64)
}
if teamID.Valid {
container.TeamID = int(teamID.Int64)
}
container.PlanID = atc.PlanID(planID.String)
container.Stage = ContainerStage(stage)
if buildName.Valid {
container.BuildName = buildName.String
}
if resourceName.Valid {
container.ResourceName = resourceName.String
}
if pipelineID.Valid {
container.PipelineID = int(pipelineID.Int64)
}
if pipelineName.Valid {
container.PipelineName = pipelineName.String
}
if jobName.Valid {
container.JobName = jobName.String
}
container.Type, err = ContainerTypeFromString(infoType)
if err != nil {
return SavedContainer{}, err
}
err = json.Unmarshal(checkSourceBlob, &container.CheckSource)
if err != nil {
return SavedContainer{}, err
}
if len(resourceTypeVersion) > 0 {
err = json.Unmarshal(resourceTypeVersion, &container.ResourceTypeVersion)
if err != nil {
return SavedContainer{}, err
}
}
err = json.Unmarshal(envVariablesBlob, &container.EnvironmentVariables)
if err != nil {
return SavedContainer{}, err
}
if attempts.Valid {
err = json.Unmarshal([]byte(attempts.String), &container.Attempts)
if err != nil {
return SavedContainer{}, err
}
}
//TODO remove this check once all containers have a user
// specifically waiting upon worker provided resources to
// use image resources that specifiy a metadata.json with
// a user
if container.User == "" {
container.User = "root"
}
if ttlInSeconds != nil {
container.ExpiresIn = time.Duration(*ttlInSeconds) * time.Second
}
return container, nil
}
func (db *SQLDB) ReapExpiredContainers() error {
_, err := db.conn.Exec(`
DELETE FROM containers
WHERE expires_at IS NOT NULL
AND expires_at < NOW()
`)
if err != nil {
return err
}
return nil
}