assign_master_controller.js 14 KB

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