assign_master_controller.js 14 KB

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