-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
432 lines (412 loc) · 14.9 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
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
var npm = require('npm')
, sha1 = require('./lib/sha1')
, readJson = require('read-package-json')
, path = require('path')
, commithash = require('./lib/commit-hash')
, request = require('request')
, amino = require('amino')
, fs = require('fs')
, archy = require('archy')
, moment = require('moment')
, yaml = require('yamljs')
, child_process = require('child_process')
function list (str) {
return str.split(/ *, */).map(function (val) {
return val.match(/^\d+$/) ? parseInt(val, 10) : val;
});
}
// since commander doesn't like unknown options or dynamic args, split off
// spawn args and store them.
if (process.argv[2] === 'deploy') {
var idx = process.argv.indexOf('--');
if (~idx) {
var spawnArgs = process.argv.splice(idx + 1);
var spawnEnv = {};
for (var i = 0; i < spawnArgs.length; i++) {
var match = spawnArgs[i].match(/^([A-Z0-9_]+)=(.*?)$/);
if (match) {
spawnEnv[match[1]] = match[2];
spawnArgs.shift();
}
else {
break;
}
}
var argv = [].concat(process.argv);
process.argv = [];
for (var i = 0; i < argv.length; i++) {
var match = argv[i].match(/^\-\-env\.([A-Z0-9_]+)(?:=(.*))?$/);
if (match && match[2]) {
spawnEnv[match[1]] = match[2];
}
else if (match && match[1]) {
spawnEnv[match[1]] = argv.splice(i + 1, 1)[0];
}
else {
process.argv.push(argv[i]);
}
}
var spawnCmd = spawnArgs.shift();
}
else {
var spawnCmd = false;
}
}
var program = require('commander')
.version(require('./package').version)
.option('-s, --service <name[@version]>', 'drone service to request, with optional semver (default: app-drone)')
.option('-r, --redis <port/host/host:port/list>', 'redis server(s) used by the service (can be comma-separated)', list)
.usage('<command>')
function ifErr (err) {
if (err) {
if (err.stack) {
console.error(err.stack);
}
else {
console.error(err);
}
process.exit(1);
}
}
// manually parse json because formidable doesn't support json content type :(
function safeParse (body) {
if (body) {
try {
body = JSON.parse(body);
}
catch (e) {}
}
return body || {};
}
function findDrones(program, cb) {
var drones = [];
if (!amino.Spec) amino.init({redis: program.parent.redis});
var spec = new amino.Spec(program.parent.service);
console.log('searching for ' + spec.service + ' drones...');
amino.subscribe('_get:' + spec.service + ':' + amino.id, function (spec) {
drones.push(new amino.Spec(spec));
});
amino.publish('_get:' + spec.service, amino.id);
// end the search after one second
var completed = 0
setTimeout(function () {
console.log('found ' + drones.length + ' drone' + (drones.length !== 1 ? 's' : '') + '.');
if (program.drones) {
drones = drones.slice(0, program.drones);
}
cb(drones);
}, 1000);
}
program
.command('deploy -- [cmd] [args...]')
.description('deploy a project to drones and optionally spawn a command')
.option('-r, --root <dir>', 'project root which will be packed and sent to drones as the cwd. (default: cwd)', process.cwd())
.option('--drones <count>', 'number of drones to spawn on. (default: all drones)')
.option('--threads <count>', 'number of threads to spawn per drone. (default: drone\'s cpu count)')
.action(function () {
var program = [].slice.call(arguments).pop();
applyConfig(program);
readJson(path.join(program.root, 'package.json'), function (err, data) {
ifErr(err);
var name = data.name
, version = data.version
, completed = 0
commithash(program.root, function (err, commit) {
ifErr(err);
npm.load(function (err) {
ifErr(err);
console.log('preparing deployment...');
npm.commands.cache(['add', program.root], function (err) {
ifErr(err);
var file = path.join(npm.cache, name, version, 'package.tgz');
sha1.get(file, function (err, sha1sum) {
ifErr(err);
console.log('\ndeploying project at ' + program.root);
console.log('sha1 sum: ' + sha1sum);
if (commit) {
console.log('git hash: ' + commit);
}
console.log();
var drones;
findDrones(program, function (foundDrones) {
drones = foundDrones;
if (!drones.length) {
ifErr(new Error('no drones to deploy to!'));
}
drones.forEach(deploy);
});
function deploy (spec) {
var baseUrl = 'http://' + spec.host + ':' + spec.port;
function spawn () {
if (!spawnCmd) {
if (++completed === drones.length) {
process.exit();
}
return;
}
var url = baseUrl + '/deployments/' + sha1sum + '/spawn';
var req = request.post({url: url, json: true}, function (err, res, body) {
ifErr(err);
body = safeParse(body);
if (res.statusCode === 200 && body.status === 'ok') {
console.log('drone ' + spec.id + ': spawned ok');
if (++completed === drones.length) {
console.log('spawned on ' + completed + ' drone' + (drones.length !== 1 ? 's' : '') + '!');
process.exit();
}
}
else if (res.status === 'error') {
ifErr(new Error('drone ' + spec.id + ': received error on spawn: ' + body.error));
}
else {
ifErr(new Error('drone ' + spec.id + ': bad response code on spawn: ' + res.statusCode));
}
});
var form = req.form();
form.append('cmd', spawnCmd);
form.append('args', JSON.stringify(spawnArgs));
form.append('env', JSON.stringify(spawnEnv));
if (commit) {
form.append('commit', commit);
}
if (program.threads) {
form.append('threads', program.threads);
}
}
var url = baseUrl + '/deployments/' + sha1sum;
var req = request(url, function (err, res, body) {
ifErr(err);
body = safeParse(body);
if (res.statusCode === 404) {
console.log('drone ' + spec.id + ': deploying...');
var req = request.put(url, function (err, res, body) {
ifErr(err);
body = safeParse(body);
if (res.statusCode === 201 || res.statusCode === 200 && body.status === 'ok') {
spawn();
}
else if (body.status === 'error') {
ifErr(new Error('drone ' + spec.id + ': received error on deploy: ' + body.error));
}
else {
ifErr(new Error('drone ' + spec.id + ': bad response code on deploy: ' + res.statusCode));
}
});
var form = req.form();
form.append('name', name);
form.append('sha1sum', sha1sum);
if (commit) {
form.append('commit', commit);
}
form.append('payload', fs.createReadStream(file));
}
else if (res.statusCode === 200) {
console.log('drone ' + spec.id + ': up-to-date');
spawn();
}
else {
ifErr(new Error('drone ' + spec.id + ': bad response code on GET ' + url + ': ' + res.statusCode));
}
});
}
});
});
});
});
});
})
program
.command('redeploy')
.description('deploy latest code, spawn copies of existing processes, and then stop old processes')
.option('-r, --root <dir>', 'project root of the existing deployment. (default: cwd)', process.cwd())
.action(function (program) {
applyConfig(program);
var args = ['deploy', '--service', program.parent.service, '--root', program.root];
if (program.parent.redis) args.push('--redis', program.parent.redis.join(','));
var proc = child_process.spawn(path.resolve(__dirname, 'bin/amino'), args);
var sha1sum, commit;
proc.stdout.on('data', function (chunk) {
var data = chunk.toString();
var match = data.match(/sha1 sum: (\w+)/);
if (match) {
sha1sum = match[1];
}
match = data.match(/git hash: (\w+)/);
if (match) {
commit = match[1];
}
});
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.once('exit', function (code) {
if (code) ifErr(new Error('deploy exited with code ' + code));
findDrones(program, function (drones) {
if (!drones.length) ifErr(new Error('no drones found!'));
var completed = 0;
console.log('redeploying...');
drones.forEach(function (spec) {
var baseUrl = 'http://' + spec.host + ':' + spec.port;
var path = '/deployments/' + sha1sum + '/redeploy';
var req = request.post(baseUrl + path, function (err, res, body) {
ifErr(err);
body = safeParse(body);
if (res.statusCode === 200) {
console.log('drone ' + spec.id + ': redeployed ' + body.count + ' processes');
}
else {
console.log('drone ' + spec.id + ': error ' + res.statusCode + ': ' + body.error);
}
completed++;
if (completed === drones.length) {
process.exit();
}
});
if (commit) {
var form = req.form();
form.append('commit', commit);
}
});
});
});
})
program
.command('respawn [sha1/id]')
.description('respawn running processes, optionally on a particular git or tarball sha1 or process id')
.action(function (sha1, program) {
applyConfig(program);
findDrones(program, function (drones) {
if (!drones.length) ifErr(new Error('no drones found!'));
var completed = 0;
drones.forEach(function (spec) {
var baseUrl = 'http://' + spec.host + ':' + spec.port;
var path = sha1 ? '/ps/' + sha1 + '/respawn' : '/respawn';
request.post(baseUrl + path, function (err, res, body) {
ifErr(err);
body = safeParse(body);
if (res.statusCode === 200) {
console.log('drone ' + spec.id + ': respawned ' + body.count + ' processes');
}
else {
console.log('drone ' + spec.id + ': error ' + res.statusCode + ': ' + body.error);
}
completed++;
if (completed === drones.length) {
process.exit();
}
});
});
});
})
program
.command('ps [sha1]')
.description('show running processes, optionally on a particular git or tarball sha1')
.action(function (sha1, program) {
applyConfig(program);
findDrones(program, function (drones) {
if (!drones.length) ifErr(new Error('no drones found!'));
var completed = 0, ps = {label: program.parent.service, nodes: []};
drones.forEach(function (spec) {
var baseUrl = 'http://' + spec.host + ':' + spec.port;
var path = sha1 ? '/ps/' + sha1 : '/ps';
request.get(baseUrl + path, function (err, res, body) {
ifErr(err);
body = safeParse(body);
if (res.statusCode === 200) {
ps.nodes.push({
label: 'drone#' + spec.id + ' (' + spec.host + ':' + spec.port + ')',
nodes: Object.keys(body.ps).map(function (pid) {
return {
label: 'proc#' + pid,
nodes: Object.keys(body.ps[pid]).filter(function (k) {
return !k.match(/^(env|id)$/);
}).map(function (k) {
var val = body.ps[pid][k];
if (k === 'uptime') {
val = moment.humanizeDuration(val);
}
else if (k === 'lastRespawn') {
val = moment.humanizeDuration(-1 * (new Date().getTime() - val), true);
}
return k + ': ' + val;
})
};
})
});
}
else {
console.log('drone ' + spec.id + ': error ' + res.statusCode + ': ' + body.error);
}
completed++;
if (completed === drones.length) {
console.log(archy(ps));
process.exit();
}
});
});
});
})
program
.command('stop [sha1/id]')
.description('stop running processes, optionally on a particular git or tarball sha1 or process id')
.action(function (sha1, program) {
applyConfig(program);
findDrones(program, function (drones) {
if (!drones.length) ifErr(new Error('no drones found!'));
var completed = 0, ps = {label: program.parent.service, nodes: []};
drones.forEach(function (spec) {
var baseUrl = 'http://' + spec.host + ':' + spec.port;
var path = sha1 ? '/ps/' + sha1 : '/ps';
request.del(baseUrl + path, function (err, res, body) {
ifErr(err);
body = safeParse(body);
if (res.statusCode === 200) {
console.log('drone ' + spec.id + ': stopped ' + body.count + ' processes');
}
else {
console.log('drone ' + spec.id + ': error ' + res.statusCode + ': ' + body.error);
}
completed++;
if (completed === drones.length) {
process.exit();
}
});
});
});
})
program
.command('config')
.description('save arguments to .amino.yml to act as defaults')
.action(function (program) {
applyConfig(program);
var opts = {
service: program.parent.service,
redis: program.parent.redis
};
fs.writeFile('.amino.yml', yaml.stringify(opts), function (err) {
ifErr(err);
console.log('wrote .amino.yml');
});
})
program
.command('*')
.description('output help')
.action(function () {
program.outputHelp();
})
program.parse(process.argv);
if (!program.args.length) {
program.outputHelp();
}
function applyConfig (program) {
try {
var opts = yaml.load('.amino.yml');
Object.keys(opts).forEach(function (k) {
if (typeof program.parent[k] === 'undefined') {
program.parent[k] = opts[k];
}
});
}
catch (e) {};
var parent = program.parent || program;
parent.service || (parent.service = 'app-drone');
}