step5_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. var App = require('app');
  19. //mock data
  20. //input
  21. App.selectedServices = [ 'HDFS', 'MapReduce', 'Ganglia', 'Nagios', 'HBase', 'Pig', 'Sqoop', 'Oozie', 'Hive', 'ZooKeeper'];
  22. App.hosts = [
  23. {
  24. host_name: 'host1',
  25. cluster_name: "test",
  26. total_mem: 7,
  27. cpu_count: 2
  28. },
  29. {
  30. host_name: 'host2',
  31. cluster_name: "test",
  32. total_mem: 4,
  33. cpu_count: 2
  34. },
  35. {
  36. host_name: 'host3',
  37. cluster_name: "test",
  38. total_mem: 8,
  39. cpu_count: 2
  40. },
  41. {
  42. host_name: 'host4',
  43. cluster_name: "test",
  44. total_mem: 8,
  45. cpu_count: 2
  46. },
  47. {
  48. host_name: 'host5',
  49. cluster_name: "test",
  50. total_mem: 8,
  51. cpu_count: 2
  52. }
  53. ];
  54. App.masterServices = [
  55. {
  56. component_name: "NameNode",
  57. selectedHost: 'host1',
  58. availableHosts: [] // filled dynAmically
  59. },
  60. {
  61. component_name: "ZooKeeper",
  62. selectedHost: 'host3',
  63. availableHosts: [] // filled dynAmically
  64. },
  65. {
  66. component_name: "JobTracker",
  67. selectedHost: 'host2',
  68. availableHosts: [] // filled dynAmically
  69. },
  70. {
  71. component_name: "HBase Master",
  72. selectedHost: 'host3',
  73. availableHosts: [] // filled dynAmically
  74. }
  75. ];
  76. //mapping format
  77. //masterHostMapping = [
  78. // {
  79. // host_name: 'host1',
  80. // masterServices: [{component_name:"NamedNode"}, {component_name:"Jobtracker"}]
  81. // },
  82. // {
  83. // host_name: 'host2',
  84. // masterServices: [{component_name:"NamedNode"}, {component_name:"Jobtracker"}]
  85. // }
  86. // ];
  87. //end - mock data
  88. App.InstallerStep5Controller = Em.Controller.extend({
  89. //properties
  90. name: "installerStep5Controller",
  91. hosts: [],
  92. selectedServices: [],
  93. selectedServicesMasters: [],
  94. masterHostMapping: function () {
  95. var mapping = [], mappingObject, self = this, mappedHosts, hostObj, hostInfo;
  96. //get the unique assigned hosts and find the master services assigned to them
  97. mappedHosts = this.get("selectedServicesMasters").mapProperty("selectedHost").uniq();
  98. mappedHosts.forEach(function (item) {
  99. hostObj = self.get("hosts").findProperty("host_name", item);
  100. hostInfo = " ( " + hostObj.get("total_mem") + "GB" + " " + hostObj.get("cpu_count") + "cores )";
  101. mappingObject = Ember.Object.create({
  102. host_name: item,
  103. hostInfo: hostInfo,
  104. masterServices: self.get("selectedServicesMasters").filterProperty("selectedHost", item)
  105. });
  106. mapping.pushObject(mappingObject);
  107. }, this);
  108. mapping.sort(this.sortHostsByName);
  109. return mapping;
  110. }.property("selectedServicesMasters.@each.selectedHost"),
  111. remainingHosts: function () {
  112. return (this.get("hosts.length") - this.get("masterHostMapping.length"));
  113. }.property("selectedServicesMasters.@each.selectedHost"),
  114. hasZookeeper: function () {
  115. return this.selectedServices.findProperty("service_name", "ZooKeeper");
  116. }.property("selectedServices"),
  117. //methods
  118. getAvailableHosts: function (componentName) {
  119. var assignableHosts = [],
  120. zookeeperHosts = null;
  121. if (componentName === "ZooKeeper") {
  122. zookeeperHosts = this.get("selectedServicesMasters").filterProperty("component_name", "ZooKeeper").mapProperty("selectedHost").uniq();
  123. this.get("hosts").forEach(function (item) {
  124. if (!(zookeeperHosts.contains(item.get("host_name")))) {
  125. assignableHosts.pushObject(item);
  126. }
  127. }, this);
  128. return assignableHosts;
  129. } else {
  130. return this.get("hosts");
  131. }
  132. },
  133. assignHostToMaster: function (masterService, selectedHost, zId) {
  134. if (selectedHost && masterService) {
  135. if ((masterService === "ZooKeeper") && zId) {
  136. this.get('selectedServicesMasters').findProperty("zId", zId).set("selectedHost", selectedHost);
  137. this.rebalanceZookeeperHosts();
  138. }
  139. else {
  140. this.get('selectedServicesMasters').findProperty("component_name", masterService).set("selectedHost", selectedHost);
  141. }
  142. }
  143. },
  144. addZookeepers: function () {
  145. /*
  146. *Logic: If ZooKeeper service is selected then there can be
  147. * minimum 1 ZooKeeper master in total, and
  148. * maximum 1 ZooKeeper on every host
  149. */
  150. var maxNumZooKeepers = this.get("hosts.length"),
  151. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("component_name", "ZooKeeper"),
  152. newZookeeper = null,
  153. zookeeperHosts = null,
  154. suggestedHost = null,
  155. i = 0,
  156. lastZoo = null;
  157. //work only if the Zookeeper service is selected in previous step
  158. if (!this.get("selectedServices").mapProperty("service_name").contains("ZooKeeper")) {
  159. return false;
  160. }
  161. if (currentZooKeepers.get("length") < maxNumZooKeepers) {
  162. currentZooKeepers.set("lastObject.showAddControl", false);
  163. if (currentZooKeepers.get("length") > 1) {
  164. currentZooKeepers.set("lastObject.showRemoveControl", true);
  165. }
  166. //create a new zookeeper based on an existing one
  167. newZookeeper = Ember.Object.create({});
  168. lastZoo = currentZooKeepers.get("lastObject");
  169. newZookeeper.set("component_name", lastZoo.get("component_name"));
  170. newZookeeper.set("selectedHost", lastZoo.get("selectedHost"));
  171. newZookeeper.set("availableHosts", this.getAvailableHosts("ZooKeeper"));
  172. if (currentZooKeepers.get("length") === (maxNumZooKeepers - 1)) {
  173. newZookeeper.set("showAddControl", false);
  174. } else {
  175. newZookeeper.set("showAddControl", true);
  176. }
  177. newZookeeper.set("showRemoveControl", true);
  178. //get recommended host for the new Zookeeper server
  179. zookeeperHosts = currentZooKeepers.mapProperty("selectedHost").uniq();
  180. for (i = 0; i < this.get("hosts.length"); i++) {
  181. if (!(zookeeperHosts.contains(this.get("hosts")[i].get("host_name")))) {
  182. suggestedHost = this.get("hosts")[i].get("host_name");
  183. break;
  184. }
  185. }
  186. newZookeeper.set("selectedHost", suggestedHost);
  187. newZookeeper.set("zId", (currentZooKeepers.get("lastObject.zId") + 1));
  188. this.get("selectedServicesMasters").pushObject(newZookeeper);
  189. this.rebalanceZookeeperHosts();
  190. return true;
  191. }
  192. return false;//if no more zookeepers can be added
  193. },
  194. removeZookeepers: function (zId) {
  195. var currentZooKeepers;
  196. //work only if the Zookeeper service is selected in previous step
  197. if (!this.get("selectedServices").mapProperty("service_name").contains("ZooKeeper")) {
  198. return false;
  199. }
  200. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("component_name", "ZooKeeper");
  201. if (currentZooKeepers.get("length") > 1) {
  202. this.get("selectedServicesMasters").removeAt(this.get("selectedServicesMasters").indexOf(this.get("selectedServicesMasters").findProperty("zId", zId)));
  203. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("component_name", "ZooKeeper");
  204. if (currentZooKeepers.get("length") < this.get("hosts.length")) {
  205. currentZooKeepers.set("lastObject.showAddControl", true);
  206. }
  207. this.rebalanceZookeeperHosts();
  208. return true;
  209. }
  210. return false;
  211. },
  212. rebalanceZookeeperHosts: function () {
  213. //for a zookeeper update the available hosts for the other zookeepers
  214. var currentZooKeepers = this.get("selectedServicesMasters").filterProperty("component_name", "ZooKeeper"),
  215. zooHosts = currentZooKeepers.mapProperty("selectedHost"),
  216. availableZooHosts = [],
  217. preparedAvailableHosts = null;
  218. //get all hosts available for zookeepers
  219. this.get("hosts").forEach(function (item) {
  220. if (!zooHosts.contains(item.get("host_name"))) {
  221. availableZooHosts.pushObject(item);
  222. }
  223. }, this);
  224. currentZooKeepers.forEach(function (item) {
  225. preparedAvailableHosts = availableZooHosts.slice(0);
  226. preparedAvailableHosts.pushObject(this.get("hosts").findProperty("host_name", item.get("selectedHost")))
  227. preparedAvailableHosts.sort(this.sortHostsByConfig, this);
  228. item.set("availableHosts", preparedAvailableHosts);
  229. }, this);
  230. },
  231. sortHostsByConfig: function (a, b) {
  232. //currently handling only total memory on the host
  233. if (a.total_mem < b.total_mem) {
  234. return 1;
  235. }
  236. else {
  237. return -1;
  238. }
  239. },
  240. sortHostsByName: function (a, b) {
  241. if (a.host_name > b.host_name) {
  242. return 1;
  243. }
  244. else {
  245. return -1;
  246. }
  247. },
  248. /*
  249. * Initialize the model data
  250. */
  251. init: function () {
  252. var zookeeperComponent = null, componentObj = null, hostObj = null;
  253. this._super();
  254. //wrap the model data into
  255. App.hosts.forEach(function (item) {
  256. hostObj = Ember.Object.create(item);
  257. hostObj.set("host_info", "" + hostObj.get("host_name") + " ( " + hostObj.get("total_mem") + "GB" + " " + hostObj.get("cpu_count") + "cores )");
  258. this.get("hosts").pushObject(hostObj);
  259. }, this);
  260. //sort the hosts
  261. this.get("hosts").sort(this.sortHostsByConfig);
  262. //todo: build masters from config instead
  263. App.masterServices.forEach(function (item) {
  264. //add the zookeeper component at the end if exists
  265. if (item.component_name === "ZooKeeper") {
  266. zookeeperComponent = Ember.Object.create(item);
  267. } else {
  268. componentObj = Ember.Object.create(item);
  269. componentObj.set("availableHosts", this.get("hosts").slice(0));
  270. this.get("selectedServicesMasters").pushObject(componentObj);
  271. }
  272. }, this);
  273. //while initialization of the controller there will be only 1 zookeeper server
  274. if (zookeeperComponent) {
  275. zookeeperComponent.set("showAddControl", true);
  276. zookeeperComponent.set("showRemoveControl", false);
  277. zookeeperComponent.set("zId", 1);
  278. zookeeperComponent.set("availableHosts", this.get("hosts").slice(0));
  279. this.get("selectedServicesMasters").pushObject(Ember.Object.create(zookeeperComponent));
  280. }
  281. App.selectedServices.forEach(function (item) {
  282. this.get("selectedServices").pushObject(Ember.Object.create({service_name: item}));
  283. }, this);
  284. }
  285. });