manage_config_groups_controller.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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(App.ConfigOverridable, {
  23. name: 'manageConfigGroupsController',
  24. /**
  25. * Determines if needed data is already loaded
  26. * Loading chain starts at <code>loadHosts</code> and is complete on the <code>loadConfigGroups</code> (if user on
  27. * the Installer) or on the <code>_onLoadConfigGroupsSuccess</code> (otherwise)
  28. * @type {boolean}
  29. */
  30. isLoaded: false,
  31. /**
  32. * Determines if user currently is on the Cluster Installer
  33. * @type {boolean}
  34. */
  35. isInstaller: false,
  36. /**
  37. * Determines if user currently is on the Add Service Wizard
  38. * @type {boolean}
  39. */
  40. isAddService: false,
  41. /**
  42. * Current service name
  43. * @type {string}
  44. */
  45. serviceName: null,
  46. /**
  47. * @type {App.ConfigGroup[]}
  48. */
  49. configGroups: [],
  50. /**
  51. * @type {App.ConfigGroup[]}
  52. */
  53. originalConfigGroups: [],
  54. /**
  55. * @type {App.ConfigGroup}
  56. */
  57. selectedConfigGroup: null,
  58. /**
  59. * @type {string[]}
  60. */
  61. selectedHosts: [],
  62. /**
  63. * List of all hosts in the cluster
  64. * @type {{
  65. * id: string,
  66. * ip: string,
  67. * osType: string,
  68. * osArch: string,
  69. * hostName: string,
  70. * publicHostName: string,
  71. * cpu: number,
  72. * memory: number,
  73. * diskTotal: string,
  74. * diskFree: string,
  75. * disksMounted: number,
  76. * hostComponents: {
  77. * componentName: string,
  78. * displayName: string
  79. * }[]
  80. * }[]}
  81. */
  82. clusterHosts: [],
  83. /**
  84. * trigger <code>selectDefaultGroup</code> after group delete
  85. * @type {null}
  86. */
  87. groupDeleteTrigger: null,
  88. /**
  89. * List of available service components for <code>serviceName</code>
  90. * @type {{componentName: string, displayName: string, selected: boolean}[]}
  91. */
  92. componentsForFilter: function () {
  93. return App.StackServiceComponent.find().filterProperty('serviceName', this.get('serviceName')).map(function (component) {
  94. return Em.Object.create({
  95. componentName: component.get('componentName'),
  96. displayName: App.format.role(component.get('componentName'), false),
  97. selected: false
  98. });
  99. });
  100. }.property('serviceName'),
  101. /**
  102. * Determines when host may be deleted from config group
  103. * @type {boolean}
  104. */
  105. isDeleteHostsDisabled: function () {
  106. var selectedConfigGroup = this.get('selectedConfigGroup');
  107. if (selectedConfigGroup) {
  108. return selectedConfigGroup.get('isDefault') || this.get('selectedHosts').length === 0;
  109. }
  110. return true;
  111. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.length', 'selectedHosts.length'),
  112. /**
  113. * Map with modified/deleted/created config groups
  114. * @type {{
  115. * toClearHosts: App.ConfigGroup[],
  116. * toDelete: App.ConfigGroup[],
  117. * toSetHosts: App.ConfigGroup[],
  118. * toCreate: App.ConfigGroup[]
  119. * }}
  120. */
  121. hostsModifiedConfigGroups: {},
  122. /**
  123. * Check when some config group was changed and updates <code>hostsModifiedConfigGroups</code> once
  124. * @method hostsModifiedConfigGroupsObs
  125. */
  126. hostsModifiedConfigGroupsObs: function() {
  127. Em.run.once(this, this.hostsModifiedConfigGroupsObsOnce);
  128. }.observes('selectedConfigGroup.hosts.@each', 'selectedConfigGroup.hosts.length', 'selectedConfigGroup.description', 'configGroups', 'isLoaded'),
  129. /**
  130. * Update <code>hostsModifiedConfigGroups</code>-value
  131. * Called once in the <code>hostsModifiedConfigGroupsObs</code>
  132. * @method hostsModifiedConfigGroupsObsOnce
  133. * @returns {boolean}
  134. */
  135. hostsModifiedConfigGroupsObsOnce: function() {
  136. if (!this.get('isLoaded')) {
  137. return false;
  138. }
  139. var groupsToClearHosts = [];
  140. var groupsToDelete = [];
  141. var groupsToSetHosts = [];
  142. var groupsToCreate = [];
  143. var groups = this.get('configGroups');
  144. var originalGroups = [];
  145. var originalGroupsMap = {};
  146. this.get('originalConfigGroups').forEach(function(item){
  147. if (!item.is_default) {
  148. originalGroupsMap[item.id] = item;
  149. originalGroups.push(item);
  150. }
  151. }, this);
  152. groups.forEach(function (groupRecord) {
  153. if (!groupRecord.get('isDefault')) {
  154. var originalGroup = originalGroupsMap[groupRecord.get('id')];
  155. if (originalGroup) {
  156. if (!(JSON.stringify(groupRecord.get('hosts').slice().sort()) === JSON.stringify(originalGroup.hosts.sort()))) {
  157. groupsToClearHosts.push(groupRecord);
  158. if (groupRecord.get('hosts').length) {
  159. groupsToSetHosts.push(groupRecord);
  160. }
  161. // should update name or description
  162. } else if (groupRecord.get('description') !== originalGroup.description || groupRecord.get('name') !== originalGroup.name) {
  163. groupsToSetHosts.push(groupRecord);
  164. }
  165. delete originalGroupsMap[groupRecord.get('id')];
  166. } else {
  167. groupsToCreate.push({
  168. id: groupRecord.get('id'),
  169. name: groupRecord.get('name'),
  170. description: groupRecord.get('description'),
  171. hosts: groupRecord.get('hosts').slice(0),
  172. service_id: groupRecord.get('serviceName'),
  173. desired_configs: groupRecord.get('desiredConfigs'),
  174. properties: groupRecord.get('properties')
  175. });
  176. }
  177. }
  178. });
  179. //groups to delete
  180. for (var id in originalGroupsMap) {
  181. groupsToDelete.push(App.ServiceConfigGroup.find(id));
  182. }
  183. this.set('hostsModifiedConfigGroups', {
  184. toClearHosts: groupsToClearHosts,
  185. toDelete: groupsToDelete,
  186. toSetHosts: groupsToSetHosts,
  187. toCreate: groupsToCreate,
  188. initialGroups: originalGroups
  189. });
  190. },
  191. /**
  192. * Determines if some changes were done with config groups
  193. * @use hostsModifiedConfigGroups
  194. * @type {boolean}
  195. */
  196. isHostsModified: function () {
  197. if (!this.get('isLoaded')) {
  198. return false;
  199. }
  200. var ignoreKeys = ['initialGroups'];
  201. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  202. return Em.keys(modifiedGroups).map(function (key) {
  203. return ignoreKeys.contains(key) ? 0 : Em.get(modifiedGroups[key], 'length');
  204. }).reduce(Em.sum, 0) > 0;
  205. }.property('hostsModifiedConfigGroups'),
  206. /**
  207. * Resort config groups according to order:
  208. * default group first, other - last
  209. * @method resortConfigGroup
  210. */
  211. resortConfigGroup: function() {
  212. var configGroups = Em.copy(this.get('configGroups'));
  213. if(configGroups.length < 2) return;
  214. var defaultConfigGroup = configGroups.findProperty('isDefault');
  215. configGroups.removeObject(defaultConfigGroup);
  216. var sorted = [defaultConfigGroup].concat(configGroups.sortProperty('name'));
  217. this.removeObserver('configGroups.@each.name', this, 'resortConfigGroup');
  218. this.set('configGroups', sorted);
  219. this.addObserver('configGroups.@each.name', this, 'resortConfigGroup');
  220. }.observes('configGroups.@each.name'),
  221. /**
  222. * Load hosts from server or
  223. * get them from installerController if user on the install wizard
  224. * get them from isAddServiceController if user on the add service wizard
  225. * @method loadHosts
  226. */
  227. loadHosts: function() {
  228. this.set('isLoaded', false);
  229. if (this.get('isInstaller')) {
  230. var allHosts = this.get('isAddService') ? App.router.get('addServiceController').get('allHosts') : App.router.get('installerController').get('allHosts');
  231. this.set('clusterHosts', allHosts);
  232. this.loadConfigGroups(this.get('serviceName'));
  233. }
  234. else {
  235. this.loadHostsFromServer();
  236. this.loadConfigGroups(this.get('serviceName'));
  237. }
  238. },
  239. /**
  240. * Request all hosts directly from server
  241. * @method loadHostsFromServer
  242. * @return {$.ajax}
  243. */
  244. loadHostsFromServer: function() {
  245. return App.ajax.send({
  246. name: 'hosts.config_groups',
  247. sender: this,
  248. data: {},
  249. success: '_loadHostsFromServerSuccessCallback',
  250. error: '_loadHostsFromServerErrorCallback'
  251. });
  252. },
  253. /**
  254. * Success-callback for <code>loadHostsFromServer</code>
  255. * Parse hosts response and wrap them into Ember.Object
  256. * @param {object} data
  257. * @method _loadHostsFromServerSuccessCallback
  258. * @private
  259. */
  260. _loadHostsFromServerSuccessCallback: function (data) {
  261. var wrappedHosts = [];
  262. data.items.forEach(function (host) {
  263. var hostComponents = [];
  264. var diskInfo = host.Hosts.disk_info.filter(function(item) {
  265. return /^ext|^ntfs|^fat|^xfs/i.test(item.type);
  266. });
  267. if (diskInfo.length) {
  268. diskInfo = diskInfo.reduce(function(a, b) {
  269. return {
  270. available: parseInt(a.available) + parseInt(b.available),
  271. size: parseInt(a.size) + parseInt(b.size)
  272. };
  273. });
  274. }
  275. host.host_components.forEach(function (hostComponent) {
  276. hostComponents.push(Em.Object.create({
  277. componentName: hostComponent.HostRoles.component_name,
  278. displayName: App.format.role(hostComponent.HostRoles.component_name, false)
  279. }));
  280. }, this);
  281. wrappedHosts.pushObject(Em.Object.create({
  282. id: host.Hosts.host_name,
  283. ip: host.Hosts.ip,
  284. osType: host.Hosts.os_type,
  285. osArch: host.Hosts.os_arch,
  286. hostName: host.Hosts.host_name,
  287. publicHostName: host.Hosts.public_host_name,
  288. cpu: host.Hosts.cpu_count,
  289. memory: host.Hosts.total_mem,
  290. diskTotal: numberUtils.bytesToSize(diskInfo.size, 0, undefined, 1024),
  291. diskFree: numberUtils.bytesToSize(diskInfo.available, 0, undefined, 1024),
  292. disksMounted: host.Hosts.disk_info.length,
  293. hostComponents: hostComponents
  294. }
  295. ));
  296. }, this);
  297. this.set('clusterHosts', wrappedHosts);
  298. },
  299. /**
  300. * Error-callback for <code>loadHostsFromServer</code>
  301. * @method _loadHostsFromServerErrorCallback
  302. * @private
  303. */
  304. _loadHostsFromServerErrorCallback: function () {
  305. this.set('clusterHosts', []);
  306. },
  307. /**
  308. * Load config groups from server if user is on the already installed cluster
  309. * If not - use loaded data form wizardStep7Controller
  310. * @param {string} serviceName
  311. * @method loadConfigGroups
  312. */
  313. loadConfigGroups: function (serviceName) {
  314. if (this.get('isInstaller')) {
  315. var configGroups = App.router.get('wizardStep7Controller.selectedService.configGroups').slice(0);
  316. var originalConfigGroups = this.generateOriginalConfigGroups(configGroups);
  317. this.setProperties({
  318. configGroups: configGroups,
  319. originalConfigGroups: originalConfigGroups,
  320. isLoaded: true
  321. });
  322. }
  323. else {
  324. this.set('serviceName', serviceName);
  325. App.ajax.send({
  326. name: 'service.load_config_groups',
  327. data: {
  328. serviceName: serviceName
  329. },
  330. sender: this,
  331. success: '_onLoadConfigGroupsSuccess'
  332. });
  333. }
  334. },
  335. /**
  336. * Success-callback for <code>loadConfigGroups</code>
  337. * @param {object} data
  338. * @private
  339. * @method _onLoadConfigGroupsSuccess
  340. */
  341. _onLoadConfigGroupsSuccess: function (data) {
  342. var serviceName = this.get('serviceName');
  343. App.configGroupsMapper.map(data, false, [serviceName]);
  344. var configGroups = App.ServiceConfigGroup.find().filterProperty('serviceName', serviceName);
  345. var rawConfigGroups = this.generateOriginalConfigGroups(configGroups);
  346. var groupToTypeToTagMap = {};
  347. rawConfigGroups.forEach(function (item) {
  348. if (Array.isArray(item.desired_configs)) {
  349. item.desired_configs.forEach(function (config) {
  350. if (!groupToTypeToTagMap[item.name]) {
  351. groupToTypeToTagMap[item.name] = {};
  352. }
  353. groupToTypeToTagMap[item.name][config.type] = config.tag;
  354. });
  355. }
  356. });
  357. this.set('configGroups', configGroups);
  358. this.set('originalConfigGroups', rawConfigGroups);
  359. this.loadProperties(groupToTypeToTagMap);
  360. this.set('isLoaded', true);
  361. },
  362. /**
  363. *
  364. * @param {Array} configGroups
  365. * @returns {Array}
  366. */
  367. generateOriginalConfigGroups: function(configGroups) {
  368. var self = this;
  369. return configGroups.map(function (item) {
  370. return self.createOriginalRecord(item);
  371. });
  372. },
  373. /**
  374. * Return object to use for loading to model with correct names for object keys
  375. * @param configGroup - config group object from model
  376. * @returns {Object}
  377. */
  378. createOriginalRecord: function (configGroup) {
  379. return {
  380. id: configGroup.get('id'),
  381. name: configGroup.get('name'),
  382. service_name: configGroup.get('serviceName'),
  383. description: configGroup.get('description'),
  384. hosts: configGroup.get('hosts').slice(0),
  385. service_id: configGroup.get('serviceName'),
  386. desired_configs: configGroup.get('desiredConfigs'),
  387. is_default: configGroup.get('isDefault'),
  388. child_config_groups: configGroup.get('childConfigGroups') ? configGroup.get('childConfigGroups').mapProperty('id') : [],
  389. parent_config_group_id: configGroup.get('parentConfigGroup.id'),
  390. properties: configGroup.get('properties')
  391. };
  392. },
  393. /**
  394. *
  395. * @param {object} groupToTypeToTagMap
  396. * @method loadProperties
  397. */
  398. loadProperties: function (groupToTypeToTagMap) {
  399. var typeTagToGroupMap = {};
  400. var urlParams = [];
  401. for (var group in groupToTypeToTagMap) {
  402. var overrideTypeTags = groupToTypeToTagMap[group];
  403. for (var type in overrideTypeTags) {
  404. var tag = overrideTypeTags[type];
  405. typeTagToGroupMap[type + "///" + tag] = group;
  406. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  407. }
  408. }
  409. var params = urlParams.join('|');
  410. if (urlParams.length) {
  411. App.ajax.send({
  412. name: 'config.host_overrides',
  413. sender: this,
  414. data: {
  415. params: params,
  416. typeTagToGroupMap: typeTagToGroupMap
  417. },
  418. success: '_onLoadPropertiesSuccess'
  419. });
  420. }
  421. },
  422. /**
  423. * Success-callback for <code>loadProperties</code>
  424. * @param {object} data
  425. * @param {object} opt
  426. * @param {object} params
  427. * @private
  428. * @method _onLoadPropertiesSuccess
  429. */
  430. _onLoadPropertiesSuccess: function (data, opt, params) {
  431. var groupToPropertiesMap = {};
  432. data.items.forEach(function (configs) {
  433. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  434. if (!groupToPropertiesMap[group]) {
  435. groupToPropertiesMap[group] = [];
  436. }
  437. for (var config in configs.properties) {
  438. groupToPropertiesMap[group].push({
  439. name: config,
  440. value: configs.properties[config],
  441. type: configs.type
  442. });
  443. }
  444. }, this);
  445. for (var g in groupToPropertiesMap) {
  446. this.get('configGroups').findProperty('name', g).set('properties', groupToPropertiesMap[g]);
  447. }
  448. },
  449. /**
  450. * Show popup with properties overridden in the selected config group
  451. * @method showProperties
  452. */
  453. showProperties: function () {
  454. var properties = this.get('selectedConfigGroup.propertiesList').htmlSafe();
  455. if (properties) {
  456. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properties);
  457. }
  458. },
  459. /**
  460. * Show popup with hosts to add to the selected config group
  461. * @returns {boolean}
  462. * @method addHosts
  463. */
  464. addHosts: function () {
  465. if (this.get('selectedConfigGroup.isAddHostsDisabled')) {
  466. return false;
  467. }
  468. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  469. var popupDescription = {
  470. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  471. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message').format(this.get('selectedConfigGroup.displayName'))
  472. };
  473. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, this.get('componentsForFilter'), this.addHostsCallback.bind(this), popupDescription);
  474. },
  475. /**
  476. * Remove selected hosts from default group (<code>selectedConfigGroup.parentConfigGroup</code>) and add them to the <code>selectedConfigGroup</code>
  477. * @param {string[]} selectedHosts
  478. * @method addHostsCallback
  479. */
  480. addHostsCallback: function (selectedHosts) {
  481. if (selectedHosts) {
  482. var group = this.get('selectedConfigGroup');
  483. var parentGroupHosts = group.get('parentConfigGroup.hosts');
  484. var newHostsForParentGroup = parentGroupHosts.filter(function(hostName) {
  485. return !selectedHosts.contains(hostName);
  486. });
  487. group.get('hosts').pushObjects(selectedHosts);
  488. group.set('parentConfigGroup.hosts', newHostsForParentGroup);
  489. }
  490. },
  491. /**
  492. * Delete hosts from <code>selectedConfigGroup</code> and move them to the Default group (<code>selectedConfigGroup.parentConfigGroup</code>)
  493. * @method deleteHosts
  494. */
  495. deleteHosts: function () {
  496. if (this.get('isDeleteHostsDisabled')) {
  497. return;
  498. }
  499. var hosts = this.get('selectedHosts').slice();
  500. var newHosts = [];
  501. this.get('selectedConfigGroup.parentConfigGroup.hosts').pushObjects(hosts);
  502. this.get('selectedConfigGroup.hosts').forEach(function(host) {
  503. if (!hosts.contains(host)) {
  504. newHosts.pushObject(host);
  505. }
  506. });
  507. this.set('selectedConfigGroup.hosts', newHosts);
  508. this.set('selectedHosts', []);
  509. },
  510. /**
  511. * show popup for confirmation delete config group
  512. * @method confirmDelete
  513. */
  514. confirmDelete: function () {
  515. var self = this;
  516. App.showConfirmationPopup(function() {
  517. self.deleteConfigGroup();
  518. });
  519. },
  520. /**
  521. * delete selected config group (stored in the <code>selectedConfigGroup</code>)
  522. * then select default config group
  523. * @method deleteConfigGroup
  524. */
  525. deleteConfigGroup: function () {
  526. var selectedConfigGroup = this.get('selectedConfigGroup');
  527. if (this.get('isDeleteGroupDisabled')) {
  528. return;
  529. }
  530. //move hosts of group to default group (available hosts)
  531. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  532. this.deleteHosts();
  533. this.get('configGroups').removeObject(selectedConfigGroup);
  534. App.configGroupsMapper.deleteRecord(selectedConfigGroup);
  535. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  536. this.propertyDidChange('groupDeleteTrigger');
  537. },
  538. /**
  539. * rename new config group (not allowed for default group)
  540. * @method renameConfigGroup
  541. */
  542. renameConfigGroup: function () {
  543. if(this.get('selectedConfigGroup.isDefault')) {
  544. return;
  545. }
  546. var self = this;
  547. var renameGroupPopup = App.ModalPopup.show({
  548. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  549. bodyClass: Em.View.extend({
  550. templateName: require('templates/main/service/new_config_group')
  551. }),
  552. configGroupName: self.get('selectedConfigGroup.name'),
  553. configGroupDesc: self.get('selectedConfigGroup.description'),
  554. warningMessage: null,
  555. isDescriptionDirty: false,
  556. validate: function () {
  557. var warningMessage = '';
  558. var originalGroup = self.get('selectedConfigGroup');
  559. var groupName = this.get('configGroupName').trim();
  560. if (originalGroup.get('description') !== this.get('configGroupDesc') && !this.get('isDescriptionDirty')) {
  561. this.set('isDescriptionDirty', true);
  562. }
  563. if (originalGroup.get('name').trim() === groupName) {
  564. if (this.get('isDescriptionDirty')) {
  565. warningMessage = '';
  566. } else {
  567. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  568. }
  569. } else {
  570. if (self.get('configGroups').mapProperty('name').contains(groupName)) {
  571. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  572. }
  573. else if (groupName && !validator.isValidConfigGroupName(groupName)) {
  574. warningMessage = Em.I18n.t("form.validator.configGroupName");
  575. }
  576. }
  577. this.set('warningMessage', warningMessage);
  578. }.observes('configGroupName', 'configGroupDesc'),
  579. disablePrimary: function () {
  580. return !(this.get('configGroupName').trim().length > 0 && (this.get('warningMessage') !== null && !this.get('warningMessage')));
  581. }.property('warningMessage', 'configGroupName', 'configGroupDesc'),
  582. onPrimary: function () {
  583. self.get('selectedConfigGroup').setProperties({
  584. name: this.get('configGroupName'),
  585. description: this.get('configGroupDesc')
  586. });
  587. App.store.commit();
  588. this.hide();
  589. }
  590. });
  591. this.set('renameGroupPopup', renameGroupPopup);
  592. },
  593. /**
  594. * add new config group (or copy existing)
  595. * @param {boolean} duplicated true - copy <code>selectedConfigGroup</code>, false - create a new one
  596. * @method addConfigGroup
  597. */
  598. addConfigGroup: function (duplicated) {
  599. duplicated = (duplicated === true);
  600. var self = this;
  601. var addGroupPopup = App.ModalPopup.show({
  602. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  603. bodyClass: Em.View.extend({
  604. templateName: require('templates/main/service/new_config_group')
  605. }),
  606. configGroupName: duplicated ? self.get('selectedConfigGroup.name') + ' Copy' : "",
  607. configGroupDesc: duplicated ? self.get('selectedConfigGroup.description') + ' (Copy)' : "",
  608. warningMessage: '',
  609. didInsertElement: function(){
  610. this._super();
  611. this.validate();
  612. this.$('input').focus();
  613. },
  614. validate: function () {
  615. var warningMessage = '';
  616. var groupName = this.get('configGroupName').trim();
  617. if (self.get('configGroups').mapProperty('name').contains(groupName)) {
  618. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  619. }
  620. else if (groupName && !validator.isValidConfigGroupName(groupName)) {
  621. warningMessage = Em.I18n.t("form.validator.configGroupName");
  622. }
  623. this.set('warningMessage', warningMessage);
  624. }.observes('configGroupName'),
  625. disablePrimary: function () {
  626. return !(this.get('configGroupName').trim().length > 0 && !this.get('warningMessage'));
  627. }.property('warningMessage', 'configGroupName'),
  628. onPrimary: function () {
  629. var defaultConfigGroup = self.get('configGroups').findProperty('isDefault'),
  630. properties = [], serviceName = self.get('serviceName'),
  631. groupName = this.get('configGroupName').trim(),
  632. newGroupId = (new Date()).getTime();
  633. if (duplicated) {
  634. self.get('selectedConfigGroup.properties').forEach(function (item) {
  635. var property = App.ServiceConfigProperty.create($.extend(false, {}, item));
  636. property.set('group', App.ServiceConfigGroup.find(newGroupId));
  637. properties.push(property);
  638. });
  639. }
  640. App.store.load(App.ServiceConfigGroup, {
  641. id: newGroupId,
  642. name: groupName,
  643. description: this.get('configGroupDesc'),
  644. isDefault: false,
  645. parent_config_group_id: App.ServiceConfigGroup.getParentConfigGroupId(serviceName),
  646. service_id: serviceName,
  647. service_name: serviceName,
  648. hosts: [],
  649. desired_configs: duplicated ? self.get('selectedConfigGroup.desiredConfigs') : [],
  650. properties: duplicated ? properties : [],
  651. is_temporary: true
  652. });
  653. App.store.commit();
  654. var childConfigGroups = defaultConfigGroup.get('childConfigGroups').mapProperty('id');
  655. childConfigGroups.push(newGroupId);
  656. App.store.load(App.ServiceConfigGroup, App.configGroupsMapper.generateDefaultGroup(self.get('serviceName'), defaultConfigGroup.get('hosts'), childConfigGroups));
  657. App.store.commit();
  658. self.get('configGroups').pushObject(App.ServiceConfigGroup.find(newGroupId));
  659. this.hide();
  660. }
  661. });
  662. this.set('addGroupPopup', addGroupPopup);
  663. },
  664. /**
  665. * Duplicate existing config group
  666. * @method duplicateConfigGroup
  667. */
  668. duplicateConfigGroup: function() {
  669. this.addConfigGroup(true);
  670. },
  671. /**
  672. * Show popup with config groups
  673. * User may edit/create/delete them
  674. * @param {Em.Controller} controller
  675. * @param {App.Service} service
  676. * @returns {App.ModalPopup}
  677. * @method manageConfigurationGroups
  678. */
  679. manageConfigurationGroups: function (controller, service) {
  680. var configsController = this;
  681. var serviceData = (controller && controller.get('selectedService')) || service;
  682. var serviceName = serviceData.get('serviceName');
  683. var displayName = serviceData.get('displayName');
  684. this.setProperties({
  685. isInstaller: !!controller,
  686. serviceName: serviceName
  687. });
  688. if (controller) {
  689. configsController.set('isAddService', controller.get('content.controllerName') == 'addServiceController');
  690. }
  691. return App.ModalPopup.show({
  692. header: Em.I18n.t('services.service.config_groups_popup.header').format(displayName),
  693. bodyClass: App.MainServiceManageConfigGroupView.extend({
  694. serviceName: serviceName,
  695. displayName: displayName,
  696. controller: configsController
  697. }),
  698. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  699. primary: Em.I18n.t('common.save'),
  700. autoHeight: false,
  701. subViewController: configsController,
  702. /**
  703. * handle onPrimary action particularly in wizard
  704. * @param {Em.Controller} controller
  705. * @param {object} modifiedConfigGroups
  706. */
  707. onPrimaryWizard: function (controller, modifiedConfigGroups) {
  708. controller.set('selectedService.configGroups', configsController.get('configGroups'));
  709. controller.selectedServiceObserver();
  710. if (controller.get('name') == "wizardStep7Controller") {
  711. if (controller.get('selectedService.selected') === false && modifiedConfigGroups.toDelete.length > 0) {
  712. controller.setGroupsToDelete(modifiedConfigGroups.toDelete);
  713. }
  714. configsController.persistConfigGroups();
  715. this.updateConfigGroupOnServicePage();
  716. }
  717. this.hide();
  718. },
  719. onClose: function () {
  720. //<code>_super</code> has to be called before <code>resetGroupChanges</code>
  721. var originalGroups = this.get('subViewController.originalConfigGroups').slice(0);
  722. this._super();
  723. this.resetGroupChanges(originalGroups);
  724. },
  725. onSecondary: function () {
  726. this.onClose();
  727. },
  728. /**
  729. * reset group changes made by user
  730. * @param {Array} originalGroups
  731. */
  732. resetGroupChanges: function (originalGroups) {
  733. if (this.get('subViewController.isHostsModified')) {
  734. App.ServiceConfigGroup.find().clear();
  735. App.store.commit();
  736. App.store.loadMany(App.ServiceConfigGroup, originalGroups);
  737. App.store.commit();
  738. }
  739. },
  740. /**
  741. * run requests which delete config group and clear its hosts
  742. * @param {Function} finishFunction
  743. * @param {object} modifiedConfigGroups
  744. */
  745. runClearCGQueue: function (finishFunction, modifiedConfigGroups) {
  746. var counter = 0;
  747. var dfd = $.Deferred();
  748. var doneFunction = function (xhr, text, errorThrown) {
  749. counter--;
  750. if (counter === 0) dfd.resolve();
  751. finishFunction(xhr, text, errorThrown);
  752. };
  753. modifiedConfigGroups.toClearHosts.forEach(function (cg) {
  754. counter++;
  755. configsController.updateConfigurationGroup(cg, doneFunction, doneFunction)
  756. }, this);
  757. modifiedConfigGroups.toDelete.forEach(function (cg) {
  758. counter++;
  759. configsController.deleteConfigurationGroup(cg, doneFunction, doneFunction);
  760. }, this);
  761. if (counter === 0) dfd.resolve();
  762. return dfd.promise();
  763. },
  764. /**
  765. * run requests which change properties of config group
  766. * @param {Function} finishFunction
  767. * @param {object} modifiedConfigGroups
  768. */
  769. runModifyCGQueue: function (finishFunction, modifiedConfigGroups) {
  770. var counter = 0;
  771. var dfd = $.Deferred();
  772. var doneFunction = function (xhr, text, errorThrown) {
  773. counter--;
  774. if (counter === 0) dfd.resolve();
  775. finishFunction(xhr, text, errorThrown);
  776. };
  777. modifiedConfigGroups.toSetHosts.forEach(function (cg) {
  778. counter++;
  779. configsController.updateConfigurationGroup(cg, doneFunction, doneFunction);
  780. }, this);
  781. if (counter === 0) dfd.resolve();
  782. return dfd.promise();
  783. },
  784. /**
  785. * run requests which create new config group
  786. * @param {Function} finishFunction
  787. * @param {object} modifiedConfigGroups
  788. */
  789. runCreateCGQueue: function (finishFunction, modifiedConfigGroups) {
  790. var counter = 0;
  791. var dfd = $.Deferred();
  792. var doneFunction = function (xhr, text, errorThrown) {
  793. counter--;
  794. if (counter === 0) dfd.resolve();
  795. finishFunction(xhr, text, errorThrown);
  796. };
  797. modifiedConfigGroups.toCreate.forEach(function (cg) {
  798. counter++;
  799. configsController.postNewConfigurationGroup(cg, doneFunction);
  800. }, this);
  801. if (counter === 0) dfd.resolve();
  802. return dfd.promise();
  803. },
  804. onPrimary: function () {
  805. var modifiedConfigGroups = configsController.get('hostsModifiedConfigGroups');
  806. var errors = [];
  807. var self = this;
  808. var finishFunction = function (xhr, text, errorThrown) {
  809. if (xhr && errorThrown) {
  810. var error = xhr.status + "(" + errorThrown + ") ";
  811. try {
  812. var json = $.parseJSON(xhr.responseText);
  813. error += json.message;
  814. } catch (err) {
  815. }
  816. errors.push(error);
  817. }
  818. };
  819. // Save modified config-groups
  820. if (controller) {
  821. //called only in Wizard
  822. return this.onPrimaryWizard(controller, modifiedConfigGroups);
  823. }
  824. this.runClearCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  825. self.runModifyCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  826. self.runCreateCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  827. if (errors.length > 0) {
  828. self.get('subViewController').set('errorMessage', errors.join(". "));
  829. } else {
  830. if (!self.get('isAddService') && !self.get('isInstaller') && !modifiedConfigGroups.toCreate.everyProperty('properties.length', 0)) {
  831. //update service config versions only if it is service configs page and at least one newly created group had properties
  832. App.router.get('mainServiceInfoConfigsController').loadServiceConfigVersions().done(function () {
  833. self.updateConfigGroupOnServicePage();
  834. self.hide();
  835. });
  836. } else {
  837. self.updateConfigGroupOnServicePage();
  838. self.hide();
  839. }
  840. }
  841. });
  842. });
  843. });
  844. },
  845. updateConfigGroupOnServicePage: function () {
  846. var selectedConfigGroup = configsController.get('selectedConfigGroup');
  847. var managedConfigGroups = configsController.get('configGroups').slice(0);
  848. if (!controller) {
  849. controller = App.router.get('mainServiceInfoConfigsController');
  850. //controller.set('configGroups', managedConfigGroups);
  851. controller.loadConfigGroups([controller.get('content.serviceName')]);
  852. } else {
  853. controller.set('selectedService.configGroups', managedConfigGroups);
  854. }
  855. var selectEventObject = {};
  856. //check whether selectedConfigGroup exists
  857. if (selectedConfigGroup && controller.get('configGroups').someProperty('name', selectedConfigGroup.get('name'))) {
  858. selectEventObject.context = selectedConfigGroup;
  859. } else {
  860. selectEventObject.context = managedConfigGroups.findProperty('isDefault', true);
  861. }
  862. controller.selectConfigGroup(selectEventObject);
  863. },
  864. updateButtons: function () {
  865. var modified = this.get('subViewController.isHostsModified');
  866. this.set('disablePrimary', !modified);
  867. }.observes('subViewController.isHostsModified')
  868. });
  869. }
  870. });