forked from cadecairos/webmaker-suite
/
run.js
executable file
·165 lines (151 loc) · 5.22 KB
/
run.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
console.log("\n=======================================");
console.log("Starting all apps in the Webmaker suite");
console.log("=======================================");
/**
* Runtime argument parsing
*/
function getRunTime() {
var argv = require("argv");
argv.option({
name: 'nomongo',
type: 'string',
description: 'Do not start mongodb (useful for when you already run mongo)',
example: "'node run --nomongo'"
});
argv.option({
name: 'noes',
type: 'string',
description: 'Do not start elastic search (useful for when you already run es)',
example: "'node run --noes'"
});
argv.option({
name: 'cleares',
type: 'boolean',
description: 'Clear the elasticsearch index when starting the suite, which makes webmaker think there are no makes.',
example: "'node run --cleares'"
});
argv.option({
name: 'exclude',
type: 'list.string',
description: 'Do not start specific apps. For convenience, apps named "xyz.webmaker.org" can be indicated as just "xyz".',
example: "'node run --exclude=goggles,webmaker.org,htmlsanitizer.org,makeapi'"
});
var runtime = argv.run().options;
runtime.excluded = function() { return false; }
if(runtime.exclude) {
runtime.exclude = runtime.exclude.split(",").map(function(v) { return v.toLowerCase(); });
runtime.excluded = function(key) {
key = key.toLowerCase();
var excluded = (runtime.exclude.indexOf(key) !== -1);
if(excluded) return true;
key = key.replace(".webmaker.org",'');
excluded = (runtime.exclude.indexOf(key) !== -1);
return excluded;
};
}
return runtime;
}
var fs = require("fs"),
batchExec = require("./lib/batch").batchExec,
commandStrings = require("./lib/commandstrings"),
repos = require("./lib/repos")(commandStrings),
runtime = getRunTime(),
spawn = require("child_process").spawn;
function run() {
var i = 1;
Object.keys(repos).forEach(function (appName) {
if(runtime.excluded(appName)) {
return; // do not run this app.
}
var app = repos[appName];
if (app.run && !app.process) {
if(typeof app.run === "string") {
app.run = [app.run];
}
app.run.forEach(function(command) {
var args = command.split(' ');
cmd = args.splice(0,1)[0];
console.log("> "+command + " ("+appName+")");
process.chdir(appName);
var sp = spawn(cmd, args);
process.chdir("..");
app.process = sp;
sp.stdout.on('data', function (data) { process.stdout.write("[" + appName + "] " + data); });
sp.stderr.on('data', function (data) { process.stderr.write("[" + appName + "] " + data); });
sp.on('error', function () {
console.log("[" + appName + "] error: ", arguments);
sp.kill();
app.process = false;
});
sp.on('close', function (code) {
console.log('child process exited with code ' + code);
sp.kill()
app.process = false;
});
});
}
});
}
/**
* Mongo DB
*/
function spawnMongo() {
// bypass?
if(runtime.nomongo) {
return run();
}
// ensure we have a data directory for mongo
if(!fs.existsSync("./mongo")) { fs.mkdirSync("mongo"); }
// clean any lock files that might exist, and set up database
batchExec(["rm mongo/mongod.lock"], function() {
var mongo = spawn("mongod", ["--dbpath","./mongo/", "-v"]),
appName = "mongodb";
mongo.stdout.on('data', function (data) {
process.stdout.write("[" + appName + "] " + data);
// don't start the Webmaker Suite until ES and Mongo have started up.
if(data.toString().indexOf("waiting for connections")>-1) {
setTimeout(run, 1000);
}
});
mongo.stderr.on('data', function (data) { process.stderr.write("[" + appName + "] " + data); });
mongo.on('error', function () { console.log("[" + appName + "] ERROR", arguments); mongo.kill(); });
mongo.on('close', function (code) { console.log(appName + ' process exited with code ' + code); });
});
};
/**
* Elastic Search
*/
(function spawnElasticSearch() {
// bypass?
if(runtime.noes) {
return spawnMongo();
}
var appName = "elasticsearch",
es;
if (process.platform == "win32") {
es = spawn("elasticsearch.bat");
} else {
es = spawn("elasticsearch", ["-f", "-D", "es.config=/usr/local/opt/elasticsearch/config/elasticsearch.yml"]);
}
es.stdout.on('data', function (data) {
process.stdout.write("[" + appName + "] " + data);
// don't start Mongo until ES has started up.
if(data.toString().indexOf("started")>-1) {
setTimeout(function() {
if(runtime.cleares) {
// clear ES index to prevent pollution:
batchExec([
"curl -X DELETE http://127.0.0.1:9200/makes"
], function () {
console.log();
setTimeout(spawnMongo, 1000);
});
}
else { spawnMongo(); }
}, 1000);
}
});
es.stderr.on('data', function (data) { process.stderr.write("[" + appName + "] " + data); });
es.on('error', function () { console.log("[" + appName + "] ERROR", arguments); es.kill(); });
es.on('close', function (code) { console.log(appName + ' process exited with code ' + code); });
}());