step5_controller.js 19 KB

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