-
Notifications
You must be signed in to change notification settings - Fork 8
/
flatten.js
393 lines (324 loc) · 12.4 KB
/
flatten.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
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
'use strict';
const Hoek = require('hoek');
const clone = require('clone');
/**
* Check if a job has duplicate steps
* @method checkAdditionalRules
* @param {Object} doc Document that went through structural parsing
* @return {Array} List of errors or null if no error
*/
function checkAdditionalRules(doc) {
const errors = [];
Object.keys(doc.jobs).forEach((job) => {
const { steps } = doc.jobs[job];
const stepList = [];
if (steps) {
for (let i = 0; i < steps.length; i += 1) {
const stepName = Object.keys(steps[i])[0];
if ((typeof (steps[i]) === 'object') && (stepList.includes(stepName))) {
errors.push(`Job ${job} has duplicate step: ${stepName}`);
}
stepList.push(stepName);
}
}
});
return errors.length === 0 ? null : errors;
}
/**
* Flatten cache settings to each job
* @method flattenCacheSettings
* @param {Object} cacheConfig top-level cache settings
* @param {Object} jobs jobs from screwdriver yaml
* @return {Object} new jobs with cache config flattened
*/
function flattenCacheSettings(cacheConfig, jobs) {
const cache = {
pipeline: Hoek.reach(cacheConfig, 'pipeline', { default: [] }),
event: Hoek.reach(cacheConfig, 'event', { default: [] })
};
Object.keys(jobs).forEach((jobName) => {
jobs[jobName].cache = Hoek.applyToDefaults(cache,
{ job: Hoek.reach(cacheConfig, `job.${jobName}`, { default: [] }) });
});
return jobs;
}
/**
* Merge oldJob into newJob
* "oldJob" takes precedence over "newJob". For ex: individual job settings vs shared settings
* @param {Object} newJob Job to be merged into. For ex: shared settings
* @param {Object} oldJob Job to merge. For ex: individual job settings
* @param {Boolean} fromTemplate Whether this is merged from template. If true, perform extra actions such as wrapping.
*/
function merge(newJob, oldJob, fromTemplate) {
// Intialize new job with default fields (environment, settings, and secrets)
newJob.annotations = newJob.annotations || {};
newJob.environment = newJob.environment || {};
newJob.settings = newJob.settings || {};
// Merge
Object.assign(newJob.annotations, oldJob.annotations || {});
Object.assign(newJob.environment, oldJob.environment || {});
Object.assign(newJob.settings, oldJob.settings || {});
newJob.image = oldJob.image || newJob.image;
if (oldJob.requires) {
newJob.requires = [].concat(oldJob.requires);
} // otherwise, keep it the same, or don't set if it wasn't set
if (oldJob.blockedBy) {
newJob.blockedBy = [].concat(oldJob.blockedBy);
}
if (oldJob.freezeWindows) {
newJob.freezeWindows = [].concat(oldJob.freezeWindows);
}
if (oldJob.cache) {
newJob.cache = oldJob.cache;
}
// Merge secrets
const newSecrets = newJob.secrets || [];
const oldSecrets = oldJob.secrets || [];
// Merge sourcePaths
let newsourcePaths = newJob.sourcePaths || [];
let oldsourcePaths = oldJob.sourcePaths || [];
newsourcePaths = Array.isArray(newsourcePaths) ? newsourcePaths : [newsourcePaths];
oldsourcePaths = Array.isArray(oldsourcePaths) ? oldsourcePaths : [oldsourcePaths];
// remove duplicate
newJob.secrets = [...new Set([...newSecrets, ...oldSecrets])];
newJob.sourcePaths = [...new Set([...newsourcePaths, ...oldsourcePaths])];
if (fromTemplate && oldJob.steps) {
let stepName;
let preStepName;
let postStepName;
const mergedSteps = [];
const teardownSteps = [];
// convert steps from oldJob from array to object for faster lookup
const oldSteps = oldJob.steps.reduce((obj, item) => {
const key = Object.keys(item)[0];
if (key.startsWith('teardown-')) {
teardownSteps.push(key);
}
obj[key] = item[key];
return obj;
}, {});
for (let i = 0; i < newJob.steps.length; i += 1) {
[stepName] = Object.keys(newJob.steps[i]);
preStepName = `pre${stepName}`;
postStepName = `post${stepName}`;
// add pre-step
if (oldSteps[preStepName]) {
mergedSteps.push({ [preStepName]: oldSteps[preStepName] });
}
// if user doesn't define the same step, add it
if (!oldSteps[stepName]) {
mergedSteps.push(newJob.steps[i]);
} else if (!stepName.startsWith('teardown-')) {
// if user defines the same step, only add if it's not teardown
// otherwise, skip (it will be overriden later, otherwise will get duplicate steps)
mergedSteps.push({ [stepName]: oldSteps[stepName] });
}
// add post-step
if (oldSteps[postStepName]) {
mergedSteps.push({ [postStepName]: oldSteps[postStepName] });
}
}
for (let i = 0; i < teardownSteps.length; i += 1) {
stepName = teardownSteps[i];
mergedSteps.push({ [stepName]: oldSteps[stepName] });
}
newJob.steps = mergedSteps;
}
}
/**
* Overlays the fields specified in a specific job on top of the defaults in shared
*
* @method flattenSharedIntoJobs
* @param {Job} shared A kind of default Job template
* @param {Object} jobs Object with all the jobs
* @return {Object} New object with jobs after merging
*/
function flattenSharedIntoJobs(shared, jobs) {
const newJobs = {};
Object.keys(jobs).forEach((jobName) => {
const newJob = clone(shared);
const oldJob = clone(jobs[jobName]);
// Replace
['image', 'matrix', 'steps', 'template', 'description'].forEach((key) => {
if (oldJob[key]) {
newJob[key] = oldJob[key];
}
});
merge(newJob, oldJob);
newJobs[jobName] = newJob;
});
return newJobs;
}
/**
* Retrieve template and merge into job config
*
* @method mergeTemplateIntoJob
* @param {String} jobName Job name
* @param {Object} jobConfig Job config
* @param {Object} newJobs Object with all the jobs
* @param {TemplateFactory} templateFactory Template Factory to get templates
* @return {Promise}
*/
function mergeTemplateIntoJob(jobName, jobConfig, newJobs, templateFactory) {
const oldJob = jobConfig;
// Try to get the template
return templateFactory.getTemplate(jobConfig.template)
.then((template) => {
if (!template) {
throw new Error(`Template ${jobConfig.template} does not exist`);
}
const newJob = template.config;
const environment = newJob.environment || {};
// Construct full template name
let fullName = template.name;
if (template.namespace && template.namespace !== 'default') {
fullName = `${template.namespace}/${template.name}`;
}
// Inject template full name, name, namespace, and version to env
newJob.environment = Hoek.merge(environment, {
SD_TEMPLATE_FULLNAME: fullName,
SD_TEMPLATE_NAME: template.name,
SD_TEMPLATE_NAMESPACE: template.namespace || '',
SD_TEMPLATE_VERSION: template.version
});
merge(newJob, oldJob, true);
delete newJob.template;
newJob.templateId = template.id;
// If template.images contains a label match for the image defined in the job
// set the job image to the respective template image
if (typeof template.images !== 'undefined'
&& typeof template.images[newJob.image] !== 'undefined') {
newJob.image = template.images[newJob.image];
}
newJobs[jobName] = newJob;
return null;
});
}
/**
* Goes through each job and if template is specified, then merge into job config
*
* @method flattenTemplates
* @param {Object} jobs Object with all the jobs
* @param {TemplateFactory} templateFactory Template Factory to get templates
* @return {Promise} Resolves to new object with jobs after merging templates
*/
function flattenTemplates(jobs, templateFactory) {
const newJobs = {};
const templates = [];
// eslint-disable-next-line
Object.keys(jobs).forEach((jobName) => {
const jobConfig = clone(jobs[jobName]);
const templateConfig = jobConfig.template;
// If template is specified, then merge
if (templateConfig) {
templates.push(mergeTemplateIntoJob(jobName, jobConfig, newJobs, templateFactory));
} else {
newJobs[jobName] = jobConfig; // Otherwise just use jobConfig
}
});
// Wait until all promises are resolved
return Promise.all(templates)
.then(() => newJobs);
}
/**
* Converts complex environment variables like objects or arrays into JSON-ified strings
*
* This is because YAML allows you to define complex structures easily, but our input to the shell
* is just simple strings (environment variables).
* @method cleanComplexEnvironment
* @param {Object} jobs Object with all the jobs
* @return {Object} Updated object with jobs after cleaning
*/
function cleanComplexEnvironment(jobs) {
Object.keys(jobs).forEach((jobName) => {
const environment = Hoek.reach(jobs, `${jobName}.environment`, {
default: {}
});
Object.keys(environment).forEach((varName) => {
switch (typeof environment[varName]) {
case 'object':
case 'number':
case 'boolean':
environment[varName] = JSON.stringify(environment[varName]);
break;
default:
}
});
});
return jobs;
}
/**
* Converts the simplified steps into the most-verbose format
*
* This is because the user can provide either:
* - "command to execute"
* - { name: command }
*
* The launcher expects it in a consistent format:
* - { name: "name", command: "command" }
* @method convertSteps
* @param {Object} jobs Object with all the jobs
* @return {Object} New object with jobs after up-converting
*/
function convertSteps(jobs) {
const newJobs = jobs;
Object.keys(newJobs).forEach((jobName) => {
const steps = clone(Hoek.reach(newJobs, `${jobName}.steps`, {
default: []
}));
newJobs[jobName].commands = steps.map((step, index) => {
let name;
let command;
switch (typeof step) {
case 'object':
name = Object.keys(step).pop();
command = step[name];
break;
case 'string':
name = `step-${index + 1}`;
command = step;
break;
default:
}
return { name, command };
});
});
return newJobs;
}
/**
* Flatten Phase
*
* This is where we compress the complexity of the yaml into a format closer to the desired output
* so that it is easier to validate and iterate on.
* - Merges shared into jobs
* - Merges templates into jobs
* - Converts complex environment definitions into JSON strings
* @method
* @param {Object} parsedDoc Document that went through structural parsing
* @returns {Promise}
*/
module.exports = (parsedDoc, templateFactory) => {
const doc = parsedDoc;
// Flatten shared into jobs
doc.jobs = flattenSharedIntoJobs(parsedDoc.shared, parsedDoc.jobs);
delete doc.shared;
if (parsedDoc.cache) {
doc.jobs = flattenCacheSettings(parsedDoc.cache, doc.jobs);
delete doc.cache;
}
// Flatten templates
return flattenTemplates(doc.jobs, templateFactory)
// Clean through the job values
.then(cleanComplexEnvironment)
// Convert steps into proper expanded output
.then(convertSteps)
// Append flattened jobs and return flattened doc
.then((jobs) => {
doc.jobs = jobs;
const errors = checkAdditionalRules(doc);
if (errors) {
throw new Error(errors.toString());
}
return doc;
});
};