step5_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. 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. var countZookeeper = masterComponents.filterProperty('display_name', 'ZooKeeper').length;
  140. masterComponents.forEach(function (item) {
  141. //add the zookeeper component at the end if exists
  142. console.log("TRACE: render master component name is: " + item.component_name);
  143. if (item.display_name === "ZooKeeper") {
  144. if (services.length) {
  145. services.forEach(function (_service) {
  146. this.get('selectedServicesMasters').pushObject(_service);
  147. }, this);
  148. }
  149. this.set('zId', parseInt(this.get('zId')) + 1);
  150. zookeeperComponent = Ember.Object.create(item);
  151. zookeeperComponent.set('zId', this.get('zId'));
  152. zookeeperComponent.set("showRemoveControl", countZookeeper > 1);
  153. zookeeperComponent.set("availableHosts", this.get("hosts").slice(0));
  154. this.get("selectedServicesMasters").pushObject(zookeeperComponent);
  155. } else {
  156. componentObj = Ember.Object.create(item);
  157. componentObj.set("availableHosts", this.get("hosts").slice(0));
  158. this.get("selectedServicesMasters").pushObject(componentObj);
  159. }
  160. }, this);
  161. },
  162. getKerberosServer:function (noOfHosts) {
  163. var hosts = this.get('hosts');
  164. if (noOfHosts === 1) {
  165. return hosts[0];
  166. } else if (noOfHosts < 3) {
  167. return hosts[1];
  168. } else if (noOfHosts <= 5) {
  169. return hosts[1];
  170. } else if (noOfHosts <= 30) {
  171. return hosts[3];
  172. } else {
  173. return hosts[5];
  174. }
  175. },
  176. getNameNode:function (noOfHosts) {
  177. var hosts = this.get('hosts');
  178. return hosts[0];
  179. },
  180. getSNameNode:function (noOfHosts) {
  181. var hosts = this.get('hosts');
  182. if (noOfHosts === 1) {
  183. return hosts[0];
  184. } else {
  185. return hosts[1];
  186. }
  187. },
  188. getJobTracker:function (noOfHosts) {
  189. var hosts = this.get('hosts');
  190. if (noOfHosts === 1) {
  191. return hosts[0];
  192. } else if (noOfHosts < 3) {
  193. return hosts[1];
  194. } else if (noOfHosts <= 5) {
  195. return hosts[1];
  196. } else if (noOfHosts <= 30) {
  197. return hosts[1];
  198. } else {
  199. return hosts[2];
  200. }
  201. },
  202. getHBaseMaster:function (noOfHosts) {
  203. var hosts = this.get('hosts');
  204. if (noOfHosts === 1) {
  205. return hosts[0];
  206. } else if (noOfHosts < 3) {
  207. return hosts[0];
  208. } else if (noOfHosts <= 5) {
  209. return hosts[0];
  210. } else if (noOfHosts <= 30) {
  211. return hosts[2];
  212. } else {
  213. return hosts[3];
  214. }
  215. },
  216. getOozieServer:function (noOfHosts) {
  217. var hosts = this.get('hosts');
  218. if (noOfHosts === 1) {
  219. return hosts[0];
  220. } else if (noOfHosts < 3) {
  221. return hosts[1];
  222. } else if (noOfHosts <= 5) {
  223. return hosts[1];
  224. } else if (noOfHosts <= 30) {
  225. return hosts[2];
  226. } else {
  227. return hosts[3];
  228. }
  229. },
  230. getOozieServer:function (noOfHosts) {
  231. var hosts = this.get('hosts');
  232. if (noOfHosts === 1) {
  233. return hosts[0];
  234. } else if (noOfHosts < 3) {
  235. return hosts[1];
  236. } else if (noOfHosts <= 5) {
  237. return hosts[1];
  238. } else if (noOfHosts <= 30) {
  239. return hosts[2];
  240. } else {
  241. return hosts[3];
  242. }
  243. },
  244. getHiveServer:function (noOfHosts) {
  245. var hosts = this.get('hosts');
  246. if (noOfHosts === 1) {
  247. return hosts[0];
  248. } else if (noOfHosts < 3) {
  249. return hosts[1];
  250. } else if (noOfHosts <= 5) {
  251. return hosts[1];
  252. } else if (noOfHosts <= 30) {
  253. return hosts[2];
  254. } else {
  255. return hosts[4];
  256. }
  257. },
  258. getTempletonServer:function (noOfHosts) {
  259. var hosts = this.get('hosts');
  260. if (noOfHosts === 1) {
  261. return hosts[0];
  262. } else if (noOfHosts < 3) {
  263. return hosts[1];
  264. } else if (noOfHosts <= 5) {
  265. return hosts[1];
  266. } else if (noOfHosts <= 30) {
  267. return hosts[2];
  268. } else {
  269. return hosts[4];
  270. }
  271. },
  272. getZooKeeperServer:function (noOfHosts) {
  273. var hosts = this.get('hosts');
  274. if (noOfHosts < 3) {
  275. return [hosts[0].host_name];
  276. } else {
  277. return [hosts[0].host_name, hosts[1].host_name, hosts[2].host_name];
  278. }
  279. },
  280. getGangliaServer:function (noOfHosts) {
  281. var hosts = this.get('hosts');
  282. var hostnames = [];
  283. var inc = 0;
  284. hosts.forEach(function (_hostname) {
  285. hostnames[inc] = _hostname.host_name;
  286. inc++;
  287. });
  288. var hostExcAmbari = hostnames.without(location.hostname);
  289. if (noOfHosts > 1) {
  290. return hostExcAmbari[0];
  291. } else {
  292. return hostnames[0];
  293. }
  294. },
  295. getNagiosServer:function (noOfHosts) {
  296. var hosts = this.get('hosts');
  297. var hostnames = [];
  298. var inc = 0;
  299. hosts.forEach(function (_hostname) {
  300. hostnames[inc] = _hostname.host_name;
  301. inc++;
  302. });
  303. var hostExcAmbari = hostnames.without(location.hostname);
  304. if (noOfHosts > 1) {
  305. return hostExcAmbari[0];
  306. } else {
  307. return hostnames[0];
  308. }
  309. },
  310. /**
  311. * Return hostName of masterNode for specified service
  312. * @param componentName
  313. * @return {*}
  314. */
  315. selectHost:function (componentName) {
  316. var noOfHosts = this.get('hosts').length;
  317. if (componentName === 'KERBEROS_SERVER') {
  318. return this.getKerberosServer(noOfHosts).host_name;
  319. } else if (componentName === 'NAMENODE') {
  320. return this.getNameNode(noOfHosts).host_name;
  321. } else if (componentName === 'SECONDARY_NAMENODE') {
  322. return this.getSNameNode(noOfHosts).host_name;
  323. } else if (componentName === 'JOBTRACKER') {
  324. return this.getJobTracker(noOfHosts).host_name;
  325. } else if (componentName === 'HBASE_MASTER') {
  326. return this.getHBaseMaster(noOfHosts).host_name;
  327. } else if (componentName === 'OOZIE_SERVER') {
  328. return this.getOozieServer(noOfHosts).host_name;
  329. } else if (componentName === 'HIVE_SERVER') {
  330. return this.getHiveServer(noOfHosts).host_name;
  331. } else if (componentName === 'TEMPLETON_SERVER') {
  332. return this.getTempletonServer(noOfHosts).host_name;
  333. } else if (componentName === 'ZOOKEEPER_SERVER') {
  334. return this.getZooKeeperServer(noOfHosts);
  335. } else if (componentName === 'GANGLIA_SERVER') {
  336. return this.getGangliaServer(noOfHosts);
  337. } else if (componentName === 'NAGIOS_SERVER') {
  338. return this.getNagiosServer(noOfHosts);
  339. }
  340. },
  341. masterHostMapping:function () {
  342. var mapping = [], mappingObject, self = this, mappedHosts, hostObj, hostInfo;
  343. //get the unique assigned hosts and find the master services assigned to them
  344. mappedHosts = this.get("selectedServicesMasters").mapProperty("selectedHost").uniq();
  345. mappedHosts.forEach(function (item) {
  346. hostObj = self.get("hosts").findProperty("host_name", item);
  347. console.log("Name of the host is: " + hostObj.host_name);
  348. mappingObject = Ember.Object.create({
  349. host_name:item,
  350. hostInfo:hostObj.host_info,
  351. masterServices:self.get("selectedServicesMasters").filterProperty("selectedHost", item)
  352. });
  353. mapping.pushObject(mappingObject);
  354. }, this);
  355. mapping.sort(this.sortHostsByName);
  356. return mapping;
  357. }.property("selectedServicesMasters.@each.selectedHost"),
  358. remainingHosts:function () {
  359. return (this.get("hosts.length") - this.get("masterHostMapping.length"));
  360. }.property("selectedServicesMasters.@each.selectedHost"),
  361. hasZookeeper:function () {
  362. return this.selectedServices.findProperty("service_name", "ZooKeeper");
  363. }.property("selectedServices"),
  364. //methods
  365. getAvailableHosts:function (componentName) {
  366. var assignableHosts = [],
  367. zookeeperHosts = null;
  368. if (componentName === "ZooKeeper") {
  369. zookeeperHosts = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper").mapProperty("selectedHost").uniq();
  370. this.get("hosts").forEach(function (item) {
  371. if (!(zookeeperHosts.contains(item.get("host_name")))) {
  372. assignableHosts.pushObject(item);
  373. }
  374. }, this);
  375. return assignableHosts;
  376. } else {
  377. return this.get("hosts");
  378. }
  379. },
  380. assignHostToMaster:function (masterService, selectedHost, zId) {
  381. if (selectedHost && masterService) {
  382. if ((masterService === "ZooKeeper") && zId) {
  383. this.get('selectedServicesMasters').findProperty("zId", zId).set("selectedHost", selectedHost);
  384. this.rebalanceZookeeperHosts();
  385. }
  386. else {
  387. this.get('selectedServicesMasters').findProperty("display_name", masterService).set("selectedHost", selectedHost);
  388. }
  389. }
  390. },
  391. lastZooKeeper:function () {
  392. var currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper");
  393. if (currentZooKeepers) {
  394. return currentZooKeepers.get("lastObject");
  395. }
  396. return null;
  397. },
  398. addZookeepers:function () {
  399. /*
  400. *Logic: If ZooKeeper service is selected then there can be
  401. * minimum 1 ZooKeeper master in total, and
  402. * maximum 1 ZooKeeper on every host
  403. */
  404. var maxNumZooKeepers = this.get("hosts.length"),
  405. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper"),
  406. newZookeeper = null,
  407. zookeeperHosts = null,
  408. suggestedHost = null,
  409. i = 0,
  410. lastZoo = null;
  411. console.log('hosts legth is: ' + maxNumZooKeepers);
  412. //work only if the Zookeeper service is selected in previous step
  413. if (!this.get("selectedServices").mapProperty("service_name").contains("ZOOKEEPER")) {
  414. console.log('ALERT: Zookeeper service was not selected');
  415. return false;
  416. }
  417. if (currentZooKeepers.get("length") < maxNumZooKeepers) {
  418. console.log('currentZookeeper length less than maximum. Its: ' + currentZooKeepers.get("length"))
  419. currentZooKeepers.set("lastObject.showAddControl", false);
  420. if (currentZooKeepers.get("length") >= 1) {
  421. currentZooKeepers.set("lastObject.showRemoveControl", true);
  422. }
  423. //create a new zookeeper based on an existing one
  424. newZookeeper = Ember.Object.create({});
  425. lastZoo = currentZooKeepers.get("lastObject");
  426. newZookeeper.set("display_name", lastZoo.get("display_name"));
  427. newZookeeper.set("selectedHost", lastZoo.get("selectedHost"));
  428. newZookeeper.set("availableHosts", this.getAvailableHosts("ZooKeeper"));
  429. if (currentZooKeepers.get("length") === (maxNumZooKeepers - 1)) {
  430. newZookeeper.set("showAddControl", false);
  431. } else {
  432. newZookeeper.set("showAddControl", true);
  433. }
  434. newZookeeper.set("showRemoveControl", true);
  435. //get recommended host for the new Zookeeper server
  436. zookeeperHosts = currentZooKeepers.mapProperty("selectedHost").uniq();
  437. for (i = 0; i < this.get("hosts.length"); i++) {
  438. if (!(zookeeperHosts.contains(this.get("hosts")[i].get("host_name")))) {
  439. suggestedHost = this.get("hosts")[i].get("host_name");
  440. break;
  441. }
  442. }
  443. newZookeeper.set("selectedHost", suggestedHost);
  444. newZookeeper.set("zId", (currentZooKeepers.get("lastObject.zId") + 1));
  445. this.set('zId', parseInt(this.get('zId')) + 1);
  446. this.get("selectedServicesMasters").pushObject(newZookeeper);
  447. this.rebalanceZookeeperHosts();
  448. return true;
  449. }
  450. return false;//if no more zookeepers can be added
  451. },
  452. removeZookeepers:function (zId) {
  453. var currentZooKeepers;
  454. //work only if the Zookeeper service is selected in previous step
  455. if (!this.get("selectedServices").mapProperty("service_name").contains("ZOOKEEPER")) {
  456. return false;
  457. }
  458. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper");
  459. if (currentZooKeepers.get("length") > 1) {
  460. this.get("selectedServicesMasters").removeAt(this.get("selectedServicesMasters").indexOf(this.get("selectedServicesMasters").findProperty("zId", zId)));
  461. currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper");
  462. if (currentZooKeepers.get("length") < this.get("hosts.length")) {
  463. currentZooKeepers.set("lastObject.showAddControl", true);
  464. }
  465. if (currentZooKeepers.get("length") === 1) {
  466. currentZooKeepers.set("lastObject.showRemoveControl", false);
  467. }
  468. this.set('zId', parseInt(this.get('zId')) - 1);
  469. this.rebalanceZookeeperHosts();
  470. return true;
  471. }
  472. return false;
  473. },
  474. rebalanceZookeeperHosts:function () {
  475. //for a zookeeper update the available hosts for the other zookeepers
  476. var currentZooKeepers = this.get("selectedServicesMasters").filterProperty("display_name", "ZooKeeper"),
  477. zooHosts = currentZooKeepers.mapProperty("selectedHost"),
  478. availableZooHosts = [],
  479. preparedAvailableHosts = null;
  480. //get all hosts available for zookeepers
  481. this.get("hosts").forEach(function (item) {
  482. if (!zooHosts.contains(item.get("host_name"))) {
  483. availableZooHosts.pushObject(item);
  484. }
  485. }, this);
  486. currentZooKeepers.forEach(function (item) {
  487. preparedAvailableHosts = availableZooHosts.slice(0);
  488. preparedAvailableHosts.pushObject(this.get("hosts").findProperty("host_name", item.get("selectedHost")))
  489. preparedAvailableHosts.sort(this.sortHostsByConfig, this);
  490. item.set("availableHosts", preparedAvailableHosts);
  491. }, this);
  492. },
  493. sortHostsByConfig:function (a, b) {
  494. //currently handling only total memory on the host
  495. if (a.memory < b.memory) {
  496. return 1;
  497. }
  498. else {
  499. return -1;
  500. }
  501. },
  502. sortHostsByName:function (a, b) {
  503. if (a.host_name > b.host_name) {
  504. return 1;
  505. }
  506. else {
  507. return -1;
  508. }
  509. }
  510. });