manage_config_groups_controller.js 20 KB

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