| 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525 |
1
1
35
35
35
35
2
33
33
33
33
33
33
33
33
33
67
67
67
67
67
67
33
33
132
132
132
34
34
34
34
34
68
34
40
34
34
34
68
68
108
38
70
70
70
70
38
70
70
2
2
68
68
38
70
68
108
68
68
108
108
108
68
15
15
15
15
28
30
28
27
15
28
15
28
15
7
7
14
28
7
1
1
2
2
2
4
4
2
4
1
2
2
2
5
5
5
2
4
2
2
2
15
14
13
23
23
13
5
5
5
15
12
5
5
5
5
5
5
8
7
3
3
3
1
3
2
3
| 'use strict';
;require.register("utils/blueprint", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/**
* @typedef {object} BlueprintObject
* @property {BlueprintMappings} blueprint
* @property {BlueprintClusterBindings} blueprint_cluster_bindings
*/
/**
* @typedef {object} BlueprintMappings
* @property {BlueprintMappingsHostGroup[]} host_groups
*/
/**
* @typedef {object[]} BlueprintMappingsHostGroup
* @property {BlueprintHostGroupComponent[]} components
* @property {string} name host group name
*/
/**
* @typedef {object} BlueprintHostGroupComponent
* @property {string} name component name
*/
/**
* @typedef {object} BlueprintClusterBindings
* @property {BlueprintClusterBindingsHostGroup[]} host_groups
*/
/**
* @typedef {object} BlueprintClusterBindingsHostGroup
* @property {BlueprintClusterBindingsHostGroupHosts[]} hosts
* @property {string} name host group name
*/
/**
* @typedef {object} BlueprintClusterBindingsHostGroupHosts
* @property {string} fqdn host fqdn
*/
module.exports = {
/**
* @param {BlueprintObject} masterBlueprint
* @param {BlueprintObject} slaveBlueprint
* @return {BlueprintObject}
*/
mergeBlueprints: function mergeBlueprints(masterBlueprint, slaveBlueprint) {
console.time('mergeBlueprints');
var self = this;
// Check edge cases
Iif (!slaveBlueprint && !masterBlueprint) {
throw 'slaveBlueprint or masterBlueprint should not be empty';
} else if (slaveBlueprint && !masterBlueprint) {
return slaveBlueprint;
} else Iif (!slaveBlueprint && masterBlueprint) {
return masterBlueprint;
}
// Work with main case (both blueprint are presented)
var matches = self.matchGroups(masterBlueprint, slaveBlueprint);
var res = {
blueprint: { host_groups: [] },
blueprint_cluster_binding: { host_groups: [] }
};
var tmpObj = { hosts: [], components: [] };
var masterBluePrintHostGroupsCluster = this.blueprintToObject(masterBlueprint, 'blueprint_cluster_binding.host_groups');
var slaveBluePrintHostGroupsCluster = this.blueprintToObject(slaveBlueprint, 'blueprint_cluster_binding.host_groups');
var masterBluePrintHostGroupsBlueprint = this.blueprintToObject(masterBlueprint, 'blueprint.host_groups');
var slaveBluePrintHostGroupsBlueprint = this.blueprintToObject(slaveBlueprint, 'blueprint.host_groups');
matches.forEach(function (match, i) {
var group_name = 'host-group-' + (i + 1);
var masterComponents = match.g1 ? Em.getWithDefault(masterBluePrintHostGroupsBlueprint, match.g1, tmpObj).components : [];
var slaveComponents = match.g2 ? Em.getWithDefault(slaveBluePrintHostGroupsBlueprint, match.g2, tmpObj).components : [];
res.blueprint.host_groups.push({
name: group_name,
components: masterComponents.concat(slaveComponents)
});
var hosts = match.g1 ? Em.getWithDefault(masterBluePrintHostGroupsCluster, match.g1, tmpObj).hosts : Em.getWithDefault(slaveBluePrintHostGroupsCluster, match.g2, tmpObj).hosts;
res.blueprint_cluster_binding.host_groups.push({
name: group_name,
hosts: hosts
});
});
console.timeEnd('mergeBlueprints');
return res;
},
/**
* Convert <code>blueprint</code>-object to the array with keys equal to the host-groups names
* Used to improve performance when user try to search value in the blueprint using host-group name as search-field
* Example:
* Before:
* <pre>
* // blueprint
* {
* blueprint: {
* host_groups: [
* {
* components: [{}, {}, ...],
* name: 'n1'
* },
* {
* components: [{}, {}, ...],
* name: 'n2'
* }
* ]
* },
* blueprint_cluster_binding: {
* host_groups: [
* {
* hosts: [{}, {}, ...],
* name: 'n1'
* },
* {
* hosts: [{}, {}, ...],
* name: 'n2'
* }
* ]
* }
* }
* </pre>
* Return:
* <pre>
* // field = 'blueprint_cluster_binding.host_groups'
* {
* n1: {
* hosts: [{}, {}, ...],
* name: 'n1'
* },
* n2: {
* hosts: [{}, {}, ...],
* name: 'n2'
* }
* }
*
* // field = 'blueprint.host_groups'
* {
* n1: {
* components: [{}, {}, ...],
* name: 'n1'
* },
* n2: {
* components: [{}, {}, ...],
* name: 'n2'
* }
* }
* </pre>
* @param {object} blueprint
* @param {string} field
* @returns {object}
*/
blueprintToObject: function blueprintToObject(blueprint, field) {
var valueToMap = Em.get(blueprint, field);
Iif (!Array.isArray(valueToMap)) {
return {};
}
return valueToMap.toMapByProperty('name');
},
matchGroups: function matchGroups(masterBlueprint, slaveBlueprint) {
var self = this;
var res = [];
var groups1 = masterBlueprint.blueprint_cluster_binding.host_groups;
var groups2 = slaveBlueprint.blueprint_cluster_binding.host_groups;
var groups1_used = groups1.map(function () {
return false;
});
var groups2_used = groups2.map(function () {
return false;
});
self.matchGroupsWithLeft(groups1, groups2, groups1_used, groups2_used, res, false);
self.matchGroupsWithLeft(groups2, groups1, groups2_used, groups1_used, res, true);
return res;
},
matchGroupsWithLeft: function matchGroupsWithLeft(groups1, groups2, groups1_used, groups2_used, res, inverse) {
var gs2 = this.groupsToObject(groups2);
for (var i = 0; i < groups1.length; i++) {
if (groups1_used[i]) {
continue;
}
var group1 = groups1[i];
groups1_used[i] = true;
var group2 = gs2[group1.hosts.mapProperty('fqdn').join(',')];
if (group2) {
groups2_used[group2.index] = true;
}
var item = {};
if (inverse) {
item.g2 = group1.name;
Iif (group2) {
item.g1 = group2.name;
}
} else {
item.g1 = group1.name;
if (group2) {
item.g2 = group2.name;
}
}
res.push(item);
}
// remove unneeded property
groups2.forEach(function (group) {
delete group.index;
});
},
/**
* Convert array of objects to the one object to improve performance with searching objects in the provided array
* Example:
* Before:
* <pre>
* // groups
* [
* {
* hosts: [
* {fqdn: 'h1'}, {fqdn: 'h2'}
* ],
* name: 'n1'
* },
* {
* hosts: [
* {fqdn: 'h3'}, {fqdn: 'h4'}
* ]
* }
* ]
* </pre>
* Return:
* <pre>
* {
* 'h1,h2': {
* hosts: [
* {fqdn: 'h1'}, {fqdn: 'h2'}
* ],
* name: 'n1',
* index: 0
* },
* 'h3,h4': {
* hosts: [
* {fqdn: 'h3'}, {fqdn: 'h4'}
* ],
* name: 'n2',
* index: 1
* }
* }
* </pre>
* @param {{hosts: object[], name: string}[]} groups
* @returns {object}
*/
groupsToObject: function groupsToObject(groups) {
var ret = {};
groups.forEach(function (group, index) {
var key = group.hosts.mapProperty('fqdn').join(',');
ret[key] = group;
ret[key].index = index;
});
return ret;
},
/**
* Remove from blueprint all components expect given components
* @param {object} blueprint
* @param {string[]} components
*/
filterByComponents: function filterByComponents(blueprint, components) {
Iif (!blueprint) {
return null;
}
var res = JSON.parse(JSON.stringify(blueprint));
var emptyGroups = [];
for (var i = 0; i < res.blueprint.host_groups.length; i++) {
res.blueprint.host_groups[i].components = res.blueprint.host_groups[i].components.filter(function (c) {
return components.contains(c.name);
});
if (res.blueprint.host_groups[i].components.length == 0) {
emptyGroups.push(res.blueprint.host_groups[i].name);
}
}
res.blueprint.host_groups = res.blueprint.host_groups.filter(function (g) {
return !emptyGroups.contains(g.name);
});
res.blueprint_cluster_binding.host_groups = res.blueprint_cluster_binding.host_groups.filter(function (g) {
return !emptyGroups.contains(g.name);
});
return res;
},
addComponentsToBlueprint: function addComponentsToBlueprint(blueprint, components) {
var res = JSON.parse(JSON.stringify(blueprint));
res.blueprint.host_groups.forEach(function (group) {
components.forEach(function (component) {
group.components.push({ name: component });
});
});
return res;
},
/**
* @method buildConfigsJSON - generates JSON according to blueprint format
* @param {Em.Array} stepConfigs - array of Ember Objects
* @returns {Object}
* Example:
* {
* "yarn-env": {
* "properties": {
* "content": "some value",
* "yarn_heapsize": "1024",
* "resourcemanager_heapsize": "1024",
* }
* },
* "yarn-log4j": {
* "properties": {
* "content": "some other value"
* }
* }
* }
*/
buildConfigsJSON: function buildConfigsJSON(stepConfigs) {
var configurations = {};
stepConfigs.forEach(function (stepConfig) {
var configs = stepConfig.get('configs');
Iif (!configs) return false;
configs.forEach(function (config) {
var type = App.config.getConfigTagFromFileName(config.get('filename'));
if (!configurations[type]) {
configurations[type] = { properties: {} };
}
configurations[type]['properties'][config.get('name')] = config.get('value');
});
});
return configurations;
},
/**
* @method generateHostGroups
* @param {Array} hostNames - list of all hostNames
* @returns {{blueprint: {host_groups: Array}, blueprint_cluster_binding: {host_groups: Array}}}
*/
generateHostGroups: function generateHostGroups(hostNames) {
var recommendations = {
blueprint: {
host_groups: []
},
blueprint_cluster_binding: {
host_groups: []
}
};
var hostsMap = this.getComponentForHosts();
for (var i = 0; i <= hostNames.length; i++) {
var host_group = {
name: "host-group-" + (i + 1),
components: []
};
var hcFiltered = hostsMap[hostNames[i]];
if (Em.isNone(hcFiltered)) continue;
for (var j = 0; j < hcFiltered.length; j++) {
host_group.components.push({
name: hcFiltered[j]
});
}
recommendations.blueprint.host_groups.push(host_group);
recommendations.blueprint_cluster_binding.host_groups.push({
name: "host-group-" + (i + 1),
hosts: [{
fqdn: hostNames[i]
}]
});
}
return recommendations;
},
/**
* Small helper method to update hostMap
* it perform update of object only
* if unique component per host is added
*
* @param {Object} hostMapObject
* @param {string[]} hostNames
* @param {string} componentName
* @returns {Object}
* @private
*/
_generateHostMap: function _generateHostMap(hostMapObject, hostNames, componentName) {
Em.assert('hostMapObject, hostNames, componentName should be defined', !!hostMapObject && !!hostNames && !!componentName);
if (!hostNames.length) return hostMapObject;
hostNames.forEach(function (hostName) {
if (!hostMapObject[hostName]) hostMapObject[hostName] = [];
if (!hostMapObject[hostName].contains(componentName)) hostMapObject[hostName].push(componentName);
});
return hostMapObject;
},
/**
* Clean up host groups from components that should be removed
*
* @param hostGroups
* @param serviceNames
*/
removeDeletedComponents: function removeDeletedComponents(hostGroups, serviceNames) {
var components = [];
App.StackService.find().forEach(function (s) {
if (serviceNames.contains(s.get('serviceName'))) {
components = components.concat(s.get('serviceComponents').mapProperty('componentName'));
}
});
hostGroups.blueprint.host_groups.forEach(function (hg) {
hg.components = hg.components.filter(function (c) {
return !components.contains(c.name);
});
});
return hostGroups;
},
/**
* collect all component names that are present on hosts
* @returns {object}
*/
getComponentForHosts: function getComponentForHosts() {
var hostsMap = {};
var componentModels = [App.ClientComponent, App.SlaveComponent, App.MasterComponent];
componentModels.forEach(function (_model) {
_model.find().forEach(function (c) {
hostsMap = this._generateHostMap(hostsMap, c.get('hostNames'), c.get('componentName'));
}, this);
}, this);
this.changeHostsMapForConfigActions(hostsMap);
return hostsMap;
},
/**
* Adds or removes the component name entry as saved in App.componentToBeAdded and App.componentToBeDeleted from the 'hostsMap'
* @param hostsMap {object}
* @private
* @method {changeHostsMapForConfigActions}
*/
changeHostsMapForConfigActions: function changeHostsMapForConfigActions(hostsMap) {
var componentToBeAdded = App.get('componentToBeAdded');
var componentToBeDeleted = App.get('componentToBeDeleted');
Iif (!App.isEmptyObject(componentToBeAdded)) {
hostsMap = this._generateHostMap(hostsMap, componentToBeAdded.get('hostNames'), componentToBeAdded.get('componentName'));
} else Iif (!App.isEmptyObject(componentToBeDeleted) && hostsMap[componentToBeDeleted.hostName]) {
var index = hostsMap[componentToBeDeleted.hostName].indexOf(componentToBeDeleted.componentName);
if (index > -1) {
hostsMap[componentToBeDeleted.hostName].splice(index, 1);
}
}
},
/**
* Returns host-group name by fqdn.
* Returns <code>null</code> when not found.
*
* @param {object} blueprint
* @param {string} fqdn
* @returns {string|null}
*/
getHostGroupByFqdn: function getHostGroupByFqdn(blueprint, fqdn) {
return Em.getWithDefault(blueprint || {}, 'blueprint_cluster_binding.host_groups', []).filter(function (i) {
return Em.getWithDefault(i, 'hosts', []).mapProperty('fqdn').contains(fqdn);
}).mapProperty('name')[0] || null;
},
/**
* Add component to specified host group.
*
* @param {object} blueprint
* @param {string} componentName
* @param {string} hostGroupName
* @return {object}
*/
addComponentToHostGroup: function addComponentToHostGroup(blueprint, componentName, hostGroupName) {
var hostGroup = blueprint.blueprint.host_groups.findProperty('name', hostGroupName);
Eif (hostGroup) {
if (!hostGroup.hasOwnProperty('components')) {
hostGroup.components = [];
}
if (!hostGroup.components.someProperty('name', componentName)) {
hostGroup.components.pushObject({ name: componentName });
}
}
return blueprint;
}
};
}); |