forked from shafiahmed/virtkick-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
starter.js
435 lines (365 loc) · 10.8 KB
/
starter.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
433
434
435
#!/usr/bin/env node
var path = require('path');
var mkdirp = require('mkdirp');
var yaml = require('js-yaml');
var fs = require('fs');
var yargs = require('yargs');
var async = require('async');
var extend = require('extend');
psTree = require('ps-tree')
var exittingApp = false;
process.setMaxListeners(100);
var argv = yargs
.alias('h', 'help')
.describe('h', 'show help')
.alias('d', 'development')
.describe('d', 'run in development mode')
.alias('m', 'migrate-db')
.describe('m', 'migrate database before running')
.alias('a', 'prepare-assets')
.describe('a', 'prepare assets before running (goes with production run)')
.alias('p', 'production')
.describe('p', 'run in production mode (add also -d to have production debug)')
.alias('r', 'livereload')
.describe('r', 'run development mode with livereload system')
.argv;
if(argv.h) {
return yargs.showHelp();
}
if(!argv.d && !argv.p) {
return yargs.showHelp();
}
var env = process.env;
if(argv.p) {
env.RAILS_ENV = 'production';
if(argv.d) {
env.PRODUCTION_DEBUG = '1';
}
env.SECRET_KEY_BASE = 'x';
env.DEVISE_SECRET_KEY = 'x';
} else if(argv.d) {
env.RAILS_ENV = 'development';
}
var BASE_DIR = env.BASE_DIR || __dirname;
env.PORT = env.PORT || 3000;
env.RAILS_PORT = env.RAILS_PORT || 60000;
env.RAILS_HOST = "localhost";
env.HDD_DIR = env.HDD_DIR || path.join(BASE_DIR, 'hdd');
env.ISO_DIR = env.ISO_DIR || path.join(BASE_DIR, 'iso');
var workingScriptCommand = false;
function sha512(file, cb) {
var stream = fs.createReadStream(file);
var algo = 'sha512';
var shasum = crypto.createHash(algo);
stream.on('data', function(data) {
shasum.update(data);
});
stream.on('error', cb);
stream.on('end', function() {
cb(null, shasum.digest('hash'))
});
}
function checkScript(cb) {
var output = child_process.spawn('script', ['--help']);
var allData = "";
output.stdout.on('data', function(data) {
allData += data.toString('utf8');
});
output.stderr.on('data', function(data) {
allData += data.toString('utf8');
});
output.once('exit', function() {
setTimeout(function() {
allData.split(/\n/).forEach(function(line) {
if(line.match(/-e/) && line.match(/--return/))
workingScriptCommand = true;
});;
cb();
}, 0);
});
output.once('error', function() {
cb();
});
}
function spawn(cwd, command, options) {
chSpawn = child_process.spawn;
var proc;
if(workingScriptCommand) {
proc =
chSpawn('script', ['/dev/null', '-e', '-q', '-c', command], extend({}, {
env: env,
cwd: cwd,
detached: true
}, options));
} else {
proc = chSpawn('bash', ['-c', command], extend({}, {
env: env,
cwd: cwd,
detached: true
}, options));
}
var exitCounter = 0;
var exitHandler = function() {
exittingApp = true;
exitCounter++;
psTree(proc.pid, function(err, children) {
var killer = chSpawn('kill', ['-9'].concat(children.map(function(p) {
return p.PID;
})));
killer.on('exit', function() {
exitCounter--;
if(exitCounter == 0) {
process.emit('removeExitHandlers');
process.exit();
}
});
});
};
proc.once('exit', function() {
process.removeListener('exit', exitHandler);
});
process.once('exit', exitHandler);
['SIGINT', 'SIGTERM', 'SIGHUP'].forEach(function(signal) {
process.once(signal, function() {
exitHandler();
});
});
proc.restart = function() {
psTree(proc.pid, function(err, children) {
var killer = chSpawn('kill', ['-9'].concat(children.map(function(p) {
return p.PID;
})));
});
};
return proc;
}
function spawnAsVirtkick(cmd) {
return spawn("./", "ssh -t -t -p " + (process.env.SSH_PORT || 22) + " -o \"StrictHostKeyChecking no\" virtkick@localhost " + cmd);
}
function runAsVirtkick(cmd, cb) {
var proc = spawnAsVirtkick(cmd);
var output = "";
proc.stdout.on('data', function(data) {
output += data.toString('utf8');
});
proc.stderr.on('data', function(data) {
output += data.toString('utf8');
});
proc.on('exit', function(code) {
cb(code, output);
});
}
function forceExit() {
process.emit('exit');
}
require('virtkick-proxy');
var child_process = require('child_process');
var watch = require('node-watch');
var split = require('split');
var webappDir = env.WEBAPP_DIR || path.join(BASE_DIR, 'webapp');
var backendDir = env.BACKEND_DIR || path.join(BASE_DIR, 'backend');
console.log("RAILS_ENV=" + env.RAILS_ENV);
console.log("webapp location:", webappDir);
console.log("backend location:", backendDir);
function bindOutput(proc, label, exitCb) {
var noLabels = false;
var lines = [];
proc.stdout.pipe(split()).on('data', function(line) {
if(line == '^^== BEGIN EXCEPTION') {
console.log('['+label+'] Unhandled exception ');
noLabels = true;
lines = [];
return;
} else if(line == '__== END EXCEPTION') {
noLabels = false;
try {
var xclip = child_process.execFile('xclip', ['-selection', 'c']);
xclip.stdin.write(lines.join('\n') + '\n');
xclip.stdin.end();
xclip.on('error', function(err) {
console.error('xclip failed, unable to copy exception to clipboard');
});
} catch(err) {
}
return;
} else if(noLabels) {
lines.push(line);
}
if(line.length)
process.stdout.write( (noLabels ? '' : '['+label+'] ') + line + '\n')
});
proc.stderr.pipe(split()).on('data', function(line) { if(line.length) process.stderr.write(line + '\n') });
proc.once('error', forceExit);
function exitHandler(code) {
console.log("Process", label, "exit:", code);
if(exittingApp)
return;
exitCb(code);
}
if(exitCb) {
proc.once('exit', exitHandler);
process.once('removeExitHandlers', function() {
proc.removeListener('exit', exitHandler);
});
}
}
var filter = function(pattern, fn) {
return function(filename) {
if (pattern.test(filename)) {
fn(filename);
}
}
}
function runEverything() {
if(argv.r) {
process.env.LIVERELOAD = 1;
}
var railsProcess;
var workerProcess = [];
function spawnRails() {
var rails = spawn(webappDir, 'bundle exec puma -C config/puma.rb -p ' + env.RAILS_PORT + '');
console.log("RAILS PID", rails.pid);
bindOutput(rails, 'rails', function() {
console.log('Process exitted, restarting');
railsProcess = spawnRails();
});
return rails;
}
railsProcess = spawnRails();
function createWorker(workerN) {
var worker = spawn(webappDir, 'bundle exec rake jobs:work');
bindOutput(worker, 'work' + workerN, function() {
console.log('Process exitted, restarting');
workerProcess[workerN-1] = createWorker(workerN);
});
return worker;
}
var workerCount = env.WORKER_COUNT || 1;
workerCount = Math.min(require('os').cpus().length, Math.max(workerCount, 1));
for(var i = 0;i < workerCount;++i) {
workerProcess[i] = createWorker(i+1);
}
watch(webappDir, {followSymLinks: true}, filter(/\.rb$/, function(f) {
railsProcess.restart();
for(var i = 0;i < workerCount;++i) {
workerProcess[i].restart();
}
}));
var backend;
function spawnBackend() {
backend = spawn(backendDir, 'python2 ./manage.py runserver');
bindOutput(backend, 'virtm', function() {
backend = spawnBackend();
});
return backend;
}
backend = spawnBackend();
watch(backendDir, filter(/\.py$/, function(f) {
backend.restart();
}));
if(argv.r) {
var guard = spawn(webappDir, 'guard -P livereload');
bindOutput(guard, 'guard', function() {
console.log('Guard exited');
});
}
}
var tasks1 = [];
var tasks2 = [];
var tasks3 = [];
var serialTasks = [[checkScript], tasks1, tasks2, tasks3];
if(argv.i) {
tasks1.push(function(cb) {
var proc = spawn(webappDir, '(bundle check || bundle install --jobs 8)');
bindOutput(proc, 'install', cb);
});
tasks1.push(function(cb) {
async.series([
function(cb) {
var proc = spawn(backendDir, 'python2 ./manage.py syncdb --noinput');
bindOutput(proc, 'backend:syncdb', cb);
},
function(cb) {
var proc = spawn(backendDir, 'python2 ./manage.py collectstatic --noinput');
bindOutput(proc, 'backend:collectstatic', cb);
}
], cb);
});
} else if(argv.u) {
tasks1.push(function(cb) {
var proc = spawn(webappDir, 'bundle update');
bindOutput(proc, 'update', cb);
});
}
if(argv.m) {
tasks2.push(function(cb) {
var proc = spawn(webappDir, 'bundle exec rake db:migrate');
bindOutput(proc, 'proc', cb);
});
tasks2.push(function(cb) {
var proc = spawn(webappDir, 'bundle exec rake db:migrate RAILS_ENV=test');
bindOutput(proc, 'proc', cb);
});
}
if(argv.c) {
tasks3.push(function(cb) {
var proc = spawn(webappDir, 'bundle exec rake assets:clean');
bindOutput(proc, 'assets:clean', cb);
});
}
if(argv.a) {
tasks3.push(function(cb) {
var proc = spawn(webappDir, 'bundle exec rake assets:precompile');
bindOutput(proc, 'assets', cb);
});
}
async.eachSeries(serialTasks, function(tasks, cb) {
async.parallel(tasks, cb);
}, function(err) {
if(err) {
return console.log("One of required tasks has failed")
}
runEverything();
if(!process.env.NO_DOWNLOAD) {
downloadIsos();
}
});
function downloadIsos() {
if(fs.existsSync(path.join(__dirname, ".isos-done"))) {
console.log("All isos are downloaded, delete .isos-done to redo")
return;
}
console.log("Starting download of ISO files")
var isos = yaml.safeLoad(fs.readFileSync(path.join(__dirname, './webapp/app/models/plans/iso_images.yml')), {});
async.eachLimit(isos, 4, function(iso, cb) {
if(!iso.mirrors) {
console.log("Iso", iso.name, "does not have mirrors");
return cb();
}
console.log('[aria2c:' +iso.long_name+'] Starting download of iso: '+ iso.file);
var aria2c = spawnAsVirtkick("aria2c -V --seed-time=0 --save-session-interval=5 --allow-overwrite=true --follow-metalink=mem -q -c -d iso " + iso.mirrors.map(function(url) {return "'" + url + "'";}).join(" "));
bindOutput(aria2c, 'aria2c:' +iso.long_name, function(code) {
if(code) {
return cb(code);
}
if(iso.sha512) {
runAsVirtkick('sha512sum "iso/' + iso.file + '"', function(code, output) {
var m = output.match(/^([0-9a-f]+)/);
if(m && m[1] === iso.sha512) {
return cb(code);
}
cb(code || new Error('sha512 of "iso/'+iso.file+'" does not match: expecting("'+iso.sha512+'") got("'+(m?(m[1]):null)+'") - output: ' + output));
});
} else {
return cb(code);
}
});
}, function(err) {
if(err) {
console.log("Not all isos could have been downloaded, will retry on next start", err);
return;
}
console.log("All isos downloaded");
fs.writeFileSync(path.join(__dirname, ".isos-done"), "DONE");
});
}