assign_master_controller.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. this.set('mastersToCreate', [hostComponent.componentName]);
  45. var missingDependentServices = this.getAllMissingDependentServices();
  46. var isNonWizardPage = !this.get('content.controllerName');
  47. switch (action) {
  48. case 'ADD':
  49. if (missingDependentServices.length && isNonWizardPage) {
  50. this.showInstallServicesPopup(missingDependentServices);
  51. } else {
  52. this.showAssignComponentPopup();
  53. }
  54. break;
  55. case 'DELETE':
  56. this.removeMasterComponent();
  57. break;
  58. }
  59. },
  60. /**
  61. * Assign Master page will be displayed in the popup
  62. * @private
  63. * @method
  64. */
  65. showAssignComponentPopup: function () {
  66. var self = this;
  67. // Master component hosts should be loaded only when content.controller name is not defined i.e non-wizard pages
  68. if (!this.get('content.controllerName')) {
  69. this.loadMasterComponentHosts();
  70. }
  71. var popup = App.ModalPopup.show({
  72. classNames: ['full-width-modal', 'add-service-wizard-modal'],
  73. header: Em.I18n.t('admin.highAvailability.wizard.step2.header'),
  74. bodyClass: App.AssignMasterOnStep7View.extend({
  75. controller: self
  76. }),
  77. primary: Em.I18n.t('form.cancel'),
  78. showFooter: false,
  79. secondary: null,
  80. showCloseButton: false,
  81. didInsertElement: function () {
  82. this._super();
  83. this.fitHeight();
  84. self.set('configWidgetContext.controller.saveInProgress', false);
  85. }
  86. });
  87. this.set('popup', popup);
  88. },
  89. /**
  90. * Displays the popup to install required service dependencies for being added component with this config change
  91. * @param missingDependentServices {String[]} Array of service display names
  92. */
  93. showInstallServicesPopup: function (missingDependentServices) {
  94. var displayServices = stringUtils.getFormattedStringFromArray(missingDependentServices);
  95. var configWidgetContext = this.get('configWidgetContext');
  96. var config = this.get('configWidgetContext.config');
  97. var configDisplayName = config.get('displayName').toLowerCase();
  98. return App.ModalPopup.show({
  99. header: Em.I18n.t('installer.step7.missing.service.header'),
  100. body: Em.I18n.t('installer.step7.missing.service.body').format(displayServices, configDisplayName),
  101. primaryClass: 'btn-danger',
  102. onPrimary: function () {
  103. var value = config.get('initialValue');
  104. config.set('value', value);
  105. configWidgetContext.setValue(value);
  106. this._super();
  107. },
  108. secondary: null,
  109. showCloseButton: false,
  110. didInsertElement: function () {
  111. this._super();
  112. configWidgetContext.set('controller.saveInProgress', false);
  113. }
  114. });
  115. },
  116. /**
  117. * This method is used while installing or adding a service
  118. * Removes the masterComponent that was previously being tracked to be added to the cluster
  119. * @private
  120. * @method {removeMasterComponent}
  121. */
  122. removeMasterComponent: function () {
  123. var componentsToDelete = this.get('mastersToCreate');
  124. if (this.get('content.controllerName')) {
  125. var parentController = App.router.get(this.get('content.controllerName'));
  126. var masterComponentHosts = this.get('content.masterComponentHosts');
  127. componentsToDelete.forEach(function (_componentName) {
  128. masterComponentHosts = masterComponentHosts.rejectProperty('component', _componentName);
  129. }, this);
  130. this.get('content').set('masterComponentHosts', masterComponentHosts);
  131. parentController.setDBProperty('masterComponentHosts', masterComponentHosts);
  132. }
  133. var configActionComponent = this.get('configActionComponent');
  134. this.get('configWidgetContext.config').set('configActionComponent', configActionComponent);
  135. },
  136. /**
  137. * Load active host list to <code>hosts</code> variable
  138. * @override
  139. * @method renderHostInfo
  140. */
  141. renderHostInfo: function () {
  142. var parentController = this.get('content.controllerName');
  143. if (parentController) {
  144. this._super();
  145. } else {
  146. var hosts = App.Host.find().toArray();
  147. var result = [];
  148. for (var p = 0; p < hosts.length; p++) {
  149. result.push(Em.Object.create({
  150. host_name: hosts[p].get('hostName'),
  151. cpu: hosts[p].get('cpu'),
  152. memory: hosts[p].get('memory'),
  153. disk_info: hosts[p].get('diskInfo'),
  154. 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'))
  155. }));
  156. }
  157. this.set("hosts", result);
  158. this.sortHosts(result);
  159. this.set('isLoaded', true);
  160. }
  161. },
  162. /**
  163. * This method is called on Service->config page and is responsible to load the "Assign Master popup"
  164. * with the installed master component hosts.
  165. * @private
  166. * @method {loadMasterComponentHosts}
  167. */
  168. loadMasterComponentHosts: function () {
  169. var stackMasterComponents = App.get('components.masters').uniq();
  170. var masterComponentHosts = [];
  171. App.HostComponent.find().filter(function (component) {
  172. return stackMasterComponents.contains(component.get('componentName'));
  173. }).forEach(function (item) {
  174. masterComponentHosts.push({
  175. component: item.get('componentName'),
  176. hostName: item.get('hostName'),
  177. isInstalled: true,
  178. serviceId: item.get('service.id'),
  179. display_name: item.get('displayName')
  180. })
  181. });
  182. this.set("masterComponentHosts", masterComponentHosts);
  183. },
  184. /**
  185. * Returns array of dependent services that are yet not installed in the cluster
  186. * @private
  187. * @method getAllMissingDependentServices
  188. * @return missingDependentServices {Array}
  189. */
  190. getAllMissingDependentServices: function () {
  191. var configActionComponentName = this.get('configActionComponent').componentName;
  192. var componentStackService = App.StackServiceComponent.find(configActionComponentName).get('stackService');
  193. var dependentServices = componentStackService.get('requiredServices');
  194. return dependentServices.filter(function (item) {
  195. return !App.Service.find().findProperty('serviceName', item);
  196. }).map(function (item) {
  197. return App.StackService.find(item).get('displayName');
  198. });
  199. },
  200. /**
  201. * Submit button click handler
  202. * @method submit
  203. */
  204. submit: function () {
  205. this.get('popup').hide();
  206. if (this.get('content.controllerName')) {
  207. var controller = App.router.get(this.get('content.controllerName'));
  208. controller.saveMasterComponentHosts(this);
  209. }
  210. var selectedServicesMasters = this.get('selectedServicesMasters');
  211. var context = this.get('configWidgetContext');
  212. var configActionComponent = this.get('configActionComponent');
  213. var componentHostName = selectedServicesMasters.findProperty('component_name', configActionComponent.componentName).selectedHost;
  214. var hostComponentConfig = context.get('config.configAction.hostComponentConfig');
  215. var serviceConfigs = context.get('controller.stepConfigs').findProperty('serviceName', context.get('config.serviceName')).get('configs');
  216. var config = serviceConfigs.filterProperty('filename', hostComponentConfig.fileName).findProperty('name', hostComponentConfig.configName);
  217. config.set('value', componentHostName);
  218. config.set('recommendedValue', componentHostName);
  219. configActionComponent.hostName = componentHostName;
  220. this.get('configWidgetContext.config').set('configActionComponent', configActionComponent);
  221. }
  222. });