1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/server.js

259 lines
8.9 KiB
JavaScript
Raw Normal View History

2013-09-05 16:02:48 +02:00
/**
2015-02-03 23:02:26 +01:00
* Copyright 2013, 2015 IBM Corp.
2013-09-05 16:02:48 +02:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
2014-02-16 01:39:30 +01:00
var express = require('express');
var when = require('when');
var child_process = require('child_process');
var path = require("path");
var fs = require("fs");
2013-09-05 16:02:48 +02:00
var redNodes = require("./nodes");
2014-05-07 21:47:25 +02:00
var comms = require("./comms");
2014-09-22 15:33:26 +02:00
var storage = require("./storage");
2015-02-03 23:02:26 +01:00
var log = require("./log");
2013-10-13 22:01:46 +02:00
2013-09-05 16:02:48 +02:00
var app = null;
2014-02-16 01:39:30 +01:00
var nodeApp = null;
2013-09-05 16:02:48 +02:00
var server = null;
var settings = null;
2013-09-05 16:02:48 +02:00
2015-02-04 23:28:17 +01:00
var runtimeMetricInterval = null;
function init(_server,_settings) {
2013-09-05 16:02:48 +02:00
server = _server;
settings = _settings;
2014-09-22 15:33:26 +02:00
2014-05-07 21:47:25 +02:00
comms.init(_server,_settings);
2014-11-21 17:35:29 +01:00
2014-09-22 15:33:26 +02:00
nodeApp = express();
app = express();
2014-11-04 12:34:49 +01:00
}
function start() {
return storage.init(settings)
.then(function() { return settings.load(storage)})
.then(function() {
if (settings.httpAdminRoot !== false) {
require("./api").init(app,storage);
}
2015-02-04 23:28:17 +01:00
if (log.metric()) {
runtimeMetricInterval = setInterval(function() {
reportMetrics();
2015-03-21 18:42:06 +01:00
}, settings.runtimeMetricInterval||15000);
2015-02-04 23:28:17 +01:00
}
console.log("\n\nWelcome to Node-RED\n===================\n");
2014-11-04 12:34:49 +01:00
if (settings.version) {
log.info("Node-RED version: v"+settings.version);
}
log.info("Node.js version: "+process.version);
2015-02-03 23:02:26 +01:00
log.info("Loading palette nodes");
2015-03-13 22:26:50 +01:00
redNodes.init(settings,storage,app);
2014-11-04 12:34:49 +01:00
redNodes.load().then(function() {
var i;
var nodes = redNodes.getNodeList();
var nodeErrors = nodes.filter(function(n) { return n.err!=null;});
var nodeMissing = nodes.filter(function(n) { return n.module && n.enabled && !n.loaded && !n.err;});
if (nodeErrors.length > 0) {
2015-02-03 23:02:26 +01:00
log.warn("------------------------------------------");
2014-11-04 12:34:49 +01:00
if (settings.verbose) {
for (i=0;i<nodeErrors.length;i+=1) {
2015-02-03 23:02:26 +01:00
log.warn("["+nodeErrors[i].name+"] "+nodeErrors[i].err);
2014-09-22 15:33:26 +02:00
}
} else {
2015-02-03 23:02:26 +01:00
log.warn("Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
log.warn("Run with -v for details");
}
2015-02-03 23:02:26 +01:00
log.warn("------------------------------------------");
2014-09-22 15:33:26 +02:00
}
2014-11-04 12:34:49 +01:00
if (nodeMissing.length > 0) {
2015-02-03 23:02:26 +01:00
log.warn("Missing node modules:");
2014-11-04 12:34:49 +01:00
var missingModules = {};
for (i=0;i<nodeMissing.length;i++) {
var missing = nodeMissing[i];
missingModules[missing.module] = (missingModules[missing.module]||[]).concat(missing.types);
}
var promises = [];
for (i in missingModules) {
if (missingModules.hasOwnProperty(i)) {
2015-02-03 23:02:26 +01:00
log.warn(" - "+i+": "+missingModules[i].join(", "));
if (settings.autoInstallModules && i != "node-red") {
2015-03-21 18:42:06 +01:00
serverAPI.installModule(i).otherwise(function(err) {
2014-11-04 12:34:49 +01:00
// Error already reported. Need the otherwise handler
// to stop the error propagating any further
});
2014-09-22 15:33:26 +02:00
}
}
}
2014-11-04 12:34:49 +01:00
if (!settings.autoInstallModules) {
2015-02-03 23:02:26 +01:00
log.info("Removing modules from config");
redNodes.cleanModuleList();
2014-11-04 12:34:49 +01:00
}
}
redNodes.loadFlows();
}).otherwise(function(err) {
console.log(err);
});
comms.start();
});
}
2014-11-04 12:34:49 +01:00
function reportAddedModules(info) {
comms.publish("node/added",info,false);
if (info.length > 0) {
2015-02-03 23:02:26 +01:00
log.info("Added node types:");
for (var i=0;i<info.length;i++) {
for (var j=0;j<info[i].types.length;j++) {
2015-02-03 23:02:26 +01:00
log.info(" - "+
(info[i].module?info[i].module+":":"")+
info[i].types[j]+
(info[i].err?" : "+info[i].err:"")
);
}
}
}
return info;
}
function reportRemovedModules(removedNodes) {
comms.publish("node/removed",removedNodes,false);
2015-02-03 23:02:26 +01:00
log.info("Removed node types:");
for (var j=0;j<removedNodes.length;j++) {
for (var i=0;i<removedNodes[j].types.length;i++) {
log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
}
}
return removedNodes;
}
2014-11-21 17:35:29 +01:00
function installModule(module) {
//TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) {
reject(new Error("Invalid module name"));
return;
}
2015-02-03 23:02:26 +01:00
log.info("Installing module: "+module);
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var child = child_process.exec('npm install --production '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
if (lookFor404.test(stdout)) {
log.warn("Installation of module "+module+" failed: module not found");
var e = new Error();
e.code = 404;
reject(e);
} else {
log.warn("Installation of module "+module+" failed:");
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error("Install failed"));
}
} else {
log.info("Installed module: "+module);
resolve(redNodes.addModule(module).then(reportAddedModules));
}
}
);
});
}
function uninstallModule(module) {
return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) {
reject(new Error("Invalid module name"));
return;
}
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module);
if (!fs.existsSync(moduleDir)) {
return reject(new Error("Unabled to uninstall "+module+"."));
}
var list = redNodes.removeModule(module);
2015-02-03 23:02:26 +01:00
log.info("Removing module: "+module);
var child = child_process.exec('npm remove '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
log.warn("Removal of module "+module+" failed:");
log.warn("------------------------------------------");
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error("Removal failed"));
} else {
log.info("Removed module: "+module);
reportRemovedModules(list);
resolve(list);
}
}
);
});
}
2013-10-13 22:01:46 +02:00
2015-02-04 23:28:17 +01:00
function reportMetrics() {
var memUsage = process.memoryUsage();
2015-03-21 18:42:06 +01:00
log.log({
level: log.METRIC,
event: "runtime.memory.rss",
value: memUsage.rss
});
log.log({
level: log.METRIC,
event: "runtime.memory.heapTotal",
value: memUsage.heapTotal
});
log.log({
level: log.METRIC,
event: "runtime.memory.heapUsed",
value: memUsage.heapUsed
});
2015-02-04 23:28:17 +01:00
}
function stop() {
2015-02-04 23:28:17 +01:00
if (runtimeMetricInterval) {
clearInterval(runtimeMetricInterval);
runtimeMetricInterval = null;
}
redNodes.stopFlows();
comms.stop();
}
2015-03-21 18:42:06 +01:00
var serverAPI = module.exports = {
init: init,
start: start,
2014-11-04 12:34:49 +01:00
stop: stop,
2014-11-21 17:35:29 +01:00
2014-11-04 12:34:49 +01:00
reportAddedModules: reportAddedModules,
reportRemovedModules: reportRemovedModules,
installModule: installModule,
uninstallModule: uninstallModule,
2013-09-05 16:02:48 +02:00
get app() { return app },
get nodeApp() { return nodeApp },
get server() { return server }
}