manage_config_groups_controller.js 19 KB

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