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-07-08 09:49:48 +02:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
var typeRegistry = require("./registry");
|
|
|
|
var credentials = require("./credentials");
|
2015-01-08 23:34:26 +01:00
|
|
|
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;
|
2015-06-17 15:18:47 +02:00
|
|
|
var settings = null;
|
2015-06-16 16:32:41 +02:00
|
|
|
var deprecated = require("./deprecated");
|
2015-01-08 23:34:26 +01:00
|
|
|
|
|
|
|
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) {
|
2015-03-26 12:37:24 +01:00
|
|
|
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
|
|
|
}
|
2015-01-08 23:34:26 +01:00
|
|
|
});
|
2014-11-23 23:25:09 +01:00
|
|
|
|
2014-05-03 23:26:35 +02:00
|
|
|
var flowNodes = module.exports = {
|
2015-06-17 15:18:47 +02:00
|
|
|
init: function(_settings, _storage) {
|
|
|
|
settings = _settings;
|
2014-05-03 23:26:35 +02:00
|
|
|
storage = _storage;
|
|
|
|
},
|
2015-08-19 22:14:45 +02:00
|
|
|
|
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() {
|
2015-01-08 23:34:26 +01:00
|
|
|
activeFlow = new Flow(flows);
|
|
|
|
flowNodes.startFlows();
|
2014-05-03 23:26:35 +02:00
|
|
|
});
|
|
|
|
}).otherwise(function(err) {
|
2015-06-16 17:09:53 +02:00
|
|
|
log.warn(log._("nodes.flows.error",{message:err.toString()}));
|
2015-01-08 23:34:26 +01:00
|
|
|
console.log(err.stack);
|
2014-05-03 23:26:35 +02:00
|
|
|
});
|
|
|
|
},
|
2015-08-19 22:14:45 +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) {
|
2015-01-08 23:34:26 +01:00
|
|
|
return activeFlow.getNode(i);
|
2014-05-03 23:26:35 +02:00
|
|
|
},
|
2015-08-19 22:14:45 +02:00
|
|
|
|
2015-01-10 23:09:37 +01:00
|
|
|
eachNode: function(cb) {
|
|
|
|
activeFlow.eachNode(cb);
|
|
|
|
},
|
2015-08-19 22:14:45 +02:00
|
|
|
|
2014-07-21 17:07:28 +02:00
|
|
|
/**
|
|
|
|
* @return the active configuration
|
|
|
|
*/
|
2014-05-03 23:26:35 +02:00
|
|
|
getFlows: function() {
|
2015-01-08 23:34:26 +01:00
|
|
|
return activeFlow.getFlow();
|
2014-05-03 23:26:35 +02:00
|
|
|
},
|
2015-08-19 22:14:45 +02:00
|
|
|
|
2014-07-21 17:07:28 +02:00
|
|
|
/**
|
|
|
|
* Sets the current active config.
|
|
|
|
* @param config the configuration to enable
|
2015-01-08 23:34:26 +01:00
|
|
|
* @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
|
|
|
|
*/
|
2015-01-08 23:34:26 +01:00
|
|
|
setFlows: function (config,type) {
|
2015-08-19 22:14:45 +02:00
|
|
|
|
2015-01-08 23:34:26 +01:00
|
|
|
type = type||"full";
|
2015-08-19 22:14:45 +02:00
|
|
|
|
2015-01-08 23:34:26 +01:00
|
|
|
var credentialsChanged = false;
|
2015-08-19 22:14:45 +02:00
|
|
|
|
2015-01-08 23:34:26 +01:00
|
|
|
var credentialSavePromise = null;
|
2015-08-19 22:14:45 +02:00
|
|
|
|
|
|
|
|
2015-02-24 23:02:39 +01:00
|
|
|
// 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);
|
2015-01-08 23:34:26 +01:00
|
|
|
credentialsChanged = true;
|
2014-11-23 23:25:09 +01:00
|
|
|
}
|
|
|
|
});
|
2015-08-19 22:14:45 +02:00
|
|
|
|
2015-01-08 23:34:26 +01:00
|
|
|
if (credentialsChanged) {
|
2015-01-10 00:21:50 +01:00
|
|
|
credentialSavePromise = credentials.save();
|
2015-01-08 23:34:26 +01:00
|
|
|
} else {
|
|
|
|
credentialSavePromise = when.resolve();
|
2014-11-23 23:25:09 +01:00
|
|
|
}
|
2015-01-08 23:34:26 +01:00
|
|
|
if (type=="full") {
|
|
|
|
return credentialSavePromise
|
2015-02-24 23:02:39 +01:00
|
|
|
.then(function() { return storage.saveFlows(cleanConfig);})
|
|
|
|
.then(function() { return flowNodes.stopFlows(); })
|
|
|
|
.then(function() { activeFlow = new Flow(config); flowNodes.startFlows();});
|
2015-01-08 23:34:26 +01:00
|
|
|
} else {
|
|
|
|
return credentialSavePromise
|
2015-02-24 23:02:39 +01:00
|
|
|
.then(function() { return storage.saveFlows(cleanConfig);})
|
2015-08-19 22:14:45 +02:00
|
|
|
.then(function() {
|
2015-03-13 18:54:58 +01:00
|
|
|
var configDiff = activeFlow.diffConfig(config,type);
|
2015-03-13 14:15:20 +01:00
|
|
|
return flowNodes.stopFlows(configDiff).then(function() {
|
|
|
|
activeFlow.parseConfig(config);
|
|
|
|
flowNodes.startFlows(configDiff);
|
|
|
|
});
|
|
|
|
});
|
2015-01-08 23:34:26 +01:00
|
|
|
}
|
|
|
|
},
|
2015-03-13 14:15:20 +01:00
|
|
|
startFlows: function(configDiff) {
|
|
|
|
if (configDiff) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.starting-modified-"+configDiff.type));
|
2015-03-13 14:15:20 +01:00
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.starting-flows"));
|
2015-03-13 14:15:20 +01:00
|
|
|
}
|
2015-01-08 23:34:26 +01:00
|
|
|
try {
|
2015-03-13 14:15:20 +01:00
|
|
|
activeFlow.start(configDiff);
|
|
|
|
if (configDiff) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.started-modified-"+configDiff.type));
|
2015-03-13 14:15:20 +01:00
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.started-flows"));
|
2015-03-13 14:15:20 +01:00
|
|
|
}
|
2015-01-08 23:34:26 +01:00
|
|
|
} 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) {
|
2015-06-16 16:39:45 +02:00
|
|
|
log.info(log._("nodes.flows.missing-type-provided",{type:missingTypes[i],module:info.module}));
|
2015-06-17 15:18:47 +02:00
|
|
|
knownUnknowns += 1;
|
2015-06-16 16:32:41 +02:00
|
|
|
} else {
|
|
|
|
log.info(" - "+missingTypes[i]);
|
|
|
|
}
|
2014-11-23 23:25:09 +01:00
|
|
|
}
|
2015-06-17 15:18:47 +02:00
|
|
|
if (knownUnknowns > 0) {
|
2015-06-16 16:39:45 +02:00
|
|
|
log.info(log._("nodes.flows.missing-type-install-1"));
|
2015-06-17 15:18:47 +02:00
|
|
|
log.info(" npm install <module name>");
|
2015-06-16 16:39:45 +02:00
|
|
|
log.info(log._("nodes.flows.missing-type-install-2"));
|
2015-06-17 15:18:47 +02:00
|
|
|
log.info(" "+settings.userDir);
|
|
|
|
}
|
2014-11-23 23:25:09 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-03 23:26:35 +02:00
|
|
|
},
|
2015-03-13 14:15:20 +01:00
|
|
|
stopFlows: function(configDiff) {
|
|
|
|
if (configDiff) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.stopping-modified-"+configDiff.type));
|
2015-03-13 14:15:20 +01:00
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.stopping-flows"));
|
2015-03-13 14:15:20 +01:00
|
|
|
}
|
2015-03-02 18:32:22 +01:00
|
|
|
if (activeFlow) {
|
2015-03-13 14:15:20 +01:00
|
|
|
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));
|
2015-03-13 14:15:20 +01:00
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.stopped-flows"));
|
2015-03-13 14:15:20 +01:00
|
|
|
}
|
2015-03-02 18:32:22 +01:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
} else {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(log._("nodes.flows.stopped"));
|
2015-02-24 23:02:39 +01:00
|
|
|
return;
|
2015-03-02 18:32:22 +01:00
|
|
|
}
|
2015-02-20 02:17:24 +01:00
|
|
|
},
|
|
|
|
handleError: function(node,logMessage,msg) {
|
|
|
|
activeFlow.handleError(node,logMessage,msg);
|
2015-08-19 22:14:45 +02:00
|
|
|
},
|
|
|
|
handleStatus: function(node,statusMessage) {
|
|
|
|
activeFlow.handleStatus(node,statusMessage);
|
2015-01-08 23:34:26 +01:00
|
|
|
}
|
2014-07-14 22:46:36 +02:00
|
|
|
};
|
2015-01-08 23:34:26 +01:00
|
|
|
|
|
|
|
var activeFlow = null;
|