forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_type_scanner.go
184 lines (156 loc) · 4.98 KB
/
resource_type_scanner.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
package radar
import (
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/creds"
"github.com/concourse/atc/db"
"github.com/concourse/atc/resource"
"github.com/concourse/atc/worker"
)
type resourceTypeScanner struct {
resourceFactory resource.ResourceFactory
resourceConfigFactory db.ResourceConfigFactory
defaultInterval time.Duration
dbPipeline db.Pipeline
externalURL string
variables creds.Variables
}
func NewResourceTypeScanner(
resourceFactory resource.ResourceFactory,
resourceConfigFactory db.ResourceConfigFactory,
defaultInterval time.Duration,
dbPipeline db.Pipeline,
externalURL string,
variables creds.Variables,
) Scanner {
return &resourceTypeScanner{
resourceFactory: resourceFactory,
resourceConfigFactory: resourceConfigFactory,
defaultInterval: defaultInterval,
dbPipeline: dbPipeline,
externalURL: externalURL,
variables: variables,
}
}
func (scanner *resourceTypeScanner) Run(logger lager.Logger, resourceTypeName string) (time.Duration, error) {
pipelinePaused, err := scanner.dbPipeline.CheckPaused()
if err != nil {
logger.Error("failed-to-check-if-pipeline-paused", err)
return 0, err
}
if pipelinePaused {
logger.Debug("pipeline-paused")
return scanner.defaultInterval, nil
}
lockLogger := logger.Session("lock", lager.Data{
"resource-type": resourceTypeName,
})
savedResourceType, found, err := scanner.dbPipeline.ResourceType(resourceTypeName)
if err != nil {
logger.Error("failed-to-get-current-version", err)
return 0, err
}
if !found {
return 0, db.ResourceTypeNotFoundError{Name: resourceTypeName}
}
resourceTypes, err := scanner.dbPipeline.ResourceTypes()
if err != nil {
logger.Error("failed-to-get-resource-types", err)
return 0, err
}
versionedResourceTypes := creds.NewVersionedResourceTypes(
scanner.variables,
resourceTypes.Deserialize(),
)
source, err := creds.NewSource(scanner.variables, savedResourceType.Source()).Evaluate()
if err != nil {
logger.Error("failed-to-evaluate-resource-type-source", err)
return 0, err
}
resourceConfig, err := scanner.resourceConfigFactory.FindOrCreateResourceConfig(
logger,
db.ForResourceType(savedResourceType.ID()),
savedResourceType.Type(),
source,
versionedResourceTypes.Without(savedResourceType.Name()),
)
if err != nil {
logger.Error("failed-to-find-or-create-resource-config", err)
return 0, err
}
lock, acquired, err := scanner.dbPipeline.AcquireResourceTypeCheckingLockWithIntervalCheck(logger, resourceTypeName, resourceConfig, scanner.defaultInterval, false)
if err != nil {
lockLogger.Error("failed-to-get-lock", err, lager.Data{
"resource-type": resourceTypeName,
})
return scanner.defaultInterval, ErrFailedToAcquireLock
}
if !acquired {
lockLogger.Debug("did-not-get-lock")
return scanner.defaultInterval, ErrFailedToAcquireLock
}
defer lock.Release()
err = scanner.resourceTypeScan(logger.Session("tick"), resourceTypeName, savedResourceType, resourceConfig, versionedResourceTypes, source)
if err != nil {
return 0, err
}
return scanner.defaultInterval, nil
}
func (scanner *resourceTypeScanner) Scan(logger lager.Logger, resourceTypeName string) error {
return nil
}
func (scanner *resourceTypeScanner) ScanFromVersion(logger lager.Logger, resourceTypeName string, fromVersion atc.Version) error {
return nil
}
func (scanner *resourceTypeScanner) resourceTypeScan(logger lager.Logger, resourceTypeName string, savedResourceType db.ResourceType, resourceConfig *db.UsedResourceConfig, versionedResourceTypes creds.VersionedResourceTypes, source atc.Source) error {
resourceSpec := worker.ContainerSpec{
ImageSpec: worker.ImageSpec{
ResourceType: savedResourceType.Type(),
},
Tags: []string{},
TeamID: scanner.dbPipeline.TeamID(),
}
res, err := scanner.resourceFactory.NewResource(
logger,
nil,
db.ForResourceType(savedResourceType.ID()),
db.NewResourceConfigCheckSessionContainerOwner(resourceConfig),
db.ContainerMetadata{
Type: db.ContainerTypeCheck,
},
resourceSpec,
versionedResourceTypes.Without(savedResourceType.Name()),
worker.NoopImageFetchingDelegate{},
)
if err != nil {
logger.Error("failed-to-initialize-new-container", err)
return err
}
newVersions, err := res.Check(source, atc.Version(savedResourceType.Version()))
if err != nil {
if rErr, ok := err.(resource.ErrResourceScriptFailed); ok {
logger.Info("check-failed", lager.Data{"exit-status": rErr.ExitStatus})
return nil
}
logger.Error("failed-to-check", err)
return err
}
if len(newVersions) == 0 {
logger.Debug("no-new-versions")
return nil
}
logger.Info("versions-found", lager.Data{
"versions": newVersions,
"total": len(newVersions),
})
version := newVersions[len(newVersions)-1]
err = savedResourceType.SaveVersion(version)
if err != nil {
logger.Error("failed-to-save-resource-type-version", err, lager.Data{
"version": version,
})
return err
}
return nil
}