Build script

A build process that generates a package with single executable for each
component.
This commit is contained in:
Zaid Abdulla committed 2019-01-04 01:13:47 -08:00
1 parent 819e8a52a3
commit 0b59811924
23 files changed
+734 -688

No files matched your search

+33 -76
View File
@@ -18,29 +18,11 @@
*/
"use strict";
const service = process.argv[2];
if (service == null) throw new Error("Missing argument cwmp, fs, or nbi");
const cluster = require("cluster");
const path = require("path");
const fs = require("fs");
const http = require("http");
const https = require("https");
const logger = require("./logger");
logger.init(service, require("../package.json").version);
const config = require("./config");
const db = require("./db");
const cache = require("./cache");
const extensions = require("./extensions");
const networkInterface = config.get(`${service.toUpperCase()}_INTERFACE`);
const port = config.get(`${service.toUpperCase()}_PORT`);
const useHttps = config.get(`${service.toUpperCase()}_SSL`);
const { listener, onConnection } = require(`./${service}`);
let server;
let server, listener;
function closeServer(timeout, callback) {
if (!server) return void callback();
@@ -66,67 +48,42 @@ function closeServer(timeout, callback) {
});
}
function exit() {
setTimeout(() => {
extensions.killAll(() => {
process.exit(1);
});
}, 30000).unref();
function start(port, networkInterface, ssl, _listener, onConnection) {
listener = _listener;
closeServer(20000, () => {
db.disconnect();
cache.disconnect();
extensions.killAll();
if (cluster.worker) cluster.worker.disconnect();
});
}
if (ssl) {
const options = {
key: fs.readFileSync(ssl.key),
cert: fs.readFileSync(ssl.cert)
};
process.on("uncaughtException", err => {
logger.error({
message: "Uncaught exception",
exception: err,
pid: process.pid
});
exit();
});
if (useHttps) {
const httpsKey = path.resolve(config.get("CONFIG_DIR"), `${service}.key`);
const httpsCert = path.resolve(config.get("CONFIG_DIR"), `${service}.crt`);
const httpsCa = path.resolve(
config.get("CONFIG_DIR"),
`${service}.ca-bundle`
);
const options = {
key: fs.readFileSync(httpsKey),
cert: fs.readFileSync(httpsCert)
};
try {
// Use intermediate certificates if available
options.ca = fs
.readFileSync(httpsCa)
.toString()
.match(/-+BEGIN CERTIFICATE-+[0-9a-zA-Z+\-/=\s]+?-+END CERTIFICATE-+/g);
} catch (error) {
// No intermediate certificate
try {
// Use intermediate certificates if available
options.ca = fs
.readFileSync(ssl.ca)
.toString()
.match(/-+BEGIN CERTIFICATE-+[0-9a-zA-Z+\-/=\s]+?-+END CERTIFICATE-+/g);
} catch (error) {
// No intermediate certificate
}
server = https.createServer(options, listener);
if (onConnection != null) server.on("secureConnection", onConnection);
} else {
server = http.createServer(listener);
if (onConnection != null) server.on("connection", onConnection);
}
server = https.createServer(options, listener);
if (onConnection != null) server.on("secureConnection", onConnection);
} else {
server = http.createServer(listener);
if (onConnection != null) server.on("connection", onConnection);
server.listen(port, networkInterface);
}
db.connect(err => {
if (err) throw err;
cache.connect(err => {
if (err) throw err;
server.listen(port, networkInterface);
function stop() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error("Could not close server in a timely manner"));
}, 30000).unref();
closeServer(20000, resolve);
});
});
}
process.on("SIGINT", exit);
process.on("SIGTERM", exit);
exports.start = start;
exports.stop = stop;