manage_config_groups_controller.js 21 KB

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