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

197 lines
6.3 KiB
JavaScript
Raw Normal View History

2014-05-03 23:26:35 +02:00
/**
2015-01-15 18:10:32 +01:00
* Copyright 2014, 2015 IBM Corp.
2014-05-03 23:26:35 +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-25 00:35:11 +01:00
var clone = require("clone");
2014-05-03 23:26:35 +02:00
var when = require("when");
2014-05-03 23:26:35 +02:00
var typeRegistry = require("./registry");
var credentials = require("./credentials");
var Flow = require("./Flow");
2014-05-03 23:26:35 +02:00
var log = require("../log");
2015-05-08 15:21:01 +02:00
2014-05-03 23:26:35 +02:00
var events = require("../events");
2014-11-23 23:25:09 +01:00
var redUtil = require("../util");
2014-05-03 23:26:35 +02:00
var storage = null;
var settings = null;
2015-06-16 16:32:41 +02:00
var deprecated = require("./deprecated");
var activeFlow = null;
2014-05-03 23:26:35 +02:00
var nodes = {};
2014-02-25 00:35:11 +01:00
var subflows = {};
2014-05-03 23:26:35 +02:00
var activeConfig = [];
2014-11-23 23:25:09 +01:00
var activeConfigNodes = {};
2014-05-03 23:26:35 +02:00
events.on('type-registered',function(type) {
if (activeFlow && activeFlow.typeRegistered(type)) {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.registered-missing", {type:type}));
2014-02-25 00:35:11 +01:00
}
});
2014-11-23 23:25:09 +01:00
2014-05-03 23:26:35 +02:00
var flowNodes = module.exports = {
init: function(_settings, _storage) {
settings = _settings;
2014-05-03 23:26:35 +02:00
storage = _storage;
},
2014-07-21 17:07:28 +02:00
/**
* Load the current activeConfig from storage and start it running
* @return a promise for the loading of the config
*/
2014-05-03 23:26:35 +02:00
load: function() {
return storage.getFlows().then(function(flows) {
return credentials.load().then(function() {
activeFlow = new Flow(flows);
flowNodes.startFlows();
2014-05-03 23:26:35 +02:00
});
}).otherwise(function(err) {
2015-05-08 15:21:01 +02:00
log.warn(log._("nodes.flows.error",{err:err}));
console.log(err.stack);
2014-05-03 23:26:35 +02:00
});
},
2014-07-21 17:07:28 +02:00
/**
* Get a node
* @param i the node id
* @return the node
*/
2014-05-03 23:26:35 +02:00
get: function(i) {
return activeFlow.getNode(i);
2014-05-03 23:26:35 +02:00
},
2014-07-21 17:07:28 +02:00
2015-01-10 23:09:37 +01:00
eachNode: function(cb) {
activeFlow.eachNode(cb);
},
2014-07-21 17:07:28 +02:00
/**
* @return the active configuration
*/
2014-05-03 23:26:35 +02:00
getFlows: function() {
return activeFlow.getFlow();
2014-05-03 23:26:35 +02:00
},
2014-07-21 17:07:28 +02:00
/**
* Sets the current active config.
* @param config the configuration to enable
* @param type the type of deployment to do: full (default), nodes, flows
2014-07-21 17:07:28 +02:00
* @return a promise for the starting of the new flow
*/
setFlows: function (config,type) {
type = type||"full";
2014-11-23 23:25:09 +01:00
var credentialsChanged = false;
2014-11-23 23:25:09 +01:00
var credentialSavePromise = null;
// Clone config and extract credentials prior to saving
// Original config needs to retain credentials so that flow.applyConfig
// knows which nodes have had changes.
var cleanConfig = clone(config);
cleanConfig.forEach(function(node) {
2014-07-21 16:56:38 +02:00
if (node.credentials) {
credentials.extract(node);
credentialsChanged = true;
2014-11-23 23:25:09 +01:00
}
});
if (credentialsChanged) {
2015-01-10 00:21:50 +01:00
credentialSavePromise = credentials.save();
} else {
credentialSavePromise = when.resolve();
2014-11-23 23:25:09 +01:00
}
if (type=="full") {
return credentialSavePromise
.then(function() { return storage.saveFlows(cleanConfig);})
.then(function() { return flowNodes.stopFlows(); })
.then(function() { activeFlow = new Flow(config); flowNodes.startFlows();});
} else {
return credentialSavePromise
.then(function() { return storage.saveFlows(cleanConfig);})
.then(function() {
var configDiff = activeFlow.diffConfig(config,type);
return flowNodes.stopFlows(configDiff).then(function() {
activeFlow.parseConfig(config);
flowNodes.startFlows(configDiff);
});
});
}
},
startFlows: function(configDiff) {
if (configDiff) {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.starting-modified-"+configDiff.type));
} else {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.starting-flows"));
}
try {
activeFlow.start(configDiff);
if (configDiff) {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.started-modified-"+configDiff.type));
} else {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.started-flows"));
}
} catch(err) {
var missingTypes = activeFlow.getMissingTypes();
if (missingTypes.length > 0) {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.missing-types"));
2015-06-17 15:27:49 +02:00
var knownUnknowns = 0;
2015-01-10 23:09:37 +01:00
for (var i=0;i<missingTypes.length;i++) {
2015-06-16 16:32:41 +02:00
var type = missingTypes[i];
var info = deprecated.get(type);
if (info) {
log.info(" - "+missingTypes[i]+" (provided by npm module "+info.module+")");
knownUnknowns += 1;
2015-06-16 16:32:41 +02:00
} else {
log.info(" - "+missingTypes[i]);
}
2014-11-23 23:25:09 +01:00
}
if (knownUnknowns > 0) {
log.info("To install any of these missing modules, run:");
log.info(" npm install <module name>");
log.info("in the directory:");
log.info(" "+settings.userDir);
}
2014-11-23 23:25:09 +01:00
}
}
2014-05-03 23:26:35 +02:00
},
stopFlows: function(configDiff) {
if (configDiff) {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.stopping-modified-"+configDiff.type));
} else {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.stopping-flows"));
}
if (activeFlow) {
return activeFlow.stop(configDiff).then(function() {
if (configDiff) {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.stopped-modified-"+configDiff.type));
} else {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.stopped-flows"));
}
return;
});
} else {
2015-05-08 15:21:01 +02:00
log.info(log._("nodes.flows.stopped"));
return;
}
2015-02-20 02:17:24 +01:00
},
handleError: function(node,logMessage,msg) {
activeFlow.handleError(node,logMessage,msg);
}
2014-07-14 22:46:36 +02:00
};
var activeFlow = null;