step5_controller.js 20 KB

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