2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2014-11-04 12:34:49 +01:00
|
|
|
* Copyright 2013, 2014 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');
|
2013-09-05 16:02:48 +02:00
|
|
|
var util = require('util');
|
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');
|
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");
|
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
|
|
|
|
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-21 17:35:29 +01:00
|
|
|
|
2014-09-22 15:33:26 +02:00
|
|
|
if (settings.httpAdminRoot !== false) {
|
2014-11-04 12:34:49 +01:00
|
|
|
require("./api").init(app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function start() {
|
|
|
|
var defer = when.defer();
|
2014-11-21 17:35:29 +01:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
storage.init(settings).then(function() {
|
|
|
|
settings.load(storage).then(function() {
|
|
|
|
console.log("\nWelcome to Node-RED\n===================\n");
|
|
|
|
if (settings.version) {
|
|
|
|
util.log("[red] Version: "+settings.version);
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
util.log("[red] Loading palette nodes");
|
|
|
|
redNodes.init(settings,storage);
|
|
|
|
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) {
|
|
|
|
util.log("------------------------------------------");
|
|
|
|
if (settings.verbose) {
|
|
|
|
for (i=0;i<nodeErrors.length;i+=1) {
|
|
|
|
util.log("["+nodeErrors[i].name+"] "+nodeErrors[i].err);
|
2014-09-22 15:33:26 +02:00
|
|
|
}
|
2014-08-28 01:35:07 +02:00
|
|
|
} else {
|
2014-11-04 12:34:49 +01:00
|
|
|
util.log("[red] Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
|
|
|
|
util.log("[red] Run with -v for details");
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
util.log("------------------------------------------");
|
2014-09-22 15:33:26 +02:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
if (nodeMissing.length > 0) {
|
|
|
|
util.log("[red] Missing node modules:");
|
|
|
|
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)) {
|
|
|
|
util.log("[red] - "+i+": "+missingModules[i].join(", "));
|
|
|
|
if (settings.autoInstallModules) {
|
|
|
|
installModule(i).otherwise(function(err) {
|
|
|
|
// 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) {
|
|
|
|
util.log("[red] Removing modules from config");
|
2014-11-26 17:41:31 +01:00
|
|
|
redNodes.cleanModuleList();
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
defer.resolve();
|
2014-11-21 17:35:29 +01:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
redNodes.loadFlows();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
comms.start();
|
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
defer.reject(err);
|
|
|
|
});
|
2014-11-21 17:35:29 +01:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
return defer.promise;
|
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) {
|
|
|
|
comms.publish("node/added",info,false);
|
|
|
|
if (info.length > 0) {
|
|
|
|
util.log("[red] Added node types:");
|
|
|
|
for (var i=0;i<info.length;i++) {
|
|
|
|
for (var j=0;j<info[i].types.length;j++) {
|
|
|
|
util.log("[red] - "+
|
|
|
|
(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);
|
|
|
|
util.log("[red] Removed node types:");
|
|
|
|
for (var j=0;j<removedNodes.length;j++) {
|
|
|
|
for (var i=0;i<removedNodes[j].types.length;i++) {
|
|
|
|
util.log("[red] - "+(removedNodes[i].module?removedNodes[i].module+":":"")+removedNodes[j].types[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removedNodes;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
reject(new Error("Invalid module name"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
util.log("[red] Installing module: "+module);
|
2014-11-04 18:05:29 +01:00
|
|
|
var child = child_process.exec('npm install --production '+module, function(err, stdin, stdout) {
|
2014-08-28 01:35:07 +02:00
|
|
|
if (err) {
|
|
|
|
var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
|
|
|
|
if (lookFor404.test(stdout)) {
|
2014-09-22 14:15:19 +02:00
|
|
|
util.log("[red] Installation of module "+module+" failed: module not found");
|
2014-08-28 01:35:07 +02:00
|
|
|
var e = new Error();
|
|
|
|
e.code = 404;
|
|
|
|
reject(e);
|
|
|
|
} else {
|
|
|
|
util.log("[red] Installation of module "+module+" failed:");
|
|
|
|
util.log("------------------------------------------");
|
|
|
|
console.log(err.toString());
|
|
|
|
util.log("------------------------------------------");
|
|
|
|
reject(new Error("Install failed"));
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-08 11:11:59 +01:00
|
|
|
util.log("[red] Installed module: "+module);
|
|
|
|
resolve(redNodes.addModule(module).then(reportAddedModules));
|
2014-08-28 01:35:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function uninstallModule(module) {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
if (/[\s;]/.test(module)) {
|
|
|
|
reject(new Error("Invalid module name"));
|
|
|
|
return;
|
|
|
|
}
|
2014-11-04 18:05:29 +01:00
|
|
|
var list = redNodes.removeModule(module);
|
2014-08-28 01:35:07 +02:00
|
|
|
util.log("[red] Removing module: "+module);
|
2014-11-04 18:05:29 +01:00
|
|
|
var child = child_process.exec('npm remove '+module, function(err, stdin, stdout) {
|
2014-08-28 01:35:07 +02:00
|
|
|
if (err) {
|
|
|
|
util.log("[red] Removal of module "+module+" failed:");
|
|
|
|
util.log("------------------------------------------");
|
|
|
|
console.log(err.toString());
|
|
|
|
util.log("------------------------------------------");
|
|
|
|
reject(new Error("Removal failed"));
|
|
|
|
} else {
|
|
|
|
util.log("[red] Removed module: "+module);
|
|
|
|
reportRemovedModules(list);
|
|
|
|
resolve(list);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-09-20 18:15:45 +02:00
|
|
|
}
|
2013-10-13 22:01:46 +02:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
|
2013-09-20 18:15:45 +02:00
|
|
|
|
2013-10-13 20:14:39 +02:00
|
|
|
function stop() {
|
|
|
|
redNodes.stopFlows();
|
2014-08-01 22:55:05 +02:00
|
|
|
comms.stop();
|
2013-10-13 20:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:35:29 +01:00
|
|
|
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,
|
|
|
|
uninstallModule: uninstallModule
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.__defineGetter__("app", function() { return app });
|
2014-02-16 01:39:30 +01:00
|
|
|
module.exports.__defineGetter__("nodeApp", function() { return nodeApp });
|
2013-09-05 16:02:48 +02:00
|
|
|
module.exports.__defineGetter__("server", function() { return server });
|