manage_config_groups_controller.js 20 KB

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