step5_controller.js 19 KB

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