assign_master_controller.js 15 KB

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