forked from Juniper/contrail-web-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebServerStart.js
More file actions
515 lines (465 loc) · 16.1 KB
/
Copy pathwebServerStart.js
File metadata and controls
515 lines (465 loc) · 16.1 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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
/* Set corePath before loading any other module */
var corePath = process.cwd();
var config = require('./src/serverroot/common/config.utils').compareAndMergeDefaultConfig();
exports.corePath = corePath;
exports.config = config;
var express = require('express')
, path = require('path')
, fs = require("fs")
, http = require('http')
, https = require("https")
, underscore = require('underscore')
, logutils = require('./src/serverroot/utils/log.utils')
, cluster = require('cluster')
, axon = require('axon')
, producerSock = axon.socket('push')
, redisSub = require('./src/serverroot/web/core/redisSub')
, global = require('./src/serverroot/common/global')
, redis = require("redis")
, eventEmitter = require('events').EventEmitter
, async = require('async')
, authApi = require('./src/serverroot/common/auth.api')
, async = require('async')
, os = require('os')
, commonUtils = require('./src/serverroot/utils/common.utils')
, discClient = require('./src/serverroot/common/discoveryclient.api')
, assert = require('assert')
, jsonPath = require('JSONPath').eval
;
var pkgList = commonUtils.mergeAllPackageList(global.service.MAINSEREVR);
assert(pkgList);
var nodeWorkerCount = config.node_worker_count;
var server_port = (config.redis_server_port) ?
config.redis_server_port : global.DFLT_REDIS_SERVER_PORT;
var server_ip = (config.redis_server_ip) ?
config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
var RedisStore = require('connect-redis')(express);
var store;
var myIdentity = global.service.MAINSEREVR;
var discServEnable = ((null != config.discoveryService) &&
(null != config.discoveryService.enable)) ?
config.discoveryService.enable : true;
var sessEvent = new eventEmitter();
var options = {
key:fs.readFileSync('./keys/cs-key.pem'),
cert:fs.readFileSync('./keys/cs-cert.pem')
};
var insecureAccessFlag = false;
if (config.insecure_access && (config.insecure_access == true)) {
insecureAccessFlag = true;
}
var httpsApp = express(),
httpApp = express(),
httpPort = config.http_port,
httpsPort = config.https_port,
redisPort = (config.redis_server_port) ?
config.redis_server_port : global.DFLT_REDIS_SERVER_PORT,
redisIP = (config.redis_server_ip) ?
config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
function initializeAppConfig (appObj)
{
var app = appObj.app;
var port = appObj.port;
app.set('port', process.env.PORT || port);
app.use(express.cookieParser());
store = new RedisStore({host:redisIP, port:redisPort,
db:global.WEBUI_SESSION_REDIS_DB,
prefix:global.STR_REDIS_STORE_SESSION_ID_PREFIX,
eventEmitter:sessEvent});
app.use(express.session({ store:store,
secret:'enterasupbK3xg8qescJK.dUbdgfVq0D70UaLTMGTzO4yx5vVJral2zIhVersecretkey',
cookie:{
maxAge:global.MAX_AGE_SESSION_ID
}}));
app.use(express.compress());
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
registerStaticFiles(app);
// Catch-all error handler
app.use(function (err, req, res, next) {
logutils.logger.error(err.stack);
res.send(500, 'An unexpected error occurred!');
});
}
function loadStaticFiles (pkgNameObj, callback)
{
var app = pkgNameObj['app'];
/* First register core webroot directory */
var dirPath = path.join(pkgNameObj['pkgDir'], 'webroot');
fs.exists(dirPath, function(exists) {
if (exists) {
logutils.logger.debug("Registering Static Directory: " + dirPath);
app.use(express.static(dirPath, {maxAge: 3600*24*3*1000}));
callback(null);
}
});
}
function registerStaticFiles (app)
{
var pkgDir;
var staticFileDirLists = [];
var pkgNameListsLen = pkgList.length;
for (var i = 0; i < pkgNameListsLen; i++) {
pkgDir = commonUtils.getPkgPathByPkgName(pkgList[i]['pkgName']);
staticFileDirLists.push({'app': app, 'pkgDir': pkgDir});
}
async.mapSeries(staticFileDirLists, loadStaticFiles, function(err) {
});
}
function initAppConfig ()
{
if (false == insecureAccessFlag) {
httpsApp.configure(function () {
initializeAppConfig({app:httpsApp, port:httpsPort});
});
} else {
httpApp.configure(function () {
initializeAppConfig({app:httpApp, port:httpPort});
});
}
}
getSessionIdByRedisSessionStore = function(redisStoreSessId) {
var sessIdPrefix = global.STR_REDIS_STORE_SESSION_ID_PREFIX;
var pos = redisStoreSessId.indexOf(sessIdPrefix);
if (pos != -1) {
return redisStoreSessId.slice(pos + sessIdPrefix.length);
}
return redisStoreSessId;
}
/* Set max listeners to 0 */
//store.eventEmitter.setMaxListeners(0);
function registerSessionDeleteEvent ()
{
store.eventEmitter.on('sessionDeleted', function (sid) {
/* Handle session delete cases here */
console.log("Session got expired:", sid);
});
}
function loadAllFeatureURLs (myApp)
{
var pkgDir;
var pkgListLen = pkgList.length;
for (var i = 0; i < pkgListLen; i++) {
if (pkgList[i]['parseURL.xml']) {
pkgDir = commonUtils.getPkgPathByPkgName(pkgList[i]['pkgName']);
var urlListLen = pkgList[i]['parseURL.xml'].length;
for (var j = 0; j < urlListLen; j++) {
logutils.logger.debug("Registered URLs to APP :" +
pkgDir + pkgList[i]['parseURL.xml'][j]);
require(pkgDir +
pkgList[i]['parseURL.xml'][j]).registerURLsToApp(myApp);
}
}
}
return;
}
function registerReqToApp ()
{
var myApp = httpsApp;
if (true == insecureAccessFlag) {
myApp = httpApp;
}
loadAllFeatureURLs(myApp);
var handler = require('./src/serverroot/web/routes/handler')
handler.addAppReqToAllowedList(myApp.routes);
}
function bindProducerSocket ()
{
var hostName = config.jobServer.server_ip
, port = config.jobServer.server_port
;
var connectURL = 'tcp://' + hostName + ":" + port;
/* Master of this nodeJS Server should connect to the worker
Server of other nodeJS server
*/
producerSock.bind(connectURL);
console.log('nodeJS Server bound to port %s to Job Server ', port);
}
function sendRequestToJobServer (msg)
{
var timer = setInterval(function () {
console.log("SENDING to jobServer:", msg);
producerSock.send(msg.reqData);
clearTimeout(timer);
}, 1000);
}
function addProducerSockListener ()
{
producerSock.on('message', function (msg) {
console.log("Got A message, [%s]", msg);
});
}
function messageHandler (msg)
{
if (msg.cmd && msg.cmd == global.STR_SEND_TO_JOB_SERVER) {
sendRequestToJobServer(msg);
}
}
var workers = [];
var timeouts = [];
function addClusterEventListener ()
{
cluster.on('fork', function (worker) {
logutils.logger.info('Forking worker #', worker.id);
cluster.workers[worker.id].on('message', messageHandler);
timeouts[worker.id] = setTimeout(function () {
logutils.logger.error(['Worker taking too long to start.']);
}, 2000);
});
cluster.on('listening', function (worker, address) {
logutils.logger.info('Worker #' + worker.id + ' listening on port: '
+ address.port);
clearTimeout(timeouts[worker.id]);
});
cluster.on('online', function (worker) {
logutils.logger.info('Worker #' + worker.id + ' is online.');
});
cluster.on('exit', function (worker, code, signal) {
logutils.logger.error(['The worker #' + worker.id +
' has exited with exit code ' +
worker.process.exitCode]);
clearTimeout(timeouts[worker.id]);
// Don't try to restart the workers when disconnect or destroy has been called
if (worker.suicide !== true) {
logutils.logger.debug('Worker #' + worker.id + ' did not commit suicide.');
workers[worker.id] = cluster.fork();
}
});
cluster.on('disconnect', function (worker) {
logutils.logger.debug('The worker #' + worker.id + ' has disconnected.');
});
}
function registerFeatureLists ()
{
var pkgDir;
var pkgListLen = pkgList.length;
for (var i = 0; i < pkgListLen; i++) {
if (pkgList[i]['featureList.xml']) {
pkgDir = commonUtils.getPkgPathByPkgName(pkgList[i]['pkgName']);
var featureLen = pkgList[i]['featureList.xml'].length;
for (var j = 0; j < featureLen; j++) {
logutils.logger.debug("Registering feature Lists: " +
pkgDir +
pkgList[i]['featureList.xml'][j]);
require(pkgDir +
pkgList[i]['featureList.xml'][j]).registerFeature();
}
}
}
return;
}
function checkAndDeleteRedisRDB (callback)
{
var redisRdb = config.redis_dump_file;
if (null == redisRdb) {
redisRdb = '/var/lib/redis/dump-webui.rdb';
}
fs.stat(redisRdb, function(err, stats) {
if ((null == err) && (null != stats) && (0 === stats.size)) {
logutils.logger.debug('dump-webui.rdb size is 0, trying to ' +
'delete');
fs.unlink(redisRdb, function(err) {
if (null != err) {
logutils.logger.error('Delete of zero sized ' +
'dump-webui.rdb failed!!!');
} else {
logutils.logger.debug('zero sized dump-webui.rdb ' +
'deleted successfully');
}
callback();
});
} else {
callback();
}
});
}
function startWebCluster ()
{
if (cluster.isMaster) {
clusterMasterInit(function(err) {
logutils.logger.info("Starting Contrail UI in clustered mode.");
bindProducerSocket();
addProducerSockListener();
var i;
for (i = 0; i < nodeWorkerCount; i += 1) {
var worker = cluster.fork();
workers[i] = worker;
}
addClusterEventListener();
// Trick by Ian Young to make cluster and supervisor play nicely together.
// https://github.com/isaacs/node-supervisor/issues/40#issuecomment-4330946
if (process.env.NODE_HOT_RELOAD === 1) {
var signals = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
underscore.each(signals, function (signal) {
process.on(signal, function () {
underscore.each(cluster.workers, function (worker) {
worker.destroy();
});
});
});
}
});
doPreStartServer(false);
} else {
clusterWorkerInit(function(error) {
initAppConfig();
registerSessionDeleteEvent();
registerReqToApp();
/* Set maxListener to unlimited */
process.setMaxListeners(0);
registerFeatureLists();
redisSub.createRedisClientAndSubscribeMsg(function() {
discClient.sendWebServerReadyMessage();
});
/* All the config should be set before this line */
startServer();
});
}
}
function doPreStartServer (isRetry)
{
var rootPath = path.join(__dirname, 'webroot');
var defLogoFile = '/img/opencontrail-logo.png';
var srcLogoFile = rootPath + defLogoFile;
if ((null != config.logo_file) && (false == isRetry)) {
srcLogoFile = config.logo_file;
}
var destLogoFile = rootPath + '/img/sdn-logo.png';
var cmd = 'cp -f ' + srcLogoFile + " " + destLogoFile;
commonUtils.executeShellCommand(cmd, function(error, stdout, stderr) {
if (error) {
logutils.logger.error("Error occurred while copying logo file:" +
srcLogoFile + ' to ' + destLogoFile +
' ['+ error + ']');
if (false == isRetry) {
logutils.logger.error("Retrying Copying default logo");
doPreStartServer(true);
} else {
/* Default logo is also missing !!! */
}
}
});
}
function startWebUIService (webUIIP, callback)
{
httpsServer = https.createServer(options, httpsApp);
httpsServer.listen(httpsPort, webUIIP, function () {
logutils.logger.info("Contrail UI HTTPS server listening on host:" +
webUIIP + " Port:" + httpsPort);
});
httpServer = http.createServer(httpApp);
httpServer.listen(httpPort, webUIIP, function () {
logutils.logger.info("Contrail UI HTTP server listening on host:" +
webUIIP + " Port:" + httpPort);
});
httpServer.on('clientError', function(exception, socket) {
logutils.logger.error("httpServer Exception: on clientError:",
exception, socket);
});
httpsServer.on('clientError', function(exception, socket) {
logutils.logger.error("httpsServer Exception: on clientError:",
exception, socket);
});
if (false == insecureAccessFlag) {
httpApp.get("*", function (req, res) {
var reqHost = req.headers.host;
var index = reqHost.indexOf(':');
if (-1 == index) {
/* here, http port: 80, in case of http port 80, req.headers.host
* does not include port, so add it
*/
reqHost = reqHost + ':80';
}
var redirectURL = global.HTTPS_URL +
reqHost.replace(httpPort, httpsPort) + req.url;
res.redirect(redirectURL);
});
}
callback(null, null);
}
function checkIfIpAddrAny (ipList)
{
if (null == ipList) {
return true;
}
var ipCnt = ipList.length;
for (var i = 0; i < ipCnt; i++) {
if (ipList[i] == global.IPADDR_ANY) {
return true;
}
}
return false;
}
function checkIpInConfig (ip)
{
var ipList = config.webui_addresses;
if (null == ipList) {
return false;
}
var ipCnt = ipList.length;
for (var i = 0; i < ipCnt; i++) {
if (ipList[i] == ip) {
return true;
}
}
return false;
}
function startServer ()
{
var ipList = config.webui_addresses;
var uiIpList = [];
var k = 0;
if (null == ipList) {
/* IPADDR_ANY */
ipList = [global.IPADDR_ANY];
}
var ipCnt = ipList.length;
var ifaces = os.networkInterfaces();
if (checkIfIpAddrAny(ipList) || (null == ifaces)) {
startWebUIService(global.IPADDR_ANY, function(err, data) {
logutils.logger.debug("**** Contrail-WebUI Server ****");
});
return;
}
for (var dev in ifaces) {
ifaces[dev].forEach(function(details) {
if (checkIpInConfig(details['address'])) {
uiIpList[k++] = details['address'];
}
});
}
if (uiIpList.length == 0) {
uiIpList = [global.IPADDR_ANY];
}
async.mapSeries(uiIpList, startWebUIService, function(err, data) {
logutils.logger.debug("**** Contrail-WebUI Server ****");
});
}
/* Function: clusterMasterInit
Initialization call for Master
*/
function clusterMasterInit (callback)
{
var mergePath = path.join(__dirname, 'webroot');
commonUtils.mergeAllMenuXMLFiles(pkgList, mergePath, function() {
checkAndDeleteRedisRDB(function() {
callback();
});
});
}
/* Function: clusterWorkerInit
Initialization call for Worker
*/
function clusterWorkerInit (callback)
{
callback();
}
/* Start Main Server */
startWebCluster();
exports.myIdentity = myIdentity;
exports.discServEnable = discServEnable;
exports.pkgList = pkgList;