step5_controller.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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.loadComponentsRecommendationsFromServer(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. var mastersLength = this.get("selectedServicesMasters").filterProperty("component_name", componentName).length;
  225. if (mastersLength < this.get("hosts.length") && !this.get('isReassignWizard') && !this.get('isHighAvailabilityWizard')) {
  226. component.set('showAddControl', true);
  227. } else if (mastersLength == 1 || this.get('isReassignWizard') || this.get('isHighAvailabilityWizard')) {
  228. component.set('showRemoveControl', false);
  229. }
  230. }
  231. },
  232. /**
  233. * Load active host list to <code>hosts</code> variable
  234. * @method renderHostInfo
  235. */
  236. renderHostInfo: function () {
  237. var hostInfo = this.get('content.hosts');
  238. var result = [];
  239. for (var index in hostInfo) {
  240. var _host = hostInfo[index];
  241. if (_host.bootStatus === 'REGISTERED') {
  242. result.push(Em.Object.create({
  243. host_name: _host.name,
  244. cpu: _host.cpu,
  245. memory: _host.memory,
  246. disk_info: _host.disk_info,
  247. host_info: Em.I18n.t('installer.step5.hostInfo').fmt(_host.name, numberUtils.bytesToSize(_host.memory, 1, 'parseFloat', 1024), _host.cpu)
  248. }));
  249. }
  250. }
  251. this.set("hosts", result);
  252. this.sortHosts(this.get('hosts'));
  253. this.set('isLoaded', true);
  254. },
  255. /**
  256. * Sort list of host-objects by properties (memory - desc, cpu - desc, hostname - asc)
  257. * @param {object[]} hosts
  258. */
  259. sortHosts: function (hosts) {
  260. hosts.sort(function (a, b) {
  261. if (a.get('memory') == b.get('memory')) {
  262. if (a.get('cpu') == b.get('cpu')) {
  263. return a.get('host_name').localeCompare(b.get('host_name')); // hostname asc
  264. }
  265. return b.get('cpu') - a.get('cpu'); // cores desc
  266. }
  267. return b.get('memory') - a.get('memory'); // ram desc
  268. });
  269. },
  270. /**
  271. * Get recommendations info from API
  272. * @return {undefined}
  273. */
  274. loadComponentsRecommendationsFromServer: function(callback) {
  275. var self = this;
  276. if (App.router.get('installerController.recommendations') !== undefined) {
  277. // Don't do AJAX call if recommendations has been already received
  278. // 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
  279. callback(self.createComponentInstallationObjects(), self);
  280. } else {
  281. var selectedServices = App.StackService.find().filterProperty('isSelected').mapProperty('serviceName');
  282. var installedServices = App.StackService.find().filterProperty('isInstalled').mapProperty('serviceName');
  283. var services = installedServices.concat(selectedServices).uniq();
  284. var hostNames = self.get('hosts').mapProperty('host_name');
  285. return App.ajax.send({
  286. name: 'wizard.step5.recommendations',
  287. sender: self,
  288. data: {
  289. stackVersionUrl: App.get('stackVersionURL'),
  290. hosts: hostNames,
  291. services: services
  292. },
  293. success: 'loadRecommendationsSuccessCallback'
  294. }).
  295. retry({
  296. times: App.maxRetries,
  297. timeout: App.timeout
  298. }).
  299. then(function () {
  300. callback(self.createComponentInstallationObjects(), self);
  301. },
  302. function () {
  303. App.showReloadPopup();
  304. console.log('Load recommendations failed');
  305. }
  306. );
  307. }
  308. },
  309. /**
  310. * Create components for displaying component-host comboboxes in UI assign dialog
  311. * expects installerController.recommendations will be filled with recommendations API call result
  312. * @return {Object[]}
  313. */
  314. createComponentInstallationObjects: function() {
  315. var self = this;
  316. var masterComponents = [];
  317. if (self.get('isAddServiceWizard')) {
  318. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnAddServiceAssignMasterPage');
  319. } else {
  320. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnInstallerAssignMasterPage');
  321. }
  322. var masterHosts = self.get('content.masterComponentHosts'); //saved to local storage info
  323. var selectedNotInstalledServices = self.get('content.services').filterProperty('isSelected').filterProperty('isInstalled', false).mapProperty('serviceName');
  324. var recommendations = App.router.get('installerController.recommendations');
  325. var resultComponents = [];
  326. var multipleComponentHasBeenAdded = {};
  327. recommendations.blueprint.host_groups.forEach(function(host_group) {
  328. var hosts = recommendations.blueprint_cluster_binding.host_groups.findProperty('name', host_group.name).hosts;
  329. hosts.forEach(function(host) {
  330. host_group.components.forEach(function(component) {
  331. var willBeAdded = true;
  332. var fullComponent = masterComponents.findProperty('componentName', component.name);
  333. // If it's master component which should be shown
  334. if (fullComponent) {
  335. // If service is already installed and not being added as a new service then render on UI only those master components
  336. // that have already installed hostComponents.
  337. // NOTE: On upgrade there might be a prior installed service with non-installed newly introduced serviceComponent
  338. var isNotSelectedService = !selectedNotInstalledServices.contains(fullComponent.get('serviceName'));
  339. if (isNotSelectedService) {
  340. willBeAdded = App.HostComponent.find().someProperty('componentName', component.name);
  341. }
  342. if (willBeAdded) {
  343. var savedComponents = masterHosts.filterProperty('component', component.name);
  344. if (self.get('multipleComponents').contains(component.name) && savedComponents.length > 0) {
  345. if (!multipleComponentHasBeenAdded[component.name]) {
  346. multipleComponentHasBeenAdded[component.name] = true;
  347. savedComponents.forEach(function(saved) {
  348. resultComponents.push(self.createComponentInstallationObject(fullComponent, host.fqdn, saved));
  349. });
  350. }
  351. } else {
  352. var savedComponent = masterHosts.findProperty('component', component.name);
  353. resultComponents.push(self.createComponentInstallationObject(fullComponent, host.fqdn, savedComponent));
  354. }
  355. }
  356. }
  357. });
  358. });
  359. });
  360. return resultComponents;
  361. },
  362. /**
  363. * Create component for displaying component-host comboboxes in UI assign dialog
  364. * @param fullComponent - full component description
  365. * @param hostName - host fqdn where component will be installed
  366. * @param savedComponent - the same object which function returns but created before
  367. * @return {Object}
  368. */
  369. createComponentInstallationObject: function(fullComponent, hostName, savedComponent) {
  370. var componentName = fullComponent.get('componentName');
  371. var componentObj = {};
  372. componentObj.component_name = componentName;
  373. componentObj.display_name = fullComponent.get('displayName');
  374. componentObj.serviceId = fullComponent.get('serviceName');
  375. componentObj.isServiceCoHost = App.StackServiceComponent.find().findProperty('componentName', componentName).get('isCoHostedComponent') && !this.get('isReassignWizard');
  376. if (savedComponent) {
  377. componentObj.selectedHost = savedComponent.hostName;
  378. componentObj.isInstalled = savedComponent.isInstalled;
  379. } else {
  380. componentObj.selectedHost = hostName;
  381. componentObj.isInstalled = false;
  382. }
  383. return componentObj;
  384. },
  385. /**
  386. * Success-callback for recommendations request
  387. * @param {object} data
  388. * @method loadRecommendationsSuccessCallback
  389. */
  390. loadRecommendationsSuccessCallback: function (data) {
  391. App.router.set('installerController.recommendations', data.resources[0].recommendations);
  392. },
  393. /**
  394. * Load services info to appropriate variable and return masterComponentHosts
  395. * @return {Object[]}
  396. */
  397. loadComponentsRecommendationsLocally: function (callback) {
  398. var selectedServices = App.StackService.find().filterProperty('isSelected').mapProperty('serviceName');
  399. var installedServices = App.StackService.find().filterProperty('isInstalled').mapProperty('serviceName');
  400. var services = installedServices.concat(selectedServices).uniq();
  401. var selectedNotInstalledServices = this.get('content.services').filterProperty('isSelected').filterProperty('isInstalled', false).mapProperty('serviceName');
  402. var masterComponents = [];
  403. //get full list from mock data
  404. if (this.get('isAddServiceWizard')) {
  405. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnAddServiceAssignMasterPage');
  406. } else {
  407. masterComponents = App.StackServiceComponent.find().filterProperty('isShownOnInstallerAssignMasterPage');
  408. }
  409. var masterHosts = this.get('content.masterComponentHosts'); //saved to local storage info
  410. var resultComponents = [];
  411. for (var index = 0; index < services.length; index++) {
  412. var componentInfo = masterComponents.filterProperty('serviceName', services[index]);
  413. // If service is already installed and not being added as a new service then render on UI only those master components
  414. // that have already installed hostComponents.
  415. // NOTE: On upgrade there might be a prior installed service with non-installed newly introduced serviceComponent
  416. var isNotSelectedService = !selectedNotInstalledServices.contains(services[index]);
  417. if (isNotSelectedService) {
  418. componentInfo = componentInfo.filter(function (_component) {
  419. return App.HostComponent.find().someProperty('componentName',_component.get('componentName'));
  420. });
  421. }
  422. componentInfo.forEach(function (_componentInfo) {
  423. if (this.get('multipleComponents').contains(_componentInfo.get('componentName'))) {
  424. var savedComponents = masterHosts.filterProperty('component', _componentInfo.get('componentName'));
  425. if (savedComponents.length) {
  426. savedComponents.forEach(function (item) {
  427. var multipleMasterHost = {};
  428. multipleMasterHost.component_name = _componentInfo.get('componentName');
  429. multipleMasterHost.display_name = _componentInfo.get('displayName');
  430. multipleMasterHost.selectedHost = item.hostName;
  431. multipleMasterHost.serviceId = services[index];
  432. multipleMasterHost.isInstalled = item.isInstalled;
  433. multipleMasterHost.isServiceCoHost = false;
  434. resultComponents.push(multipleMasterHost);
  435. })
  436. } else {
  437. var multipleMasterHosts = this.selectHostLocally(_componentInfo.get('componentName'));
  438. multipleMasterHosts.forEach(function (_host) {
  439. var multipleMasterHost = {};
  440. multipleMasterHost.component_name = _componentInfo.get('componentName');
  441. multipleMasterHost.display_name = _componentInfo.get('displayName');
  442. multipleMasterHost.selectedHost = _host;
  443. multipleMasterHost.serviceId = services[index];
  444. multipleMasterHost.isInstalled = false;
  445. multipleMasterHost.isServiceCoHost = false;
  446. resultComponents.push(multipleMasterHost);
  447. });
  448. }
  449. } else {
  450. var savedComponent = masterHosts.findProperty('component', _componentInfo.get('componentName'));
  451. var componentObj = {};
  452. componentObj.component_name = _componentInfo.get('componentName');
  453. componentObj.display_name = _componentInfo.get('displayName');
  454. componentObj.selectedHost = savedComponent ? savedComponent.hostName : this.selectHostLocally(_componentInfo.get('componentName')); // call the method that plays selectNode algorithm or fetches from server
  455. componentObj.isInstalled = savedComponent ? savedComponent.isInstalled : false;
  456. componentObj.serviceId = services[index];
  457. componentObj.isServiceCoHost = App.StackServiceComponent.find().findProperty('componentName', _componentInfo.get('componentName')).get('isCoHostedComponent') && !this.get('isReassignWizard');
  458. resultComponents.push(componentObj);
  459. }
  460. }, this);
  461. }
  462. callback(resultComponents, this);
  463. },
  464. /**
  465. * @param {string} componentName
  466. * @returns {bool}
  467. * @private
  468. * @method _isHiveCoHost
  469. */
  470. _isHiveCoHost: function (componentName) {
  471. return ['HIVE_METASTORE', 'WEBHCAT_SERVER'].contains(componentName) && !this.get('isReassignWizard');
  472. },
  473. /**
  474. * Put master components to <code>selectedServicesMasters</code>, which will be automatically rendered in template
  475. * @param {Ember.Enumerable} masterComponents
  476. * @method renderComponents
  477. */
  478. renderComponents: function (masterComponents) {
  479. var installedServices = App.StackService.find().filterProperty('isSelected').filterProperty('isInstalled', false).mapProperty('serviceName'); //list of shown services
  480. var result = [];
  481. var serviceComponentId, previousComponentName;
  482. masterComponents.forEach(function (item) {
  483. var serviceComponent = App.StackServiceComponent.find().findProperty('componentName', item.component_name);
  484. var showRemoveControl = installedServices.contains(serviceComponent.get('stackService.serviceName')) &&
  485. (masterComponents.filterProperty('component_name', item.component_name).length > 1);
  486. var componentObj = Em.Object.create(item);
  487. console.log("TRACE: render master component name is: " + item.component_name);
  488. var masterComponent = App.StackServiceComponent.find().findProperty('componentName', item.component_name);
  489. if (masterComponent.get('isMasterWithMultipleInstances')) {
  490. previousComponentName = item.component_name;
  491. componentObj.set('serviceComponentId', result.filterProperty('component_name', item.component_name).length + 1);
  492. componentObj.set("showRemoveControl", showRemoveControl);
  493. }
  494. componentObj.set('isHostNameValid', true);
  495. result.push(componentObj);
  496. }, this);
  497. result = this.sortComponentsByServiceName(result);
  498. this.set("selectedServicesMasters", result);
  499. if (this.get('isReassignWizard')) {
  500. var components = result.filterProperty('component_name', this.get('content.reassign.component_name'));
  501. components.setEach('isInstalled', false);
  502. this.set('servicesMasters', components);
  503. } else {
  504. this.set('servicesMasters', result);
  505. }
  506. },
  507. sortComponentsByServiceName: function(components) {
  508. var displayOrder = App.StackService.displayOrder;
  509. return components.sort(function (a, b) {
  510. var aValue = displayOrder.indexOf(a.serviceId) != -1 ? displayOrder.indexOf(a.serviceId) : components.length;
  511. var bValue = displayOrder.indexOf(b.serviceId) != -1 ? displayOrder.indexOf(b.serviceId) : components.length;
  512. return aValue - bValue;
  513. });
  514. },
  515. /**
  516. * Update dependent co-hosted components according to the change in the component host
  517. * @method updateCoHosts
  518. */
  519. updateCoHosts: function () {
  520. var components = App.StackServiceComponent.find().filterProperty('isOtherComponentCoHosted');
  521. var selectedServicesMasters = this.get('selectedServicesMasters');
  522. components.forEach(function (component) {
  523. var componentName = component.get('componentName');
  524. var hostComponent = selectedServicesMasters.findProperty('component_name', componentName);
  525. var dependentCoHosts = component.get('coHostedComponents');
  526. dependentCoHosts.forEach(function (coHostedComponent) {
  527. var dependentHostComponent = selectedServicesMasters.findProperty('component_name', coHostedComponent);
  528. if (hostComponent && dependentHostComponent) dependentHostComponent.set('selectedHost', hostComponent.get('selectedHost'));
  529. }, this);
  530. }, this);
  531. }.observes('selectedServicesMasters.@each.selectedHost'),
  532. /**
  533. * select and return host for component by scheme
  534. * Scheme is an object that has keys which compared to number of hosts,
  535. * if key more that number of hosts, then return value of that key.
  536. * Value is index of host in hosts array.
  537. *
  538. * @param {object} componentName
  539. * @param {object} hosts
  540. * @return {string}
  541. * @method getHostForComponent
  542. */
  543. getHostForComponent: function (componentName, hosts) {
  544. var component = App.StackServiceComponent.find().findProperty('componentName', componentName);
  545. if (component) {
  546. var selectionScheme = App.StackServiceComponent.find().findProperty('componentName', componentName).get('selectionSchemeForMasterComponent');
  547. } else {
  548. return hosts[0];
  549. }
  550. if (hosts.length === 1 || $.isEmptyObject(selectionScheme)) {
  551. return hosts[0];
  552. } else {
  553. for (var i in selectionScheme) {
  554. if (window.isFinite(i)) {
  555. if (hosts.length < window.parseInt(i)) {
  556. return hosts[selectionScheme[i]];
  557. }
  558. }
  559. }
  560. return hosts[selectionScheme['else']]
  561. }
  562. },
  563. /**
  564. * Get list of host names for master component with multiple instances
  565. * @param {Object} component
  566. * @param {Object} hosts
  567. * @returns {string[]}
  568. * @method getHostsForComponent
  569. */
  570. getHostsForComponent: function (component, hosts) {
  571. var defaultNoOfMasterHosts = component.get('defaultNoOfMasterHosts');
  572. var masterHosts = [];
  573. if (hosts.length < defaultNoOfMasterHosts) {
  574. defaultNoOfMasterHosts = hosts.length;
  575. }
  576. for (var index = 0; index < defaultNoOfMasterHosts; index++) {
  577. masterHosts.push(hosts[index]);
  578. }
  579. return masterHosts;
  580. },
  581. /**
  582. * Return hostName of masterNode for specified service
  583. * @param componentName
  584. * @return {string|string[]}
  585. * @method selectHostLocally
  586. */
  587. selectHostLocally: function (componentName) {
  588. var component = App.StackServiceComponent.find().findProperty('componentName', componentName);
  589. var hostNames = this.get('hosts').mapProperty('host_name');
  590. if (hostNames.length > 1 && App.StackServiceComponent.find().filterProperty('isNotPreferableOnAmbariServerHost').mapProperty('componentName').contains(componentName)) {
  591. hostNames = this.get('hosts').mapProperty('host_name').filter(function (item) {
  592. return item !== location.hostname;
  593. }, this);
  594. }
  595. if (this.get('multipleComponents').contains(componentName)) {
  596. if (component.get('defaultNoOfMasterHosts') > 1) {
  597. return this.getHostsForComponent(component, hostNames);
  598. } else {
  599. return [this.getHostForComponent(componentName, hostNames)];
  600. }
  601. } else {
  602. return this.getHostForComponent(componentName, hostNames);
  603. }
  604. },
  605. /**
  606. * On change callback for inputs
  607. * @param {string} componentName
  608. * @param {string} selectedHost
  609. * @param {number} serviceComponentId
  610. * @method assignHostToMaster
  611. */
  612. assignHostToMaster: function (componentName, selectedHost, serviceComponentId) {
  613. var flag = this.isHostNameValid(componentName, selectedHost);
  614. this.updateIsHostNameValidFlag(componentName, serviceComponentId, flag);
  615. if (serviceComponentId) {
  616. this.get('selectedServicesMasters').filterProperty('component_name', componentName).findProperty("serviceComponentId", serviceComponentId).set("selectedHost", selectedHost);
  617. }
  618. else {
  619. this.get('selectedServicesMasters').findProperty("component_name", componentName).set("selectedHost", selectedHost);
  620. }
  621. },
  622. /**
  623. * Determines if hostName is valid for component:
  624. * <ul>
  625. * <li>host name shouldn't be empty</li>
  626. * <li>host should exist</li>
  627. * <li>host should have only one component with <code>componentName</code></li>
  628. * </ul>
  629. * @param {string} componentName
  630. * @param {string} selectedHost
  631. * @returns {boolean} true - valid, false - invalid
  632. * @method isHostNameValid
  633. */
  634. isHostNameValid: function (componentName, selectedHost) {
  635. return (selectedHost.trim() !== '') &&
  636. this.get('hosts').mapProperty('host_name').contains(selectedHost) &&
  637. (this.get('selectedServicesMasters').
  638. filterProperty('component_name', componentName).
  639. mapProperty('selectedHost').
  640. filter(function (h) {
  641. return h === selectedHost;
  642. }).length <= 1);
  643. },
  644. /**
  645. * Update <code>isHostNameValid</code> property with <code>flag</code> value
  646. * for component with name <code>componentName</code> and
  647. * <code>serviceComponentId</code>-property equal to <code>serviceComponentId</code>-parameter value
  648. * @param {string} componentName
  649. * @param {number} serviceComponentId
  650. * @param {bool} flag
  651. * @method updateIsHostNameValidFlag
  652. */
  653. updateIsHostNameValidFlag: function (componentName, serviceComponentId, flag) {
  654. if (componentName) {
  655. if (serviceComponentId) {
  656. this.get('selectedServicesMasters').filterProperty('component_name', componentName).findProperty("serviceComponentId", serviceComponentId).set("isHostNameValid", flag);
  657. } else {
  658. this.get('selectedServicesMasters').findProperty("component_name", componentName).set("isHostNameValid", flag);
  659. }
  660. }
  661. },
  662. /**
  663. * Returns last component of selected type
  664. * @param {string} componentName
  665. * @return {Em.Object|null}
  666. * @method last
  667. */
  668. last: function (componentName) {
  669. return this.get("selectedServicesMasters").filterProperty("component_name", componentName).get("lastObject");
  670. },
  671. /**
  672. * Add new component to ZooKeeper Server and Hbase master
  673. * @param {string} componentName
  674. * @return {bool} true - added, false - not added
  675. * @method addComponent
  676. */
  677. addComponent: function (componentName) {
  678. /*
  679. * Logic: If ZooKeeper or Hbase service is selected then there can be
  680. * minimum 1 ZooKeeper or Hbase master in total, and
  681. * maximum 1 ZooKeeper or Hbase on every host
  682. */
  683. var maxNumMasters = this.get("hosts.length"),
  684. currentMasters = this.get("selectedServicesMasters").filterProperty("component_name", componentName),
  685. newMaster = null,
  686. masterHosts = null,
  687. suggestedHost = null,
  688. i = 0,
  689. lastMaster = null;
  690. if (!currentMasters.length) {
  691. console.log('ALERT: Zookeeper service was not selected');
  692. return false;
  693. }
  694. if (currentMasters.get("length") < maxNumMasters) {
  695. currentMasters.set("lastObject.showAddControl", false);
  696. currentMasters.set("lastObject.showRemoveControl", true);
  697. //create a new master component host based on an existing one
  698. newMaster = Em.Object.create({});
  699. lastMaster = currentMasters.get("lastObject");
  700. newMaster.set("display_name", lastMaster.get("display_name"));
  701. newMaster.set("component_name", lastMaster.get("component_name"));
  702. newMaster.set("selectedHost", lastMaster.get("selectedHost"));
  703. newMaster.set("serviceId", lastMaster.get("serviceId"));
  704. newMaster.set("isInstalled", false);
  705. if (currentMasters.get("length") === (maxNumMasters - 1)) {
  706. newMaster.set("showAddControl", false);
  707. } else {
  708. newMaster.set("showAddControl", true);
  709. }
  710. newMaster.set("showRemoveControl", true);
  711. //get recommended host for the new Zookeeper server
  712. masterHosts = currentMasters.mapProperty("selectedHost").uniq();
  713. for (i = 0; i < this.get("hosts.length"); i++) {
  714. if (!(masterHosts.contains(this.get("hosts")[i].get("host_name")))) {
  715. suggestedHost = this.get("hosts")[i].get("host_name");
  716. break;
  717. }
  718. }
  719. newMaster.set("selectedHost", suggestedHost);
  720. newMaster.set("serviceComponentId", (currentMasters.get("lastObject.serviceComponentId") + 1));
  721. this.get("selectedServicesMasters").insertAt(this.get("selectedServicesMasters").indexOf(lastMaster) + 1, newMaster);
  722. this.set('componentToRebalance', componentName);
  723. this.incrementProperty('rebalanceComponentHostsCounter');
  724. this.toggleProperty('hostNameCheckTrigger');
  725. return true;
  726. }
  727. return false;//if no more zookeepers can be added
  728. },
  729. /**
  730. * Remove component from ZooKeeper server or Hbase Master
  731. * @param {string} componentName
  732. * @param {number} serviceComponentId
  733. * @return {bool} true - removed, false - no
  734. * @method removeComponent
  735. */
  736. removeComponent: function (componentName, serviceComponentId) {
  737. var currentMasters = this.get("selectedServicesMasters").filterProperty("component_name", componentName);
  738. //work only if the multiple master service is selected in previous step
  739. if (currentMasters.length <= 1) {
  740. return false;
  741. }
  742. this.get("selectedServicesMasters").removeAt(this.get("selectedServicesMasters").indexOf(currentMasters.findProperty("serviceComponentId", serviceComponentId)));
  743. currentMasters = this.get("selectedServicesMasters").filterProperty("component_name", componentName);
  744. if (currentMasters.get("length") < this.get("hosts.length")) {
  745. currentMasters.set("lastObject.showAddControl", true);
  746. }
  747. if (currentMasters.get("length") === 1) {
  748. currentMasters.set("lastObject.showRemoveControl", false);
  749. }
  750. this.set('componentToRebalance', componentName);
  751. this.incrementProperty('rebalanceComponentHostsCounter');
  752. this.toggleProperty('hostNameCheckTrigger');
  753. return true;
  754. },
  755. /**
  756. * Submit button click handler
  757. * @metohd submit
  758. */
  759. submit: function () {
  760. this.getIsSubmitDisabled();
  761. if (!this.get('submitDisabled')) {
  762. App.router.send('next');
  763. }
  764. }
  765. });