manage_config_groups_controller.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 App = require('app');
  19. var hostsManagement = require('utils/hosts');
  20. var componentHelper = require('utils/component');
  21. var serviceComponents = require('data/service_components');
  22. App.ManageConfigGroupsController = Em.Controller.extend({
  23. name: 'manageConfigGroupsController',
  24. isLoaded: false,
  25. isInstaller: false,
  26. serviceName: null,
  27. configGroups: [],
  28. originalConfigGroups: [],
  29. selectedConfigGroup: null,
  30. selectedHosts: [],
  31. resortConfigGroup: function() {
  32. var configGroups = Ember.copy(this.get('configGroups'));
  33. if(configGroups.length < 2){
  34. return;
  35. }
  36. var defaultConfigGroup = configGroups.findProperty('isDefault');
  37. configGroups.removeObject(defaultConfigGroup);
  38. var sorted = configGroups.sort(function(configGroupA, configGroupB){
  39. return String(configGroupA.get('name')) >= String(configGroupB.get('name'));
  40. });
  41. sorted = [defaultConfigGroup].concat(sorted);
  42. this.removeObserver('configGroups.@each.name', this, 'resortConfigGroup');
  43. this.set('configGroups', sorted);
  44. this.addObserver('configGroups.@each.name', this, 'resortConfigGroup');
  45. }.observes('configGroups.@each.name'),
  46. loadConfigGroups: function (serviceName) {
  47. if (this.get('isInstaller')) {
  48. this.set('serviceName', serviceName);
  49. var configGroups = this.copyConfigGroups(App.router.get('wizardStep7Controller.selectedService.configGroups'));
  50. var originalConfigGroups = this.copyConfigGroups(configGroups);
  51. this.set('configGroups', configGroups);
  52. this.set('originalConfigGroups', originalConfigGroups);
  53. this.set('isLoaded', true);
  54. } else {
  55. this.set('serviceName', serviceName);
  56. App.ajax.send({
  57. name: 'service.load_config_groups',
  58. data: {
  59. serviceName: serviceName
  60. },
  61. sender: this,
  62. success: 'onLoadConfigGroupsSuccess',
  63. error: 'onLoadConfigGroupsError'
  64. });
  65. }
  66. },
  67. onLoadConfigGroupsSuccess: function (data) {
  68. var usedHosts = [];
  69. var unusedHosts = [];
  70. var serviceName = this.get('serviceName');
  71. var defaultConfigGroup = App.ConfigGroup.create({
  72. name: App.Service.DisplayNames[serviceName] + " Default",
  73. description: "Default cluster level " + this.get('serviceName') + " configuration",
  74. isDefault: true,
  75. parentConfigGroup: null,
  76. service: this.get('content'),
  77. configSiteTags: [],
  78. serviceName: serviceName
  79. });
  80. if (data && data.items) {
  81. var groupToTypeToTagMap = {};
  82. var configGroups = [];
  83. data.items.forEach(function (configGroup) {
  84. configGroup = configGroup.ConfigGroup;
  85. var hostNames = configGroup.hosts.mapProperty('host_name');
  86. var newConfigGroup = App.ConfigGroup.create({
  87. id: configGroup.id,
  88. name: configGroup.group_name,
  89. description: configGroup.description,
  90. isDefault: false,
  91. parentConfigGroup: defaultConfigGroup,
  92. service: App.Service.find().findProperty('serviceName', configGroup.tag),
  93. hosts: hostNames,
  94. configSiteTags: [],
  95. properties: [],
  96. apiResponse: configGroup
  97. });
  98. usedHosts = usedHosts.concat(newConfigGroup.get('hosts'));
  99. configGroups.push(newConfigGroup);
  100. var newConfigGroupSiteTags = newConfigGroup.get('configSiteTags');
  101. configGroup.desired_configs.forEach(function (config) {
  102. newConfigGroupSiteTags.push(App.ConfigSiteTag.create({
  103. site: config.type,
  104. tag: config.tag
  105. }));
  106. if (!groupToTypeToTagMap[configGroup.group_name]) {
  107. groupToTypeToTagMap[configGroup.group_name] = {}
  108. }
  109. groupToTypeToTagMap[configGroup.group_name][config.type] = config.tag;
  110. });
  111. }, this);
  112. unusedHosts = App.Host.find().mapProperty('hostName');
  113. usedHosts.uniq().forEach(function (host) {
  114. unusedHosts = unusedHosts.without(host);
  115. }, this);
  116. defaultConfigGroup.set('childConfigGroups', configGroups);
  117. defaultConfigGroup.set('hosts', unusedHosts);
  118. var allGroups = [defaultConfigGroup].concat(configGroups);
  119. this.set('configGroups', allGroups);
  120. var originalGroups = this.copyConfigGroups(allGroups);
  121. this.set('originalConfigGroups', originalGroups);
  122. this.loadProperties(groupToTypeToTagMap);
  123. this.set('isLoaded', true);
  124. }
  125. },
  126. onLoadConfigGroupsError: function () {
  127. console.error('Unable to load config groups for service.');
  128. },
  129. loadProperties: function (groupToTypeToTagMap) {
  130. var typeTagToGroupMap = {};
  131. var urlParams = [];
  132. for (var group in groupToTypeToTagMap) {
  133. var overrideTypeTags = groupToTypeToTagMap[group];
  134. for (var type in overrideTypeTags) {
  135. var tag = overrideTypeTags[type];
  136. typeTagToGroupMap[type + "///" + tag] = group;
  137. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  138. }
  139. }
  140. var params = urlParams.join('|');
  141. if (urlParams.length) {
  142. App.ajax.send({
  143. name: 'config.host_overrides',
  144. sender: this,
  145. data: {
  146. params: params,
  147. typeTagToGroupMap: typeTagToGroupMap
  148. },
  149. success: 'onLoadPropertiesSuccess'
  150. });
  151. }
  152. },
  153. onLoadPropertiesSuccess: function (data, opt, params) {
  154. data.items.forEach(function (configs) {
  155. var typeTagConfigs = [];
  156. App.config.loadedConfigurationsCache[configs.type + "_" + configs.tag] = configs.properties;
  157. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  158. for (var config in configs.properties) {
  159. typeTagConfigs.push({
  160. name: config,
  161. value: configs.properties[config]
  162. });
  163. }
  164. this.get('configGroups').findProperty('name', group).get('properties').pushObjects(typeTagConfigs);
  165. }, this);
  166. },
  167. showProperties: function () {
  168. var properies = this.get('selectedConfigGroup.propertiesList').htmlSafe();
  169. if (properies) {
  170. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properies);
  171. }
  172. },
  173. /**
  174. * add hosts to group
  175. * @return {Array}
  176. */
  177. componentsForFilter: function() {
  178. var components = componentHelper.getInstalledComponents().filterProperty('serviceName', this.get('serviceName'));
  179. return components.map(function(component) {
  180. return Em.Object.create({
  181. displayName: component.displayName,
  182. componentName: component.id,
  183. selected: false
  184. });
  185. });
  186. }.property('serviceName'),
  187. addHosts: function () {
  188. if (this.get('selectedConfigGroup.isAddHostsDisabled')){
  189. return false;
  190. }
  191. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  192. var popupDescription = {
  193. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  194. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message').format(App.Service.DisplayNames[this.get('serviceName')])
  195. };
  196. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, this.get('componentsForFilter'), this.addHostsCallback.bind(this), popupDescription);
  197. },
  198. /**
  199. * add hosts callback
  200. */
  201. addHostsCallback: function (selectedHosts) {
  202. var group = this.get('selectedConfigGroup');
  203. if (selectedHosts) {
  204. var defaultHosts = group.get('parentConfigGroup.hosts');
  205. var configGroupHosts = group.get('hosts');
  206. selectedHosts.forEach(function (hostName) {
  207. configGroupHosts.pushObject(hostName);
  208. defaultHosts.removeObject(hostName);
  209. });
  210. }
  211. },
  212. /**
  213. * delete hosts from group
  214. */
  215. deleteHosts: function () {
  216. if (this.get('isDeleteHostsDisabled')) {
  217. return false;
  218. }
  219. var groupHosts = this.get('selectedConfigGroup.hosts');
  220. var defaultGroupHosts = this.get('selectedConfigGroup.parentConfigGroup.hosts');
  221. this.get('selectedHosts').slice().forEach(function (hostName) {
  222. defaultGroupHosts.pushObject(hostName);
  223. groupHosts.removeObject(hostName);
  224. });
  225. this.set('selectedHosts', []);
  226. },
  227. isDeleteHostsDisabled: function () {
  228. var selectedConfigGroup = this.get('selectedConfigGroup');
  229. if (selectedConfigGroup) {
  230. if (selectedConfigGroup.isDefault || this.get('selectedHosts').length === 0) {
  231. return true;
  232. } else {
  233. return false;
  234. }
  235. }
  236. return true;
  237. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.length', 'selectedHosts.length'),
  238. /**
  239. * confirm delete config group
  240. */
  241. confirmDelete : function () {
  242. var self = this;
  243. App.showConfirmationPopup(function() {
  244. self.deleteConfigGroup();
  245. });
  246. },
  247. componentsForFilter: function () {
  248. return serviceComponents.filterProperty('service_name', this.get('serviceName')).map(function (component) {
  249. return Em.Object.create({
  250. displayName: component.display_name,
  251. componentName: component.isClient ? 'CLIENT' : component.component_name,
  252. selected: false
  253. });
  254. });
  255. }.property('serviceName'),
  256. /**
  257. * delete selected config group
  258. */
  259. deleteConfigGroup: function () {
  260. var selectedConfigGroup = this.get('selectedConfigGroup');
  261. if (this.get('isDeleteGroupDisabled')) {
  262. return;
  263. }
  264. //move hosts of group to default group (available hosts)
  265. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  266. this.deleteHosts();
  267. this.get('configGroups').removeObject(selectedConfigGroup);
  268. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  269. },
  270. /**
  271. * rename new config group
  272. */
  273. renameConfigGroup: function () {
  274. if(this.get('selectedConfigGroup.isDefault')) {
  275. return;
  276. }
  277. var self = this;
  278. this.renameGroupPopup = App.ModalPopup.show({
  279. primary: Em.I18n.t('ok'),
  280. secondary: Em.I18n.t('common.cancel'),
  281. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  282. bodyClass: Ember.View.extend({
  283. templateName: require('templates/main/service/new_config_group')
  284. }),
  285. configGroupName: self.get('selectedConfigGroup.name'),
  286. configGroupDesc: self.get('selectedConfigGroup.description'),
  287. warningMessage: '',
  288. isDescriptionDirty: false,
  289. validate: function () {
  290. var warningMessage = '';
  291. var originalGroup = self.get('selectedConfigGroup');
  292. if (originalGroup.get('description') !== this.get('configGroupDesc') && !this.get('isDescriptionDirty')) {
  293. this.set('isDescriptionDirty', true);
  294. }
  295. if (originalGroup.get('name').trim() === this.get('configGroupName').trim()) {
  296. if (this.get('isDescriptionDirty')) {
  297. warningMessage = '';
  298. } else {
  299. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  300. }
  301. } else {
  302. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName'))) {
  303. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  304. }
  305. }
  306. this.set('warningMessage', warningMessage);
  307. }.observes('configGroupName', 'configGroupDesc'),
  308. enablePrimary: function () {
  309. return this.get('configGroupName').trim().length > 0 && !this.get('warningMessage');
  310. }.property('warningMessage', 'configGroupName', 'configGroupDesc'),
  311. onPrimary: function () {
  312. if (!this.get('enablePrimary')) {
  313. return false;
  314. }
  315. self.set('selectedConfigGroup.name', this.get('configGroupName'));
  316. self.set('selectedConfigGroup.description', this.get('configGroupDesc'));
  317. this.hide();
  318. }
  319. });
  320. this.get('renameGroupPopup').validate();
  321. },
  322. /**
  323. * add new config group
  324. */
  325. addConfigGroup: function (duplicated) {
  326. duplicated = (duplicated === true);
  327. var self = this;
  328. this.addGroupPopup = App.ModalPopup.show({
  329. primary: Em.I18n.t('ok'),
  330. secondary: Em.I18n.t('common.cancel'),
  331. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  332. bodyClass: Ember.View.extend({
  333. templateName: require('templates/main/service/new_config_group')
  334. }),
  335. configGroupName: duplicated ? self.get('selectedConfigGroup.name') + ' Copy' : "",
  336. configGroupDesc: duplicated ? self.get('selectedConfigGroup.description') + ' (Copy)' : "",
  337. warningMessage: '',
  338. validate: function () {
  339. var warningMessage = '';
  340. if (self.get('configGroups').mapProperty('name').contains(this.get('configGroupName').trim())) {
  341. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  342. }
  343. this.set('warningMessage', warningMessage);
  344. }.observes('configGroupName'),
  345. enablePrimary: function () {
  346. return this.get('configGroupName').trim().length > 0 && !this.get('warningMessage');
  347. }.property('warningMessage', 'configGroupName'),
  348. onPrimary: function () {
  349. if (!this.get('enablePrimary')) {
  350. return false;
  351. }
  352. var defaultConfigGroup = self.get('configGroups').findProperty('isDefault');
  353. var properties = [];
  354. var newConfigGroupData = App.ConfigGroup.create({
  355. id: null,
  356. name: this.get('configGroupName').trim(),
  357. description: this.get('configGroupDesc'),
  358. isDefault: false,
  359. parentConfigGroup: defaultConfigGroup,
  360. service: Em.Object.create({id: self.get('serviceName')}),
  361. hosts: [],
  362. configSiteTags: [],
  363. properties: []
  364. });
  365. if (duplicated) {
  366. self.get('selectedConfigGroup.properties').forEach(function(property) {
  367. var property = $.extend(false, {},property);
  368. property.set('group', newConfigGroupData);
  369. properties.push(App.ServiceConfigProperty.create(property));
  370. });
  371. newConfigGroupData.set('properties', properties);
  372. } else {
  373. newConfigGroupData.set('properties', []);
  374. }
  375. self.get('configGroups').pushObject(newConfigGroupData);
  376. defaultConfigGroup.get('childConfigGroups').pushObject(newConfigGroupData);
  377. this.hide();
  378. }
  379. });
  380. },
  381. duplicateConfigGroup: function() {
  382. this.addConfigGroup(true);
  383. },
  384. hostsModifiedConfigGroups: function () {
  385. if (!this.get('isLoaded')) {
  386. return false;
  387. }
  388. var groupsToClearHosts = [];
  389. var groupsToDelete = [];
  390. var groupsToSetHosts = [];
  391. var groupsToCreate = [];
  392. var groups = this.get('configGroups');
  393. var originalGroups = this.get('originalConfigGroups');
  394. var originalGroupsNames = originalGroups.mapProperty('name').without(originalGroups.findProperty('isDefault').get('name'));
  395. groups.forEach(function (group) {
  396. if (!group.get('isDefault')) {
  397. var originalGroup = originalGroups.findProperty('name', group.get('name'));
  398. if (originalGroup) {
  399. if (!(JSON.stringify(group.get('hosts').slice().sort()) === JSON.stringify(originalGroup.get('hosts').sort()))) {
  400. groupsToClearHosts.push(group.set('id', originalGroup.get('id')));
  401. if (group.get('hosts').length) {
  402. groupsToSetHosts.push(group.set('id', originalGroup.get('id')));
  403. }
  404. } else if (group.get('description') !== originalGroup.get('description')) {
  405. groupsToSetHosts.push(group.set('id', originalGroup.get('id')));
  406. }
  407. originalGroupsNames = originalGroupsNames.without(group.get('name'));
  408. } else {
  409. groupsToCreate.push(group);
  410. }
  411. }
  412. });
  413. originalGroupsNames.forEach(function (groupName) {
  414. groupsToDelete.push(originalGroups.findProperty('name', groupName));
  415. }, this);
  416. return {
  417. toClearHosts: groupsToClearHosts,
  418. toDelete: groupsToDelete,
  419. toSetHosts: groupsToSetHosts,
  420. toCreate: groupsToCreate
  421. };
  422. }.property('selectedConfigGroup.hosts.@each', 'selectedConfigGroup.description', 'configGroups', 'isLoaded'),
  423. isHostsModified: function () {
  424. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  425. if (!this.get('isLoaded')) {
  426. return false;
  427. }
  428. return !!(modifiedGroups.toClearHosts.length || modifiedGroups.toSetHosts.length || modifiedGroups.toCreate.length || modifiedGroups.toDelete.length);
  429. }.property('hostsModifiedConfigGroups'),
  430. /**
  431. * copy config groups to manage popup to give user choice whether or not save changes
  432. * @param originGroups
  433. * @return {Array}
  434. */
  435. copyConfigGroups: function (originGroups) {
  436. var configGroups = [];
  437. var defaultConfigGroup = App.ConfigGroup.create($.extend(false, {},originGroups.findProperty('isDefault')));
  438. originGroups.forEach(function (configGroup) {
  439. if (!configGroup.get('isDefault')) {
  440. var copiedGroup = App.ConfigGroup.create($.extend(false, {}, configGroup));
  441. copiedGroup.set('parentConfigGroup', defaultConfigGroup);
  442. configGroups.pushObject(copiedGroup);
  443. }
  444. });
  445. defaultConfigGroup.set('childConfigGroups', configGroups.slice());
  446. configGroups.pushObject(defaultConfigGroup);
  447. return configGroups;
  448. }
  449. });