blueprint.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. module.exports = {
  19. mergeBlueprints: function(masterBlueprint, slaveBlueprint) {
  20. var self = this;
  21. // Check edge cases
  22. if (!slaveBlueprint && !masterBlueprint) {
  23. throw 'slaveBlueprint or masterBlueprint should not be empty';
  24. } else if (slaveBlueprint && !masterBlueprint) {
  25. return slaveBlueprint;
  26. } else if (!slaveBlueprint && masterBlueprint) {
  27. return masterBlueprint;
  28. }
  29. // Work with main case (both blueprint are presented)
  30. var matches = self.matchGroups(masterBlueprint, slaveBlueprint);
  31. var res = {
  32. blueprint: { host_groups: [] },
  33. blueprint_cluster_binding: { host_groups: [] }
  34. };
  35. matches.forEach(function(match, i){
  36. var group_name = 'host-group-' + (i+1);
  37. var masterComponents = self.getComponentsFromBlueprintByGroupName(masterBlueprint, match.g1);
  38. var slaveComponents = self.getComponentsFromBlueprintByGroupName(slaveBlueprint, match.g2);
  39. res.blueprint.host_groups.push({
  40. name: group_name,
  41. components: masterComponents.concat(slaveComponents)
  42. });
  43. res.blueprint_cluster_binding.host_groups.push({
  44. name: group_name,
  45. hosts: self.getHostsFromBlueprintByGroupName(match.g1 ? masterBlueprint : slaveBlueprint, match.g1 ? match.g1 : match.g2)
  46. });
  47. });
  48. return res;
  49. },
  50. getHostsFromBlueprint: function(blueprint) {
  51. return blueprint.blueprint_cluster_binding.host_groups.mapProperty("hosts").reduce(function(prev, curr){ return prev.concat(curr); }, []).mapProperty("fqdn");
  52. },
  53. getHostsFromBlueprintByGroupName: function(blueprint, groupName) {
  54. if (groupName) {
  55. var group = blueprint.blueprint_cluster_binding.host_groups.find(function(g) {
  56. return g.name === groupName;
  57. });
  58. if (group) {
  59. return group.hosts;
  60. }
  61. }
  62. return [];
  63. },
  64. getComponentsFromBlueprintByGroupName: function(blueprint, groupName) {
  65. if (groupName) {
  66. var group = blueprint.blueprint.host_groups.find(function(g) {
  67. return g.name === groupName;
  68. });
  69. if (group) {
  70. return group.components;
  71. }
  72. }
  73. return [];
  74. },
  75. matchGroups: function(masterBlueprint, slaveBlueprint) {
  76. var self = this;
  77. var res = [];
  78. var groups1 = masterBlueprint.blueprint_cluster_binding.host_groups;
  79. var groups2 = slaveBlueprint.blueprint_cluster_binding.host_groups;
  80. var groups1_used = groups1.map(function() { return false; });
  81. var groups2_used = groups2.map(function() { return false; });
  82. self.matchGroupsWithLeft(groups1, groups2, groups1_used, groups2_used, res, false);
  83. self.matchGroupsWithLeft(groups2, groups1, groups2_used, groups1_used, res, true);
  84. return res;
  85. },
  86. matchGroupsWithLeft: function(groups1, groups2, groups1_used, groups2_used, res, inverse) {
  87. for (var i = 0; i < groups1.length; i++) {
  88. if (groups1_used[i]) {
  89. continue;
  90. }
  91. var group1 = groups1[i];
  92. groups1_used[i] = true;
  93. var group2 = groups2.find(function(g2, index) {
  94. if (group1.hosts.length != g2.hosts.length) {
  95. return false;
  96. }
  97. for (var gi = 0; gi < group1.hosts.length; gi++) {
  98. if (group1.hosts[gi].fqdn != g2.hosts[gi].fqdn) {
  99. return false;
  100. }
  101. }
  102. groups2_used[index] = true;
  103. return true;
  104. });
  105. var item = {};
  106. if (inverse) {
  107. item.g2 = group1.name;
  108. if (group2) {
  109. item.g1 = group2.name;
  110. }
  111. } else {
  112. item.g1 = group1.name;
  113. if (group2) {
  114. item.g2 = group2.name;
  115. }
  116. }
  117. res.push(item);
  118. }
  119. },
  120. /**
  121. * Remove from blueprint all components expect given components
  122. * @param blueprint
  123. * @param [string] components
  124. */
  125. filterByComponents: function(blueprint, components) {
  126. if (!blueprint) {
  127. return null;
  128. }
  129. var res = JSON.parse(JSON.stringify(blueprint));
  130. var emptyGroups = [];
  131. for (var i = 0; i < res.blueprint.host_groups.length; i++) {
  132. res.blueprint.host_groups[i].components = res.blueprint.host_groups[i].components.filter(function(c) {
  133. return components.contains(c.name);
  134. });
  135. if (res.blueprint.host_groups[i].components.length == 0) {
  136. emptyGroups.push(res.blueprint.host_groups[i].name);
  137. }
  138. }
  139. res.blueprint.host_groups = res.blueprint.host_groups.filter(function(g) {
  140. return !emptyGroups.contains(g.name);
  141. });
  142. res.blueprint_cluster_binding.host_groups = res.blueprint_cluster_binding.host_groups.filter(function(g) {
  143. return !emptyGroups.contains(g.name);
  144. });
  145. return res;
  146. },
  147. addComponentsToBlueprint: function(blueprint, components) {
  148. var res = JSON.parse(JSON.stringify(blueprint))
  149. res.blueprint.host_groups.forEach(function(group) {
  150. components.forEach(function(component) {
  151. group.components.push({ name: component });
  152. });
  153. });
  154. return res;
  155. },
  156. /**
  157. * @method buildConfisJSON - generet JSON according to blueprint format
  158. * @param {Em.Array} stepConfigs - array of Ember Objects
  159. * @param {Array} services
  160. * @returns {Object}
  161. * Example:
  162. * {
  163. * "yarn-env": {
  164. * "properties": {
  165. * "content": "some value",
  166. * "yarn_heapsize": "1024",
  167. * "resourcemanager_heapsize": "1024",
  168. * }
  169. * },
  170. * "yarn-log4j": {
  171. * "properties": {
  172. * "content": "some other value"
  173. * }
  174. * }
  175. * }
  176. */
  177. buildConfisJSON: function(services, stepConfigs) {
  178. var configurations = {};
  179. services.forEach(function(service) {
  180. var config = stepConfigs.findProperty('serviceName', service.get('serviceName'));
  181. if (config && service.get('configTypes')) {
  182. Object.keys(service.get('configTypes')).forEach(function(type) {
  183. if(!configurations[type]){
  184. configurations[type] = {
  185. properties: {}
  186. }
  187. }
  188. });
  189. config.get('configs').forEach(function(property){
  190. if (configurations[property.get('filename').replace('.xml','')]){
  191. configurations[property.get('filename').replace('.xml','')]['properties'][property.get('name')] = property.get('value');
  192. } else {
  193. console.warn(property.get('name') + " from " + property.get('filename') + " can't be validate");
  194. }
  195. });
  196. }
  197. });
  198. return configurations;
  199. },
  200. /**
  201. * @method generateHostGroups
  202. * @param {Array} hostNames - list of all hostNames
  203. * @param {Array} hostComponents - list of all hostComponents
  204. * @returns {{blueprint: {host_groups: Array}, blueprint_cluster_binding: {host_groups: Array}}}
  205. */
  206. generateHostGroups: function(hostNames, hostComponents) {
  207. var recommendations = {
  208. blueprint: {
  209. host_groups: []
  210. },
  211. blueprint_cluster_binding: {
  212. host_groups: []
  213. }
  214. };
  215. for (var i = 1; i <= hostNames.length; i++) {
  216. var host_group = {
  217. name: "host-group-" + i,
  218. components: []
  219. };
  220. var hcFiltered = hostComponents.filterProperty('hostName', hostNames[i-1]).mapProperty('componentName');
  221. for (var j = 0; j < hcFiltered.length; j++) {
  222. host_group.components.push({
  223. name: hcFiltered[j]
  224. });
  225. }
  226. recommendations.blueprint.host_groups.push(host_group);
  227. recommendations.blueprint_cluster_binding.host_groups.push({
  228. name: "host-group-" + i,
  229. hosts: [{
  230. fqdn: hostNames[i-1]
  231. }]
  232. });
  233. }
  234. return recommendations;
  235. }
  236. };