-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkevoree-core.js
466 lines (421 loc) · 12.7 KB
/
kevoree-core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
'use strict';
var kevoree = require('kevoree-library');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var adaptationsExecutor = require('./lib/adaptation-executor');
var NAME_PATTERN = /^[\w]+$/;
/**
*
* @param modulesPath
* @param logger
* @constructor
*/
function KevoreeCore(kevscript, modulesPath, logger) {
if (!kevscript || !modulesPath || !logger) {
throw new Error('KevoreeCore constructor needs a KevScript engine, modulesPath and a KevoreeLogger');
}
this.log = logger;
this.kevs = kevscript;
this.stopping = false;
this.currentModel = null;
this.deployModel = null;
this.nodeName = null;
this.nodeInstance = null;
this.modulesPath = modulesPath;
this.bootstrapper = null;
this.firstBoot = true;
this.scriptQueue = [];
this.emitter = new EventEmitter();
}
util.inherits(KevoreeCore, EventEmitter);
/**
*
* @param nodeName
*/
KevoreeCore.prototype.start = function (nodeName) {
if (!nodeName || nodeName.length === 0) {
nodeName = 'node0';
}
if (nodeName.match(NAME_PATTERN)) {
this.nodeName = nodeName;
var factory = new kevoree.factory.DefaultKevoreeFactory();
this.currentModel = factory.createContainerRoot();
factory.root(this.currentModel);
// create platform node
var node = factory.createContainerNode();
node.name = this.nodeName;
node.started = false;
// add platform node
this.currentModel.addNodes(node);
var id = setInterval(function () {}, 10e10);
// hang-on until the core is stopped
this.emitter.on('stopped', function () {
clearInterval(id);
this.emit('stopped');
}.bind(this));
this.log.info(this.toString(), 'Platform node name: ' + nodeName);
} else {
throw new Error('Platform node name must match this regex ' + NAME_PATTERN.toString());
}
};
/**
*
*/
KevoreeCore.prototype.stop = function (callback) {
callback = callback || function noop() {};
this.emitter.once('stopped', callback);
var factory = new kevoree.factory.DefaultKevoreeFactory();
var cloner = factory.createModelCloner();
var stopModel = cloner.clone(this.currentModel, false);
var node = stopModel.findNodesByID(this.nodeName);
if (node.started) {
node.started = false;
var subNodes = node.hosts.iterator();
while (subNodes.hasNext()) {
subNodes.next().delete();
}
var groups = node.groups.iterator();
while (groups.hasNext()) {
groups.next().delete();
}
var bindings = stopModel.mBindings.iterator();
while (bindings.hasNext()) {
var binding = bindings.next();
if (binding.port.eContainer() &&
binding.port.eContainer().eContainer() &&
binding.port.eContainer().eContainer().name === node.name) {
if (binding.hub) {
binding.hub.delete();
}
}
}
var comps = node.components.iterator();
while (comps.hasNext()) {
comps.next().delete();
}
this.stopping = true;
this.deploy(stopModel, function () {
if (this.nodeInstance === null) {
this.log.info(this.toString(), 'Platform stopped before bootstrapped');
this.emitter.emit('stopped');
} else {
this.log.info(this.toString(), 'Platform stopped: ' + this.nodeInstance.getName());
this.emitter.emit('stopped');
}
}.bind(this));
} else {
this.emitter.emit('stopped');
}
};
/**
*
* @param model
* @param callback
*/
KevoreeCore.prototype.deploy = function (model, callback) {
callback = callback || function deployNoopCallback() {};
if (!this.deployModel) {
this.emit('deploying', model);
if (model && !model.findNodesByID(this.nodeName)) {
callback(new Error('Deploy model failure: unable to find ' + this.nodeName + ' in given model'));
} else {
this.log.debug(this.toString(), (this.stopping ? 'Stopping':'Deploy') + ' process started...');
//var start = new Date().getTime();
if (model) {
// check if there is an instance currently running
// if not, it will try to run it
var self = this;
this.checkBootstrapNode(model)
.then(function () {
if (self.nodeInstance) {
var adaptations;
try {
// monkey-patch model because of KMF
monkeyPatchKMF(model);
var factory = new kevoree.factory.DefaultKevoreeFactory();
// clone model so that adaptations won't modify the proposed one
var cloner = factory.createModelCloner();
self.deployModel = cloner.clone(model, true);
// set it read-only to ensure adaptations consistency
self.deployModel.setRecursiveReadOnly();
// make a diff between the current model and the model to deploy
var diffSeq = factory.createModelCompare().diff(self.currentModel, self.deployModel);
// ask the node platform to create the needed adaptation commands
adaptations = self.nodeInstance.processTraces(diffSeq, self.deployModel);
// execute adaptation commands
adaptationsExecutor(self, model, adaptations, callback);
} catch (err) {
self.log.error(self.toString(), err.stack);
var error = new Error('Something went wrong while creating adaptations (deployment ignored)');
self.log.warn(self.toString(), error.message);
self.deployModel = null;
if (self.firstBoot) {
// === If firstBoot adaptations creation failed then it is bad => exit
self.log.warn(self.toString(), 'Shutting down Kevoree because bootstrap failed...');
callback(error);
self.emit('error', error);
} else {
callback(error);
self.emit('error', error);
}
}
} else {
callback(new Error('There is no instance to bootstrap on'));
}
})
.catch(function (err) {
self.emit('error', err);
callback(err);
});
} else {
callback(new Error('Model is not defined or null. Deploy aborted.'));
}
}
} else {
// TODO add the possibility to put new deployment in pending queue
this.log.warn(this.toString(), 'New deploy process requested: aborted because another one is in process (retry later?)');
callback(new Error('New deploy process requested: aborted because another one is in process (retry later?)'));
}
};
KevoreeCore.prototype.submitScript = function (script, callback) {
var self = this;
if (typeof callback !== 'function') {
callback = function (err) {
if (err) {
// even if the user did not register any callback to submitScript()
// display the error so that he gets notified in case of error
self.log.error(self.toString(), err.message);
}
};
}
if (this.deployModel === null) {
// not in "deploying state"
this.kevs.parse(script, this.currentModel, function (err, model) {
if (err) {
var e = new Error('KevScript submission failed (' + err.message + ')');
callback(e);
return;
}
var deployHandler, errHandler, adaptHandler;
deployHandler = function () {
self.off('error', errHandler);
self.off('adaptationError', adaptHandler);
callback();
};
errHandler = function (err) {
self.off('deployed', deployHandler);
self.off('adaptationError', adaptHandler);
var e = new Error('KevScript submission failed (' + err.message + ')');
callback(e);
};
adaptHandler = function (err) {
self.off('error', errHandler);
self.off('deployed', deployHandler);
var e = new Error('KevScript submission failed (' + err.message + ')');
callback(e);
};
self.once('deployed', deployHandler);
self.once('error', errHandler);
self.once('adaptationError', adaptHandler);
self.deploy(model);
});
} else {
// in "deploying state" => need to queue request to process it afterwards
this.scriptQueue.push({
script: script,
callback: callback
});
this.log.debug(this.toString(), 'Script added to queue at position ' + this.scriptQueue.length - 1);
}
};
KevoreeCore.prototype.processScriptQueue = function () {
var self = this;
if (this.scriptQueue.length > 0) {
// retrieve first queued script
var item = this.scriptQueue[0];
// remove first queued script from the queue
this.scriptQueue.splice(0, 1);
// execute first queued script
this.log.debug(this.toString(), 'Core.processScriptQueue parsing ' + item.script);
this.kevs.parse(item.script, this.currentModel, function (err, model) {
if (err) {
// queued script submission failed
var e = new Error('KevScript submission failed (' + err.message + ')');
item.callback(e);
} else {
// queued script submission succeed
var deployHandler, errHandler, adaptHandler;
deployHandler = function () {
self.off('error', errHandler);
self.off('adaptationError', adaptHandler);
item.callback();
};
errHandler = function (err) {
self.off('deployed', deployHandler);
self.off('adaptationError', adaptHandler);
var e = new Error('KevScript submission failed (' + err.message + ')');
item.callback(e);
};
adaptHandler = function (err) {
self.off('error', errHandler);
self.off('deployed', deployHandler);
var e = new Error('KevScript submission failed (' + err.message + ')');
item.callback(e);
};
self.once('deployed', deployHandler);
self.once('error', errHandler);
self.once('adaptationError', adaptHandler);
self.deploy(model);
}
});
}
};
/**
*
* @param model
* @param callback
*/
KevoreeCore.prototype.checkBootstrapNode = function (deployModel) {
var self = this;
return new Promise(function (resolve, reject) {
if (self.bootstrapper) {
if (!self.nodeInstance) {
self.log.debug(self.toString(), 'Start \'' + self.nodeName + '\' bootstrapping...');
self.bootstrapper.bootstrapNodeType(self.nodeName, deployModel, function (err, AbstractNode) {
if (err) {
reject(err);
} else {
try {
var deployNode = deployModel.findNodesByID(self.nodeName);
var currentNode = self.currentModel.findNodesByID(self.nodeName);
// create node instance
self.nodeInstance = new AbstractNode(self, deployNode, self.nodeName);
// bootstrap node dictionary
var factory = new kevoree.factory.DefaultKevoreeFactory();
currentNode.dictionary = factory.createDictionary().withGenerated_KMF_ID('0');
if (deployNode.typeDefinition.dictionaryType) {
deployNode.typeDefinition.dictionaryType.attributes.array.forEach(function (attr) {
if (!attr.fragmentDependant) {
var param = factory.createValue();
param.name = attr.name;
var currVal = deployNode.dictionary.findValuesByID(param.name);
if (!currVal) {
param.value = attr.defaultValue;
currentNode.dictionary.addValues(param);
self.log.debug(self.toString(), 'Set default node param: ' + param.name + '=' + param.value);
}
}
});
}
resolve();
} catch (err) {
reject(err);
}
}
});
} else {
// bootstrap already done :)
resolve();
}
} else {
reject(new Error('No bootstrapper given to this core. Did you set one?'));
}
});
};
/**
*
* @returns {string}
*/
KevoreeCore.prototype.toString = function () {
return 'Core';
};
/**
*
* @returns {null|*}
*/
KevoreeCore.prototype.getBootstrapper = function () {
return this.bootstrapper;
};
/**
*
* @param bootstrapper
*/
KevoreeCore.prototype.setBootstrapper = function (bootstrapper) {
this.bootstrapper = bootstrapper;
};
/**
*
* @returns {string}
*/
KevoreeCore.prototype.getModulesPath = function () {
return this.modulesPath;
};
/**
*
* @returns {null|*}
*/
KevoreeCore.prototype.getCurrentModel = function () {
return this.currentModel;
};
/**
*
* @returns {null|*}
*/
KevoreeCore.prototype.getLastModel = function () {
if (typeof this.deployModel !== 'undefined' && this.deployModel !== null) {
return this.deployModel;
} else {
return this.currentModel;
}
};
/**
*
* @returns {null|*}
*/
KevoreeCore.prototype.getDeployModel = function () {
return this.deployModel;
};
/**
*
* @returns {null|*|string}
*/
KevoreeCore.prototype.getNodeName = function () {
return this.nodeName;
};
/**
*
* @returns {*}
*/
KevoreeCore.prototype.getLogger = function () {
return this.log;
};
KevoreeCore.prototype.off = function (event, listener) {
this.removeListener(event, listener);
};
function hash(str) {
var val = 0;
if (str.length === 0) {
return val + '';
}
for (var i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
val = ((val<<5) - val) + char;
val = val & val; // Convert to 32bit integer
}
return (val & 0xfffffff) + '';
}
function bindingHash(binding) {
var hubPath = binding.hub ? binding.hub.path() : 'UNDEFINED';
var portPath = binding.port ? binding.port.path() : 'UNDEFINED';
return hash(hubPath + '_' + portPath);
}
function monkeyPatchKMF(proposedModel) {
proposedModel.mBindings.array.forEach(function (possibleBinding) {
possibleBinding.generated_KMF_ID = bindingHash(possibleBinding);
});
}
/**
*
* @type {KevoreeCore}
*/
module.exports = KevoreeCore;