-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteps.js
More file actions
570 lines (535 loc) · 18.4 KB
/
steps.js
File metadata and controls
570 lines (535 loc) · 18.4 KB
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
'use strict';
var async = require('async');
var childProcess = require('child_process');
var colors = require('colors');
var crypto = require('crypto');
var exists = require('101/exists');
var fs = require('fs');
var lockfile = require('lockfile');
var path = require('path');
var Transformer = require('fs-transform');
var Builder = require('./steps/build.js');
var injector = require('./steps/injector.js');
var utils = require('./utils');
var thisDir = __dirname;
// these are really for npm test so that we can re-direct the caches
var cacheDir = process.env.CACHE_DIR || '/cache';
var layerCacheDir = process.env.LAYER_CACHE_DIR || '/layer-cache';
var steps = module.exports = {
dirs: {},
logs: {},
data: {},
checkForRequiredEnvVars: function (cb) {
var err;
[
'RUNNABLE_AWS_ACCESS_KEY',
'RUNNABLE_AWS_SECRET_KEY'
].forEach(function (reqEnv) {
if (!process.env[reqEnv] && !err) {
err = new Error('Missing credentials.'.bold.red);
}
});
cb(err);
},
makeWorkingFolders: function (cb) {
async.parallel({
dockerContext: mkDir.bind(null, '/tmp/rnnbl.XXXXXXXXXXXXXXXXXXXX'),
keyDirectory: mkDir.bind(null, '/tmp/rnnbl.key.XXXXXXXXXXXXXXXXXXXX'),
dockerBuild: mkFile.bind(null, '/tmp/rnnbl.log.XXXXXXXXXXXXXXXXXXXX'),
stdout: mkFile.bind(null, '/tmp/rnnbl.ib.stdout.XXXXXXXXXXXXXXXXXXXX'),
stderr: mkFile.bind(null, '/tmp/rnnbl.ib.stderr.XXXXXXXXXXXXXXXXXXXX')
}, function (err, results) {
if (err) { return cb(err); }
steps.dirs.dockerContext = results.dockerContext;
steps.dirs.keyDirectory = results.keyDirectory;
steps.logs.dockerBuild = results.dockerBuild;
steps.logs.stdout = results.stdout;
steps.logs.stderr = results.stderr;
cb();
});
function mkDir (template, cb) {
childProcess.execFile('mktemp', ['-d', template],
function (err, stdout) {
if (err) { return cb(err); }
cb(null, stdout.toString().trim());
});
}
function mkFile (template, cb) {
childProcess.execFile('mktemp', [template],
function (err, stdout) {
if (err) { return cb(err); }
cb(null, stdout.toString().trim());
});
}
},
downloadDeployKeys: function (cb) {
if (!process.env.RUNNABLE_DEPLOYKEY) { return cb(); }
if (!process.env.RUNNABLE_KEYS_BUCKET) {
return cb(new Error('RUNNABLE_KEYS_BUCKET is missing.'));
}
var deployKeys = process.env.RUNNABLE_DEPLOYKEY.split(';');
async.map(deployKeys,
function (key, cb) {
var args = [
'downloadS3Files.js',
'--bucket', process.env.RUNNABLE_KEYS_BUCKET,
'--file', key,
'--dest', steps.dirs.keyDirectory
];
childProcess.execFile('node', args,
{
cwd: thisDir,
maxBuffer: 1024 * 5000
},
steps.saveToLogs(cb)
);
},
cb);
},
chmodAllKeys: function (cb) {
if (!process.env.RUNNABLE_DEPLOYKEY ||
process.env.RUNNABLE_DEPLOYKEY.length === 0) { return cb(); }
// Can't use execFile here because we need to use bash's * construct
childProcess.exec('chmod -R 600 *',
{
cwd: steps.dirs.keyDirectory
},
steps.saveToLogs(cb)
);
},
downloadBuildFiles: function (cb) {
if (!process.env.RUNNABLE_FILES) { return cb(); }
if (!process.env.RUNNABLE_FILES_BUCKET) {
return cb(new Error('RUNNABLE_FILES_BUCKET is missing.'));
}
if (!process.env.RUNNABLE_PREFIX) {
// since we use this, let's sanity set it
process.env.RUNNABLE_PREFIX = '';
}
var files;
try {
files = JSON.parse(process.env.RUNNABLE_FILES);
} catch (err) {
return cb(new Error('RUNNABLE_FILES is poorly formatted JSON.'));
}
// one call to downloadS3Files with this object
var args = [
'downloadS3Files.js', // this should be first
'--bucket', process.env.RUNNABLE_FILES_BUCKET,
'--files', process.env.RUNNABLE_FILES,
'--prefix', process.env.RUNNABLE_PREFIX,
'--dest', steps.dirs.dockerContext
];
childProcess.execFile(
'node',
args,
{
cwd: thisDir,
maxBuffer: 1024 * 5000
},
steps.saveToLogs(cb)
);
},
getRepositories: function (cb) {
if (!process.env.RUNNABLE_REPO) {
return cb();
} else if (!process.env.RUNNABLE_COMMITISH) {
return cb(new Error('RUNNABLE_COMMITISH is missing.'));
}
// make sure we have a cache directory
childProcess.execFile('mkdir', ['-p', cacheDir], function (err) {
if (err) { return cb(err); }
var reposAndBranches = zip(
process.env.RUNNABLE_REPO.split(';'),
process.env.RUNNABLE_COMMITISH.split(';'));
var reposAndKeys = zip(
process.env.RUNNABLE_REPO.split(';'),
process.env.RUNNABLE_DEPLOYKEY.split(';'));
// TODO this could be in parallel
async.mapSeries(
Object.keys(reposAndBranches),
function (repo, cb) {
var repoName = repo.split('/').pop();
var repoFullName = repo.split(':').pop();
var repoCacheDir = path.join(cacheDir, repoFullName);
var repoTargetDir = path.join(steps.dirs.dockerContext, repoName);
var repoKey = reposAndKeys[repo];
var repoKeyPath = path.join(steps.dirs.keyDirectory, repoKey);
var commitish = reposAndBranches[repo];
var lockfileName = path.join(cacheDir, repoFullName + '.lock');
var aquiredLock = false;
utils.log(
colors.yellow.bold('Cloning ' + repoFullName +
' into ' + repoName));
async.waterfall([
function makeRepoDir (cb) {
childProcess.execFile('mkdir', ['-p', repoFullName],
steps.saveToLogs(function (err) {
cb(err);
})
);
},
function addKeyToAgent (cb) {
childProcess.execFile('ssh-add', ['-D'], function (err) {
if (err) { return cb(err); }
childProcess.execFile('ssh-add', [repoKeyPath],
steps.saveToLogs(function (err) {
cb(err);
})
);
});
},
function tryForLock (cb) {
// need to make sure the directory for the username is there...
childProcess.execFile('mkdir', ['-p', path.dirname(lockfileName)],
function (err) {
if (err) { return cb(err); }
// TODO no steps and retries yet
lockfile.lock(lockfileName, function (err) {
// if no err, then we got the lock
aquiredLock = !err;
cb(null, aquiredLock);
});
}
);
},
function cacheOrClone (lockAquired, cb) {
if (lockAquired) {
var fetchAndCheckout = true;
async.series([
function upsertCachedRepoDir (cb) {
if (fs.existsSync(path.join(repoCacheDir, '.git'))) {
// repo exists, just will update
cb();
} else {
childProcess.execFile('git', [
'clone',
'-q',
'--depth=1',
repo,
repoCacheDir
],
{ maxBuffer: 1024 * 5000 },
steps.saveToLogs(cb));
}
},
function getHEADCommit (cb) {
childProcess.execFile('git', ['rev-parse', 'HEAD'],
{
cwd: repoCacheDir
},
steps.saveToLogs(function (err, stdout) {
if (err) { return cb(err); }
fetchAndCheckout = (stdout.trim() !== commitish);
cb();
})
);
},
function updateCachedRepo (cb) {
if (fetchAndCheckout) {
childProcess.execFile('git', ['fetch', '--all'],
{
cwd: repoCacheDir,
maxBuffer: 1024 * 5000
},
steps.saveToLogs(cb)
);
} else {
cb();
}
},
function checkoutCommitish (cb) {
if (fetchAndCheckout) {
childProcess.execFile('git', [
'checkout',
'-q',
commitish
],
{
cwd: repoCacheDir
},
steps.saveToLogs(cb)
);
} else {
cb();
}
},
function copyToTargetDir (cb) {
// NOTE: a potential hole here. check how links are handled
var args = [
'-p',
'-r',
'-d',
repoCacheDir,
repoTargetDir
];
childProcess.execFile('cp', args, steps.saveToLogs(cb));
},
// TODO touch all the things (fix times after copy)
// function touchAllTheThings (cb) {}
], function (err) {
// trim out all the other stuff in arguments
cb(err);
});
} else {
childProcess.execFile('git', [
'clone',
'-q',
'--depth=1',
repo,
repoTargetDir
],
{ maxBuffer: 1024 * 5000 },
steps.saveToLogs(function (err) {
if (err) { return cb(err); }
childProcess.execFile('git', ['checkout', '-q', commitish],
{
cwd: repoTargetDir,
maxBuffer: 1024 * 5000
},
steps.saveToLogs(function (err) { cb(err); }));
}));
}
},
function releaseLock (cb) {
if (aquiredLock) {
lockfile.unlock(lockfileName, function (err) {
if (!err) {
aquiredLock = null;
}
cb(err);
});
} else {
cb();
}
}
], function (err) {
if (aquiredLock) {
lockfile.unlock(lockfileName, function () {});
}
cb(err);
});
},
cb
);
});
},
applySearchAndReplace: function (cb) {
// Just-in-case check
if (!process.env.SEARCH_AND_REPLACE_RULES) {
utils.log('Skipping search and replace.');
return cb();
}
utils.log('Applying search and replace rules.');
var reposAndRules = zip(
process.env.RUNNABLE_REPO.split(';'),
process.env.SEARCH_AND_REPLACE_RULES.split(';'));
async.mapSeries(Object.keys(reposAndRules), function (repo, transformCb) {
var repoName = repo.split('/').pop();
var repoTargetDir = path.join(steps.dirs.dockerContext, repoName);
var rules = JSON.parse(reposAndRules[repo]);
Transformer.transform(repoTargetDir, rules, transformCb);
}, cb);
},
parseDockerfile: function (cb) {
steps.dirs.buildRoot = steps.dirs.dockerContext;
var dockerfilePath = path.join(steps.dirs.buildRoot, 'Dockerfile');
if (process.env.RUNNABLE_BUILD_DOCKERFILE) {
// there is only one repo if we are in this mode
var repoName = process.env.RUNNABLE_REPO.split('/')[1];
var dockerfileFolderPath = path.dirname(
process.env.RUNNABLE_BUILD_DOCKERFILE
);
var dockerfileName = path.basename(process.env.RUNNABLE_BUILD_DOCKERFILE);
steps.dirs.buildRoot = path.join(
steps.dirs.dockerContext,
repoName,
dockerfileFolderPath
);
dockerfilePath = path.join(
steps.dirs.buildRoot,
dockerfileName);
}
var dockerfile = fs.readFileSync(dockerfilePath).toString();
async.series([
handleRunnableCache,
handleDockerfileInjection,
saveDockerfile
], cb);
function handleRunnableCache (cb) {
var runCmdRegex;
/* jshint ignore:start */
// sorry it's so looooong
runCmdRegex =
/^([R|r][U|u][N|n](.|\\\s)+\# ?[R|r][U|u][N|n][N|n][A|a][B|b][L|l][E|e]-[C|c][A|a][C|c][H|h][E|e])$/m;
/* jshint ignore:end */
var match = runCmdRegex.exec(dockerfile);
if (!match) {
return cb();
}
steps.data.cachedLine = match[1];
var md5sum = crypto.createHash('md5');
md5sum.update(steps.data.cachedLine);
steps.data.createdByHash = md5sum.digest('hex');
// if the cache layer hash exists, use the layer.tar that should be there
var layerTar = path.join(
layerCacheDir,
process.env.RUNNABLE_DOCKERTAG.split(':')[0],
steps.data.createdByHash + '.tar');
childProcess.execFile('cp', ['-p', layerTar, steps.dirs.dockerContext],
steps.saveToLogs(function (err) {
// if this error'd, just don't use the layer
if (err) { return cb(); }
dockerfile = dockerfile.replace(
match[1],
'ADD ' + steps.data.createdByHash + '.tar /\n' + match[1]);
steps.data.usingCache = true;
cb();
})
);
}
function handleDockerfileInjection (cb) {
dockerfile = injector(dockerfile);
cb();
}
function saveDockerfile (cb) {
fs.writeFileSync(dockerfilePath, dockerfile);
cb();
}
},
runDockerBuild: function (cb) {
utils.log('Building server...'.bold.yellow);
var builder = new Builder(steps);
builder.runDockerBuild(cb);
},
parseBuildLogAndHistory: function (cb) {
if (steps.data.usingCache) { return cb(); }
var buildLog = fs.readFileSync(steps.logs.dockerBuild).toString();
var lines = buildLog.split('\n');
var match = null;
for (var i = lines.length-1; i >= 0; --i) {
match = /Successfully built ([0-9a-f]+)/.exec(lines[i]);
if (match) { break; }
}
if (!match) {
return cb(new Error('could not determine image built'));
}
var imageId = match[1];
var historyArgs = [
'--host', process.env.RUNNABLE_DOCKER,
'history',
'--no-trunc',
imageId
];
childProcess.execFile('docker', historyArgs,
{ maxBuffer: 1024 * 5000 },
steps.saveToLogs(function (err, stdout) {
if (err) { return cb(err); }
var lines = stdout.toString().split('\n');
var layer;
for (var i = 0; i < lines.length; ++i) {
/* jshint ignore:start */
if (/\# ?[R|r][U|u][N|n][N|n][A|a][B|b][L|l][E|e]-[C|c][A|a][C|c][H|h][E|e]/
.test(lines[i].trim())) {
layer = lines[i].trim().split(' ').shift();
break;
}
/* jshint ignore:end */
}
if (layer) {
steps.data.getLayerFromThisImage = imageId;
steps.data.cacheThisLayer = layer;
}
cb();
})
);
},
copyLayer: function (cb) {
if (steps.data.usingCache &&
!steps.data.cacheThisLayer ||
!steps.data.getLayerFromThisImage) { return cb(); }
var args = [
'--host', process.env.RUNNABLE_DOCKER,
'run',
'-d',
'--label', 'type=layerCopy',
'-e', 'IMAGE_ID=' + steps.data.getLayerFromThisImage,
'-e', 'CACHED_LAYER=' + steps.data.cacheThisLayer,
'-e', 'CACHED_LAYER_HASH=' + steps.data.createdByHash,
'-e', 'RUNNABLE_DOCKER=' + process.env.RUNNABLE_DOCKER,
'-e', 'RUNNABLE_DOCKERTAG=' + process.env.RUNNABLE_DOCKERTAG,
'-v', process.env.DOCKER_IMAGE_BUILDER_LAYER_CACHE +
':' + layerCacheDir
];
if (/docker.sock/.test(process.env.RUNNABLE_DOCKER)) {
args.push('-v');
args.push('/var/run/docker.sock:/var/run/docker.sock');
}
args.push(process.env.RUNNABLE_IMAGE_BUILDER_NAME + ':' +
process.env.RUNNABLE_IMAGE_BUILDER_TAG);
args.push('./lib/dockerLayerArchive.sh');
childProcess.execFile('docker', args, steps.saveToLogs(cb));
},
pushImage: function (cb) {
if (!process.env.RUNNABLE_PUSH_IMAGE) {
return cb();
}
var args = [
'--host', process.env.RUNNABLE_DOCKER,
'run',
'-d',
'--label="type=imagePush"',
// retry if failed up to 5 times
'--restart=on-failure:5',
'-e', 'RUNNABLE_DOCKER=' + process.env.RUNNABLE_DOCKER,
'-e', 'RUNNABLE_DOCKERTAG=' + process.env.RUNNABLE_DOCKERTAG,
'-e', 'NODE_ENV=' + process.env.NODE_ENV
];
if (/docker.sock/.test(process.env.RUNNABLE_DOCKER)) {
args.push('-v');
args.push('/var/run/docker.sock:/var/run/docker.sock');
}
args.push(process.env.RUNNABLE_IMAGE_BUILDER_NAME + ':' +
process.env.RUNNABLE_IMAGE_BUILDER_TAG);
args.push('node', './lib/push-image.js');
childProcess.execFile('docker', args, steps.saveToLogs(cb));
},
saveToLogs: function (cb) {
return function (err, stdout, stderr) {
if (!exists(stdout)) { stdout = ''; }
stdout = stdout.toString();
if (!exists(stderr)) { stderr = ''; }
stderr = stderr.toString();
async.parallel([
function saveStdout (cb) {
if (stdout.length) {
fs.appendFile(steps.logs.stdout, stdout, cb);
} else {
cb();
}
},
function saveStderr (cb) {
if (stderr.length) {
fs.appendFile(steps.logs.stderr, stderr, cb);
} else {
cb();
}
}
], function (asyncErr) {
if (asyncErr) {
cb(new Error('error saving output to files'));
} else if (err) {
cb(err, stdout, stderr);
} else {
cb(null, stdout, stderr);
}
});
};
}
};
function zip (a, b) {
return a.reduce(function (memo, curr, index) {
memo[curr] = b[index];
return memo;
}, {});
}