This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
forked from tobilg/mesos-framework-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
325 lines (288 loc) · 11.8 KB
/
init.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
/*jslint
this: true,
es6: true,
node: true
for
*/
"use strict";
// Internal modules
const fs = require("fs");
const path = require('path');
// NPM modules
var express = require("express");
// Project modules
var appConfig = require("./lib/config");
var RestartHelper = require("./lib/restartHelper");
var linkHelper = require("./lib/linkHelper");
var baseApi = require("./lib/baseApi");
var initAuth = require("./lib/auth");
var ejs = require("./routes/ejs");
var populateTasks = require("./lib/populateTasks").populateTasks;
var ConfigHelper = require("./lib/configHelper").ConfigHelper;
// Modules loaded array
var moduleSetups = [];
// Check if we got the necessary info from the environment, otherwise fail directly!
require("require-environment-variables")([
// Scheduler settings
"HOST",
"PORT0",
"MESOS_SANDBOX",
"FRAMEWORK_NAME"
]);
// Create the Express object
var app = express();
app.set('view engine', 'ejs');
// Set application properties
app.set("port", process.env.PORT0 || appConfig.application.port);
app.set("host", process.env.HOST || appConfig.application.host);
app.set("env", process.env.NODE_ENV || appConfig.application.environment);
app.set("logLevel", process.env.LOG_LEVEL || appConfig.application.logLevel);
// Initialize optional user authorization
initAuth(app);
// Define static files path
app.use(express.static("public"));
app.use("/bower_components", express.static("bower_components"));
var helpers = require("./lib/helpers");
var Scheduler = require("./lib/scheduler");
// The framework's overall configuration
var frameworkConfiguration = {
"masterUrl": process.env.MASTER_IP || "leader.mesos",
"port": 5050,
"frameworkName": process.env.FRAMEWORK_NAME,
"appName": process.env.MARATHON_APP_ID,
"marathonResources": {
"cpu": process.env.MARATHON_APP_RESOURCE_CPUS,
"mem": process.env.MARATHON_APP_RESOURCE_MEM
},
"logging": {
"path": process.env.MESOS_SANDBOX + "/logs/",
"fileName": process.env.FRAMEWORK_NAME + ".log",
"level": app.get("logLevel")
},
"killUnknownTasks": true,
"exponentialBackoffMinimum": Math.round(Math.random() * 1000 + 500),
"useZk": true,
"staticPorts": true,
"serialNumberedTasks": false,
"restartStates": ["TASK_FAILED", "TASK_LOST", "TASK_ERROR", "TASK_FINISHED", "TASK_KILLED"], // A sealed vault is killed and does not finish normally
"authExemptPaths": [], // Paths that are authentication aware that do not need to be checked for authentication.
"moduleList": []
};
fs.mkdirSync(path.join(process.env.MESOS_SANDBOX, "logs"));
function requireModules(scheduler) {
// Importing pluggable modules
var moduleFiles = fs.readdirSync(process.env.MESOS_SANDBOX);
if (moduleFiles) {
// Adding link to the project
fs.symlinkSync(__dirname, path.join(process.env.MESOS_SANDBOX, "framework-core"));
var index;
var currentModule;
for (index = 0; index < moduleFiles.length; index += 1) {
currentModule = moduleFiles[index];
if (currentModule.match(/-module$/) && fs.existsSync(path.join(process.env.MESOS_SANDBOX, currentModule, "index.js"))) {
scheduler.logger.info("Loading module " + currentModule);
// Adding node_modules link
fs.symlinkSync(path.join(__dirname, "node_modules"), path.join(process.env.MESOS_SANDBOX, currentModule, "node_modules"));
moduleSetups.push(require(path.join(process.env.MESOS_SANDBOX, currentModule)));
}
}
}
}
var configHelper;
configHelper = new ConfigHelper(app, frameworkConfiguration, function (error, config) {
var propertyType;
var tasksDefined = false;
var linksDefined = false;
var envVars = {};
this.logger.debug("Config from server:" + JSON.stringify(config));
Object.getOwnPropertyNames(config).forEach(function (property) {
propertyType = typeof config[property];
if (property.toUpperCase() === property && (propertyType === "number" || propertyType === "string")) {
process.env[property] = config[property];
envVars[property] = config[property];
} else if (property.toUpperCase() === property && propertyType === "object") {
envVars[property] = JSON.stringify(config[property]);
}
});
if (process.env.ZK_PATH) {
frameworkConfiguration.zkPrefix = process.env.ZK_PATH;
}
if (config.configVersion) {
if (config.TASK_DEF_NUM) {
frameworkConfiguration.tasks = populateTasks(config);
frameworkConfiguration.zkConfigOverwrite = true;
tasksDefined = true;
}
if (config.FRAMEWORK_LINKS) {
frameworkConfiguration.frameworkLinks = linkHelper.populateLinkConfig(config);
linksDefined = true;
}
frameworkConfiguration.configVersion = config.configVersion;
} else {
if (process.env.TASK_DEF_NUM) {
frameworkConfiguration.tasks = populateTasks();
tasksDefined = true;
}
if (process.env.FRAMEWORK_LINKS) {
frameworkConfiguration.frameworkLinks = linkHelper.populateLinkConfig();
linksDefined = true;
}
}
frameworkConfiguration.healthCheckTimeout = process.env.HEALTH_TIMEOUT || appConfig.application.healthTimeout;
if (process.env.AUTH_COOKIE_ENCRYPTION_KEY) {
frameworkConfiguration.userAuthSupport = true;
} else if (helpers.checkBooleanString(process.env.AUTH_DISABLED, false)) {
frameworkConfiguration.userAuthSupport = false;
} else {
console.log("ERROR: Authentication not configured and not disabled");
setTimeout(function () {
process.exit(1);
}, 120000);
}
if (Object.getOwnPropertyNames(envVars).length > 0) {
frameworkConfiguration.env = envVars;
}
var scheduler;
var envSet = false;
function optionsHandler(options, callback) {
scheduler.logger.debug("Starting optionsHandler with: " + JSON.stringify(options));
if (options && options.env) {
Object.getOwnPropertyNames(options.env).forEach(function (property) {
propertyType = typeof options.env[property];
if ((!config.hasOwnProperty(property)) // Don't override existing configuration
&& property.toUpperCase() === property && (propertyType === "number" || propertyType === "string")) {
scheduler.logger.debug("Setting env." + property + ": " + options.env[property]);
process.env[property] = options.env[property];
} else {
scheduler.logger.debug("NOT setting env." + property + ": " + options.env[property] + " type: " + propertyType);
}
});
}
envSet = true;
if (!tasksDefined) {
try {
options.tasks = populateTasks();
tasksDefined = true;
} catch (err) {
scheduler.logger.error("Error defining tasks: " + err.toString());
}
}
if (!linksDefined) {
options.frameworkLinks = linkHelper.populateLinkConfig();
}
if (tasksDefined) {
requireModules(scheduler);
callback();
} else {
scheduler.logger.info("Tasks were not defined, exiting.");
setTimeout(() => {
process.exit(1);
}, 120000);
}
scheduler.logger.debug("Ended optionsHandler");
}
frameworkConfiguration.optionsHandler = optionsHandler;
if (config.LOG_LEVEL) {
app.set("logLevel", config.LOG_LEVEL);
frameworkConfiguration.logging.level = config.LOG_LEVEL;
}
scheduler = new Scheduler(frameworkConfiguration);
var restartHelper;
// Start framework scheduler
scheduler.on("ready", function () {
// To set loggers that initialized already
if (config.LOG_LEVEL) {
app.set("logLevel", config.LOG_LEVEL);
scheduler.logModules.forEach(function (module) {
module.logger.transports.dailyRotateFile.level = config.LOG_LEVEL;
module.logger.transports.console.level = config.LOG_LEVEL;
});
}
scheduler.logger.info("Ready");
if (!envSet) {
requireModules(scheduler);
}
if (!tasksDefined) {
scheduler.logger.info("Tasks were not defined, exiting.");
setTimeout(function () {
process.exit(1);
}, 120000);
} else {
scheduler.subscribe();
}
});
// Capture "error" events
scheduler.on("error", function (error) {
scheduler.logger.error("ERROR: " + JSON.stringify(error));
scheduler.logger.error(error.stack);
});
// Wait for the framework scheduler to be subscribed to the leading Mesos Master
scheduler.once("subscribed", function () {
linkHelper.linkCheckSetup(scheduler, frameworkConfiguration);
scheduler.sync();
scheduler.logger.debug("Subscribe sync issued");
if (process.env.SYNC_INTERVAL) {
setInterval(function () {
scheduler.sync();
}, process.env.SYNC_INTERVAL * 1000);
}
scheduler.logger.debug("Sync timer set up");
restartHelper = new RestartHelper(scheduler, {
"timeout": 300000,
"logging": {
"path": process.env.MESOS_SANDBOX + "/logs/",
"fileName": process.env.FRAMEWORK_NAME + ".log",
"level": app.get("logLevel")
}
});
scheduler.logger.debug("Restart helper set up");
// Instantiate API (pass the scheduler and framework configuration)
var api = require("./routes/api")(scheduler, frameworkConfiguration, restartHelper);
scheduler.logger.debug("API set up");
// Middleware for health check API - must stay before the modules setup
app.use(function (req, res, next) {
req.scheduler = scheduler;
req.frameworkConfiguration = frameworkConfiguration;
next();
});
scheduler.logger.debug("Middleware set up");
// Setup extended modules
if (moduleSetups) {
var index;
try {
for (index = 0; index < moduleSetups.length; index += 1) {
moduleSetups[index](scheduler, frameworkConfiguration, api, app, restartHelper);
}
} catch (err) {
scheduler.logger.error("Modules could not be loaded: " + err.toString() + " exiting.");
setTimeout(function () {
process.exit(1);
}, 120000);
return;
}
}
scheduler.logger.debug("Modules set up");
require("./routes/configApi")(api);
var bodyParser = require('body-parser');
app.use(bodyParser.json());
scheduler.logger.debug("Config API set up");
// Create routes
app.use("/api/" + appConfig.application.apiVersion, api);
scheduler.logger.debug("API set up");
// /health endpoint for Marathon health checks
app.get("/health", baseApi.healthCheck);
scheduler.logger.debug("Health check set up");
ejs.setup(app);
scheduler.logger.debug("EJS set up");
app.get("/moduleList", baseApi.moduleList);
});
// Setting up the express server
var server;
server = app.listen(app.get("port"), app.get("host"), function () {
scheduler.logger.info("Express server listening on port " + server.address().port + " on " + server.address().address);
});
process.on("uncaughtException", function (error) {
scheduler.logger.error("Caught exception: ");
scheduler.logger.error(error.stack);
});
});