step5_controller.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. App.WizardStep5Controller = Em.Controller.extend({
  20. name: "wizardStep5Controller",
  21. hosts: [],
  22. selectedServices: [],
  23. selectedServicesMasters: [],
  24. zId: 0,
  25. components: require('data/service_components'),
  26. clearStep: function () {
  27. this.set('hosts', []);
  28. this.set('selectedServices', []);
  29. this.set('selectedServicesMasters', []);
  30. this.set('zId', 0);
  31. },
  32. loadStep: function () {
  33. console.log("WizardStep5Controller: Loading step5: Assign Masters");
  34. this.clearStep();
  35. this.renderHostInfo();
  36. this.renderComponents(this.loadComponents());
  37. },
  38. /**
  39. * Load active host list to <code>hosts</code> variable
  40. */
  41. renderHostInfo: function () {
  42. var hostInfo = this.get('content.hostsInfo');
  43. for (var index in hostInfo) {
  44. var _host = hostInfo[index];
  45. if (_host.bootStatus === 'success' || true) { // TODO: remove "true" after integrating with bootstrap
  46. var hostObj = Ember.Object.create({
  47. host_name: _host.name,
  48. cpu: _host.cpu,
  49. memory: _host.memory,
  50. host_info: "%@ ( %@GB %@cores )".fmt(_host.name, _host.memory, _host.cpu)
  51. });
  52. this.get("hosts").pushObject(hostObj);
  53. }
  54. }
  55. },
  56. /**
  57. * Load services info to appropriate variable and return masterComponentHosts
  58. * @return {Ember.Set}
  59. */
  60. loadComponents: function () {
  61. var services = this.get('content.services')
  62. .filterProperty('isSelected', true).mapProperty('serviceName');
  63. services.forEach(function (item) {
  64. this.get("selectedServices").pushObject(Ember.Object.create({service_name: item}));
  65. }, this);
  66. var masterHosts = this.get('content.masterComponentHosts');
  67. var components = new Ember.Set();
  68. if (!masterHosts) {
  69. var masterComponents = this.get('components').filterProperty('isMaster', true);
  70. for (var index in services) {
  71. var componentInfo = masterComponents.filterProperty('service_name', services[index]);
  72. componentInfo.forEach(function (_componentInfo) {
  73. console.log("TRACE: master component name is: " + _componentInfo.display_name);
  74. var componentObj = {};
  75. componentObj.component_name = _componentInfo.component_name;
  76. componentObj.display_name = _componentInfo.display_name;
  77. componentObj.selectedHost = this.selectHost(_componentInfo.component_name); // call the method that plays selectNode algorithm or fetches from server
  78. componentObj.availableHosts = [];
  79. components.add(componentObj);
  80. }, this);
  81. }
  82. } else {
  83. masterHosts.forEach(function (_masterComponentHost) {
  84. var componentObj = {};
  85. componentObj.component_name = _masterComponentHost.component_name;
  86. componentObj.display_name = _masterComponentHost.display_name;
  87. componentObj.selectedHost = _masterComponentHost.hostName;
  88. componentObj.availableHosts = [];
  89. components.add(componentObj);
  90. }, this);
  91. }
  92. return components;
  93. },
  94. /**
  95. * Put master components to <code>selectedServicesMasters</code>, which will be automatically rendered in template
  96. * @param masterComponents
  97. */
  98. renderComponents: function (masterComponents) {
  99. var zookeeperComponent = null, componentObj = null;
  100. var services = this.get('selectedServicesMasters').slice(0);
  101. if (services.length) {
  102. this.set('selectedServicesMasters', []);
  103. }
  104. masterComponents.forEach(function (item) {
  105. //add the zookeeper component at the end if exists
  106. console.log("TRACE: render master component name is: " + item.component_name);
  107. if (item.display_name === "ZooKeeper") {
  108. if (services.length) {
  109. services.forEach(function (_service) {
  110. this.get('selectedServicesMasters').pushObject(_service);
  111. }, this);
  112. }
  113. this.set('zId', parseInt(this.get('zId')) + 1);
  114. zookeeperComponent = Ember.Object.create(item);
  115. zookeeperComponent.set('zId', this.get('zId'));
  116. zookeeperComponent.set("showRemoveControl", true);
  117. zookeeperComponent.set("availableHosts", this.get("hosts").slice(0));
  118. this.get("selectedServicesMasters").pushObject(Ember.Object.create(zookeeperComponent));
  119. } else {
  120. componentObj = Ember.Object.create(item);
  121. componentObj.set("availableHosts", this.get("hosts").slice(0));
  122. this.get("selectedServicesMasters").pushObject(componentObj);
  123. }
  124. }, this);
  125. },
  126. getKerberosServer: function (noOfHosts) {
  127. var hosts = this.get('hosts');
  128. if (noOfHosts === 1) {
  129. return hosts[0];
  130. } else if (noOfHosts < 3) {
  131. return hosts[1];
  132. } else if (noOfHosts <= 5) {
  133. return hosts[1];
  134. } else if (noOfHosts <= 30) {
  135. return hosts[3];
  136. } else {
  137. return hosts[5];
  138. }
  139. },
  140. getNameNode: function (noOfHosts) {
  141. var hosts = this.get('hosts');
  142. return hosts[0];
  143. },
  144. getSNameNode: function (noOfHosts) {
  145. var hosts = this.get('hosts');
  146. if (noOfHosts === 1) {
  147. return hosts[0];
  148. } else {
  149. return hosts[1];
  150. }
  151. },
  152. getJobTracker: function (noOfHosts) {
  153. var hosts = this.get('hosts');
  154. if (noOfHosts === 1) {
  155. return hosts[0];
  156. } else if (noOfHosts < 3) {
  157. return hosts[1];
  158. } else if (noOfHosts <= 5) {
  159. return hosts[1];
  160. } else if (noOfHosts <= 30) {
  161. return hosts[1];
  162. } else {
  163. return hosts[2];
  164. }
  165. },
  166. getHBaseMaster: function (noOfHosts) {
  167. var hosts = this.get('hosts');
  168. if (noOfHosts === 1) {
  169. return hosts[0];
  170. } else if (noOfHosts < 3) {
  171. return hosts[0];
  172. } else if (noOfHosts <= 5) {
  173. return hosts[0];
  174. } else if (noOfHosts <= 30) {
  175. return hosts[2];
  176. } else {
  177. return hosts[3];
  178. }
  179. },
  180. getOozieServer: function (noOfHosts) {
  181. var hosts = this.get('hosts');
  182. if (noOfHosts === 1) {
  183. return hosts[0];
  184. } else if (noOfHosts < 3) {
  185. return hosts[1];
  186. } else if (noOfHosts <= 5) {
  187. return hosts[1];
  188. } else if (noOfHosts <= 30) {
  189. return hosts[2];
  190. } else {
  191. return hosts[3];
  192. }
  193. },
  194. getOozieServer: function (noOfHosts) {
  195. var hosts = this.get('hosts');
  196. if (noOfHosts === 1) {
  197. return hosts[0];
  198. } else if (noOfHosts < 3) {
  199. return hosts[1];
  200. } else if (noOfHosts <= 5) {
  201. return hosts[1];
  202. } else if (noOfHosts <= 30) {
  203. return hosts[2];
  204. } else {
  205. return hosts[3];
  206. }
  207. },
  208. getHiveServer: function (noOfHosts) {
  209. var hosts = this.get('hosts');
  210. if (noOfHosts === 1) {
  211. return hosts[0];
  212. } else if (noOfHosts < 3) {
  213. return hosts[1];
  214. } else if (noOfHosts <= 5) {
  215. return hosts[1];
  216. } else if (noOfHosts <= 30) {
  217. return hosts[2];
  218. } else {
  219. return hosts[4];
  220. }
  221. },
  222. getTempletonServer: function (noOfHosts) {
  223. var hosts = this.get('hosts');
  224. if (noOfHosts === 1) {
  225. return hosts[0];
  226. } else if (noOfHosts < 3) {
  227. return hosts[1];
  228. } else if (noOfHosts <= 5) {
  229. return hosts[1];
  230. } else if (noOfHosts <= 30) {
  231. return hosts[2];
  232. } else {
  233. return hosts[4];
  234. }
  235. },
  236. getZooKeeperServer: function (noOfHosts) {
  237. var hosts = this.get('hosts');
  238. if (noOfHosts < 3) {
  239. return [hosts[0].host_name];
  240. } else {
  241. return [hosts[0].host_name, hosts[1].host_name, hosts[2].host_name];
  242. }
  243. },
  244. getGangliaServer: function (noOfHosts) {
  245. var hosts = this.get('hosts');
  246. var hostnames = [];
  247. var inc = 0;
  248. hosts.forEach(function (_hostname) {
  249. hostnames[inc] = _hostname.host_name;
  250. inc++;
  251. });
  252. var hostExcAmbari = hostnames.without(location.hostname);
  253. if (hostExcAmbari !== null || hostExcAmbari !== undefined || hostExcAmbari.length !== 0) {
  254. return hostExcAmbari[0];
  255. } else {
  256. return hostnames[0];
  257. }
  258. },
  259. getNagiosServer: function (noOfHosts) {
  260. var hosts = this.get('hosts');
  261. var hostnames = [];
  262. var inc = 0;
  263. hosts.forEach(function (_hostname) {
  264. hostnames[inc] = _hostname.host_name;
  265. inc++;
  266. });
  267. var hostExcAmbari = hostnames.without(location.hostname);
  268. if (hostExcAmbari !== null || hostExcAmbari !== undefined || hostExcAmbari.length !== 0) {
  269. return hostExcAmbari[0];
  270. } else {
  271. return hostnames[0];
  272. }
  273. },
  274. /**
  275. * Return hostName of masterNode for specified service
  276. * @param componentName
  277. * @return {*}
  278. */
  279. selectHost: function (componentName) {
  280. var noOfHosts = this.get('hosts').length;
  281. if (componentName === 'KERBEROS_SERVER') {
  282. return this.getKerberosServer(noOfHosts).host_name;
  283. } else if (componentName === 'NAMENODE') {
  284. return this.getNameNode(noOfHosts).host_name;
  285. } else if (componentName === 'SNAMENODE') {
  286. return this.getSNameNode(noOfHosts).host_name;
  287. } else if (componentName === 'JOBTRACKER') {
  288. return this.getJobTracker(noOfHosts).host_name;
  289. } else if (componentName === 'HBASE_MASTER') {
  290. return this.getHBaseMaster(noOfHosts).host_name;
  291. } else if (componentName === 'OOZIE_SERVER') {
  292. return this.getOozieServer(noOfHosts).host_name;
  293. } else if (componentName === 'HIVE_SERVER') {
  294. return this.getHiveServer(noOfHosts).host_name;
  295. } else if (componentName === 'TEMPLETON_SERVER') {
  296. return this.getTempletonServer(noOfHosts).host_name;
  297. } else if (componentName === 'ZOOKEEPER_SERVER') {
  298. var zhosts = this.getZooKeeperServer(noOfHosts);
  299. var extraHosts = zhosts.slice(0, zhosts.length - 1);
  300. var zooKeeperHosts = new Ember.Set();
  301. extraHosts.forEach(function (_host) {
  302. var zooKeeperHost = {};
  303. zooKeeperHost.display_name = 'ZooKeeper';
  304. zooKeeperHost.component_name = 'ZOOKEEPER';
  305. zooKeeperHost.selectedHost = _host;
  306. zooKeeperHost.availableHosts = [];
  307. zooKeeperHosts.add(zooKeeperHost);
  308. });
  309. this.renderComponents(zooKeeperHosts);
  310. var lastHost = zhosts[zhosts.length - 1];
  311. return lastHost;
  312. } else if (componentName === 'GANGLIA_MONITOR_SERVER') {
  313. return this.getGangliaServer(noOfHosts);
  314. } else if (componentName === 'NAGIOS_SERVER') {
  315. return this.getNagiosServer(noOfHosts);
  316. }
  317. },
  318. masterHostMapping: function () {
  319. var mapping = [], mappingObject, self = this, mappedHosts, hostObj, hostInfo;
  320. //get the unique assigned hosts and find the master services assigned to them
  321. mappedHosts = this.get("selectedServicesMasters").mapProperty("selectedHost").uniq();
  322. mappedHosts.forEach(function (item) {
  323. hostObj = self.get("hosts").findProperty("host_name", item);
  324. console.log("Name of the host is: " + hostObj.host_name);
  325. hostInfo = " ( " + hostObj.get("memory") + "GB" + " " + hostObj.get("cpu") + "cores )";
  326. mappingObject = Ember.Object.create({
  327. host_name: item,
  328. hostInfo: hostInfo,
  329. masterServices: self.get("selectedServicesMasters").filterProperty("selectedHost", item)
  330. });
  331. mapping.pushObject(mappingObject);
  332. }, this);
  333. mapping.sort(this.sortHostsByName);
  334. return mapping;
  335. }.property("selectedServicesMasters.@each.selectedHost"),
  336. remainingHosts: function () {
  337. return (this.get("hosts.length") - this.get("masterHostMapping.length"));
  338. }.property("selectedServicesMasters.@each.selectedHost"),
  339. hasZookeeper: function () {
  340. return this.selectedServices.findProperty("service_name", "ZooKeeper");
  341. }.property("selectedServices"),
  342. //methods
  343. getAvailableHosts: function (componentName) {
  344. var assignableHosts = [],
  345. zookeeperHosts = null;
  346. if (componentName === "ZooKeeper") {
  347. zookeeperHosts = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper").mapProperty("selectedHost").uniq();
  348. this.get("hosts").forEach(function (item) {
  349. if (!(zookeeperHosts.contains(item.get("host_name")))) {
  350. assignableHosts.pushObject(item);
  351. }
  352. }, this);
  353. return assignableHosts;
  354. } else {
  355. return this.get("hosts");
  356. }
  357. },
  358. assignHostToMaster: function (masterService, selectedHost, zId) {
  359. if (selectedHost && masterService) {
  360. if ((masterService === "ZooKeeper") && zId) {
  361. this.get('selectedServicesMasters').findProperty("zId", zId).set("selectedHost", selectedHost);
  362. this.rebalanceZookeeperHosts();
  363. }
  364. else {
  365. this.get('selectedServicesMasters').findProperty("display_name", masterService).set("selectedHost", selectedHost);
  366. }
  367. }
  368. },
  369. lastZooKeeper: function () {
  370. var currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper");
  371. if (currentZooKeepers) {
  372. return currentZooKeepers.get("lastObject");
  373. }
  374. return null;
  375. },
  376. addZookeepers: function () {
  377. /*
  378. *Logic: If ZooKeeper service is selected then there can be
  379. * minimum 1 ZooKeeper master in total, and
  380. * maximum 1 ZooKeeper on every host
  381. */
  382. var maxNumZooKeepers = this.get("hosts.length"),
  383. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper"),
  384. newZookeeper = null,
  385. zookeeperHosts = null,
  386. suggestedHost = null,
  387. i = 0,
  388. lastZoo = null;
  389. console.log('hosts legth is: ' + maxNumZooKeepers);
  390. //work only if the Zookeeper service is selected in previous step
  391. if (!this.get("selectedServices").mapProperty("service_name").contains("ZOOKEEPER")) {
  392. console.log('ALERT: Zookeeper service was not selected');
  393. return false;
  394. }
  395. if (currentZooKeepers.get("length") < maxNumZooKeepers) {
  396. console.log('currentZookeeper length less than maximum. Its: ' + currentZooKeepers.get("length"))
  397. currentZooKeepers.set("lastObject.showAddControl", false);
  398. if (currentZooKeepers.get("length") >= 1) {
  399. currentZooKeepers.set("lastObject.showRemoveControl", true);
  400. }
  401. //create a new zookeeper based on an existing one
  402. newZookeeper = Ember.Object.create({});
  403. lastZoo = currentZooKeepers.get("lastObject");
  404. newZookeeper.set("display_name", lastZoo.get("display_name"));
  405. newZookeeper.set("selectedHost", lastZoo.get("selectedHost"));
  406. newZookeeper.set("availableHosts", this.getAvailableHosts("ZooKeeper"));
  407. if (currentZooKeepers.get("length") === (maxNumZooKeepers - 1)) {
  408. newZookeeper.set("showAddControl", false);
  409. } else {
  410. newZookeeper.set("showAddControl", true);
  411. }
  412. newZookeeper.set("showRemoveControl", true);
  413. //get recommended host for the new Zookeeper server
  414. zookeeperHosts = currentZooKeepers.mapProperty("selectedHost").uniq();
  415. for (i = 0; i < this.get("hosts.length"); i++) {
  416. if (!(zookeeperHosts.contains(this.get("hosts")[i].get("host_name")))) {
  417. suggestedHost = this.get("hosts")[i].get("host_name");
  418. break;
  419. }
  420. }
  421. newZookeeper.set("selectedHost", suggestedHost);
  422. newZookeeper.set("zId", (currentZooKeepers.get("lastObject.zId") + 1));
  423. this.set('zId', parseInt(this.get('zId')) + 1);
  424. this.get("selectedServicesMasters").pushObject(newZookeeper);
  425. this.rebalanceZookeeperHosts();
  426. return true;
  427. }
  428. return false;//if no more zookeepers can be added
  429. },
  430. removeZookeepers: function (zId) {
  431. var currentZooKeepers;
  432. //work only if the Zookeeper service is selected in previous step
  433. if (!this.get("selectedServices").mapProperty("service_name").contains("ZOOKEEPER")) {
  434. return false;
  435. }
  436. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper");
  437. if (currentZooKeepers.get("length") > 1) {
  438. this.get("selectedServicesMasters").removeAt(this.get("selectedServicesMasters").indexOf(this.get("selectedServicesMasters").findProperty("zId", zId)));
  439. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper");
  440. if (currentZooKeepers.get("length") < this.get("hosts.length")) {
  441. currentZooKeepers.set("lastObject.showAddControl", true);
  442. }
  443. if (currentZooKeepers.get("length") === 1) {
  444. currentZooKeepers.set("lastObject.showRemoveControl", false);
  445. }
  446. this.set('zId', parseInt(this.get('zId')) - 1);
  447. this.rebalanceZookeeperHosts();
  448. return true;
  449. }
  450. return false;
  451. },
  452. rebalanceZookeeperHosts: function () {
  453. //for a zookeeper update the available hosts for the other zookeepers
  454. var currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper"),
  455. zooHosts = currentZooKeepers.mapProperty("selectedHost"),
  456. availableZooHosts = [],
  457. preparedAvailableHosts = null;
  458. //get all hosts available for zookeepers
  459. this.get("hosts").forEach(function (item) {
  460. if (!zooHosts.contains(item.get("host_name"))) {
  461. availableZooHosts.pushObject(item);
  462. }
  463. }, this);
  464. currentZooKeepers.forEach(function (item) {
  465. preparedAvailableHosts = availableZooHosts.slice(0);
  466. preparedAvailableHosts.pushObject(this.get("hosts").findProperty("host_name", item.get("selectedHost")))
  467. preparedAvailableHosts.sort(this.sortHostsByConfig, this);
  468. item.set("availableHosts", preparedAvailableHosts);
  469. }, this);
  470. },
  471. sortHostsByConfig: function (a, b) {
  472. //currently handling only total memory on the host
  473. if (a.memory < b.memory) {
  474. return 1;
  475. }
  476. else {
  477. return -1;
  478. }
  479. },
  480. sortHostsByName: function (a, b) {
  481. if (a.host_name > b.host_name) {
  482. return 1;
  483. }
  484. else {
  485. return -1;
  486. }
  487. }
  488. });