assign_master_controller.js 14 KB

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