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

Change default data dir

Changes the default location for user data to $HOME/.node-red.
This commit is contained in:
Nick O'Leary 2015-02-25 14:23:59 +00:00
parent 54b0debb3b
commit ce1cd1ab9c
6 changed files with 161 additions and 88 deletions

34
red.js Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env node
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013, 2015 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,21 +21,24 @@ var express = require("express");
var crypto = require("crypto"); var crypto = require("crypto");
var nopt = require("nopt"); var nopt = require("nopt");
var path = require("path"); var path = require("path");
var fs = require("fs");
var RED = require("./red/red.js"); var RED = require("./red/red.js");
var server; var server;
var app = express(); var app = express();
var settingsFile = "./settings"; var settingsFile;
var flowFile; var flowFile;
var knownOpts = { var knownOpts = {
"settings":[path], "settings":[path],
"userDir":[path],
"v": Boolean, "v": Boolean,
"help": Boolean "help": Boolean
}; };
var shortHands = { var shortHands = {
"s":["--settings"], "s":["--settings"],
"u":["--userDir"],
"?":["--help"] "?":["--help"]
}; };
nopt.invalidHandler = function(k,v,t) { nopt.invalidHandler = function(k,v,t) {
@ -45,10 +49,11 @@ var parsedArgs = nopt(knownOpts,shortHands,process.argv,2)
if (parsedArgs.help) { if (parsedArgs.help) {
console.log("Node-RED v"+RED.version()); console.log("Node-RED v"+RED.version());
console.log("Usage: node red.js [-v] [-?] [--settings settings.js] [flows.json]"); console.log("Usage: node red.js [-v] [-?] [--settings settings.js] [--userDir DIR] [flows.json]");
console.log(""); console.log("");
console.log("Options:"); console.log("Options:");
console.log(" -s, --settings FILE use specified settings file"); console.log(" -s, --settings FILE use specified settings file");
console.log(" -u, --userDir DIR use specified user directory");
console.log(" -v enable verbose output"); console.log(" -v enable verbose output");
console.log(" -?, --help show usage"); console.log(" -?, --help show usage");
console.log(""); console.log("");
@ -60,13 +65,27 @@ if (parsedArgs.argv.remain.length > 0) {
} }
if (parsedArgs.settings) { if (parsedArgs.settings) {
// User-specified settings file
settingsFile = parsedArgs.settings; settingsFile = parsedArgs.settings;
} else if (parsedArgs.userDir && fs.existsSync(path.join(parsedArgs.userDir,"settings.js"))) {
// User-specified userDir that contains a settings.js
settingsFile = path.join(parsedArgs.userDir,"settings.js");
} else {
var userSettingsFile = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red","settings.js");
if (fs.existsSync(userSettingsFile)) {
// $HOME/.node-red/settings.js exists
settingsFile = userSettingsFile;
} else {
// Use default settings.js
settingsFile = "./settings";
}
} }
try { try {
var settings = require(settingsFile); var settings = require(settingsFile);
} catch(err) { } catch(err) {
if (err.code == 'MODULE_NOT_FOUND') { if (err.code == 'MODULE_NOT_FOUND') {
console.log("Unable to load settings file "+settingsFile); console.log("Unable to load settings file: "+settingsFile);
} else { } else {
console.log(err); console.log(err);
} }
@ -117,7 +136,12 @@ if (settings.httpNodeRoot !== false) {
settings.uiPort = settings.uiPort||1880; settings.uiPort = settings.uiPort||1880;
settings.uiHost = settings.uiHost||"0.0.0.0"; settings.uiHost = settings.uiHost||"0.0.0.0";
settings.flowFile = flowFile || settings.flowFile; if (flowFile) {
settings.flowFile = flowFile;
}
if (parsedArgs.userDir) {
settings.userDir = parsedArgs.userDir;
}
RED.init(server,settings); RED.init(server,settings);

View File

@ -504,6 +504,37 @@ function getNodeFiles(dir) {
return result; return result;
} }
function scanDirForNodesModules(dir,moduleName) {
var results = [];
try {
var files = fs.readdirSync(dir);
for (var i=0;i<files.length;i++) {
var fn = files[i];
if (!registry.getNodeModuleInfo(fn)) {
if (!moduleName || fn == moduleName) {
var pkgfn = path.join(dir,fn,"package.json");
try {
var pkg = require(pkgfn);
if (pkg['node-red']) {
var moduleDir = path.join(dir,fn);
results.push({dir:moduleDir,package:pkg});
}
} catch(err) {
if (err.code != "MODULE_NOT_FOUND") {
// TODO: handle unexpected error
}
}
if (fn == moduleName) {
break;
}
}
}
}
} catch(err) {
}
return results;
}
/** /**
* Scans the node_modules path for nodes * Scans the node_modules path for nodes
* @param moduleName the name of the module to be found * @param moduleName the name of the module to be found
@ -512,36 +543,19 @@ function getNodeFiles(dir) {
function scanTreeForNodesModules(moduleName) { function scanTreeForNodesModules(moduleName) {
var dir = __dirname+"/../../nodes"; var dir = __dirname+"/../../nodes";
var results = []; var results = [];
var userDir;
if (settings.userDir) {
userDir = path.join(settings.userDir,"node_modules");
results = results.concat(scanDirForNodesModules(userDir,moduleName));
}
var up = path.resolve(path.join(dir,"..")); var up = path.resolve(path.join(dir,".."));
while (up !== dir) { while (up !== dir) {
var pm = path.join(dir,"node_modules"); var pm = path.join(dir,"node_modules");
try { if (pm != userDir) {
var files = fs.readdirSync(pm); results = results.concat(scanDirForNodesModules(pm,moduleName));
for (var i=0;i<files.length;i++) {
var fn = files[i];
if (!registry.getNodeModuleInfo(fn)) {
if (!moduleName || fn == moduleName) {
var pkgfn = path.join(pm,fn,"package.json");
try {
var pkg = require(pkgfn);
if (pkg['node-red']) {
var moduleDir = path.join(pm,fn);
results.push({dir:moduleDir,package:pkg});
}
} catch(err) {
if (err.code != "MODULE_NOT_FOUND") {
// TODO: handle unexpected error
}
}
if (fn == moduleName) {
break;
}
}
}
}
} catch(err) {
} }
dir = up; dir = up;
up = path.resolve(path.join(dir,"..")); up = path.resolve(path.join(dir,".."));
} }
@ -661,14 +675,19 @@ function load(defaultNodesDir,disableNodePathScan) {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
// Find all of the nodes to load // Find all of the nodes to load
var nodeFiles; var nodeFiles;
var dir;
if(defaultNodesDir) { if(defaultNodesDir) {
nodeFiles = getNodeFiles(path.resolve(defaultNodesDir)); nodeFiles = getNodeFiles(path.resolve(defaultNodesDir));
} else { } else {
nodeFiles = getNodeFiles(__dirname+"/../../nodes"); nodeFiles = getNodeFiles(__dirname+"/../../nodes");
} }
if (settings.userDir) {
dir = path.join(settings.userDir,"nodes");
nodeFiles = nodeFiles.concat(getNodeFiles(dir));
}
if (settings.nodesDir) { if (settings.nodesDir) {
var dir = settings.nodesDir; dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") { if (typeof settings.nodesDir == "string") {
dir = [dir]; dir = [dir];
} }

View File

@ -17,6 +17,8 @@
var express = require('express'); var express = require('express');
var when = require('when'); var when = require('when');
var child_process = require('child_process'); var child_process = require('child_process');
var path = require("path");
var fs = require("fs");
var redNodes = require("./nodes"); var redNodes = require("./nodes");
var comms = require("./comms"); var comms = require("./comms");
@ -129,7 +131,7 @@ function reportAddedModules(info) {
(info[i].module?info[i].module+":":"")+ (info[i].module?info[i].module+":":"")+
info[i].types[j]+ info[i].types[j]+
(info[i].err?" : "+info[i].err:"") (info[i].err?" : "+info[i].err:"")
); );
} }
} }
} }
@ -141,7 +143,7 @@ function reportRemovedModules(removedNodes) {
log.info("Removed node types:"); log.info("Removed node types:");
for (var j=0;j<removedNodes.length;j++) { for (var j=0;j<removedNodes.length;j++) {
for (var i=0;i<removedNodes[j].types.length;i++) { for (var i=0;i<removedNodes[j].types.length;i++) {
log.info(" - "+(removedNodes[i].module?removedNodes[i].module+":":"")+removedNodes[j].types[i]); log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
} }
} }
return removedNodes; return removedNodes;
@ -155,26 +157,32 @@ function installModule(module) {
return; return;
} }
log.info("Installing module: "+module); log.info("Installing module: "+module);
var child = child_process.exec('npm install --production '+module, function(err, stdin, stdout) { var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
if (err) { var child = child_process.exec('npm install --production '+module,
var lookFor404 = new RegExp(" 404 .*"+module+"$","m"); {
if (lookFor404.test(stdout)) { cwd: installDir
log.warn("Installation of module "+module+" failed: module not found"); },
var e = new Error(); function(err, stdin, stdout) {
e.code = 404; if (err) {
reject(e); 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 { } else {
log.warn("Installation of module "+module+" failed:"); log.info("Installed module: "+module);
log.warn("------------------------------------------"); resolve(redNodes.addModule(module).then(reportAddedModules));
log.warn(err.toString());
log.warn("------------------------------------------");
reject(new Error("Install failed"));
} }
} else {
log.info("Installed module: "+module);
resolve(redNodes.addModule(module).then(reportAddedModules));
} }
}); );
}); });
} }
@ -184,40 +192,51 @@ function uninstallModule(module) {
reject(new Error("Invalid module name")); reject(new Error("Invalid module name"));
return; 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); var list = redNodes.removeModule(module);
log.info("Removing module: "+module); log.info("Removing module: "+module);
var child = child_process.exec('npm remove '+module, function(err, stdin, stdout) { var child = child_process.exec('npm remove '+module,
if (err) { {
log.warn("Removal of module "+module+" failed:"); cwd: installDir
log.warn("------------------------------------------"); },
log.warn(err.toString()); function(err, stdin, stdout) {
log.warn("------------------------------------------"); if (err) {
reject(new Error("Removal failed")); log.warn("Removal of module "+module+" failed:");
} else { log.warn("------------------------------------------");
log.info("Removed module: "+module); log.warn(err.toString());
reportRemovedModules(list); log.warn("------------------------------------------");
resolve(list); reject(new Error("Removal failed"));
} else {
log.info("Removed module: "+module);
reportRemovedModules(list);
resolve(list);
}
} }
}); );
}); });
} }
function reportMetrics() { function reportMetrics() {
var memUsage = process.memoryUsage(); var memUsage = process.memoryUsage();
// only need to init these once per report // only need to init these once per report
var metrics = {}; var metrics = {};
metrics.level = log.METRIC; metrics.level = log.METRIC;
//report it //report it
metrics.event = "runtime.memory.rss" metrics.event = "runtime.memory.rss"
metrics.value = memUsage.rss; metrics.value = memUsage.rss;
log.log(metrics); log.log(metrics);
metrics.event = "runtime.memory.heapTotal" metrics.event = "runtime.memory.heapTotal"
metrics.value = memUsage.heapTotal; metrics.value = memUsage.heapTotal;
log.log(metrics); log.log(metrics);
metrics.event = "runtime.memory.heapUsed" metrics.event = "runtime.memory.heapUsed"
metrics.value = memUsage.heapUsed; metrics.value = memUsage.heapUsed;
log.log(metrics); log.log(metrics);

View File

@ -25,13 +25,12 @@ var storage = null;
var persistentSettings = { var persistentSettings = {
init: function(settings) { init: function(settings) {
userSettings = settings; userSettings = settings;
for (var i in settings) { for (var i in settings) {
if (settings.hasOwnProperty(i)) { if (settings.hasOwnProperty(i)) {
(function() { (function() {
var j = i; var j = i;
persistentSettings.__defineGetter__(j,function() { return userSettings[j]; }); persistentSettings.__defineGetter__(j,function() { return userSettings[j]; });
persistentSettings.__defineSetter__(j,function() { throw new Error("Property '"+i+"' is read-only"); }); persistentSettings.__defineSetter__(j,function() { throw new Error("Property '"+j+"' is read-only"); });
})(); })();
} }
} }

View File

@ -32,7 +32,6 @@ var flowsFileBackup;
var credentialsFile; var credentialsFile;
var credentialsFileBackup; var credentialsFileBackup;
var oldCredentialsFile; var oldCredentialsFile;
var userDir;
var libDir; var libDir;
var libFlowsDir; var libFlowsDir;
var globalSettingsFile; var globalSettingsFile;
@ -145,38 +144,50 @@ function writeFile(path,content) {
var localfilesystem = { var localfilesystem = {
init: function(_settings) { init: function(_settings) {
settings = _settings; settings = _settings;
userDir = settings.userDir || process.env.NODE_RED_HOME;
var promises = [];
if (!settings.userDir) {
if (fs.existsSync(fspath.join(process.env.NODE_RED_HOME,".config.json"))) {
settings.userDir = process.env.NODE_RED_HOME;
} else {
settings.userDir = fspath.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red");
promises.push(promiseDir(settings.userDir));
}
}
if (settings.flowFile) { if (settings.flowFile) {
flowsFile = settings.flowFile; flowsFile = settings.flowFile;
flowsFullPath = flowsFile; flowsFullPath = flowsFile;
} else { } else {
flowsFile = 'flows_'+require('os').hostname()+'.json'; flowsFile = 'flows_'+require('os').hostname()+'.json';
flowsFullPath = fspath.join(userDir,flowsFile); flowsFullPath = fspath.join(settings.userDir,flowsFile);
} }
var ffExt = fspath.extname(flowsFullPath); var ffExt = fspath.extname(flowsFullPath);
var ffName = fspath.basename(flowsFullPath); var ffName = fspath.basename(flowsFullPath);
var ffBase = fspath.basename(flowsFullPath,ffExt); var ffBase = fspath.basename(flowsFullPath,ffExt);
var ffDir = fspath.dirname(flowsFullPath); var ffDir = fspath.dirname(flowsFullPath);
credentialsFile = fspath.join(userDir,ffBase+"_cred"+ffExt); credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
credentialsFileBackup = fspath.join(userDir,"."+ffBase+"_cred"+ffExt+".backup"); credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup");
oldCredentialsFile = fspath.join(userDir,"credentials.json"); oldCredentialsFile = fspath.join(settings.userDir,"credentials.json");
flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup"); flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup");
libDir = fspath.join(userDir,"lib"); libDir = fspath.join(settings.userDir,"lib");
libFlowsDir = fspath.join(libDir,"flows"); libFlowsDir = fspath.join(libDir,"flows");
globalSettingsFile = fspath.join(userDir,".config.json"); globalSettingsFile = fspath.join(settings.userDir,".config.json");
return promiseDir(libFlowsDir); promises.push(promiseDir(libFlowsDir));
return when.all(promises);
}, },
getFlows: function() { getFlows: function() {
var defer = when.defer(); var defer = when.defer();
log.info("User Directory : "+settings.userDir);
fs.exists(flowsFullPath, function(exists) { fs.exists(flowsFullPath, function(exists) {
if (exists) { if (exists) {
log.info("Loading flows : "+flowsFile); log.info("Loading flows : "+flowsFile);
@ -184,7 +195,7 @@ var localfilesystem = {
return JSON.parse(data); return JSON.parse(data);
})); }));
} else { } else {
log.info("Flows file not found : "+flowsFile ); log.info("Creating new flows file : "+flowsFile );
defer.resolve([]); defer.resolve([]);
} }
}); });

View File

@ -17,6 +17,7 @@ var should = require("should");
var when = require("when"); var when = require("when");
var sinon = require("sinon"); var sinon = require("sinon");
var child_process = require('child_process'); var child_process = require('child_process');
var fs = require("fs");
var comms = require("../../red/comms"); var comms = require("../../red/comms");
var redNodes = require("../../red/nodes"); var redNodes = require("../../red/nodes");
@ -133,7 +134,7 @@ describe("red/server", function() {
}); });
it("rejects when npm returns a 404", function(done) { it("rejects when npm returns a 404", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,cb) { var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error(),""," 404 this_wont_exist"); cb(new Error(),""," 404 this_wont_exist");
}); });
@ -145,14 +146,13 @@ describe("red/server", function() {
}); });
}); });
it("rejects with generic error", function(done) { it("rejects with generic error", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,cb) { var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"",""); cb(new Error("test_error"),"","");
}); });
server.installModule("this_wont_exist").then(function() { server.installModule("this_wont_exist").then(function() {
done(new Error("Unexpected success")); done(new Error("Unexpected success"));
}).otherwise(function(err) { }).otherwise(function(err) {
err.message.should.be.eql("Install failed");
done(); done();
}).finally(function() { }).finally(function() {
exec.restore(); exec.restore();
@ -160,7 +160,7 @@ describe("red/server", function() {
}); });
it("succeeds when module is found", function(done) { it("succeeds when module is found", function(done) {
var nodeInfo = {module:"foo",types:["a"]}; var nodeInfo = {module:"foo",types:["a"]};
var exec = sinon.stub(child_process,"exec",function(cmd,cb) { var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"",""); cb(null,"","");
}); });
var addModule = sinon.stub(redNodes,"addModule",function(md) { var addModule = sinon.stub(redNodes,"addModule",function(md) {
@ -198,14 +198,13 @@ describe("red/server", function() {
var removeModule = sinon.stub(redNodes,"removeModule",function(md) { var removeModule = sinon.stub(redNodes,"removeModule",function(md) {
return when.resolve(nodeInfo); return when.resolve(nodeInfo);
}); });
var exec = sinon.stub(child_process,"exec",function(cmd,cb) { var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"",""); cb(new Error("test_error"),"","");
}); });
server.uninstallModule("this_wont_exist").then(function() { server.uninstallModule("this_wont_exist").then(function() {
done(new Error("Unexpected success")); done(new Error("Unexpected success"));
}).otherwise(function(err) { }).otherwise(function(err) {
err.message.should.be.eql("Removal failed");
done(); done();
}).finally(function() { }).finally(function() {
exec.restore(); exec.restore();
@ -217,9 +216,10 @@ describe("red/server", function() {
var removeModule = sinon.stub(redNodes,"removeModule",function(md) { var removeModule = sinon.stub(redNodes,"removeModule",function(md) {
return nodeInfo; return nodeInfo;
}); });
var exec = sinon.stub(child_process,"exec",function(cmd,cb) { var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"",""); cb(null,"","");
}); });
var exists = sinon.stub(fs,"existsSync", function(fn) { return true; });
server.uninstallModule("this_wont_exist").then(function(info) { server.uninstallModule("this_wont_exist").then(function(info) {
info.should.eql(nodeInfo); info.should.eql(nodeInfo);
@ -232,6 +232,7 @@ describe("red/server", function() {
}).finally(function() { }).finally(function() {
exec.restore(); exec.restore();
removeModule.restore(); removeModule.restore();
exists.restore();
}); });
}); });
}); });