assign_master_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. this._super();
  146. },
  147. secondary: null,
  148. showCloseButton: false,
  149. didInsertElement: function () {
  150. this._super();
  151. configWidgetContext.set('controller.saveInProgress', false);
  152. }
  153. });
  154. },
  155. /**
  156. * This method is used while installing or adding a service
  157. * Removes the masterComponent that was previously being tracked to be added to the cluster
  158. * @private
  159. * @method {removeMasterComponent}
  160. */
  161. removeMasterComponent: function () {
  162. var componentsToDelete = this.get('mastersToCreate');
  163. if (this.get('content.controllerName')) {
  164. var parentController = App.router.get(this.get('content.controllerName'));
  165. var masterComponentHosts = this.get('content.masterComponentHosts');
  166. var recommendationsHostGroups = this.get('content.recommendationsHostGroups');
  167. componentsToDelete.forEach(function (_componentName) {
  168. masterComponentHosts = masterComponentHosts.rejectProperty('component', _componentName);
  169. recommendationsHostGroups.blueprint.host_groups.forEach(function(hostGroup){
  170. hostGroup.components = hostGroup.components.rejectProperty('name', _componentName);
  171. }, this);
  172. }, this);
  173. this.get('content').set('masterComponentHosts', masterComponentHosts);
  174. parentController.setDBProperty('masterComponentHosts', masterComponentHosts);
  175. parentController.setDBProperty('recommendationsHostGroups', recommendationsHostGroups);
  176. } else {
  177. this.clearComponentsToBeAdded(componentsToDelete[0]);
  178. var hostComponent = App.HostComponent.find().findProperty('componentName', componentsToDelete[0]);
  179. if (hostComponent) {
  180. App.set('componentToBeDeleted', Em.Object.create({
  181. componentName: componentsToDelete[0],
  182. hostName: hostComponent.get('hostName')
  183. }));
  184. }
  185. }
  186. var configActionComponent = this.get('configActionComponent');
  187. this.get('configWidgetContext.config').set('configActionComponent', configActionComponent);
  188. },
  189. /**
  190. * Load active host list to <code>hosts</code> variable
  191. * @override
  192. * @method renderHostInfo
  193. */
  194. renderHostInfo: function () {
  195. var parentController = this.get('content.controllerName');
  196. if (parentController) {
  197. this._super();
  198. } else {
  199. var hosts = App.Host.find().toArray();
  200. var result = [];
  201. for (var p = 0; p < hosts.length; p++) {
  202. result.push(Em.Object.create({
  203. host_name: hosts[p].get('hostName'),
  204. cpu: hosts[p].get('cpu'),
  205. memory: hosts[p].get('memory'),
  206. maintenance_state: hosts[p].get('maintenance_state'),
  207. disk_info: hosts[p].get('diskInfo'),
  208. 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'))
  209. }));
  210. }
  211. this.set("hosts", result);
  212. this.sortHosts(result);
  213. this.set('isLoaded', true);
  214. }
  215. },
  216. /**
  217. * This method is called on Service->config page and is responsible to load the "Assign Master popup"
  218. * with the installed master component hosts.
  219. * @private
  220. * @method {loadMasterComponentHosts}
  221. */
  222. loadMasterComponentHosts: function () {
  223. var stackMasterComponents = App.get('components.masters').uniq();
  224. var masterComponentHosts = [];
  225. App.HostComponent.find().filter(function (component) {
  226. return stackMasterComponents.contains(component.get('componentName'));
  227. }).forEach(function (item) {
  228. masterComponentHosts.push({
  229. component: item.get('componentName'),
  230. hostName: item.get('hostName'),
  231. isInstalled: true,
  232. serviceId: item.get('service.id'),
  233. display_name: item.get('displayName')
  234. })
  235. });
  236. this.set("masterComponentHosts", masterComponentHosts);
  237. },
  238. /**
  239. * Returns array of dependent services that are yet not installed in the cluster
  240. * @private
  241. * @method getAllMissingDependentServices
  242. * @return missingDependentServices {Array}
  243. */
  244. getAllMissingDependentServices: function () {
  245. var configActionComponentName = this.get('configActionComponent').componentName;
  246. var componentStackService = App.StackServiceComponent.find(configActionComponentName).get('stackService');
  247. var dependentServices = componentStackService.get('requiredServices');
  248. return dependentServices.filter(function (item) {
  249. return !App.Service.find().findProperty('serviceName', item);
  250. }).map(function (item) {
  251. return App.StackService.find(item).get('displayName');
  252. });
  253. },
  254. /**
  255. * This method saves masterComponent layout that is used on subsequent "Review" and "Install start and Test services" pages.
  256. * @private
  257. * @method {saveMasterComponentHosts}
  258. */
  259. saveMasterComponentHosts: function() {
  260. var controller = App.router.get(this.get('content.controllerName'));
  261. controller.saveMasterComponentHosts(this);
  262. controller.loadMasterComponentHosts();
  263. },
  264. /**
  265. * This method saves host group layout that is used for blueprint validation call made while transitioning to "Review" page.
  266. * @private
  267. * @method {saveRecommendationsHostGroups}
  268. */
  269. saveRecommendationsHostGroups: function() {
  270. var controller = App.router.get(this.get('content.controllerName'));
  271. var recommendationsHostGroups = this.get('content.recommendationsHostGroups');
  272. var mastersToCreate = this.get('mastersToCreate');
  273. mastersToCreate.forEach(function(componentName) {
  274. var hostName = this.getSelectedHostName(componentName);
  275. if (hostName && recommendationsHostGroups) {
  276. var hostGroups = recommendationsHostGroups.blueprint_cluster_binding.host_groups;
  277. var isHostPresent = false;
  278. var i = 0;
  279. while (i < hostGroups.length) {
  280. var hosts = hostGroups[i].hosts;
  281. isHostPresent = hosts.someProperty('fqdn', hostName);
  282. if (isHostPresent) break;
  283. i++;
  284. }
  285. if (isHostPresent) {
  286. var hostGroupName = hostGroups[i].name;
  287. var hostGroup = recommendationsHostGroups.blueprint.host_groups.findProperty('name', hostGroupName);
  288. var addHostComponentInGroup = !hostGroup.components.someProperty('name', componentName);
  289. if (addHostComponentInGroup) {
  290. hostGroup.components.pushObject({name: componentName});
  291. }
  292. }
  293. }
  294. }, this);
  295. controller.setDBProperty('recommendationsHostGroups', recommendationsHostGroups);
  296. },
  297. /**
  298. * Get the fqdn hostname as selected by the user for the component.
  299. * @param componentName
  300. * @return {String}
  301. */
  302. getSelectedHostName: function(componentName) {
  303. var selectedServicesMasters = this.get('selectedServicesMasters');
  304. return selectedServicesMasters.findProperty('component_name', componentName).selectedHost;
  305. },
  306. /**
  307. * set App.componentToBeAdded to use it on subsequent validation call while saving configuration
  308. * @param componentName {String}
  309. * @param hostName {String}
  310. * @method {setGlobalComponentToBeAdded}
  311. */
  312. setGlobalComponentToBeAdded: function(componentName, hostName) {
  313. var componentToBeAdded = Em.Object.create({
  314. componentName: componentName,
  315. hostNames: [hostName]
  316. });
  317. App.set('componentToBeAdded', componentToBeAdded);
  318. },
  319. /**
  320. * clear 'componentToBeDeleted' object
  321. * @param componentName {String}
  322. * @public
  323. * @method {clearComponentsToBeDeleted}
  324. */
  325. clearComponentsToBeDeleted: function(componentName) {
  326. var componentsToBeDeleted = App.get('componentToBeDeleted');
  327. if (!App.isEmptyObject(componentsToBeDeleted) && componentsToBeDeleted.get('componentName') === componentName) {
  328. App.set('componentToBeDeleted', {});
  329. }
  330. },
  331. /**
  332. * clear 'componentToBeAdded' object
  333. * @param componentName {String}
  334. */
  335. clearComponentsToBeAdded: function(componentName) {
  336. var componentsToBeAdded = App.get('componentToBeAdded');
  337. if (!App.isEmptyObject(componentsToBeAdded) && componentsToBeAdded.get('componentName') === componentName) {
  338. App.set('componentToBeAdded', {});
  339. }
  340. },
  341. /**
  342. * Submit button click handler
  343. * @method submit
  344. */
  345. submit: function () {
  346. this.get('popup').hide();
  347. var context = this.get('configWidgetContext');
  348. context.toggleProperty('controller.forceUpdateBoundaries');
  349. var configActionComponent = this.get('configActionComponent');
  350. var componentHostName = this.getSelectedHostName(configActionComponent.componentName);
  351. if (this.get('content.controllerName')) {
  352. this.saveMasterComponentHosts();
  353. this.saveRecommendationsHostGroups();
  354. } else {
  355. this.setGlobalComponentToBeAdded(configActionComponent.componentName, componentHostName);
  356. this.clearComponentsToBeDeleted(configActionComponent.componentName);
  357. }
  358. var hostComponentConfig = context.get('config.configAction.hostComponentConfig');
  359. var serviceConfigs = context.get('controller.stepConfigs').findProperty('serviceName', context.get('config.serviceName')).get('configs');
  360. var config = serviceConfigs.filterProperty('filename', hostComponentConfig.fileName).findProperty('name', hostComponentConfig.configName);
  361. config.set('value', componentHostName);
  362. config.set('recommendedValue', componentHostName);
  363. configActionComponent.hostName = componentHostName;
  364. this.get('configWidgetContext.config').set('configActionComponent', configActionComponent);
  365. },
  366. /**
  367. * function called for onclcik event on cancel button for the popup
  368. */
  369. onCancel: function() {
  370. this.get('popup').showWarningPopup();
  371. }
  372. });