assign_master_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 stringUtils = require('utils/string_utils');
  19. var numberUtils = require('utils/number_utils');
  20. App.AssignMasterOnStep7Controller = Em.Controller.extend(App.BlueprintMixin, App.AssignMasterComponents, {
  21. name: "assignMasterOnStep7Controller",
  22. useServerValidation: false,
  23. showInstalledMastersFirst: false,
  24. configWidgetContext: {},
  25. configActionComponent: {},
  26. content: function () {
  27. return this.get('configWidgetContext.controller.content') || {};
  28. }.property('configWidgetContext.controller.content'),
  29. popup: null,
  30. mastersToCreate: [],
  31. markSavedComponentsAsInstalled: true,
  32. /**
  33. * Array of master component names, that should be addable
  34. * Are used in HA wizards to add components, that are not addable for other wizards
  35. * @type {Array}
  36. * @override
  37. */
  38. mastersAddableInHA: Em.computed.alias('App.components.isMasterAddableOnlyOnHA'),
  39. /**
  40. * Marks component add/delete action to be performed ahead.
  41. * @param context {Object} Context of the calling function
  42. * @param action {String} ADD|DELETE
  43. * @param hostComponent {Object}
  44. * @public
  45. * @method {execute}
  46. */
  47. execute: function (context, action, hostComponent) {
  48. this.set('configWidgetContext', context);
  49. this.set('content', context.get('controller.content'));
  50. this.set('configActionComponent', hostComponent);
  51. var missingDependentServices = this.getAllMissingDependentServices();
  52. var isNonWizardPage = !this.get('content.controllerName');
  53. switch (action) {
  54. case 'ADD':
  55. if (missingDependentServices.length && isNonWizardPage) {
  56. this.showInstallServicesPopup(missingDependentServices);
  57. } else {
  58. this.set('mastersToCreate', [hostComponent.componentName]);
  59. this.showAssignComponentPopup();
  60. }
  61. break;
  62. case 'DELETE':
  63. this.set('mastersToCreate', [hostComponent.componentName]);
  64. this.removeMasterComponent();
  65. break;
  66. }
  67. },
  68. /**
  69. * Used to set showAddControl/showRemoveControl flag
  70. * @param componentName
  71. * @override
  72. */
  73. updateComponent: function(componentName) {
  74. this._super(componentName);
  75. if (!this.get('mastersToCreate').contains(componentName)) {
  76. this.get("selectedServicesMasters").filterProperty("component_name", componentName).forEach(function(c) {
  77. c.set('showAddControl', false);
  78. c.set('showRemoveControl', false);
  79. });
  80. }
  81. },
  82. /**
  83. * Assign Master page will be displayed in the popup
  84. * @private
  85. * @method
  86. */
  87. showAssignComponentPopup: function () {
  88. var self = this;
  89. // Master component hosts should be loaded only when content.controller name is not defined i.e non-wizard pages
  90. if (!this.get('content.controllerName')) {
  91. this.loadMasterComponentHosts();
  92. }
  93. var mastersToCreate = this.get('mastersToCreate');
  94. var masterToCreateDisplayName = App.format.role(mastersToCreate[0]);
  95. var configWidgetContext = this.get('configWidgetContext');
  96. var config = this.get('configWidgetContext.config');
  97. var popup = App.ModalPopup.show({
  98. classNames: ['full-width-modal', 'add-service-wizard-modal'],
  99. header: Em.I18n.t('assign.master.popup.header').format(masterToCreateDisplayName),
  100. bodyClass: App.AssignMasterOnStep7View.extend({
  101. controller: self
  102. }),
  103. primary: Em.I18n.t('form.cancel'),
  104. showFooter: false,
  105. onClose: function () {
  106. this.showWarningPopup();
  107. },
  108. showWarningPopup: function() {
  109. var mainPopupContext = this;
  110. App.ModalPopup.show({
  111. encodeBody: false,
  112. header: Em.I18n.t('common.warning'),
  113. primaryClass: 'btn-warning',
  114. body: Em.I18n.t('assign.master.popup.cancel.body').format(masterToCreateDisplayName),
  115. onPrimary: function () {
  116. configWidgetContext.toggleProperty('controller.forceUpdateBoundaries');
  117. var value = config.get('initialValue');
  118. config.set('value', value);
  119. configWidgetContext.setValue(value);
  120. configWidgetContext.sendRequestRorDependentConfigs(config);
  121. this.hide();
  122. mainPopupContext.hide();
  123. }
  124. });
  125. },
  126. didInsertElement: function () {
  127. this._super();
  128. this.fitHeight();
  129. self.set('configWidgetContext.controller.saveInProgress', false);
  130. }
  131. });
  132. this.set('popup', popup);
  133. },
  134. /**
  135. * Displays the popup to install required service dependencies for being added component with this config change
  136. * @param missingDependentServices {String[]} Array of service display names
  137. */
  138. showInstallServicesPopup: function (missingDependentServices) {
  139. var displayServices = stringUtils.getFormattedStringFromArray(missingDependentServices);
  140. var configWidgetContext = this.get('configWidgetContext');
  141. var config = this.get('configWidgetContext.config');
  142. var configDisplayName = config.get('displayName').toLowerCase();
  143. return App.ModalPopup.show({
  144. header: Em.I18n.t('installer.step7.missing.service.header'),
  145. body: Em.I18n.t('installer.step7.missing.service.body').format(displayServices, configDisplayName),
  146. primaryClass: 'btn-danger',
  147. onPrimary: function () {
  148. configWidgetContext.toggleProperty('controller.forceUpdateBoundaries');
  149. var value = config.get('initialValue');
  150. config.set('value', value);
  151. configWidgetContext.setValue(value);
  152. configWidgetContext.sendRequestRorDependentConfigs(config);
  153. this._super();
  154. },
  155. secondary: null,
  156. showCloseButton: false,
  157. didInsertElement: function () {
  158. this._super();
  159. configWidgetContext.set('controller.saveInProgress', false);
  160. }
  161. });
  162. },
  163. /**
  164. * This method is used while installing or adding a service
  165. * Removes the masterComponent that was previously being tracked to be added to the cluster
  166. * @private
  167. * @method {removeMasterComponent}
  168. */
  169. removeMasterComponent: function () {
  170. var componentsToDelete = this.get('mastersToCreate');
  171. if (this.get('content.controllerName')) {
  172. var parentController = App.router.get(this.get('content.controllerName'));
  173. var masterComponentHosts = this.get('content.masterComponentHosts');
  174. var recommendationsHostGroups = this.get('content.recommendationsHostGroups');
  175. componentsToDelete.forEach(function (_componentName) {
  176. masterComponentHosts = masterComponentHosts.rejectProperty('component', _componentName);
  177. recommendationsHostGroups.blueprint.host_groups.forEach(function(hostGroup){
  178. hostGroup.components = hostGroup.components.rejectProperty('name', _componentName);
  179. }, this);
  180. }, this);
  181. this.get('content').set('masterComponentHosts', masterComponentHosts);
  182. parentController.setDBProperty('masterComponentHosts', masterComponentHosts);
  183. parentController.setDBProperty('recommendationsHostGroups', recommendationsHostGroups);
  184. } else {
  185. this.clearComponentsToBeAdded(componentsToDelete[0]);
  186. var hostComponent = App.HostComponent.find().findProperty('componentName', componentsToDelete[0]);
  187. if (hostComponent) {
  188. App.set('componentToBeDeleted', Em.Object.create({
  189. componentName: componentsToDelete[0],
  190. hostName: hostComponent.get('hostName')
  191. }));
  192. }
  193. }
  194. var configActionComponent = this.get('configActionComponent');
  195. this.get('configWidgetContext.config').set('configActionComponent', configActionComponent);
  196. },
  197. /**
  198. * Load active host list to <code>hosts</code> variable
  199. * @override
  200. * @method renderHostInfo
  201. */
  202. renderHostInfo: function () {
  203. var parentController = this.get('content.controllerName');
  204. if (parentController) {
  205. this._super();
  206. } else {
  207. var hosts = App.Host.find().toArray();
  208. var result = [];
  209. for (var p = 0; p < hosts.length; p++) {
  210. result.push(Em.Object.create({
  211. host_name: hosts[p].get('hostName'),
  212. cpu: hosts[p].get('cpu'),
  213. memory: hosts[p].get('memory'),
  214. maintenance_state: hosts[p].get('maintenance_state'),
  215. disk_info: hosts[p].get('diskInfo'),
  216. host_info: Em.I18n.t('installer.step5.hostInfo').fmt(hosts[p].get('hostName'), numberUtils.bytesToSize(hosts[p].get('memory'), 1, 'parseFloat', 1024), hosts[p].get('cpu'))
  217. }));
  218. }
  219. this.set("hosts", result);
  220. this.sortHosts(result);
  221. }
  222. },
  223. /**
  224. * This method is called on Service->config page and is responsible to load the "Assign Master popup"
  225. * with the installed master component hosts.
  226. * @private
  227. * @method {loadMasterComponentHosts}
  228. */
  229. loadMasterComponentHosts: function () {
  230. var stackMasterComponents = App.get('components.masters').uniq();
  231. var masterComponentHosts = [];
  232. App.HostComponent.find().filter(function (component) {
  233. return stackMasterComponents.contains(component.get('componentName'));
  234. }).forEach(function (item) {
  235. masterComponentHosts.push({
  236. component: item.get('componentName'),
  237. hostName: item.get('hostName'),
  238. isInstalled: true,
  239. serviceId: item.get('service.id'),
  240. display_name: item.get('displayName')
  241. })
  242. });
  243. this.set("masterComponentHosts", masterComponentHosts);
  244. },
  245. /**
  246. * Returns array of dependent services that are yet not installed in the cluster
  247. * @private
  248. * @method getAllMissingDependentServices
  249. * @return missingDependentServices {Array}
  250. */
  251. getAllMissingDependentServices: function () {
  252. var configActionComponentName = this.get('configActionComponent').componentName;
  253. var componentStackService = App.StackServiceComponent.find(configActionComponentName).get('stackService');
  254. var dependentServices = componentStackService.get('requiredServices');
  255. return dependentServices.filter(function (item) {
  256. return !App.Service.find().findProperty('serviceName', item);
  257. }).map(function (item) {
  258. return App.StackService.find(item).get('displayName');
  259. });
  260. },
  261. /**
  262. * This method saves masterComponent layout that is used on subsequent "Review" and "Install start and Test services" pages.
  263. * @private
  264. * @method {saveMasterComponentHosts}
  265. */
  266. saveMasterComponentHosts: function() {
  267. var controller = App.router.get(this.get('content.controllerName'));
  268. controller.saveMasterComponentHosts(this);
  269. controller.loadMasterComponentHosts();
  270. },
  271. /**
  272. * This method saves host group layout that is used for blueprint validation call made while transitioning to "Review" page.
  273. * @private
  274. * @method {saveRecommendationsHostGroups}
  275. */
  276. saveRecommendationsHostGroups: function() {
  277. var controller = App.router.get(this.get('content.controllerName'));
  278. var recommendationsHostGroups = this.get('content.recommendationsHostGroups');
  279. var mastersToCreate = this.get('mastersToCreate');
  280. mastersToCreate.forEach(function(componentName) {
  281. var hostName = this.getSelectedHostName(componentName);
  282. if (hostName && recommendationsHostGroups) {
  283. var hostGroups = recommendationsHostGroups.blueprint_cluster_binding.host_groups;
  284. var isHostPresent = false;
  285. var i = 0;
  286. while (i < hostGroups.length) {
  287. var hosts = hostGroups[i].hosts;
  288. isHostPresent = hosts.someProperty('fqdn', hostName);
  289. if (isHostPresent) break;
  290. i++;
  291. }
  292. if (isHostPresent) {
  293. var hostGroupName = hostGroups[i].name;
  294. var hostGroup = recommendationsHostGroups.blueprint.host_groups.findProperty('name', hostGroupName);
  295. var addHostComponentInGroup = !hostGroup.components.someProperty('name', componentName);
  296. if (addHostComponentInGroup) {
  297. hostGroup.components.pushObject({name: componentName});
  298. }
  299. }
  300. }
  301. }, this);
  302. controller.setDBProperty('recommendationsHostGroups', recommendationsHostGroups);
  303. },
  304. /**
  305. * Get the fqdn hostname as selected by the user for the component.
  306. * @param componentName
  307. * @return {String}
  308. */
  309. getSelectedHostName: function(componentName) {
  310. var selectedServicesMasters = this.get('selectedServicesMasters');
  311. return selectedServicesMasters.findProperty('component_name', componentName).selectedHost;
  312. },
  313. /**
  314. * set App.componentToBeAdded to use it on subsequent validation call while saving configuration
  315. * @param componentName {String}
  316. * @param hostName {String}
  317. * @method {setGlobalComponentToBeAdded}
  318. */
  319. setGlobalComponentToBeAdded: function(componentName, hostName) {
  320. var componentToBeAdded = Em.Object.create({
  321. componentName: componentName,
  322. hostNames: [hostName]
  323. });
  324. App.set('componentToBeAdded', componentToBeAdded);
  325. },
  326. /**
  327. * clear 'componentToBeDeleted' object
  328. * @param componentName {String}
  329. * @public
  330. * @method {clearComponentsToBeDeleted}
  331. */
  332. clearComponentsToBeDeleted: function(componentName) {
  333. var componentsToBeDeleted = App.get('componentToBeDeleted');
  334. if (!App.isEmptyObject(componentsToBeDeleted) && componentsToBeDeleted.get('componentName') === componentName) {
  335. App.set('componentToBeDeleted', {});
  336. }
  337. },
  338. /**
  339. * clear 'componentToBeAdded' object
  340. * @param componentName {String}
  341. */
  342. clearComponentsToBeAdded: function(componentName) {
  343. var componentsToBeAdded = App.get('componentToBeAdded');
  344. if (!App.isEmptyObject(componentsToBeAdded) && componentsToBeAdded.get('componentName') === componentName) {
  345. App.set('componentToBeAdded', {});
  346. }
  347. },
  348. /**
  349. * Submit button click handler
  350. * @method submit
  351. */
  352. submit: function () {
  353. var self = this;
  354. App.get('router.mainAdminKerberosController').getKDCSessionState(function() {
  355. self.get('popup').hide();
  356. var context = self.get('configWidgetContext');
  357. context.toggleProperty('controller.forceUpdateBoundaries');
  358. var configActionComponent = self.get('configActionComponent');
  359. var componentHostName = self.getSelectedHostName(configActionComponent.componentName);
  360. if (self.get('content.controllerName')) {
  361. self.saveMasterComponentHosts();
  362. self.saveRecommendationsHostGroups();
  363. } else {
  364. self.setGlobalComponentToBeAdded(configActionComponent.componentName, componentHostName);
  365. self.clearComponentsToBeDeleted(configActionComponent.componentName);
  366. }
  367. var hostComponentConfig = context.get('config.configAction.hostComponentConfig');
  368. var serviceConfigs = context.get('controller.stepConfigs').findProperty('serviceName', context.get('config.serviceName')).get('configs');
  369. var config = serviceConfigs.filterProperty('filename', hostComponentConfig.fileName).findProperty('name', hostComponentConfig.configName);
  370. var oldValue = config.get('value');
  371. config.set('value', componentHostName);
  372. config.set('recommendedValue', componentHostName);
  373. configActionComponent.hostName = componentHostName;
  374. self.get('configWidgetContext.config').set('configActionComponent', configActionComponent);
  375. context.get('controller').loadConfigRecommendations([{
  376. "type": App.config.getConfigTagFromFileName(config.get('filename')),
  377. "name": config.get('name'),
  378. "old_value": oldValue
  379. }])
  380. });
  381. },
  382. /**
  383. * function called for onclcik event on cancel button for the popup
  384. */
  385. onCancel: function() {
  386. this.get('popup').showWarningPopup();
  387. }
  388. });