forked from vmware-archive/atc
/
container_owner.go
186 lines (159 loc) · 4.86 KB
/
container_owner.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
package db
import (
"database/sql"
sq "github.com/Masterminds/squirrel"
"github.com/concourse/atc"
)
//go:generate counterfeiter . ContainerOwner
// ContainerOwner designates the data the container should reference that
// identifies its lifecycle. When the owner goes away, the container should
// be garbage collected.
type ContainerOwner interface {
Find(conn Conn) (sq.Eq, bool, error)
Create(tx Tx, workerName string) (map[string]interface{}, error)
}
// NewImageCheckContainerOwner references a container whose image resource this
// container is checking. When the referenced container transitions to another
// state, or disappears, the container can be removed.
func NewImageCheckContainerOwner(
container CreatingContainer,
) ContainerOwner {
return imageCheckContainerOwner{
Container: container,
}
}
type imageCheckContainerOwner struct {
Container CreatingContainer
}
func (c imageCheckContainerOwner) Find(Conn) (sq.Eq, bool, error) {
return sq.Eq(c.sqlMap()), true, nil
}
func (c imageCheckContainerOwner) Create(Tx, string) (map[string]interface{}, error) {
return c.sqlMap(), nil
}
func (c imageCheckContainerOwner) sqlMap() map[string]interface{} {
return map[string]interface{}{
"image_check_container_id": c.Container.ID(),
}
}
// NewImageGetContainerOwner references a container whose image resource this
// container is fetching. When the referenced container transitions to another
// state, or disappears, the container can be removed.
func NewImageGetContainerOwner(
container CreatingContainer,
) ContainerOwner {
return imageGetContainerOwner{
Container: container,
}
}
type imageGetContainerOwner struct {
Container CreatingContainer
}
func (c imageGetContainerOwner) Find(Conn) (sq.Eq, bool, error) {
return sq.Eq(c.sqlMap()), true, nil
}
func (c imageGetContainerOwner) Create(Tx, string) (map[string]interface{}, error) {
return c.sqlMap(), nil
}
func (c imageGetContainerOwner) sqlMap() map[string]interface{} {
return map[string]interface{}{
"image_get_container_id": c.Container.ID(),
}
}
// NewBuildStepContainerOwner references a step within a build. When the build
// becomes non-interceptible or disappears, the container can be removed.
func NewBuildStepContainerOwner(
buildID int,
planID atc.PlanID,
) ContainerOwner {
return buildStepContainerOwner{
BuildID: buildID,
PlanID: planID,
}
}
type buildStepContainerOwner struct {
BuildID int
PlanID atc.PlanID
}
func (c buildStepContainerOwner) Find(Conn) (sq.Eq, bool, error) {
return sq.Eq(c.sqlMap()), true, nil
}
func (c buildStepContainerOwner) Create(Tx, string) (map[string]interface{}, error) {
return c.sqlMap(), nil
}
func (c buildStepContainerOwner) sqlMap() map[string]interface{} {
return map[string]interface{}{
"build_id": c.BuildID,
"plan_id": c.PlanID,
}
}
// NewResourceConfigCheckSessionContainerOwner references a resource config and
// worker base resource type, with an expiry. When the resource config or
// worker base resource type disappear, or the expiry is reached, the container
// can be removed.
func NewResourceConfigCheckSessionContainerOwner(
resourceConfigCheckSession ResourceConfigCheckSession,
teamID int,
) ContainerOwner {
return resourceConfigCheckSessionContainerOwner{
resourceConfigCheckSession: resourceConfigCheckSession,
teamID: teamID,
}
}
type resourceConfigCheckSessionContainerOwner struct {
resourceConfigCheckSession ResourceConfigCheckSession
teamID int
}
func (c resourceConfigCheckSessionContainerOwner) Find(conn Conn) (sq.Eq, bool, error) {
var id int
err := psql.Select("id").
From("worker_resource_config_check_sessions").
Where(sq.And{
sq.Eq{"resource_config_check_session_id": c.resourceConfigCheckSession.ID()},
sq.Eq{"team_id": c.teamID},
}).
RunWith(conn).
QueryRow().
Scan(&id)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return sq.Eq{
"worker_resource_config_check_session_id": id,
}, true, nil
}
func (c resourceConfigCheckSessionContainerOwner) Create(tx Tx, workerName string) (map[string]interface{}, error) {
var wbrtID int
err := psql.Select("id").
From("worker_base_resource_types").
Where(sq.Eq{
"worker_name": workerName,
"base_resource_type_id": c.resourceConfigCheckSession.ResourceConfig().OriginBaseResourceType().ID,
}).
RunWith(tx).
QueryRow().
Scan(&wbrtID)
if err != nil {
return nil, err
}
var wrccsID int
err = psql.Insert("worker_resource_config_check_sessions").
SetMap(map[string]interface{}{
"resource_config_check_session_id": c.resourceConfigCheckSession.ID(),
"worker_base_resource_type_id": wbrtID,
"team_id": c.teamID,
}).
Suffix("RETURNING id").
RunWith(tx).
QueryRow().
Scan(&wrccsID)
if err != nil {
return nil, err
}
return map[string]interface{}{
"worker_resource_config_check_session_id": wrccsID,
}, nil
}