manage_config_groups_controller.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. * List of available service components for <code>serviceName</code>
  85. * @type {{componentName: string, displayName: string, selected: boolean}[]}
  86. */
  87. componentsForFilter: function () {
  88. return App.StackServiceComponent.find().filterProperty('serviceName', this.get('serviceName')).map(function (component) {
  89. return Em.Object.create({
  90. componentName: component.get('componentName'),
  91. displayName: App.format.role(component.get('componentName')),
  92. selected: false
  93. });
  94. });
  95. }.property('serviceName'),
  96. /**
  97. * Determines when host may be deleted from config group
  98. * @type {boolean}
  99. */
  100. isDeleteHostsDisabled: function () {
  101. var selectedConfigGroup = this.get('selectedConfigGroup');
  102. if (selectedConfigGroup) {
  103. return selectedConfigGroup.isDefault || this.get('selectedHosts').length === 0;
  104. }
  105. return true;
  106. }.property('selectedConfigGroup', 'selectedConfigGroup.hosts.length', 'selectedHosts.length'),
  107. /**
  108. * Map with modified/deleted/created config groups
  109. * @type {{
  110. * toClearHosts: App.ConfigGroup[],
  111. * toDelete: App.ConfigGroup[],
  112. * toSetHosts: App.ConfigGroup[],
  113. * toCreate: App.ConfigGroup[]
  114. * }}
  115. */
  116. hostsModifiedConfigGroups: function () {
  117. if (!this.get('isLoaded')) {
  118. return false;
  119. }
  120. var groupsToClearHosts = [];
  121. var groupsToDelete = [];
  122. var groupsToSetHosts = [];
  123. var groupsToCreate = [];
  124. var groups = this.get('configGroups');
  125. var originalGroups = this.get('originalConfigGroups');
  126. // remove default group
  127. var originalGroupsCopy = originalGroups.without(originalGroups.findProperty('isDefault'));
  128. var originalGroupsIds = originalGroupsCopy.mapProperty('id');
  129. groups.forEach(function (group) {
  130. if (!group.get('isDefault')) {
  131. var originalGroup = originalGroupsCopy.findProperty('id', group.get('id'));
  132. if (originalGroup) {
  133. if (!(JSON.stringify(group.get('hosts').slice().sort()) === JSON.stringify(originalGroup.get('hosts').sort()))) {
  134. groupsToClearHosts.push(group.set('id', originalGroup.get('id')));
  135. if (group.get('hosts').length) {
  136. groupsToSetHosts.push(group.set('id', originalGroup.get('id')));
  137. }
  138. // should update name or description
  139. } else if (group.get('description') !== originalGroup.get('description') || group.get('name') !== originalGroup.get('name') ) {
  140. groupsToSetHosts.push(group.set('id', originalGroup.get('id')));
  141. }
  142. originalGroupsIds = originalGroupsIds.without(group.get('id'));
  143. } else {
  144. groupsToCreate.push(group);
  145. }
  146. }
  147. });
  148. originalGroupsIds.forEach(function (id) {
  149. groupsToDelete.push(originalGroupsCopy.findProperty('id', id));
  150. }, this);
  151. return {
  152. toClearHosts: groupsToClearHosts,
  153. toDelete: groupsToDelete,
  154. toSetHosts: groupsToSetHosts,
  155. toCreate: groupsToCreate,
  156. initialGroups: originalGroupsCopy
  157. };
  158. }.property('selectedConfigGroup.hosts.@each', 'selectedConfigGroup.hosts.length', 'selectedConfigGroup.description', 'configGroups', 'isLoaded'),
  159. /**
  160. * Determines if some changes were done with config groups
  161. * @use hostsModifiedConfigGroups
  162. * @type {boolean}
  163. */
  164. isHostsModified: function () {
  165. if (!this.get('isLoaded')) {
  166. return false;
  167. }
  168. var modifiedGroups = this.get('hostsModifiedConfigGroups');
  169. return Em.keys(modifiedGroups).map(function (key) {
  170. return Em.get(modifiedGroups[key], 'length');
  171. }).reduce(Em.sum) > 0;
  172. }.property('hostsModifiedConfigGroups'),
  173. /**
  174. * Resort config groups according to order:
  175. * default group first, other - last
  176. * @method resortConfigGroup
  177. */
  178. resortConfigGroup: function() {
  179. var configGroups = Em.copy(this.get('configGroups'));
  180. if(configGroups.length < 2) return;
  181. var defaultConfigGroup = configGroups.findProperty('isDefault');
  182. configGroups.removeObject(defaultConfigGroup);
  183. var sorted = [defaultConfigGroup].concat(configGroups.sortProperty('name'));
  184. this.removeObserver('configGroups.@each.name', this, 'resortConfigGroup');
  185. this.set('configGroups', sorted);
  186. this.addObserver('configGroups.@each.name', this, 'resortConfigGroup');
  187. }.observes('configGroups.@each.name'),
  188. /**
  189. * Load hosts from server or
  190. * get them from installerController if user on the install wizard
  191. * get them from isAddServiceController if user on the add service wizard
  192. * @method loadHosts
  193. */
  194. loadHosts: function() {
  195. this.set('isLoaded', false);
  196. if (this.get('isInstaller')) {
  197. var allHosts = this.get('isAddService') ? App.router.get('addServiceController').get('allHosts') : App.router.get('installerController').get('allHosts');
  198. this.set('clusterHosts', allHosts);
  199. this.loadConfigGroups(this.get('serviceName'));
  200. }
  201. else {
  202. this.loadHostsFromServer();
  203. }
  204. },
  205. /**
  206. * Request all hosts directly from server
  207. * @method loadHostsFromServer
  208. * @return {$.ajax}
  209. */
  210. loadHostsFromServer: function() {
  211. return App.ajax.send({
  212. name: 'hosts.config_groups',
  213. sender: this,
  214. data: {},
  215. success: '_loadHostsFromServerSuccessCallback',
  216. error: '_loadHostsFromServerErrorCallback'
  217. });
  218. },
  219. /**
  220. * Success-callback for <code>loadHostsFromServer</code>
  221. * Parse hosts response and wrap them into Ember.Object
  222. * @param {object} data
  223. * @method _loadHostsFromServerSuccessCallback
  224. * @private
  225. */
  226. _loadHostsFromServerSuccessCallback: function (data) {
  227. var wrappedHosts = [];
  228. data.items.forEach(function (host) {
  229. var hostComponents = [];
  230. var diskInfo = host.Hosts.disk_info.filter(function(item) {
  231. return /^ext|^ntfs|^fat|^xfs/i.test(item.type);
  232. });
  233. if (diskInfo.length) {
  234. diskInfo = diskInfo.reduce(function(a, b) {
  235. return {
  236. available: parseInt(a.available) + parseInt(b.available),
  237. size: parseInt(a.size) + parseInt(b.size)
  238. };
  239. });
  240. }
  241. host.host_components.forEach(function (hostComponent) {
  242. hostComponents.push(Em.Object.create({
  243. componentName: hostComponent.HostRoles.component_name,
  244. displayName: App.format.role(hostComponent.HostRoles.component_name)
  245. }));
  246. }, this);
  247. wrappedHosts.pushObject(Em.Object.create({
  248. id: host.Hosts.host_name,
  249. ip: host.Hosts.ip,
  250. osType: host.Hosts.os_type,
  251. osArch: host.Hosts.os_arch,
  252. hostName: host.Hosts.host_name,
  253. publicHostName: host.Hosts.public_host_name,
  254. cpu: host.Hosts.cpu_count,
  255. memory: host.Hosts.total_mem,
  256. diskTotal: numberUtils.bytesToSize(diskInfo.size, 0, undefined, 1024),
  257. diskFree: numberUtils.bytesToSize(diskInfo.available, 0, undefined, 1024),
  258. disksMounted: host.Hosts.disk_info.length,
  259. hostComponents: hostComponents
  260. }
  261. ));
  262. }, this);
  263. this.set('clusterHosts', wrappedHosts);
  264. this.loadConfigGroups(this.get('serviceName'));
  265. },
  266. /**
  267. * Error-callback for <code>loadHostsFromServer</code>
  268. * @method _loadHostsFromServerErrorCallback
  269. * @private
  270. */
  271. _loadHostsFromServerErrorCallback: function () {
  272. console.warn('ERROR: request to fetch all hosts failed');
  273. this.set('clusterHosts', []);
  274. this.loadConfigGroups(this.get('serviceName'));
  275. },
  276. /**
  277. * Load config groups from server if user is on the already installed cluster
  278. * If not - use loaded data form wizardStep7Controller
  279. * @param {string} serviceName
  280. * @method loadConfigGroups
  281. */
  282. loadConfigGroups: function (serviceName) {
  283. if (this.get('isInstaller')) {
  284. this.set('serviceName', serviceName);
  285. var configGroups = this.copyConfigGroups(App.router.get('wizardStep7Controller.selectedService.configGroups'));
  286. var originalConfigGroups = this.copyConfigGroups(configGroups);
  287. this.setProperties({
  288. configGroups: configGroups,
  289. originalConfigGroups: originalConfigGroups,
  290. isLoaded: true
  291. });
  292. }
  293. else {
  294. this.set('serviceName', serviceName);
  295. App.ajax.send({
  296. name: 'service.load_config_groups',
  297. data: {
  298. serviceName: serviceName
  299. },
  300. sender: this,
  301. success: '_onLoadConfigGroupsSuccess'
  302. });
  303. }
  304. },
  305. /**
  306. * Success-callback for <code>loadConfigGroups</code>
  307. * @param {object} data
  308. * @private
  309. * @method _onLoadConfigGroupsSuccess
  310. */
  311. _onLoadConfigGroupsSuccess: function (data) {
  312. var usedHosts = [];
  313. var unusedHosts = [];
  314. var serviceName = this.get('serviceName');
  315. var serviceDisplayName = App.StackService.find().findProperty('serviceName', this.get('serviceName')).get('displayName');
  316. var defaultConfigGroup = App.ConfigGroup.create({
  317. name: serviceDisplayName + " Default",
  318. description: "Default cluster level " + this.get('serviceName') + " configuration",
  319. isDefault: true,
  320. parentConfigGroup: null,
  321. service: this.get('content'),
  322. configSiteTags: [],
  323. serviceName: serviceName
  324. });
  325. if (data && data.items) {
  326. var groupToTypeToTagMap = {};
  327. var configGroups = [];
  328. data.items.forEach(function (configGroup) {
  329. configGroup = configGroup.ConfigGroup;
  330. var hostNames = configGroup.hosts.mapProperty('host_name');
  331. var newConfigGroup = App.ConfigGroup.create({
  332. id: configGroup.id,
  333. name: configGroup.group_name,
  334. description: configGroup.description,
  335. isDefault: false,
  336. parentConfigGroup: defaultConfigGroup,
  337. service: App.Service.find().findProperty('serviceName', configGroup.tag),
  338. hosts: hostNames,
  339. configSiteTags: [],
  340. properties: [],
  341. apiResponse: configGroup
  342. });
  343. usedHosts = usedHosts.concat(newConfigGroup.get('hosts'));
  344. configGroups.push(newConfigGroup);
  345. var newConfigGroupSiteTags = newConfigGroup.get('configSiteTags');
  346. configGroup.desired_configs.forEach(function (config) {
  347. newConfigGroupSiteTags.push(App.ConfigSiteTag.create({
  348. site: config.type,
  349. tag: config.tag
  350. }));
  351. if (!groupToTypeToTagMap[configGroup.group_name]) {
  352. groupToTypeToTagMap[configGroup.group_name] = {}
  353. }
  354. groupToTypeToTagMap[configGroup.group_name][config.type] = config.tag;
  355. });
  356. }, this);
  357. unusedHosts = this.get('clusterHosts').mapProperty('hostName');
  358. usedHosts.uniq().forEach(function (host) {
  359. unusedHosts = unusedHosts.without(host);
  360. }, this);
  361. defaultConfigGroup.set('childConfigGroups', configGroups);
  362. defaultConfigGroup.set('hosts', unusedHosts);
  363. var allGroups = [defaultConfigGroup].concat(configGroups);
  364. this.set('configGroups', allGroups);
  365. var originalGroups = this.copyConfigGroups(allGroups);
  366. this.set('originalConfigGroups', originalGroups);
  367. this.loadProperties(groupToTypeToTagMap);
  368. this.set('isLoaded', true);
  369. }
  370. },
  371. /**
  372. *
  373. * @param {object} groupToTypeToTagMap
  374. * @method loadProperties
  375. */
  376. loadProperties: function (groupToTypeToTagMap) {
  377. var typeTagToGroupMap = {};
  378. var urlParams = [];
  379. for (var group in groupToTypeToTagMap) {
  380. var overrideTypeTags = groupToTypeToTagMap[group];
  381. for (var type in overrideTypeTags) {
  382. var tag = overrideTypeTags[type];
  383. typeTagToGroupMap[type + "///" + tag] = group;
  384. urlParams.push('(type=' + type + '&tag=' + tag + ')');
  385. }
  386. }
  387. var params = urlParams.join('|');
  388. if (urlParams.length) {
  389. App.ajax.send({
  390. name: 'config.host_overrides',
  391. sender: this,
  392. data: {
  393. params: params,
  394. typeTagToGroupMap: typeTagToGroupMap
  395. },
  396. success: '_onLoadPropertiesSuccess'
  397. });
  398. }
  399. },
  400. /**
  401. * Success-callback for <code>loadProperties</code>
  402. * @param {object} data
  403. * @param {object} opt
  404. * @param {object} params
  405. * @private
  406. * @method _onLoadPropertiesSuccess
  407. */
  408. _onLoadPropertiesSuccess: function (data, opt, params) {
  409. data.items.forEach(function (configs) {
  410. var typeTagConfigs = [];
  411. var group = params.typeTagToGroupMap[configs.type + "///" + configs.tag];
  412. for (var config in configs.properties) {
  413. typeTagConfigs.push(Em.Object.create({
  414. name: config,
  415. value: configs.properties[config]
  416. }));
  417. }
  418. this.get('configGroups').findProperty('name', group).get('properties').pushObjects(typeTagConfigs);
  419. }, this);
  420. },
  421. /**
  422. * Show popup with properties overridden in the selected config group
  423. * @method showProperties
  424. */
  425. showProperties: function () {
  426. var properies = this.get('selectedConfigGroup.propertiesList').htmlSafe();
  427. if (properies) {
  428. App.showAlertPopup(Em.I18n.t('services.service.config_groups_popup.properties'), properies);
  429. }
  430. },
  431. /**
  432. * Show popup with hosts to add to the selected config group
  433. * @returns {boolean}
  434. * @method addHosts
  435. */
  436. addHosts: function () {
  437. if (this.get('selectedConfigGroup.isAddHostsDisabled')) {
  438. return false;
  439. }
  440. var availableHosts = this.get('selectedConfigGroup.availableHosts');
  441. var popupDescription = {
  442. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  443. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message').format(this.get('selectedConfigGroup.displayName'))
  444. };
  445. hostsManagement.launchHostsSelectionDialog(availableHosts, [], false, this.get('componentsForFilter'), this.addHostsCallback.bind(this), popupDescription);
  446. },
  447. /**
  448. * add hosts callback
  449. * @param {string[]} selectedHosts
  450. * @method addHostsCallback
  451. */
  452. addHostsCallback: function (selectedHosts) {
  453. if (selectedHosts) {
  454. var group = this.get('selectedConfigGroup');
  455. selectedHosts.forEach(function (hostName) {
  456. group.get('hosts').pushObject(hostName);
  457. group.get('parentConfigGroup.hosts').removeObject(hostName);
  458. });
  459. }
  460. },
  461. /**
  462. * delete hosts from group
  463. * @method deleteHosts
  464. */
  465. deleteHosts: function () {
  466. if (this.get('isDeleteHostsDisabled')) {
  467. return;
  468. }
  469. this.get('selectedHosts').slice().forEach(function (hostName) {
  470. this.get('selectedConfigGroup.parentConfigGroup.hosts').pushObject(hostName);
  471. this.get('selectedConfigGroup.hosts').removeObject(hostName);
  472. }, this);
  473. this.set('selectedHosts', []);
  474. },
  475. /**
  476. * show popup for confirmation delete config group
  477. * @method confirmDelete
  478. */
  479. confirmDelete: function () {
  480. var self = this;
  481. App.showConfirmationPopup(function() {
  482. self.deleteConfigGroup();
  483. });
  484. },
  485. /**
  486. * delete selected config group (stored in the <code>selectedConfigGroup</code>)
  487. * then select default config group
  488. * @method deleteConfigGroup
  489. */
  490. deleteConfigGroup: function () {
  491. var selectedConfigGroup = this.get('selectedConfigGroup');
  492. if (this.get('isDeleteGroupDisabled')) {
  493. return;
  494. }
  495. //move hosts of group to default group (available hosts)
  496. this.set('selectedHosts', selectedConfigGroup.get('hosts'));
  497. this.deleteHosts();
  498. this.get('configGroups').removeObject(selectedConfigGroup);
  499. var groupFromModel = App.ServiceConfigGroup.find().filterProperty('serviceName', selectedConfigGroup.get('service.serviceName')).findProperty('name', selectedConfigGroup.get('name'));
  500. if (groupFromModel) {
  501. App.configGroupsMapper.deleteRecord(groupFromModel);
  502. }
  503. this.set('selectedConfigGroup', this.get('configGroups').findProperty('isDefault'));
  504. },
  505. /**
  506. * rename new config group (not allowed for default group)
  507. * @method renameConfigGroup
  508. */
  509. renameConfigGroup: function () {
  510. if(this.get('selectedConfigGroup.isDefault')) {
  511. return;
  512. }
  513. var self = this;
  514. var renameGroupPopup = App.ModalPopup.show({
  515. header: Em.I18n.t('services.service.config_groups.rename_config_group_popup.header'),
  516. bodyClass: Em.View.extend({
  517. templateName: require('templates/main/service/new_config_group')
  518. }),
  519. configGroupName: self.get('selectedConfigGroup.name'),
  520. configGroupDesc: self.get('selectedConfigGroup.description'),
  521. warningMessage: null,
  522. isDescriptionDirty: false,
  523. validate: function () {
  524. var warningMessage = '';
  525. var originalGroup = self.get('selectedConfigGroup');
  526. var groupName = this.get('configGroupName').trim();
  527. if (originalGroup.get('description') !== this.get('configGroupDesc') && !this.get('isDescriptionDirty')) {
  528. this.set('isDescriptionDirty', true);
  529. }
  530. if (originalGroup.get('name').trim() === groupName) {
  531. if (this.get('isDescriptionDirty')) {
  532. warningMessage = '';
  533. } else {
  534. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  535. }
  536. } else {
  537. if (self.get('configGroups').mapProperty('name').contains(groupName)) {
  538. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  539. }
  540. else if (groupName && !validator.isValidConfigGroupName(groupName)) {
  541. warningMessage = Em.I18n.t("form.validator.configGroupName");
  542. }
  543. }
  544. this.set('warningMessage', warningMessage);
  545. }.observes('configGroupName', 'configGroupDesc'),
  546. disablePrimary: function () {
  547. return !(this.get('configGroupName').trim().length > 0 && (this.get('warningMessage') !== null && !this.get('warningMessage')));
  548. }.property('warningMessage', 'configGroupName', 'configGroupDesc'),
  549. onPrimary: function () {
  550. self.set('selectedConfigGroup.name', this.get('configGroupName'));
  551. self.set('selectedConfigGroup.description', this.get('configGroupDesc'));
  552. self.get('selectedConfigGroup.properties').forEach(function(property){
  553. property.set('group', self.get('selectedConfigGroup'));
  554. });
  555. this.hide();
  556. }
  557. });
  558. this.set('renameGroupPopup', renameGroupPopup);
  559. },
  560. /**
  561. * add new config group (or copy existing)
  562. * @param {boolean} duplicated true - copy <code>selectedConfigGroup</code>, false - create a new one
  563. * @method addConfigGroup
  564. */
  565. addConfigGroup: function (duplicated) {
  566. duplicated = (duplicated === true);
  567. var self = this;
  568. var addGroupPopup = App.ModalPopup.show({
  569. header: Em.I18n.t('services.service.config_groups.add_config_group_popup.header'),
  570. bodyClass: Em.View.extend({
  571. templateName: require('templates/main/service/new_config_group')
  572. }),
  573. configGroupName: duplicated ? self.get('selectedConfigGroup.name') + ' Copy' : "",
  574. configGroupDesc: duplicated ? self.get('selectedConfigGroup.description') + ' (Copy)' : "",
  575. warningMessage: '',
  576. didInsertElement: function(){
  577. this.validate();
  578. this.$('input').focus();
  579. },
  580. validate: function () {
  581. var warningMessage = '';
  582. var groupName = this.get('configGroupName').trim();
  583. if (self.get('configGroups').mapProperty('name').contains(groupName)) {
  584. warningMessage = Em.I18n.t("config.group.selection.dialog.err.name.exists");
  585. }
  586. else if (groupName && !validator.isValidConfigGroupName(groupName)) {
  587. warningMessage = Em.I18n.t("form.validator.configGroupName");
  588. }
  589. this.set('warningMessage', warningMessage);
  590. }.observes('configGroupName'),
  591. disablePrimary: function () {
  592. return !(this.get('configGroupName').trim().length > 0 && !this.get('warningMessage'));
  593. }.property('warningMessage', 'configGroupName'),
  594. onPrimary: function () {
  595. var defaultConfigGroup = self.get('configGroups').findProperty('isDefault');
  596. var properties = [];
  597. var newConfigGroupData = App.ConfigGroup.create({
  598. id: null,
  599. name: this.get('configGroupName').trim(),
  600. description: this.get('configGroupDesc'),
  601. isDefault: false,
  602. parentConfigGroup: defaultConfigGroup,
  603. service: Em.Object.create({id: self.get('serviceName')}),
  604. hosts: [],
  605. configSiteTags: [],
  606. properties: []
  607. });
  608. if (duplicated) {
  609. self.get('selectedConfigGroup.properties').forEach(function(property) {
  610. var property = App.ServiceConfigProperty.create($.extend(false, {}, property));
  611. property.set('group', newConfigGroupData);
  612. properties.push(property);
  613. });
  614. newConfigGroupData.set('properties', properties);
  615. } else {
  616. newConfigGroupData.set('properties', []);
  617. }
  618. self.get('configGroups').pushObject(newConfigGroupData);
  619. defaultConfigGroup.get('childConfigGroups').pushObject(newConfigGroupData);
  620. this.hide();
  621. }
  622. });
  623. this.set('addGroupPopup', addGroupPopup);
  624. },
  625. /**
  626. * Duplicate existing config group
  627. * @method duplicateConfigGroup
  628. */
  629. duplicateConfigGroup: function() {
  630. this.addConfigGroup(true);
  631. },
  632. /**
  633. * copy config groups to manage popup to give user choice whether or not save changes
  634. * @param originGroups
  635. * @return {Array}
  636. * @method copyConfigGroups
  637. */
  638. copyConfigGroups: function (originGroups) {
  639. var configGroups = [];
  640. var result = [];
  641. var defaultConfigGroup = App.ConfigGroup.create($.extend(true, {}, originGroups.findProperty('isDefault')));
  642. originGroups.forEach(function (configGroup) {
  643. if (!configGroup.get('isDefault')) {
  644. var copiedGroup = App.ConfigGroup.create($.extend(true, {}, configGroup));
  645. copiedGroup.set('parentConfigGroup', defaultConfigGroup);
  646. configGroups.pushObject(copiedGroup);
  647. }
  648. });
  649. defaultConfigGroup.set('childConfigGroups', configGroups.slice());
  650. configGroups.pushObject(defaultConfigGroup);
  651. configGroups.forEach(function (group) {
  652. var groupCopy = {};
  653. for (var prop in group) {
  654. if (group.hasOwnProperty(prop)) {
  655. groupCopy[prop] = group[prop];
  656. }
  657. }
  658. groupCopy.properties.forEach(function(property){
  659. property.set('group', group);
  660. });
  661. result.push(App.ConfigGroup.create(groupCopy));
  662. }, this);
  663. return result;
  664. },
  665. /**
  666. * Show popup with config groups
  667. * User may edit/create/delete them
  668. * @param {Em.Controller} controller
  669. * @param {App.Service} service
  670. * @returns {App.ModalPopup}
  671. * @method manageConfigurationGroups
  672. */
  673. manageConfigurationGroups: function (controller, service) {
  674. var configsController = this;
  675. var serviceData = (controller && controller.get('selectedService')) || service;
  676. var serviceName = serviceData.get('serviceName');
  677. var displayName = serviceData.get('displayName');
  678. this.setProperties({
  679. isInstaller: !!controller,
  680. serviceName: serviceName
  681. });
  682. if (controller) {
  683. configsController.set('isAddService', controller.get('content.controllerName') == 'addServiceController');
  684. }
  685. return App.ModalPopup.show({
  686. header: Em.I18n.t('services.service.config_groups_popup.header').format(displayName),
  687. bodyClass: App.MainServiceManageConfigGroupView.extend({
  688. serviceName: serviceName,
  689. displayName: displayName,
  690. controller: configsController
  691. }),
  692. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  693. primary: Em.I18n.t('common.save'),
  694. subViewController: function () {
  695. return configsController;
  696. }.property(),
  697. onPrimary: function () {
  698. var modifiedConfigGroups = configsController.get('hostsModifiedConfigGroups');
  699. // Save modified config-groups
  700. if (!!controller) {
  701. controller.set('selectedService.configGroups', configsController.get('configGroups'));
  702. controller.selectedServiceObserver();
  703. if (controller.get('name') == "wizardStep7Controller") {
  704. if (controller.get('selectedService.selected') === false && modifiedConfigGroups.toDelete.length > 0) {
  705. controller.setGroupsToDelete(modifiedConfigGroups.toDelete);
  706. }
  707. configsController.persistConfigGroups();
  708. this.updateConfigGroupOnServicePage();
  709. }
  710. this.hide();
  711. return;
  712. }
  713. var self = this;
  714. var errors = [];
  715. var deleteQueriesCounter = modifiedConfigGroups.toClearHosts.length + modifiedConfigGroups.toDelete.length;
  716. var createQueriesCounter = modifiedConfigGroups.toSetHosts.length + modifiedConfigGroups.toCreate.length;
  717. var deleteQueriesRun = false;
  718. var createQueriesRun = false;
  719. var runNextQuery = function () {
  720. if (!deleteQueriesRun && deleteQueriesCounter > 0) {
  721. deleteQueriesRun = true;
  722. modifiedConfigGroups.toClearHosts.forEach(function (cg) {
  723. var initalGroupState = modifiedConfigGroups.initialGroups.findProperty('name', cg.get('name'));
  724. configsController.clearConfigurationGroupHosts(cg, initalGroupState, finishFunction, finishFunction);
  725. }, this);
  726. modifiedConfigGroups.toDelete.forEach(function (cg) {
  727. configsController.deleteConfigurationGroup(cg, finishFunction, finishFunction);
  728. }, this);
  729. } else if (!createQueriesRun && deleteQueriesCounter < 1) {
  730. createQueriesRun = true;
  731. modifiedConfigGroups.toSetHosts.forEach(function (cg) {
  732. configsController.updateConfigurationGroup(cg, finishFunction, finishFunction);
  733. }, this);
  734. modifiedConfigGroups.toCreate.forEach(function (cg) {
  735. configsController.postNewConfigurationGroup(cg, finishFunction);
  736. }, this);
  737. }
  738. };
  739. var finishFunction = function (xhr, text, errorThrown) {
  740. if (xhr && errorThrown) {
  741. var error = xhr.status + "(" + errorThrown + ") ";
  742. try {
  743. var json = $.parseJSON(xhr.responseText);
  744. error += json.message;
  745. } catch (err) {}
  746. errors.push(error);
  747. }
  748. createQueriesRun ? createQueriesCounter-- : deleteQueriesCounter--;
  749. if (deleteQueriesCounter + createQueriesCounter < 1) {
  750. if (errors.length > 0) {
  751. self.get('subViewController').set('errorMessage', errors.join(". "));
  752. } else {
  753. self.updateConfigGroupOnServicePage();
  754. self.hide();
  755. }
  756. } else {
  757. runNextQuery();
  758. }
  759. };
  760. runNextQuery();
  761. },
  762. updateConfigGroupOnServicePage: function () {
  763. var selectedConfigGroup = configsController.get('selectedConfigGroup');
  764. var managedConfigGroups = configsController.get('configGroups');
  765. if (!controller) {
  766. controller = App.router.get('mainServiceInfoConfigsController');
  767. //controller.set('configGroups', managedConfigGroups);
  768. controller.loadConfigGroups([controller.get('content.serviceName')]);
  769. } else {
  770. controller.set('selectedService.configGroups', managedConfigGroups);
  771. }
  772. var selectEventObject = {};
  773. //check whether selectedConfigGroup exists
  774. if (selectedConfigGroup && controller.get('configGroups').someProperty('name', selectedConfigGroup.get('name'))) {
  775. selectEventObject.context = selectedConfigGroup;
  776. } else {
  777. selectEventObject.context = managedConfigGroups.findProperty('isDefault', true);
  778. }
  779. controller.selectConfigGroup(selectEventObject);
  780. },
  781. updateButtons: function () {
  782. var modified = this.get('subViewController.isHostsModified');
  783. this.set('disablePrimary', !modified);
  784. }.observes('subViewController.isHostsModified'),
  785. didInsertElement: Em.K
  786. });
  787. }
  788. });