manage_config_groups_controller.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. this.hide();
  588. }
  589. });
  590. this.set('renameGroupPopup', renameGroupPopup);
  591. },
  592. /**
  593. * add new config group (or copy existing)
  594. * @param {boolean} duplicated true - copy <code>selectedConfigGroup</code>, false - create a new one
  595. * @method addConfigGroup
  596. */
  597. addConfigGroup: function (duplicated) {
  598. duplicated = (duplicated === true);
  599. var self = this;
  600. var addGroupPopup = App.ModalPopup.show({
  601. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  602. bodyClass: Em.View.extend({
  603. templateName: require('templates/main/service/new_config_group')
  604. }),
  605. configGroupName: duplicated ? self.get('selectedConfigGroup.name') + ' Copy' : "",
  606. configGroupDesc: duplicated ? self.get('selectedConfigGroup.description') + ' (Copy)' : "",
  607. warningMessage: '',
  608. didInsertElement: function(){
  609. this._super();
  610. this.validate();
  611. this.$('input').focus();
  612. },
  613. validate: function () {
  614. var warningMessage = '';
  615. var groupName = this.get('configGroupName').trim();
  616. if (self.get('configGroups').mapProperty('name').contains(groupName)) {
  617. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  618. }
  619. else if (groupName && !validator.isValidConfigGroupName(groupName)) {
  620. warningMessage = Em.I18n.t("form.validator.configGroupName");
  621. }
  622. this.set('warningMessage', warningMessage);
  623. }.observes('configGroupName'),
  624. disablePrimary: function () {
  625. return !(this.get('configGroupName').trim().length > 0 && !this.get('warningMessage'));
  626. }.property('warningMessage', 'configGroupName'),
  627. onPrimary: function () {
  628. var defaultConfigGroup = self.get('configGroups').findProperty('isDefault'),
  629. properties = [], serviceName = self.get('serviceName'),
  630. groupName = this.get('configGroupName').trim(),
  631. newGroupId = (new Date()).getTime();
  632. if (duplicated) {
  633. self.get('selectedConfigGroup.properties').forEach(function (item) {
  634. var property = App.ServiceConfigProperty.create($.extend(false, {}, item));
  635. property.set('group', App.ServiceConfigGroup.find(newGroupId));
  636. properties.push(property);
  637. });
  638. }
  639. App.store.load(App.ServiceConfigGroup, {
  640. id: newGroupId,
  641. name: groupName,
  642. description: this.get('configGroupDesc'),
  643. isDefault: false,
  644. parent_config_group_id: App.ServiceConfigGroup.getParentConfigGroupId(serviceName),
  645. service_id: serviceName,
  646. service_name: serviceName,
  647. hosts: [],
  648. desired_configs: duplicated ? self.get('selectedConfigGroup.desiredConfigs') : [],
  649. properties: duplicated ? properties : [],
  650. is_temporary: true
  651. });
  652. App.store.commit();
  653. var childConfigGroups = defaultConfigGroup.get('childConfigGroups').mapProperty('id');
  654. childConfigGroups.push(newGroupId);
  655. App.store.load(App.ServiceConfigGroup, App.configGroupsMapper.generateDefaultGroup(self.get('serviceName'), defaultConfigGroup.get('hosts'), childConfigGroups));
  656. App.store.commit();
  657. self.get('configGroups').pushObject(App.ServiceConfigGroup.find(newGroupId));
  658. this.hide();
  659. }
  660. });
  661. this.set('addGroupPopup', addGroupPopup);
  662. },
  663. /**
  664. * Duplicate existing config group
  665. * @method duplicateConfigGroup
  666. */
  667. duplicateConfigGroup: function() {
  668. this.addConfigGroup(true);
  669. },
  670. /**
  671. * Show popup with config groups
  672. * User may edit/create/delete them
  673. * @param {Em.Controller} controller
  674. * @param {App.Service} service
  675. * @returns {App.ModalPopup}
  676. * @method manageConfigurationGroups
  677. */
  678. manageConfigurationGroups: function (controller, service) {
  679. var configsController = this;
  680. var serviceData = (controller && controller.get('selectedService')) || service;
  681. var serviceName = serviceData.get('serviceName');
  682. var displayName = serviceData.get('displayName');
  683. this.setProperties({
  684. isInstaller: !!controller,
  685. serviceName: serviceName
  686. });
  687. if (controller) {
  688. configsController.set('isAddService', controller.get('content.controllerName') == 'addServiceController');
  689. }
  690. return App.ModalPopup.show({
  691. header: Em.I18n.t('services.service.config_groups_popup.header').format(displayName),
  692. bodyClass: App.MainServiceManageConfigGroupView.extend({
  693. serviceName: serviceName,
  694. displayName: displayName,
  695. controller: configsController
  696. }),
  697. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  698. primary: Em.I18n.t('common.save'),
  699. autoHeight: false,
  700. subViewController: configsController,
  701. /**
  702. * handle onPrimary action particularly in wizard
  703. * @param {Em.Controller} controller
  704. * @param {object} modifiedConfigGroups
  705. */
  706. onPrimaryWizard: function (controller, modifiedConfigGroups) {
  707. controller.set('selectedService.configGroups', configsController.get('configGroups'));
  708. controller.selectedServiceObserver();
  709. if (controller.get('name') == "wizardStep7Controller") {
  710. if (controller.get('selectedService.selected') === false && modifiedConfigGroups.toDelete.length > 0) {
  711. controller.setGroupsToDelete(modifiedConfigGroups.toDelete);
  712. }
  713. configsController.persistConfigGroups();
  714. this.updateConfigGroupOnServicePage();
  715. }
  716. this.hide();
  717. },
  718. onClose: function () {
  719. //<code>_super</code> has to be called before <code>resetGroupChanges</code>
  720. var originalGroups = this.get('subViewController.originalConfigGroups').slice(0);
  721. this._super();
  722. this.resetGroupChanges(originalGroups);
  723. },
  724. onSecondary: function () {
  725. this.onClose();
  726. },
  727. /**
  728. * reset group changes made by user
  729. * @param {Array} originalGroups
  730. */
  731. resetGroupChanges: function (originalGroups) {
  732. if (this.get('subViewController.isHostsModified')) {
  733. App.ServiceConfigGroup.find().clear();
  734. App.store.commit();
  735. App.store.loadMany(App.ServiceConfigGroup, originalGroups);
  736. App.store.commit();
  737. }
  738. },
  739. /**
  740. * run requests which delete config group and clear its hosts
  741. * @param {Function} finishFunction
  742. * @param {object} modifiedConfigGroups
  743. */
  744. runClearCGQueue: function (finishFunction, modifiedConfigGroups) {
  745. var counter = 0;
  746. var dfd = $.Deferred();
  747. var doneFunction = function (xhr, text, errorThrown) {
  748. counter--;
  749. if (counter === 0) dfd.resolve();
  750. finishFunction(xhr, text, errorThrown);
  751. };
  752. modifiedConfigGroups.toClearHosts.forEach(function (cg) {
  753. counter++;
  754. configsController.updateConfigurationGroup(cg, doneFunction, doneFunction)
  755. }, this);
  756. modifiedConfigGroups.toDelete.forEach(function (cg) {
  757. counter++;
  758. configsController.deleteConfigurationGroup(cg, doneFunction, doneFunction);
  759. }, this);
  760. if (counter === 0) dfd.resolve();
  761. return dfd.promise();
  762. },
  763. /**
  764. * run requests which change properties of config group
  765. * @param {Function} finishFunction
  766. * @param {object} modifiedConfigGroups
  767. */
  768. runModifyCGQueue: function (finishFunction, modifiedConfigGroups) {
  769. var counter = 0;
  770. var dfd = $.Deferred();
  771. var doneFunction = function (xhr, text, errorThrown) {
  772. counter--;
  773. if (counter === 0) dfd.resolve();
  774. finishFunction(xhr, text, errorThrown);
  775. };
  776. modifiedConfigGroups.toSetHosts.forEach(function (cg) {
  777. counter++;
  778. configsController.updateConfigurationGroup(cg, doneFunction, doneFunction);
  779. }, this);
  780. if (counter === 0) dfd.resolve();
  781. return dfd.promise();
  782. },
  783. /**
  784. * run requests which create new config group
  785. * @param {Function} finishFunction
  786. * @param {object} modifiedConfigGroups
  787. */
  788. runCreateCGQueue: function (finishFunction, modifiedConfigGroups) {
  789. var counter = 0;
  790. var dfd = $.Deferred();
  791. var doneFunction = function (xhr, text, errorThrown) {
  792. counter--;
  793. if (counter === 0) dfd.resolve();
  794. finishFunction(xhr, text, errorThrown);
  795. };
  796. modifiedConfigGroups.toCreate.forEach(function (cg) {
  797. counter++;
  798. configsController.postNewConfigurationGroup(cg, doneFunction);
  799. }, this);
  800. if (counter === 0) dfd.resolve();
  801. return dfd.promise();
  802. },
  803. onPrimary: function () {
  804. var modifiedConfigGroups = configsController.get('hostsModifiedConfigGroups');
  805. var errors = [];
  806. var self = this;
  807. var finishFunction = function (xhr, text, errorThrown) {
  808. if (xhr && errorThrown) {
  809. var error = xhr.status + "(" + errorThrown + ") ";
  810. try {
  811. var json = $.parseJSON(xhr.responseText);
  812. error += json.message;
  813. } catch (err) {
  814. }
  815. errors.push(error);
  816. }
  817. };
  818. // Save modified config-groups
  819. if (controller) {
  820. //called only in Wizard
  821. return this.onPrimaryWizard(controller, modifiedConfigGroups);
  822. }
  823. this.runClearCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  824. self.runModifyCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  825. self.runCreateCGQueue(finishFunction, modifiedConfigGroups).done(function () {
  826. if (errors.length > 0) {
  827. self.get('subViewController').set('errorMessage', errors.join(". "));
  828. } else {
  829. if (!self.get('isAddService') && !self.get('isInstaller') && !modifiedConfigGroups.toCreate.everyProperty('properties.length', 0)) {
  830. //update service config versions only if it is service configs page and at least one newly created group had properties
  831. App.router.get('mainServiceInfoConfigsController').loadServiceConfigVersions().done(function () {
  832. self.updateConfigGroupOnServicePage();
  833. self.hide();
  834. });
  835. } else {
  836. self.updateConfigGroupOnServicePage();
  837. self.hide();
  838. }
  839. }
  840. });
  841. });
  842. });
  843. },
  844. updateConfigGroupOnServicePage: function () {
  845. var selectedConfigGroup = configsController.get('selectedConfigGroup');
  846. var managedConfigGroups = configsController.get('configGroups').slice(0);
  847. if (!controller) {
  848. controller = App.router.get('mainServiceInfoConfigsController');
  849. //controller.set('configGroups', managedConfigGroups);
  850. controller.loadConfigGroups([controller.get('content.serviceName')]);
  851. } else {
  852. controller.set('selectedService.configGroups', managedConfigGroups);
  853. }
  854. var selectEventObject = {};
  855. //check whether selectedConfigGroup exists
  856. if (selectedConfigGroup && controller.get('configGroups').someProperty('name', selectedConfigGroup.get('name'))) {
  857. selectEventObject.context = selectedConfigGroup;
  858. } else {
  859. selectEventObject.context = managedConfigGroups.findProperty('isDefault', true);
  860. }
  861. controller.selectConfigGroup(selectEventObject);
  862. },
  863. updateButtons: function () {
  864. var modified = this.get('subViewController.isHostsModified');
  865. this.set('disablePrimary', !modified);
  866. }.observes('subViewController.isHostsModified')
  867. });
  868. }
  869. });