step5_controller.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. var numberUtils = require('utils/number_utils');
  20. App.WizardStep5Controller = Em.Controller.extend({
  21. name: "wizardStep5Controller",
  22. /**
  23. * Step title
  24. * Custom if <code>App.ReassignMasterController</code> is used
  25. * @type {string}
  26. */
  27. title: function () {
  28. if (this.get('content.controllerName') == 'reassignMasterController') {
  29. return Em.I18n.t('installer.step5.reassign.header');
  30. }
  31. return Em.I18n.t('installer.step5.header');
  32. }.property('content.controllerName'),
  33. /**
  34. * Is ReassignWizard used
  35. * @type {bool}
  36. */
  37. isReassignWizard: function () {
  38. return this.get('content.controllerName') == 'reassignMasterController';
  39. }.property('content.controllerName'),
  40. /**
  41. * Is isHighAvailabilityWizard used
  42. * @type {bool}
  43. */
  44. isHighAvailabilityWizard: function () {
  45. return this.get('content.controllerName') == 'highAvailabilityWizardController';
  46. }.property('content.controllerName'),
  47. /**
  48. * Is AddServiceWizard used
  49. * @type {bool}
  50. */
  51. isAddServiceWizard: function () {
  52. return this.get('content.controllerName') == 'addServiceController';
  53. }.property('content.controllerName'),
  54. /**
  55. * Master components which could be assigned to multiple hosts
  56. * @type {string[]}
  57. */
  58. multipleComponents: function () {
  59. return App.get('components.multipleMasters');
  60. }.property('App.components.multipleMasters'),
  61. /**
  62. * Master components which could be assigned to multiple hosts
  63. * @type {string[]}
  64. */
  65. addableComponents: function () {
  66. return App.get('components.addableMasterInstallerWizard');
  67. }.property('App.components.addableMasterInstallerWizard'),
  68. /**
  69. * Define state for submit button
  70. * @type {bool}
  71. */
  72. submitDisabled: false,
  73. /**
  74. * Trigger for executing host names check for components
  75. * Should de "triggered" when host changed for some component and when new multiple component is added/removed
  76. * @type {bool}
  77. */
  78. hostNameCheckTrigger: false,
  79. /**
  80. * List of hosts
  81. * @type {Array}
  82. */
  83. hosts: [],
  84. /**
  85. * Name of multiple component which host name was changed last
  86. * @type {Object|null}
  87. */
  88. componentToRebalance: null,
  89. /**
  90. * Name of component which host was changed last
  91. * @type {string}
  92. */
  93. lastChangedComponent: null,
  94. /**
  95. * Flag for rebalance multiple components
  96. * @type {number}
  97. */
  98. rebalanceComponentHostsCounter: 0,
  99. /**
  100. * @type {Ember.Enumerable}
  101. */
  102. servicesMasters: [],
  103. /**
  104. * @type {Ember.Enumerable}
  105. */
  106. selectedServicesMasters: [],
  107. /**
  108. * Is data for current step loaded
  109. * @type {bool}
  110. */
  111. isLoaded: false,
  112. /**
  113. * List of host with assigned masters
  114. * Format:
  115. * <code>
  116. * [
  117. * {
  118. * host_name: '',
  119. * hostInfo: {},
  120. * masterServices: [],
  121. * masterServicesToDisplay: [] // used only in template
  122. * },
  123. * ....
  124. * ]
  125. * </code>
  126. * @type {Ember.Enumerable}
  127. */
  128. masterHostMapping: function () {
  129. var mapping = [], mappingObject, mappedHosts, hostObj;
  130. //get the unique assigned hosts and find the master services assigned to them
  131. mappedHosts = this.get("selectedServicesMasters").mapProperty("selectedHost").uniq();
  132. mappedHosts.forEach(function (item) {
  133. hostObj = this.get("hosts").findProperty("host_name", item);
  134. // User may input invalid host name (this is handled in hostname checker). Here we just skip it
  135. if (!hostObj) return;
  136. var masterServices = this.get("selectedServicesMasters").filterProperty("selectedHost", item),
  137. masterServicesToDisplay = [];
  138. masterServices.mapProperty('display_name').uniq().forEach(function (n) {
  139. masterServicesToDisplay.pushObject(masterServices.findProperty('display_name', n));
  140. });
  141. mappingObject = Em.Object.create({
  142. host_name: item,
  143. hostInfo: hostObj.host_info,
  144. masterServices: masterServices,
  145. masterServicesToDisplay: masterServicesToDisplay
  146. });
  147. mapping.pushObject(mappingObject);
  148. }, this);
  149. return mapping.sortProperty('host_name');
  150. }.property("selectedServicesMasters.@each.selectedHost", 'selectedServicesMasters.@each.isHostNameValid'),
  151. /**
  152. * Count of hosts without masters
  153. * @type {number}
  154. */
  155. remainingHosts: function () {
  156. if (this.get('content.controllerName') === 'installerController') {
  157. return 0;
  158. } else {
  159. return (this.get("hosts.length") - this.get("masterHostMapping.length"));
  160. }
  161. }.property('masterHostMapping.length', 'selectedServicesMasters.@each.selectedHost'),
  162. /**
  163. * Update submit button status
  164. * @metohd getIsSubmitDisabled
  165. */
  166. getIsSubmitDisabled: function () {
  167. var isSubmitDisabled = this.get('servicesMasters').someProperty('isHostNameValid', false);
  168. this.set('submitDisabled', isSubmitDisabled);
  169. return isSubmitDisabled;
  170. }.observes('servicesMasters.@each.selectedHost', 'servicesMasters.@each.isHostNameValid'),
  171. /**
  172. * Clear controller data (hosts, masters etc)
  173. * @method clearStep
  174. */
  175. clearStep: function () {
  176. this.set('hosts', []);
  177. this.set('selectedServicesMasters', []);
  178. this.set('servicesMasters', []);
  179. App.StackServiceComponent.find().forEach(function (stackComponent) {
  180. stackComponent.set('serviceComponentId', 1);
  181. }, this);
  182. },
  183. /**
  184. * Load controller data (hosts, host components etc)
  185. * @method loadStep
  186. */
  187. loadStep: function () {
  188. console.log("WizardStep5Controller: Loading step5: Assign Masters");
  189. this.clearStep();
  190. this.renderHostInfo();
  191. if (App.supports.serverRecommendValidate ) {
  192. this.loadComponentRecommendationsFromServer(this.loadStepCallback);
  193. } else {
  194. this.loadComponentsRecommendationsLocally(this.loadStepCallback);
  195. }
  196. },
  197. /**
  198. * Callback after load controller data (hosts, host components etc)
  199. * @method loadStepCallback
  200. */
  201. loadStepCallback: function(components, self) {
  202. self.renderComponents(components);
  203. self.get('addableComponents').forEach(function (componentName) {
  204. self.updateComponent(componentName);
  205. }, self);
  206. if (!self.get("selectedServicesMasters").filterProperty('isInstalled', false).length) {
  207. console.log('no master components to add');
  208. App.router.send('next');
  209. }
  210. },
  211. /**
  212. * Used to set showAddControl flag for installer wizard
  213. * @method updateComponent
  214. */
  215. updateComponent: function (componentName) {
  216. var component = this.last(componentName);
  217. if (!component) {
  218. return;
  219. }
  220. var services = App.StackService.find().filterProperty('isInstalled', true).mapProperty('serviceName');
  221. var currentService = componentName.split('_')[0];
  222. var showControl = !services.contains(currentService);
  223. if (showControl) {
  224. if (this.get("selectedServicesMasters").filterProperty("component_name", componentName).length < this.get("hosts.length") && !this.get('isReassignWizard') && !this.get('isHighAvailabilityWizard')) {
  225. component.set('showAddControl', true);
  226. } else {
  227. component.set('showRemoveControl', false);
  228. }
  229. }
  230. },
  231. /**
  232. * Load active host list to <code>hosts</code> variable
  233. * @method renderHostInfo
  234. */
  235. renderHostInfo: function () {
  236. var hostInfo = this.get('content.hosts');
  237. var result = [];
  238. for (var index in hostInfo) {
  239. var _host = hostInfo[index];
  240. if (_host.bootStatus === 'REGISTERED') {
  241. result.push(Em.Object.create({
  242. host_name: _host.name,
  243. cpu: _host.cpu,
  244. memory: _host.memory,
  245. disk_info: _host.disk_info,
  246. host_info: Em.I18n.t('installer.step5.hostInfo').fmt(_host.name, numberUtils.bytesToSize(_host.memory, 1, 'parseFloat', 1024), _host.cpu)
  247. }));
  248. }
  249. }
  250. this.set("hosts", result);
  251. this.sortHosts(this.get('hosts'));
  252. this.set('isLoaded', true);
  253. },
  254. /**
  255. * Sort list of host-objects by properties (memory - desc, cpu - desc, hostname - asc)
  256. * @param {object[]} hosts
  257. */
  258. sortHosts: function (hosts) {
  259. hosts.sort(function (a, b) {
  260. if (a.get('memory') == b.get('memory')) {
  261. if (a.get('cpu') == b.get('cpu')) {
  262. return a.get('host_name').localeCompare(b.get('host_name')); // hostname asc
  263. }
  264. return b.get('cpu') - a.get('cpu'); // cores desc
  265. }
  266. return b.get('memory') - a.get('memory'); // ram desc
  267. });
  268. },
  269. /**
  270. * Get recommendations info from API
  271. * @return {undefined}
  272. */
  273. loadComponentRecommendationsFromServer: function(callback) {
  274. var self = this;
  275. if (App.router.get('installerController.recommendations') !== undefined) {
  276. // Don't do AJAX call if recommendations has been already received
  277. // But if user returns to previous step (selecting services), stored recommendations will be cleared in routers' next handler and AJAX call will be made again
  278. callback(self.createComponentInstallationObjects(), self);
  279. } else {
  280. var selectedServices = App.StackService.find().filterProperty('isSelected').mapProperty('serviceName');
  281. var installedServices = App.StackService.find().filterProperty('isInstalled').mapProperty('serviceName');
  282. var services = installedServices.concat(selectedServices).uniq();
  283. var hostNames = self.get('hosts').mapProperty('host_name');
  284. return App.ajax.send({
  285. name: 'wizard.step5.recommendations',
  286. sender: self,
  287. data: {
  288. stackVersionUrl: App.get('stackVersionURL'),
  289. hosts: hostNames,
  290. services: services
  291. },
  292. success: 'loadRecommendationsSuccessCallback'
  293. }).
  294. retry({
  295. times: App.maxRetries,
  296. timeout: App.timeout
  297. }).
  298. then(function () {
  299. callback(self.createComponentInstallationObjects(), self);
  300. },
  301. function () {
  302. App.showReloadPopup();
  303. console.log('Load recommendations failed');
  304. }
  305. );
  306. }
  307. },
  308. /**
  309. * Create components for displaying component-host comboboxes in UI assign dialog
  310. * expects installerController.recommendations will be filled with recommendations API call result
  311. * @return {Object[]}
  312. */
  313. createComponentInstallationObjects: function() {
  314. var self = this;
  315. var masterComponents = [];
  316. if (self.get('isAddServiceWizard')) {
  317. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnAddServiceAssignMasterPage');
  318. } else {
  319. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnInstallerAssignMasterPage');
  320. }
  321. var masterHosts = self.get('content.masterComponentHosts'); //saved to local storage info
  322. var selectedNotInstalledServices = self.get('content.services').filterProperty('isSelected').filterProperty('isInstalled', false).mapProperty('serviceName');
  323. var recommendations = App.router.get('installerController.recommendations');
  324. var resultComponents = [];
  325. var multipleComponentHasBeenAdded = {};
  326. recommendations.blueprint.host_groups.forEach(function(host_group) {
  327. var hosts = recommendations.blueprint_cluster_binding.host_groups.findProperty('name', host_group.name).hosts;
  328. hosts.forEach(function(host) {
  329. host_group.components.forEach(function(component) {
  330. var willBeAdded = true;
  331. var fullComponent = masterComponents.findProperty('componentName', component.name);
  332. // If it's master component which should be shown
  333. if (fullComponent) {
  334. // If service is already installed and not being added as a new service then render on UI only those master components
  335. // that have already installed hostComponents.
  336. // NOTE: On upgrade there might be a prior installed service with non-installed newly introduced serviceComponent
  337. var isNotSelectedService = !selectedNotInstalledServices.contains(fullComponent.get('serviceName'));
  338. if (isNotSelectedService) {
  339. willBeAdded = App.HostComponent.find().someProperty('componentName', component.name);
  340. }
  341. if (willBeAdded) {
  342. var savedComponents = masterHosts.filterProperty('component', component.name);
  343. if (self.get('multipleComponents').contains(component.name) && savedComponents.length > 0) {
  344. if (!multipleComponentHasBeenAdded[component.name]) {
  345. multipleComponentHasBeenAdded[component.name] = true;
  346. savedComponents.forEach(function(saved) {
  347. resultComponents.push(self.createComponentInstallationObject(fullComponent, host.fqdn, saved));
  348. });
  349. }
  350. } else {
  351. var savedComponent = masterHosts.findProperty('component', component.name);
  352. resultComponents.push(self.createComponentInstallationObject(fullComponent, host.fqdn, savedComponent));
  353. }
  354. }
  355. }
  356. });
  357. });
  358. });
  359. return resultComponents;
  360. },
  361. /**
  362. * Create component for displaying component-host comboboxes in UI assign dialog
  363. * @param fullComponent - full component description
  364. * @param hostName - host fqdn where component will be installed
  365. * @param savedComponent - the same object which function returns but created before
  366. * @return {Object}
  367. */
  368. createComponentInstallationObject: function(fullComponent, hostName, savedComponent) {
  369. var componentName = fullComponent.get('componentName');
  370. var componentObj = {};
  371. componentObj.component_name = componentName;
  372. componentObj.display_name = fullComponent.get('displayName');
  373. componentObj.serviceId = fullComponent.get('serviceName');
  374. componentObj.isServiceCoHost = App.StackServiceComponent.find().findProperty('componentName', componentName).get('isCoHostedComponent') && !this.get('isReassignWizard');
  375. if (savedComponent) {
  376. componentObj.selectedHost = savedComponent.hostName;
  377. componentObj.isInstalled = savedComponent.isInstalled;
  378. } else {
  379. componentObj.selectedHost = hostName;
  380. componentObj.isInstalled = false;
  381. }
  382. return componentObj;
  383. },
  384. /**
  385. * Success-callback for recommendations request
  386. * @param {object} data
  387. * @method loadRecommendationsSuccessCallback
  388. */
  389. loadRecommendationsSuccessCallback: function (data) {
  390. App.router.set('installerController.recommendations', data.resources[0].recommendations);
  391. },
  392. /**
  393. * Load services info to appropriate variable and return masterComponentHosts
  394. * @return {Object[]}
  395. */
  396. loadComponentsRecommendationsLocally: function (callback) {
  397. var selectedServices = App.StackService.find().filterProperty('isSelected').mapProperty('serviceName');
  398. var installedServices = App.StackService.find().filterProperty('isInstalled').mapProperty('serviceName');
  399. var services = installedServices.concat(selectedServices).uniq();
  400. var selectedNotInstalledServices = this.get('content.services').filterProperty('isSelected').filterProperty('isInstalled', false).mapProperty('serviceName');
  401. var masterComponents = [];
  402. //get full list from mock data
  403. if (this.get('isAddServiceWizard')) {
  404. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnAddServiceAssignMasterPage');
  405. } else {
  406. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnInstallerAssignMasterPage');
  407. }
  408. var masterHosts = this.get('content.masterComponentHosts'); //saved to local storage info
  409. var resultComponents = [];
  410. for (var index = 0; index < services.length; index++) {
  411. var componentInfo = masterComponents.filterProperty('serviceName', services[index]);
  412. // If service is already installed and not being added as a new service then render on UI only those master components
  413. // that have already installed hostComponents.
  414. // NOTE: On upgrade there might be a prior installed service with non-installed newly introduced serviceComponent
  415. var isNotSelectedService = !selectedNotInstalledServices.contains(services[index]);
  416. if (isNotSelectedService) {
  417. componentInfo = componentInfo.filter(function (_component) {
  418. return App.HostComponent.find().someProperty('componentName',_component.get('componentName'));
  419. });
  420. }
  421. componentInfo.forEach(function (_componentInfo) {
  422. if (this.get('multipleComponents').contains(_componentInfo.get('componentName'))) {
  423. var savedComponents = masterHosts.filterProperty('component', _componentInfo.get('componentName'));
  424. if (savedComponents.length) {
  425. savedComponents.forEach(function (item) {
  426. var multipleMasterHost = {};
  427. multipleMasterHost.component_name = _componentInfo.get('componentName');
  428. multipleMasterHost.display_name = _componentInfo.get('displayName');
  429. multipleMasterHost.selectedHost = item.hostName;
  430. multipleMasterHost.serviceId = services[index];
  431. multipleMasterHost.isInstalled = item.isInstalled;
  432. multipleMasterHost.isServiceCoHost = false;
  433. resultComponents.push(multipleMasterHost);
  434. })
  435. } else {
  436. var multipleMasterHosts = this.selectHostLocally(_componentInfo.get('componentName'));
  437. multipleMasterHosts.forEach(function (_host) {
  438. var multipleMasterHost = {};
  439. multipleMasterHost.component_name = _componentInfo.get('componentName');
  440. multipleMasterHost.display_name = _componentInfo.get('displayName');
  441. multipleMasterHost.selectedHost = _host;
  442. multipleMasterHost.serviceId = services[index];
  443. multipleMasterHost.isInstalled = false;
  444. multipleMasterHost.isServiceCoHost = false;
  445. resultComponents.push(multipleMasterHost);
  446. });
  447. }
  448. } else {
  449. var savedComponent = masterHosts.findProperty('component', _componentInfo.get('componentName'));
  450. var componentObj = {};
  451. componentObj.component_name = _componentInfo.get('componentName');
  452. componentObj.display_name = _componentInfo.get('displayName');
  453. componentObj.selectedHost = savedComponent ? savedComponent.hostName : this.selectHostLocally(_componentInfo.get('componentName')); // call the method that plays selectNode algorithm or fetches from server
  454. componentObj.isInstalled = savedComponent ? savedComponent.isInstalled : false;
  455. componentObj.serviceId = services[index];
  456. componentObj.isServiceCoHost = App.StackServiceComponent.find().findProperty('componentName', _componentInfo.get('componentName')).get('isCoHostedComponent') && !this.get('isReassignWizard');
  457. resultComponents.push(componentObj);
  458. }
  459. }, this);
  460. }
  461. callback(resultComponents, this);
  462. },
  463. /**
  464. * @param {string} componentName
  465. * @returns {bool}
  466. * @private
  467. * @method _isHiveCoHost
  468. */
  469. _isHiveCoHost: function (componentName) {
  470. return ['HIVE_METASTORE', 'WEBHCAT_SERVER'].contains(componentName) && !this.get('isReassignWizard');
  471. },
  472. /**
  473. * Put master components to <code>selectedServicesMasters</code>, which will be automatically rendered in template
  474. * @param {Ember.Enumerable} masterComponents
  475. * @method renderComponents
  476. */
  477. renderComponents: function (masterComponents) {
  478. var installedServices = App.StackService.find().filterProperty('isSelected').filterProperty('isInstalled', false).mapProperty('serviceName'); //list of shown services
  479. var result = [];
  480. var serviceComponentId, previousComponentName;
  481. masterComponents.forEach(function (item) {
  482. var serviceComponent = App.StackServiceComponent.find().findProperty('componentName', item.component_name);
  483. var showRemoveControl = installedServices.contains(serviceComponent.get('stackService.serviceName')) &&
  484. (masterComponents.filterProperty('component_name', item.component_name).length > 1);
  485. var componentObj = Em.Object.create(item);
  486. console.log("TRACE: render master component name is: " + item.component_name);
  487. var masterComponent = App.StackServiceComponent.find().findProperty('componentName', item.component_name);
  488. if (masterComponent.get('isMasterWithMultipleInstances')) {
  489. previousComponentName = item.component_name;
  490. componentObj.set('serviceComponentId', result.filterProperty('component_name', item.component_name).length + 1);
  491. componentObj.set("showRemoveControl", showRemoveControl);
  492. }
  493. componentObj.set('isHostNameValid', true);
  494. result.push(componentObj);
  495. }, this);
  496. this.set("selectedServicesMasters", result);
  497. if (this.get('isReassignWizard')) {
  498. var components = result.filterProperty('component_name', this.get('content.reassign.component_name'));
  499. components.setEach('isInstalled', false);
  500. this.set('servicesMasters', components);
  501. } else {
  502. this.set('servicesMasters', result);
  503. }
  504. },
  505. /**
  506. * Update dependent co-hosted components according to the change in the component host
  507. * @method updateCoHosts
  508. */
  509. updateCoHosts: function () {
  510. var components = App.StackServiceComponent.find().filterProperty('isOtherComponentCoHosted');
  511. var selectedServicesMasters = this.get('selectedServicesMasters');
  512. components.forEach(function (component) {
  513. var componentName = component.get('componentName');
  514. var hostComponent = selectedServicesMasters.findProperty('component_name', componentName);
  515. var dependentCoHosts = component.get('coHostedComponents');
  516. dependentCoHosts.forEach(function (coHostedComponent) {
  517. var dependentHostComponent = selectedServicesMasters.findProperty('component_name', coHostedComponent);
  518. if (hostComponent && dependentHostComponent) dependentHostComponent.set('selectedHost', hostComponent.get('selectedHost'));
  519. }, this);
  520. }, this);
  521. }.observes('selectedServicesMasters.@each.selectedHost'),
  522. /**
  523. * select and return host for component by scheme
  524. * Scheme is an object that has keys which compared to number of hosts,
  525. * if key more that number of hosts, then return value of that key.
  526. * Value is index of host in hosts array.
  527. *
  528. * @param {object} componentName
  529. * @param {object} hosts
  530. * @return {string}
  531. * @method getHostForComponent
  532. */
  533. getHostForComponent: function (componentName, hosts) {
  534. var component = App.StackServiceComponent.find().findProperty('componentName', componentName);
  535. if (component) {
  536. var selectionScheme = App.StackServiceComponent.find().findProperty('componentName', componentName).get('selectionSchemeForMasterComponent');
  537. } else {
  538. return hosts[0];
  539. }
  540. if (hosts.length === 1 || $.isEmptyObject(selectionScheme)) {
  541. return hosts[0];
  542. } else {
  543. for (var i in selectionScheme) {
  544. if (window.isFinite(i)) {
  545. if (hosts.length < window.parseInt(i)) {
  546. return hosts[selectionScheme[i]];
  547. }
  548. }
  549. }
  550. return hosts[selectionScheme['else']]
  551. }
  552. },
  553. /**
  554. * Get list of host names for master component with multiple instances
  555. * @param {Object} component
  556. * @param {Object} hosts
  557. * @returns {string[]}
  558. * @method getHostsForComponent
  559. */
  560. getHostsForComponent: function (component, hosts) {
  561. var defaultNoOfMasterHosts = component.get('defaultNoOfMasterHosts');
  562. var masterHosts = [];
  563. if (hosts.length < defaultNoOfMasterHosts) {
  564. defaultNoOfMasterHosts = hosts.length;
  565. }
  566. for (var index = 0; index < defaultNoOfMasterHosts; index++) {
  567. masterHosts.push(hosts[index]);
  568. }
  569. return masterHosts;
  570. },
  571. /**
  572. * Return hostName of masterNode for specified service
  573. * @param componentName
  574. * @return {string|string[]}
  575. * @method selectHostLocally
  576. */
  577. selectHostLocally: function (componentName) {
  578. var component = App.StackServiceComponent.find().findProperty('componentName', componentName);
  579. var hostNames = this.get('hosts').mapProperty('host_name');
  580. if (hostNames.length > 1 && App.StackServiceComponent.find().filterProperty('isNotPreferableOnAmbariServerHost').mapProperty('componentName').contains(componentName)) {
  581. hostNames = this.get('hosts').mapProperty('host_name').filter(function (item) {
  582. return item !== location.hostname;
  583. }, this);
  584. }
  585. if (this.get('multipleComponents').contains(componentName)) {
  586. if (component.get('defaultNoOfMasterHosts') > 1) {
  587. return this.getHostsForComponent(component, hostNames);
  588. } else {
  589. return [this.getHostForComponent(componentName, hostNames)];
  590. }
  591. } else {
  592. return this.getHostForComponent(componentName, hostNames);
  593. }
  594. },
  595. /**
  596. * On change callback for inputs
  597. * @param {string} componentName
  598. * @param {string} selectedHost
  599. * @param {number} serviceComponentId
  600. * @method assignHostToMaster
  601. */
  602. assignHostToMaster: function (componentName, selectedHost, serviceComponentId) {
  603. var flag = this.isHostNameValid(componentName, selectedHost);
  604. this.updateIsHostNameValidFlag(componentName, serviceComponentId, flag);
  605. if (serviceComponentId) {
  606. this.get('selectedServicesMasters').filterProperty('component_name', componentName).findProperty("serviceComponentId", serviceComponentId).set("selectedHost", selectedHost);
  607. }
  608. else {
  609. this.get('selectedServicesMasters').findProperty("component_name", componentName).set("selectedHost", selectedHost);
  610. }
  611. },
  612. /**
  613. * Determines if hostName is valid for component:
  614. * <ul>
  615. * <li>host name shouldn't be empty</li>
  616. * <li>host should exist</li>
  617. * <li>host should have only one component with <code>componentName</code></li>
  618. * </ul>
  619. * @param {string} componentName
  620. * @param {string} selectedHost
  621. * @returns {boolean} true - valid, false - invalid
  622. * @method isHostNameValid
  623. */
  624. isHostNameValid: function (componentName, selectedHost) {
  625. return (selectedHost.trim() !== '') &&
  626. this.get('hosts').mapProperty('host_name').contains(selectedHost) &&
  627. (this.get('selectedServicesMasters').
  628. filterProperty('component_name', componentName).
  629. mapProperty('selectedHost').
  630. filter(function (h) {
  631. return h === selectedHost;
  632. }).length <= 1);
  633. },
  634. /**
  635. * Update <code>isHostNameValid</code> property with <code>flag</code> value
  636. * for component with name <code>componentName</code> and
  637. * <code>serviceComponentId</code>-property equal to <code>serviceComponentId</code>-parameter value
  638. * @param {string} componentName
  639. * @param {number} serviceComponentId
  640. * @param {bool} flag
  641. * @method updateIsHostNameValidFlag
  642. */
  643. updateIsHostNameValidFlag: function (componentName, serviceComponentId, flag) {
  644. if (componentName) {
  645. if (serviceComponentId) {
  646. this.get('selectedServicesMasters').filterProperty('component_name', componentName).findProperty("serviceComponentId", serviceComponentId).set("isHostNameValid", flag);
  647. } else {
  648. this.get('selectedServicesMasters').findProperty("component_name", componentName).set("isHostNameValid", flag);
  649. }
  650. }
  651. },
  652. /**
  653. * Returns last component of selected type
  654. * @param {string} componentName
  655. * @return {Em.Object|null}
  656. * @method last
  657. */
  658. last: function (componentName) {
  659. return this.get("selectedServicesMasters").filterProperty("component_name", componentName).get("lastObject");
  660. },
  661. /**
  662. * Add new component to ZooKeeper Server and Hbase master
  663. * @param {string} componentName
  664. * @return {bool} true - added, false - not added
  665. * @method addComponent
  666. */
  667. addComponent: function (componentName) {
  668. /*
  669. * Logic: If ZooKeeper or Hbase service is selected then there can be
  670. * minimum 1 ZooKeeper or Hbase master in total, and
  671. * maximum 1 ZooKeeper or Hbase on every host
  672. */
  673. var maxNumMasters = this.get("hosts.length"),
  674. currentMasters = this.get("selectedServicesMasters").filterProperty("component_name", componentName),
  675. newMaster = null,
  676. masterHosts = null,
  677. suggestedHost = null,
  678. i = 0,
  679. lastMaster = null;
  680. if (!currentMasters.length) {
  681. console.log('ALERT: Zookeeper service was not selected');
  682. return false;
  683. }
  684. if (currentMasters.get("length") < maxNumMasters) {
  685. currentMasters.set("lastObject.showAddControl", false);
  686. currentMasters.set("lastObject.showRemoveControl", true);
  687. //create a new master component host based on an existing one
  688. newMaster = Em.Object.create({});
  689. lastMaster = currentMasters.get("lastObject");
  690. newMaster.set("display_name", lastMaster.get("display_name"));
  691. newMaster.set("component_name", lastMaster.get("component_name"));
  692. newMaster.set("selectedHost", lastMaster.get("selectedHost"));
  693. newMaster.set("isInstalled", false);
  694. if (currentMasters.get("length") === (maxNumMasters - 1)) {
  695. newMaster.set("showAddControl", false);
  696. } else {
  697. newMaster.set("showAddControl", true);
  698. }
  699. newMaster.set("showRemoveControl", true);
  700. //get recommended host for the new Zookeeper server
  701. masterHosts = currentMasters.mapProperty("selectedHost").uniq();
  702. for (i = 0; i < this.get("hosts.length"); i++) {
  703. if (!(masterHosts.contains(this.get("hosts")[i].get("host_name")))) {
  704. suggestedHost = this.get("hosts")[i].get("host_name");
  705. break;
  706. }
  707. }
  708. newMaster.set("selectedHost", suggestedHost);
  709. newMaster.set("serviceComponentId", (currentMasters.get("lastObject.serviceComponentId") + 1));
  710. this.get("selectedServicesMasters").insertAt(this.get("selectedServicesMasters").indexOf(lastMaster) + 1, newMaster);
  711. this.set('componentToRebalance', componentName);
  712. this.incrementProperty('rebalanceComponentHostsCounter');
  713. this.toggleProperty('hostNameCheckTrigger');
  714. return true;
  715. }
  716. return false;//if no more zookeepers can be added
  717. },
  718. /**
  719. * Remove component from ZooKeeper server or Hbase Master
  720. * @param {string} componentName
  721. * @param {number} serviceComponentId
  722. * @return {bool} true - removed, false - no
  723. * @method removeComponent
  724. */
  725. removeComponent: function (componentName, serviceComponentId) {
  726. var currentMasters = this.get("selectedServicesMasters").filterProperty("component_name", componentName);
  727. //work only if the multiple master service is selected in previous step
  728. if (currentMasters.length <= 1) {
  729. return false;
  730. }
  731. this.get("selectedServicesMasters").removeAt(this.get("selectedServicesMasters").indexOf(currentMasters.findProperty("serviceComponentId", serviceComponentId)));
  732. currentMasters = this.get("selectedServicesMasters").filterProperty("component_name", componentName);
  733. if (currentMasters.get("length") < this.get("hosts.length")) {
  734. currentMasters.set("lastObject.showAddControl", true);
  735. }
  736. if (currentMasters.get("length") === 1) {
  737. currentMasters.set("lastObject.showRemoveControl", false);
  738. }
  739. this.set('componentToRebalance', componentName);
  740. this.incrementProperty('rebalanceComponentHostsCounter');
  741. this.toggleProperty('hostNameCheckTrigger');
  742. return true;
  743. },
  744. /**
  745. * Submit button click handler
  746. * @metohd submit
  747. */
  748. submit: function () {
  749. this.getIsSubmitDisabled();
  750. if (!this.get('submitDisabled')) {
  751. App.router.send('next');
  752. }
  753. }
  754. });