forked from LockerProject/Locker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockerd.js
157 lines (135 loc) · 4.85 KB
/
lockerd.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
/*
*
* Copyright (C) 2011, The Locker Project
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/
/* random notes:
on startup scan all folders
Apps Collections Connectors - generate lists of "available"
Me/* - generate lists of "existing"
when asked, run any existing and return localhost:port
if first time
check dependencies
create Me/ dir
create me.json settings
pick a port
*/
require.paths.push(__dirname + "/Common/node");
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
// This lconfig stuff has to come before and other locker modules are loaded!!
var lconfig = require('lconfig');
lconfig.load((process.argv[2] == '--config'? process.argv[3] : 'config.json'));
var logger = require("logger");
//var crypto = require('crypto');
var lconsole = require("lconsole");
var lscheduler = require("lscheduler");
var serviceManager = require("lservicemanager");
var dashboard = require(__dirname + "/Ops/dashboard.js");
var mongodb = require('mongodb');
var webservice = require(__dirname + "/Ops/webservice.js");
var lcrypto = require("lcrypto");
var thservice = require(__dirname + "/Ops/thservice.js");
if(lconfig.lockerHost != "localhost" && lconfig.lockerHost != "127.0.0.1") {
console.warn('if I\'m running on a public IP I needs to have password protection,' + // uniquely self (de?)referential? lolz!
'which if so inclined can be hacked into lockerd.js and added since' +
' it\'s apparently still not implemented :)\n\n');
}
var shuttingDown_ = false;
var mongoProcess;
path.exists(lconfig.me + '/' + lconfig.mongo.dataDir, function(exists) {
if(!exists) {
try {
//ensure there is a Me dir
fs.mkdirSync(lconfig.me, 0755);
} catch(err) {
if(err.code !== 'EEXIST')
console.error('err', err);
}
fs.mkdirSync(lconfig.me + '/' + lconfig.mongo.dataDir, 0755);
}
mongoProcess = spawn('mongod', ['--dbpath', lconfig.lockerDir + '/' + lconfig.me + '/' + lconfig.mongo.dataDir,
'--port', lconfig.mongo.port]);
mongoProcess.stderr.on('data', function(data) {
console.error('mongod err: ' + data);
});
var mongoOutput = "";
var mongodExit = function(errorCode) {
if(shuttingDown_) return;
if(errorCode !== 0) {
var db = new mongodb.Db('locker', new mongodb.Server('127.0.0.1', lconfig.mongo.port, {}), {});
db.open(function(error, client) {
if(error) {
console.error('mongod did not start successfully and was not already running ('+errorCode+'), here was the stdout: '+mongoOutput);
shutdown(1);
} else {
console.error('found a previously running mongodb running on port '+lconfig.mongo.port+' so we will use that');
db.close();
checkKeys();
}
});
}
};
mongoProcess.on('exit', mongodExit);
// watch for mongo startup
var callback = function(data) {
mongoOutput += data;
if(mongoOutput.match(/ waiting for connections on port/g)) {
mongoProcess.stdout.removeListener('data', callback);
checkKeys();
}
};
mongoProcess.stdout.on('data', callback);
});
function checkKeys() {
lcrypto.generateSymKey(function(hasKey) {
if (!hasKey) {
shutdown(1);
return;
}
lcrypto.generatePKKeys(function(hasKeys) {
if (!hasKeys) {
shutdown(1);
return;
}
finishStartup();
});
});
}
function finishStartup() {
// look for available things
lconfig.scannedDirs.forEach(function(dirToScan) {
console.log(dirToScan);
var installable = true;
if (dirToScan === "Collections") installable = false;
serviceManager.scanDirectory(dirToScan, installable);
});
// look for existing things
serviceManager.findInstalled();
thservice.start();
webservice.startService(lconfig.lockerPort);
lscheduler.masterScheduler.loadAndStart();
var lockerPortNext = "1"+lconfig.lockerPort;
dashboard.start(lockerPortNext);
lockerPortNext++;
console.log('locker is running, use your browser and visit ' + lconfig.lockerBase);
}
function shutdown(returnCode) {
process.stdout.write("\n");
shuttingDown_ = true;
dashboard.instance.kill(dashboard.pid, "SIGINT");
serviceManager.shutdown(function() {
mongoProcess.kill();
console.log("Shutdown complete.");
process.exit(returnCode);
});
}
process.on("SIGINT", function() {
shutdown(0);
});
// Export some things so this can be used by other processes, mainly for the test runner
exports.shutdown = shutdown;