step5_controller.js 20 KB

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