step5_controller.js 19 KB

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