/
pipeline.go
1371 lines (1139 loc) · 30.4 KB
/
pipeline.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package db
import (
"database/sql"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/lager"
sq "github.com/Masterminds/squirrel"
"github.com/pkg/errors"
"github.com/concourse/concourse/atc"
"github.com/concourse/concourse/atc/creds"
"github.com/concourse/concourse/atc/db/lock"
"github.com/concourse/concourse/atc/event"
"github.com/concourse/concourse/vars"
)
// pipelineObjectTables contains a list of tables that are objects within a
// pipeline. These objects have their configs cleared when the pipeline is
// archived, and are marked as inactive when they are removed from the
// pipeline config.
//
// Each of these tables must have the following columns:
// * pipeline_id
// * active
// * config
// * nonce
//
// config must be encrypted, and thus must be added to atc/db/migration/encryption.go
var pipelineObjectTables = []string{
"jobs",
"resources",
"resource_types",
"prototypes",
}
type ErrResourceNotFound struct {
Name string
}
func (e ErrResourceNotFound) Error() string {
return fmt.Sprintf("resource '%s' not found", e.Name)
}
//counterfeiter:generate . Pipeline
type Cause struct {
ResourceVersionID int `json:"resource_version_id"`
BuildID int `json:"build_id"`
}
type Pipeline interface {
ID() int
Name() string
TeamID() int
TeamName() string
InstanceVars() atc.InstanceVars
ParentJobID() int
ParentBuildID() int
Groups() atc.GroupConfigs
VarSources() atc.VarSourceConfigs
Display() *atc.DisplayConfig
ConfigVersion() ConfigVersion
Config() (atc.Config, error)
Public() bool
Archived() bool
LastUpdated() time.Time
CheckPaused() (bool, error)
Reload() (bool, error)
ResourceVersion(resourceConfigVersionID int) (atc.ResourceVersion, bool, error)
GetBuildsWithVersionAsInput(int, int) ([]Build, error)
GetBuildsWithVersionAsOutput(int, int) ([]Build, error)
CreateOneOffBuild() (Build, error)
CreateStartedBuild(plan atc.Plan) (Build, error)
Builds(page Page) ([]BuildForAPI, Pagination, error)
BuildsWithTime(page Page) ([]BuildForAPI, Pagination, error)
DeleteBuildEventsByBuildIDs(buildIDs []int) error
LoadDebugVersionsDB() (*atc.DebugVersionsDB, error)
Resource(name string) (Resource, bool, error)
ResourceID(name string) (int, bool, error)
ResourceByID(id int) (Resource, bool, error)
Resources() (Resources, error)
// XXX(prototypes): with resource prototypes, we probably need a method to
// get all the prototypes AND resource types, since a resource's parent
// could be either.
ResourceTypes() (ResourceTypes, error)
ResourceType(name string) (ResourceType, bool, error)
Prototypes() (Prototypes, error)
Prototype(name string) (Prototype, bool, error)
SetResourceConfigScopeForResource(name string, scope ResourceConfigScope) error
SetResourceConfigScopeForResourceType(name string, scope ResourceConfigScope) error
SetResourceConfigScopeForPrototype(name string, scope ResourceConfigScope) error
Job(name string) (Job, bool, error)
Jobs() (Jobs, error)
Dashboard() ([]atc.JobSummary, error)
Expose() error
Hide() error
Paused() bool
PausedBy() string
PausedAt() time.Time
Pause(pausedBy string) error
Unpause() error
Archive() error
Destroy() error
Variables(lager.Logger, creds.Secrets, creds.VarSourcePool) (vars.Variables, error)
SetParentIDs(jobID, buildID int) error
DetachParent() error
}
type pipeline struct {
id int
name string
teamID int
teamName string
instanceVars atc.InstanceVars
parentJobID int
parentBuildID int
groups atc.GroupConfigs
varSources atc.VarSourceConfigs
display *atc.DisplayConfig
configVersion ConfigVersion
paused bool
pausedBy string
pausedAt time.Time
public bool
archived bool
lastUpdated time.Time
conn Conn
lockFactory lock.LockFactory
}
// ConfigVersion is a sequence identifier used for compare-and-swap.
type ConfigVersion int
var pipelinesQuery = psql.Select(`
p.id,
p.name,
p.groups,
p.var_sources,
p.display,
p.nonce,
p.version,
p.team_id,
t.name,
p.paused,
p.public,
p.archived,
p.last_updated,
p.parent_job_id,
p.parent_build_id,
p.instance_vars,
p.paused_by,
p.paused_at`).
From("pipelines p").
LeftJoin("teams t ON p.team_id = t.id")
func newPipeline(conn Conn, lockFactory lock.LockFactory) *pipeline {
return &pipeline{
conn: conn,
lockFactory: lockFactory,
}
}
func (p *pipeline) ID() int { return p.id }
func (p *pipeline) Name() string { return p.name }
func (p *pipeline) TeamID() int { return p.teamID }
func (p *pipeline) TeamName() string { return p.teamName }
func (p *pipeline) ParentJobID() int { return p.parentJobID }
func (p *pipeline) ParentBuildID() int { return p.parentBuildID }
func (p *pipeline) InstanceVars() atc.InstanceVars { return p.instanceVars }
func (p *pipeline) Groups() atc.GroupConfigs { return p.groups }
func (p *pipeline) VarSources() atc.VarSourceConfigs { return p.varSources }
func (p *pipeline) Display() *atc.DisplayConfig { return p.display }
func (p *pipeline) ConfigVersion() ConfigVersion { return p.configVersion }
func (p *pipeline) Public() bool { return p.public }
func (p *pipeline) Paused() bool { return p.paused }
func (p *pipeline) PausedAt() time.Time { return p.pausedAt }
func (p *pipeline) PausedBy() string { return p.pausedBy }
func (p *pipeline) Archived() bool { return p.archived }
func (p *pipeline) LastUpdated() time.Time { return p.lastUpdated }
func (p *pipeline) CheckPaused() (bool, error) {
var paused bool
err := psql.Select("paused").
From("pipelines").
Where(sq.Eq{"id": p.id}).
RunWith(p.conn).
QueryRow().
Scan(&paused)
if err != nil {
return false, err
}
return paused, nil
}
func (p *pipeline) Reload() (bool, error) {
row := pipelinesQuery.Where(sq.Eq{"p.id": p.id}).
RunWith(p.conn).
QueryRow()
err := scanPipeline(p, row)
if err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, err
}
return true, nil
}
func (p *pipeline) Config() (atc.Config, error) {
jobs, err := p.Jobs()
if err != nil {
return atc.Config{}, fmt.Errorf("failed to get jobs: %w", err)
}
resources, err := p.Resources()
if err != nil {
return atc.Config{}, fmt.Errorf("failed to get resources: %w", err)
}
resourceTypes, err := p.ResourceTypes()
if err != nil {
return atc.Config{}, fmt.Errorf("failed to get resources-types: %w", err)
}
prototypes, err := p.Prototypes()
if err != nil {
return atc.Config{}, fmt.Errorf("failed to get prototypes: %w", err)
}
jobConfigs, err := jobs.Configs()
if err != nil {
return atc.Config{}, fmt.Errorf("failed to get job configs: %w", err)
}
config := atc.Config{
Groups: p.Groups(),
VarSources: p.VarSources(),
Resources: resources.Configs(),
ResourceTypes: resourceTypes.Configs(),
Prototypes: prototypes.Configs(),
Jobs: jobConfigs,
Display: p.Display(),
}
return config, nil
}
func (p *pipeline) CreateJobBuild(jobName string) (Build, error) {
tx, err := p.conn.Begin()
if err != nil {
return nil, err
}
defer Rollback(tx)
buildName, jobID, err := getNewBuildNameForJob(tx, jobName, p.id)
if err != nil {
return nil, err
}
var buildID int
err = psql.Insert("builds").
Columns("name", "job_id", "team_id", "status", "manually_triggered").
Values(buildName, jobID, p.teamID, "pending", true).
Suffix("RETURNING id").
RunWith(tx).
QueryRow().
Scan(&buildID)
if err != nil {
return nil, err
}
build := newEmptyBuild(p.conn, p.lockFactory)
err = scanBuild(build, buildsQuery.
Where(sq.Eq{"b.id": buildID}).
RunWith(tx).
QueryRow(),
p.conn.EncryptionStrategy(),
)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
return build, nil
}
// ResourceVersion is given a resource config version id and returns the
// resource version struct. This method is used by the API call
// GetResourceVersion to get all the attributes for that version of the
// resource.
func (p *pipeline) ResourceVersion(resourceConfigVersionID int) (atc.ResourceVersion, bool, error) {
rv := atc.ResourceVersion{}
var (
versionBytes string
metadataBytes string
)
enabled := `
NOT EXISTS (
SELECT 1
FROM resource_disabled_versions d, resources r
WHERE v.version_md5 = d.version_md5
AND r.resource_config_scope_id = v.resource_config_scope_id
AND r.id = d.resource_id
)`
err := psql.Select("v.id", "v.version", "v.metadata", enabled).
From("resource_config_versions v").
Where(sq.Eq{
"v.id": resourceConfigVersionID,
}).
RunWith(p.conn).
QueryRow().
Scan(&rv.ID, &versionBytes, &metadataBytes, &rv.Enabled)
if err != nil {
if err == sql.ErrNoRows {
return atc.ResourceVersion{}, false, nil
}
return atc.ResourceVersion{}, false, err
}
err = json.Unmarshal([]byte(versionBytes), &rv.Version)
if err != nil {
return atc.ResourceVersion{}, false, err
}
err = json.Unmarshal([]byte(metadataBytes), &rv.Metadata)
if err != nil {
return atc.ResourceVersion{}, false, err
}
return rv, true, nil
}
func (p *pipeline) GetBuildsWithVersionAsInput(resourceID, resourceConfigVersionID int) ([]Build, error) {
rows, err := buildsQuery.
Join("build_resource_config_version_inputs bi ON bi.build_id = b.id").
Join("resource_config_versions rcv ON rcv.version_md5 = bi.version_md5").
Where(sq.Eq{
"rcv.id": resourceConfigVersionID,
"bi.resource_id": resourceID,
}).
RunWith(p.conn).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
builds := []Build{}
for rows.Next() {
build := newEmptyBuild(p.conn, p.lockFactory)
err = scanBuild(build, rows, p.conn.EncryptionStrategy())
if err != nil {
return nil, err
}
builds = append(builds, build)
}
return builds, err
}
func (p *pipeline) GetBuildsWithVersionAsOutput(resourceID, resourceConfigVersionID int) ([]Build, error) {
rows, err := buildsQuery.
Join("build_resource_config_version_outputs bo ON bo.build_id = b.id").
Join("resource_config_versions rcv ON rcv.version_md5 = bo.version_md5").
Where(sq.Eq{
"rcv.id": resourceConfigVersionID,
"bo.resource_id": resourceID,
}).
RunWith(p.conn).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
builds := []Build{}
for rows.Next() {
build := newEmptyBuild(p.conn, p.lockFactory)
err = scanBuild(build, rows, p.conn.EncryptionStrategy())
if err != nil {
return nil, err
}
builds = append(builds, build)
}
return builds, err
}
func (p *pipeline) Resource(name string) (Resource, bool, error) {
return p.resource(sq.Eq{
"r.pipeline_id": p.id,
"r.name": name,
})
}
func (p *pipeline) ResourceID(name string) (int, bool, error) {
var id int
err := psql.Select("r.id").
From("resources r").
Where(sq.Eq{
"r.pipeline_id": p.id,
"r.name": name,
"r.active": true,
}).
RunWith(p.conn).
QueryRow().
Scan(&id)
if err != nil {
if err == sql.ErrNoRows {
return 0, false, nil
}
return 0, false, err
}
return id, true, nil
}
func (p *pipeline) ResourceByID(id int) (Resource, bool, error) {
return p.resource(sq.Eq{
"r.pipeline_id": p.id,
"r.id": id,
})
}
func (p *pipeline) resource(where map[string]interface{}) (Resource, bool, error) {
row := resourcesQuery.
Where(where).
RunWith(p.conn).
QueryRow()
resource := newEmptyResource(p.conn, p.lockFactory)
err := scanResource(resource, row)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return resource, true, nil
}
func (p *pipeline) Builds(page Page) ([]BuildForAPI, Pagination, error) {
return getBuildsWithPagination(
buildsQuery.Where(sq.Eq{"b.pipeline_id": p.id}), minMaxIdQuery, page, p.conn, p.lockFactory, false)
}
func (p *pipeline) BuildsWithTime(page Page) ([]BuildForAPI, Pagination, error) {
return getBuildsWithDates(
buildsQuery.Where(sq.Eq{"b.pipeline_id": p.id}), minMaxIdQuery, page, p.conn, p.lockFactory)
}
func (p *pipeline) Resources() (Resources, error) {
return resources(p.id, p.conn, p.lockFactory)
}
func (p *pipeline) ResourceTypes() (ResourceTypes, error) {
rows, err := resourceTypesQuery.
Where(sq.Eq{"r.pipeline_id": p.id}).
OrderBy("r.name").
RunWith(p.conn).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
resourceTypes := []ResourceType{}
for rows.Next() {
resourceType := newEmptyResourceType(p.conn, p.lockFactory)
err := scanResourceType(resourceType, rows)
if err != nil {
return nil, err
}
resourceTypes = append(resourceTypes, resourceType)
}
return resourceTypes, nil
}
func (p *pipeline) ResourceType(name string) (ResourceType, bool, error) {
return p.resourceType(sq.Eq{
"r.pipeline_id": p.id,
"r.name": name,
})
}
func (p *pipeline) resourceType(where map[string]interface{}) (ResourceType, bool, error) {
row := resourceTypesQuery.
Where(where).
RunWith(p.conn).
QueryRow()
resourceType := newEmptyResourceType(p.conn, p.lockFactory)
err := scanResourceType(resourceType, row)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return resourceType, true, nil
}
func (p *pipeline) Prototypes() (Prototypes, error) {
rows, err := prototypesQuery.
Where(sq.Eq{"pt.pipeline_id": p.id}).
OrderBy("pt.name").
RunWith(p.conn).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
prototypes := Prototypes{}
for rows.Next() {
prototype := newEmptyPrototype(p.conn, p.lockFactory)
err := scanPrototype(prototype, rows)
if err != nil {
return nil, err
}
prototypes = append(prototypes, prototype)
}
return prototypes, nil
}
func (p *pipeline) Prototype(name string) (Prototype, bool, error) {
return p.prototype(sq.Eq{
"pt.pipeline_id": p.id,
"pt.name": name,
})
}
func (p *pipeline) prototype(where map[string]interface{}) (Prototype, bool, error) {
row := prototypesQuery.
Where(where).
RunWith(p.conn).
QueryRow()
prototype := newEmptyPrototype(p.conn, p.lockFactory)
err := scanPrototype(prototype, row)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return prototype, true, nil
}
func (p *pipeline) Job(name string) (Job, bool, error) {
row := jobsQuery.Where(sq.Eq{
"j.name": name,
"j.active": true,
"j.pipeline_id": p.id,
}).RunWith(p.conn).QueryRow()
job := newEmptyJob(p.conn, p.lockFactory)
err := scanJob(job, row)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return job, true, nil
}
func (p *pipeline) Jobs() (Jobs, error) {
rows, err := jobsQuery.
Where(sq.Eq{
"j.pipeline_id": p.id,
"active": true,
}).
OrderBy("j.id ASC").
RunWith(p.conn).
Query()
if err != nil {
return nil, err
}
jobs, err := scanJobs(p.conn, p.lockFactory, rows)
return jobs, err
}
func (p *pipeline) Dashboard() ([]atc.JobSummary, error) {
tx, err := p.conn.Begin()
if err != nil {
return nil, err
}
defer Rollback(tx)
dashboardFactory := newDashboardFactory(tx, sq.Eq{
"j.pipeline_id": p.id,
})
dashboard, err := dashboardFactory.buildDashboard()
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
return dashboard, nil
}
func (p *pipeline) Pause(pausedBy string) error {
_, err := psql.Update("pipelines").
Set("paused", true).
Set("paused_at", time.Now()).
Set("paused_by", pausedBy).
Where(sq.Eq{"id": p.id, "paused": false}).
RunWith(p.conn).
Exec()
if err != nil {
return err
}
return nil
}
func (p *pipeline) Unpause() error {
tx, err := p.conn.Begin()
if err != nil {
return err
}
defer Rollback(tx)
_, err = psql.Update("pipelines").
Set("paused", false).
Set("paused_by", nil).
Set("paused_at", nil).
Where(sq.Eq{"id": p.id, "paused": true}).
RunWith(tx).
Exec()
if err != nil {
return err
}
err = requestScheduleForJobsInPipeline(tx, p.id)
if err != nil {
return err
}
return tx.Commit()
}
func (p *pipeline) Archive() error {
tx, err := p.conn.Begin()
if err != nil {
return err
}
defer Rollback(tx)
err = p.archive(tx)
if err != nil {
return err
}
return tx.Commit()
}
func (p *pipeline) archive(tx Tx) error {
_, err := psql.Update("pipelines").
Set("archived", true).
Set("last_updated", sq.Expr("now()")).
Set("paused", true).
Set("paused_by", "automatic-pipeline-archiver").
Set("paused_at", time.Now()).
Set("version", 0).
Where(sq.Eq{"id": p.id}).
RunWith(tx).
Exec()
if err != nil {
return err
}
for _, table := range pipelineObjectTables {
err = clearConfigForPipelineObject(tx, p.id, table)
if err != nil {
return err
}
}
return nil
}
func (p *pipeline) Hide() error {
_, err := psql.Update("pipelines").
Set("public", false).
Where(sq.Eq{
"id": p.id,
}).
RunWith(p.conn).
Exec()
return err
}
func (p *pipeline) Expose() error {
_, err := psql.Update("pipelines").
Set("public", true).
Where(sq.Eq{
"id": p.id,
}).
RunWith(p.conn).
Exec()
return err
}
func (p *pipeline) Destroy() error {
_, err := psql.Delete("pipelines").
Where(sq.Eq{
"id": p.id,
}).
RunWith(p.conn).
Exec()
return err
}
func (p *pipeline) LoadDebugVersionsDB() (*atc.DebugVersionsDB, error) {
db := &atc.DebugVersionsDB{
BuildOutputs: []atc.DebugBuildOutput{},
BuildInputs: []atc.DebugBuildInput{},
ResourceVersions: []atc.DebugResourceVersion{},
BuildReruns: []atc.DebugBuildRerun{},
Resources: []atc.DebugResource{},
Jobs: []atc.DebugJob{},
}
tx, err := p.conn.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
rows, err := psql.Select("v.id, v.check_order, r.id, v.resource_config_scope_id, o.build_id, b.job_id").
From("build_resource_config_version_outputs o").
Join("builds b ON b.id = o.build_id").
Join("resource_config_versions v ON v.version_md5 = o.version_md5").
Join("resources r ON r.id = o.resource_id").
Where(sq.Expr("r.resource_config_scope_id = v.resource_config_scope_id")).
Where(sq.Expr("(r.id, v.version_md5) NOT IN (SELECT resource_id, version_md5 from resource_disabled_versions)")).
Where(sq.Eq{
"b.status": BuildStatusSucceeded,
"r.pipeline_id": p.id,
"r.active": true,
}).
RunWith(tx).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
for rows.Next() {
var output atc.DebugBuildOutput
err = rows.Scan(&output.VersionID, &output.CheckOrder, &output.ResourceID, &output.ScopeID, &output.BuildID, &output.JobID)
if err != nil {
return nil, err
}
output.DebugResourceVersion.CheckOrder = output.CheckOrder
db.BuildOutputs = append(db.BuildOutputs, output)
}
rows, err = psql.Select("v.id, v.check_order, r.id, v.resource_config_scope_id, i.build_id, i.name, b.job_id, b.status = 'succeeded'").
From("build_resource_config_version_inputs i").
Join("builds b ON b.id = i.build_id").
Join("resource_config_versions v ON v.version_md5 = i.version_md5").
Join("resources r ON r.id = i.resource_id").
Where(sq.Expr("r.resource_config_scope_id = v.resource_config_scope_id")).
Where(sq.Expr("(r.id, v.version_md5) NOT IN (SELECT resource_id, version_md5 from resource_disabled_versions)")).
Where(sq.Eq{
"r.pipeline_id": p.id,
"r.active": true,
}).
RunWith(tx).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
for rows.Next() {
var succeeded bool
var input atc.DebugBuildInput
err = rows.Scan(&input.VersionID, &input.CheckOrder, &input.ResourceID, &input.ScopeID, &input.BuildID, &input.InputName, &input.JobID, &succeeded)
if err != nil {
return nil, err
}
input.DebugResourceVersion.CheckOrder = input.CheckOrder
db.BuildInputs = append(db.BuildInputs, input)
if succeeded {
// implicit output
db.BuildOutputs = append(db.BuildOutputs, atc.DebugBuildOutput{
DebugResourceVersion: input.DebugResourceVersion,
JobID: input.JobID,
BuildID: input.BuildID,
})
}
}
rows, err = psql.Select("v.id, v.check_order, r.id, v.resource_config_scope_id").
From("resource_config_versions v").
Join("resources r ON r.resource_config_scope_id = v.resource_config_scope_id").
LeftJoin("resource_disabled_versions d ON d.resource_id = r.id AND d.version_md5 = v.version_md5").
Where(sq.Eq{
"r.pipeline_id": p.id,
"r.active": true,
"d.resource_id": nil,
"d.version_md5": nil,
}).
RunWith(tx).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
for rows.Next() {
var output atc.DebugResourceVersion
err = rows.Scan(&output.VersionID, &output.CheckOrder, &output.ResourceID, &output.ScopeID)
if err != nil {
return nil, err
}
db.ResourceVersions = append(db.ResourceVersions, output)
}
rows, err = psql.Select("j.id, b.id, b.rerun_of").
From("builds b").
Join("jobs j ON j.id = b.job_id").
Where(sq.Eq{
"j.active": true,
"b.pipeline_id": p.id,
}).
Where(sq.NotEq{
"b.rerun_of": nil,
}).
RunWith(tx).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
for rows.Next() {
var rerun atc.DebugBuildRerun
err = rows.Scan(&rerun.JobID, &rerun.BuildID, &rerun.RerunOf)
if err != nil {
return nil, err
}
db.BuildReruns = append(db.BuildReruns, rerun)
}
rows, err = psql.Select("j.name, j.id").
From("jobs j").
Where(sq.Eq{
"j.pipeline_id": p.id,
"j.active": true,
}).
RunWith(tx).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
for rows.Next() {
var job atc.DebugJob
err = rows.Scan(&job.Name, &job.ID)
if err != nil {
return nil, err
}
db.Jobs = append(db.Jobs, job)
}
rows, err = psql.Select("r.name, r.id, r.resource_config_scope_id").
From("resources r").
Where(sq.Eq{
"r.pipeline_id": p.id,
"r.active": true,
}).
RunWith(tx).
Query()
if err != nil {
return nil, err
}
defer Close(rows)
for rows.Next() {
var scopeID sql.NullInt64
var resource atc.DebugResource
err = rows.Scan(&resource.Name, &resource.ID, &scopeID)
if err != nil {
return nil, err
}
if scopeID.Valid {
i := int(scopeID.Int64)
resource.ScopeID = &i
}
db.Resources = append(db.Resources, resource)
}
err = tx.Commit()
if err != nil {
return nil, err
}
return db, nil
}
func (p *pipeline) DeleteBuildEventsByBuildIDs(buildIDs []int) error {
if len(buildIDs) == 0 {
return nil
}
interfaceBuildIDs := make([]interface{}, len(buildIDs))
for i, buildID := range buildIDs {
interfaceBuildIDs[i] = buildID
}