-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
275 lines (238 loc) · 7.22 KB
/
index.js
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
'use strict';
/* eslint-disable no-underscore-dangle */
const dataSchema = require('screwdriver-data-schema');
const executorSchema = dataSchema.plugins.executor;
const ANNOTATIONS = [
'screwdriver.cd/cpu',
'screwdriver.cd/ram',
'screwdriver.cd/disk',
'screwdriver.cd/diskSpeed',
'screwdriver.cd/timeout',
'screwdriver.cd/executor',
'screwdriver.cd/buildPeriodically',
'screwdriver.cd/repoManifest',
'screwdriver.cd/dockerEnabled',
'screwdriver.cd/dockerCpu',
'screwdriver.cd/dockerRam',
'screwdriver.cd/nodeLabel',
'screwdriver.cd/terminationGracePeriodSeconds'
];
const annotationRe = /screwdriver.cd\/(\w+)/;
/**
* Validate the config using the schema
* @async validate
* @param {Object} config Configuration
* @param {Object} schema Joi object used for validation
* @return {Promise}
*/
async function validate(config, schema) {
const result = schema.validate(config);
if (result.error) {
throw result.error;
}
return config;
}
class Executor {
/**
* Constructor for Executor
* @method constructor
* @param {Object} config Configuration
* @return {Executor}
*/
constructor(config) {
this.config = config;
}
/**
* Start a new build
* @method start
* @param {Object} config Configuration
* @param {Object} [config.annotations] Optional key/value object
* @param {String} config.apiUri Screwdriver's API
* @param {Object} [config.build] Build object
* @param {String} config.buildId Unique ID for a build
* @param {String} config.container Container for the build to run in
* @param {String} config.token JWT to act on behalf of the build
* @return {Promise}
*/
start(config) {
return validate(config, executorSchema.start).then(validConfig => this._start(validConfig));
}
async _start() {
throw new Error('Not implemented');
}
/**
* Stop a running or finished build
* @method stop
* @param {Object} config Configuration
* @param {String} config.buildId Unique ID for a build
* @return {Promise}
*/
stop(config) {
return validate(config, executorSchema.stop).then(validConfig => this._stop(validConfig));
}
async _stop() {
throw new Error('Not implemented');
}
/**
* verify if a build is running
* @method verify
* @param {Object} config Configuration
* @param {String} config.buildId Unique ID for a build
* @return {Promise}
*/
verify(config) {
return validate(config, executorSchema.verify).then(validConfig => this._verify(validConfig));
}
async _verify() {
// no-op in case not implemented in extenders
}
/**
* Starts a new periodic build in an executor
* @method _startPeriodic
* @param {Object} config Configuration
* @return {Promise}
*/
startPeriodic(config) {
return this._startPeriodic(config);
}
async _startPeriodic() {
throw new Error('Not implemented');
}
/**
* Stops a previously scheduled periodic build in an executor
* @async _stopPeriodic
* @param {Object} config Configuration
* @return {Promise}
*/
stopPeriodic(config) {
return this._stopPeriodic(config);
}
async _stopPeriodic() {
throw new Error('Not implemented');
}
/**
* Starts a new frozen build in an executor
* @method _startFrozen
* @param {Object} config Configuration
* @return {Promise}
*/
startFrozen(config) {
return this._startFrozen(config);
}
async _startFrozen() {
throw new Error('Not implemented');
}
/**
* Stops a previously scheduled frozen build in an executor
* @async _stopFrozen
* @param {Object} config Configuration
* @return {Promise}
*/
stopFrozen(config) {
return this._stopFrozen(config);
}
async _stopFrozen() {
throw new Error('Not implemented');
}
/**
* Get the status of a build
* @method status
* @param {Object} config Configuration
* @param {String} config.buildId Unique ID for a build
* @return {Promise}
*/
status(config) {
return validate(config, executorSchema.status).then(validConfig => this._status(validConfig));
}
async _status() {
throw new Error('Not implemented');
}
/**
* Clean up any processing tasks
* @method cleanUp
*/
async cleanUp() {
await this._cleanUp();
}
async _cleanUp() {
// no-op in case not implemented in extenders
}
/**
* Adds information to the timeout queue
* @method status
* @param {Object} config Configuration object
* @param {String} config.buildId Unique ID for a build
* @param {String} config.startTime Start time fo build
* @param {String} config.buildStatus Status of build
* @return {Promise}
*/
startTimer(config) {
return this._startTimer(config);
}
async _startTimer() {
// no-op in case not implemented in extenders
}
/**
* Removes information from the timeout queue
* @method status
* @param {Object} config Configuration object
* @param {String} config.buildId Unique ID for a build
* @return {Promise}
*/
stopTimer(config) {
return this._stopTimer(config);
}
async _stopTimer() {
// no-op in case not implemented in extenders
}
/**
* Return statistics on the executor
* @method stats
* @return {Object} object Hash containing metrics for the executor
*/
stats() {
return {};
}
/**
* Parsed annotations object
* @method parseAnnotations
* @return {Object} object Object contains parsed annotations
*/
parseAnnotations(annotations) {
const parsedAnnotations = {};
Object.keys(annotations).forEach(key => {
const parsedKey = key.replace(/^beta./, '');
if (ANNOTATIONS.includes(parsedKey)) {
// First group will be the part after slash, e.g. cpu, ram, disk
parsedAnnotations[parsedKey.match(annotationRe)[1]] = annotations[key];
}
});
return parsedAnnotations;
}
/**
* Unzip the ZIP of artifacts
* @method unzipArtifacts
* @param {Object} config Configuration
* @param {Number} config.buildId Unique ID for a build
* @return {Promise}
*/
unzipArtifacts(config) {
return this._unzipArtifacts(config);
}
async _unzipArtifacts() {
throw new Error('Not implemented');
}
/**
* Enqueue webhookConfig to queue service
* @method enqueueWebhook
* @param {Object} config Configuration
* @return {Promise}
*/
enqueueWebhook(config) {
return this._enqueueWebhook(config);
}
async _enqueueWebhook() {
throw new Error('Not implemented');
}
}
module.exports = Executor;