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');
|
2014-03-06 23:32:23 +01:00
|
|
|
var when = require('when');
|
2014-11-04 18:05:29 +01:00
|
|
|
var child_process = require('child_process');
|
2015-02-25 15:23:59 +01:00
|
|
|
var path = require("path");
|
|
|
|
var fs = require("fs");
|
2014-03-06 23:32:23 +01:00
|
|
|
|
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");
|
2015-04-15 21:59:39 +02:00
|
|
|
var i18n = require("./i18n");
|
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;
|
2013-11-12 18:13:06 +01:00
|
|
|
var settings = null;
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2015-02-04 23:28:17 +01:00
|
|
|
var runtimeMetricInterval = null;
|
|
|
|
|
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
function init(_server,_settings) {
|
2013-09-05 16:02:48 +02:00
|
|
|
server = _server;
|
2013-11-12 18:13:06 +01:00
|
|
|
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() {
|
2015-04-15 21:59:39 +02:00
|
|
|
return i18n.init()
|
|
|
|
.then(function() { return storage.init(settings)})
|
2015-03-22 21:55:38 +01:00
|
|
|
.then(function() { return settings.load(storage)})
|
2015-03-14 00:37:59 +01:00
|
|
|
.then(function() {
|
|
|
|
if (settings.httpAdminRoot !== false) {
|
|
|
|
require("./api").init(app,storage);
|
|
|
|
}
|
2015-07-02 14:25:15 +02:00
|
|
|
|
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
|
|
|
}
|
2015-05-08 15:21:01 +02:00
|
|
|
console.log("\n\n"+log._("runtime.welcome")+"\n===================\n");
|
2014-11-04 12:34:49 +01:00
|
|
|
if (settings.version) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("runtime.version",{component:"Node-RED",version:"v"+settings.version}));
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("runtime.version",{component:"Node.js ",version:process.version}));
|
|
|
|
log.info(log._("server.loading"));
|
2015-03-13 22:26:50 +01:00
|
|
|
redNodes.init(settings,storage,app);
|
2015-04-08 21:17:24 +02:00
|
|
|
return redNodes.load().then(function() {
|
2015-07-02 14:25:15 +02:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
var i;
|
2015-03-30 22:49:20 +02:00
|
|
|
var nodeErrors = redNodes.getNodeList(function(n) { return n.err!=null;});
|
|
|
|
var nodeMissing = redNodes.getNodeList(function(n) { return n.module && n.enabled && !n.loaded && !n.err;});
|
2014-11-04 12:34:49 +01:00
|
|
|
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
|
|
|
}
|
2014-08-28 01:35:07 +02:00
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.warn(log._("server.errors",{count:nodeErrors.length}));
|
|
|
|
log.warn(log._("server.errors-help"));
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
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-05-08 15:21:01 +02:00
|
|
|
log.warn(log._("server.missing-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(", "));
|
2015-02-04 11:27:02 +01:00
|
|
|
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-08-28 01:35:07 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
if (!settings.autoInstallModules) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("server.removing-modules"));
|
2014-11-26 17:41:31 +01:00
|
|
|
redNodes.cleanModuleList();
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("runtime.paths.settings",{path:settings.settingsFile}));
|
2015-10-11 21:37:11 +02:00
|
|
|
redNodes.loadFlows().then(redNodes.startFlows);
|
2015-04-08 21:17:24 +02:00
|
|
|
comms.start();
|
2014-11-04 12:34:49 +01:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
});
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
|
2014-08-28 01:35:07 +02:00
|
|
|
function reportAddedModules(info) {
|
2015-03-30 22:49:20 +02:00
|
|
|
comms.publish("node/added",info.nodes,false);
|
|
|
|
if (info.nodes.length > 0) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("server.added-types"));
|
2015-03-30 22:49:20 +02:00
|
|
|
for (var i=0;i<info.nodes.length;i++) {
|
|
|
|
for (var j=0;j<info.nodes[i].types.length;j++) {
|
2015-02-03 23:02:26 +01:00
|
|
|
log.info(" - "+
|
2015-03-30 22:49:20 +02:00
|
|
|
(info.nodes[i].module?info.nodes[i].module+":":"")+
|
|
|
|
info.nodes[i].types[j]+
|
|
|
|
(info.nodes[i].err?" : "+info.nodes[i].err:"")
|
2015-02-25 15:23:59 +01:00
|
|
|
);
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reportRemovedModules(removedNodes) {
|
|
|
|
comms.publish("node/removed",removedNodes,false);
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("server.removed-types"));
|
2014-08-28 01:35:07 +02:00
|
|
|
for (var j=0;j<removedNodes.length;j++) {
|
|
|
|
for (var i=0;i<removedNodes[j].types.length;i++) {
|
2015-02-25 15:23:59 +01:00
|
|
|
log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return removedNodes;
|
|
|
|
}
|
|
|
|
|
2015-05-27 15:11:11 +02:00
|
|
|
function installNode(file) {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
resolve(redNodes.addFile(file).then(function(info) {
|
|
|
|
var module = redNodes.getModuleInfo(info.module);
|
|
|
|
module.nodes = module.nodes.filter(function(d) {
|
|
|
|
return d.id==info.id;
|
|
|
|
});
|
|
|
|
return reportAddedModules(module);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-21 17:35:29 +01:00
|
|
|
function installModule(module) {
|
2014-08-28 01:35:07 +02:00
|
|
|
//TODO: ensure module is 'safe'
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
if (/[\s;]/.test(module)) {
|
2015-05-08 15:21:01 +02:00
|
|
|
reject(new Error(log._("server.install.invalid")));
|
2014-08-28 01:35:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-05-27 15:11:11 +02:00
|
|
|
if (redNodes.getModuleInfo(module)) {
|
2015-04-15 21:59:39 +02:00
|
|
|
// TODO: nls
|
2015-05-27 15:11:11 +02:00
|
|
|
var err = new Error("Module already loaded");
|
|
|
|
err.code = "module_already_loaded";
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-02 14:25:15 +02:00
|
|
|
log.info(i18n._("server.install.installing",{name: module}));
|
|
|
|
|
2015-02-25 15:23:59 +01:00
|
|
|
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)) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.warn(log._("server.install.install-failed-not-found",{name:module}));
|
2015-02-25 15:23:59 +01:00
|
|
|
var e = new Error();
|
|
|
|
e.code = 404;
|
|
|
|
reject(e);
|
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.warn(log._("server.install.install-failed-long",{name:module}));
|
2015-02-25 15:23:59 +01:00
|
|
|
log.warn("------------------------------------------");
|
|
|
|
log.warn(err.toString());
|
|
|
|
log.warn("------------------------------------------");
|
2015-05-08 15:21:01 +02:00
|
|
|
reject(new Error(log._("server.install.install-failed")));
|
2015-02-25 15:23:59 +01:00
|
|
|
}
|
2014-08-28 01:35:07 +02:00
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("server.install.installed",{name:module}));
|
2015-02-25 15:23:59 +01:00
|
|
|
resolve(redNodes.addModule(module).then(reportAddedModules));
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-25 15:23:59 +01:00
|
|
|
);
|
2014-08-28 01:35:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function uninstallModule(module) {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
if (/[\s;]/.test(module)) {
|
2015-05-08 15:21:01 +02:00
|
|
|
reject(new Error(log._("server.install.invalid")));
|
2014-08-28 01:35:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-02-25 15:23:59 +01:00
|
|
|
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
|
|
|
|
var moduleDir = path.join(installDir,"node_modules",module);
|
|
|
|
if (!fs.existsSync(moduleDir)) {
|
2015-05-08 15:21:01 +02:00
|
|
|
return reject(new Error(log._("server.install.uninstall-failed",{name:module})));
|
2015-02-25 15:23:59 +01:00
|
|
|
}
|
2015-03-06 11:17:00 +01:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
var list = redNodes.removeModule(module);
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("server.install.uninstalling",{name:module}));
|
2015-03-06 11:17:00 +01:00
|
|
|
var child = child_process.exec('npm remove '+module,
|
2015-02-25 15:23:59 +01:00
|
|
|
{
|
|
|
|
cwd: installDir
|
|
|
|
},
|
|
|
|
function(err, stdin, stdout) {
|
|
|
|
if (err) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.warn(log._("server.install.uninstall-failed-long",{name:module}));
|
2015-02-25 15:23:59 +01:00
|
|
|
log.warn("------------------------------------------");
|
|
|
|
log.warn(err.toString());
|
|
|
|
log.warn("------------------------------------------");
|
2015-05-08 15:21:01 +02:00
|
|
|
reject(new Error(log._("server.install.uninstall-failed",{name:module})));
|
2015-02-25 15:23:59 +01:00
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("server.install.uninstalled",{name:module}));
|
2015-02-25 15:23:59 +01:00
|
|
|
reportRemovedModules(list);
|
|
|
|
resolve(list);
|
|
|
|
}
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
2015-02-25 15:23:59 +01:00
|
|
|
);
|
2014-08-28 01:35:07 +02:00
|
|
|
});
|
2013-09-20 18:15:45 +02:00
|
|
|
}
|
2013-10-13 22:01:46 +02:00
|
|
|
|
2015-02-04 23:28:17 +01:00
|
|
|
function reportMetrics() {
|
|
|
|
var memUsage = process.memoryUsage();
|
2015-03-06 11:17:00 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-13 20:14:39 +02:00
|
|
|
function stop() {
|
2015-02-04 23:28:17 +01:00
|
|
|
if (runtimeMetricInterval) {
|
|
|
|
clearInterval(runtimeMetricInterval);
|
|
|
|
runtimeMetricInterval = null;
|
|
|
|
}
|
2013-10-13 20:14:39 +02:00
|
|
|
redNodes.stopFlows();
|
2014-08-01 22:55:05 +02:00
|
|
|
comms.stop();
|
2013-10-13 20:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-03-21 18:42:06 +01:00
|
|
|
var serverAPI = module.exports = {
|
2014-11-04 18:05:29 +01:00
|
|
|
init: init,
|
2013-10-13 20:14:39 +02:00
|
|
|
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,
|
2015-03-06 11:17:00 +01:00
|
|
|
uninstallModule: uninstallModule,
|
2015-05-27 15:11:11 +02:00
|
|
|
installNode: installNode,
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2015-03-06 11:17:00 +01:00
|
|
|
get app() { return app },
|
|
|
|
get nodeApp() { return nodeApp },
|
|
|
|
get server() { return server }
|
|
|
|
}
|