enhanced_configs.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. App.EnhancedConfigsMixin = Em.Mixin.create({
  20. modifiedFileNames: [],
  21. /**
  22. * merge step configs from model
  23. * for default config group properties should be list
  24. * of changed properties
  25. * @param properties
  26. * @param currentVersionNumber
  27. */
  28. loadConfigsToModel: function(properties, currentVersionNumber) {
  29. var serviceName = this.get('content.serviceName');
  30. if (properties && properties.length) {
  31. properties.forEach(function(p) {
  32. var configFromModel = App.ConfigProperty.find(p.get('name') + '_' + App.config.getConfigTagFromFileName(p.get('filename')) + '_' + currentVersionNumber);
  33. if (configFromModel && configFromModel.get('name')) {
  34. configFromModel.setProperties({
  35. 'value': p.get('value'),
  36. 'isFinal': p.get('isFinal'),
  37. 'defaultValue': p.get('defaultValue'),
  38. 'defaultIsFinal': p.get('defaultIsFinal'),
  39. 'isRequiredByAgent': p.get('isRequiredByAgent'),
  40. 'isNotSaved': p.get('isNotSaved')
  41. });
  42. } else {
  43. App.store.load(App.ConfigProperty, {
  44. id: p.get('name') + '_' + App.config.getConfigTagFromFileName(p.get('filename')) + '_' + currentVersionNumber,
  45. name: p.get('name'),
  46. file_name: p.get('filename'),
  47. value: p.get('value'),
  48. is_final: p.get('isFinal'),
  49. default_value: p.get('defaultValue'),
  50. default_is_final: p.get('defaultIsFinal'),
  51. is_required_by_agent: p.get('isRequiredByAgent'),
  52. is_not_saved: p.get('isNotSaved'),
  53. is_required: false,
  54. config_version_id: serviceName + '_' + currentVersionNumber
  55. })
  56. }
  57. });
  58. }
  59. },
  60. /**
  61. * generates data and save configs for default group
  62. * @method saveEnhancedConfigs
  63. */
  64. saveEnhancedConfigs: function() {
  65. var fileNamesToSave = this.getFileNamesToSave(this.get('modifiedFileNames'));
  66. var configsToSave = this.getConfigsToSave(fileNamesToSave);
  67. var desired_configs = this.generateDesiredConfigsJSON(configsToSave, fileNamesToSave, this.get('serviceConfigNote'));
  68. this.doPUTClusterConfigurationSites(desired_configs);
  69. },
  70. /**
  71. * generates data and save configs for not default group
  72. * @param selectedConfigGroup
  73. * @method saveEnhancedConfigsAndGroup
  74. */
  75. saveEnhancedConfigsAndGroup: function(selectedConfigGroup) {
  76. //TODO update for dependent configs
  77. var serviceConfigVersion = App.ConfigVersion.find().findProperty('groupName', selectedConfigGroup.get('name'));
  78. var overridenConfigs = App.ConfigProperty.find().filter(function(cp) {
  79. return cp.get('configVersion.groupId') === selectedConfigGroup.get('id') || cp.get('isNotDefaultValue');
  80. });
  81. var hostNames = serviceConfigVersion.get('hosts').map(function(hostName) {
  82. return {
  83. "host_name": hostName
  84. }
  85. });
  86. var fileNamesToSave = overridenConfigs.mapProperty('fileName').uniq();
  87. this.putConfigGroupChanges({
  88. ConfigGroup: {
  89. "id": selectedConfigGroup.get('id'),
  90. "cluster_name": App.get('clusterName'),
  91. "group_name": selectedConfigGroup.get('name'),
  92. "tag": selectedConfigGroup.get('service.id'),
  93. "description": selectedConfigGroup.get('description'),
  94. "hosts": hostNames,
  95. "service_config_version_note": this.get('serviceConfigNote'),
  96. "desired_configs": this.generateDesiredConfigsJSON(overridenConfigs, fileNamesToSave, null, true)
  97. }
  98. }, true);
  99. },
  100. /**
  101. * get file names that need to be saved
  102. * @param {Array} modifiedFileNames
  103. * @returns {Ember.Enumerable}
  104. */
  105. getFileNamesToSave: function(modifiedFileNames) {
  106. return App.ConfigProperty.find().filter(function(cp) {
  107. return cp.get('isNotDefaultValue') || cp.get('isNotSaved');
  108. }, this).mapProperty('fileName').concat(modifiedFileNames).uniq();
  109. },
  110. /**
  111. * get configs that need to be saved, for default group
  112. * @param fileNamesToSave
  113. * @returns {App.ConfigProperty[]}
  114. */
  115. getConfigsToSave: function(fileNamesToSave) {
  116. if (Em.isArray(fileNamesToSave) && fileNamesToSave.length) {
  117. return App.ConfigProperty.find().filter(function(cp) {
  118. return (fileNamesToSave.contains(cp.get('fileName')) && cp.get('isOriginalSCP')) || cp.get('isNotSaved');
  119. });
  120. } else {
  121. return Em.A([]);
  122. }
  123. },
  124. /**
  125. * generating common JSON object for desired configs
  126. * @param configsToSave
  127. * @param fileNamesToSave
  128. * @param serviceConfigNote
  129. * @param {boolean} [isNotDefaultGroup=false]
  130. * @returns {Array}
  131. */
  132. generateDesiredConfigsJSON: function(configsToSave, fileNamesToSave, serviceConfigNote, isNotDefaultGroup) {
  133. var desired_config = [];
  134. if (Em.isArray(configsToSave) && Em.isArray(fileNamesToSave) && fileNamesToSave.length && configsToSave.length) {
  135. serviceConfigNote = serviceConfigNote || "";
  136. var tagVersion = "version" + (new Date).getTime();
  137. fileNamesToSave.forEach(function(fName) {
  138. if (this.allowSaveSite(fName)) {
  139. var properties = configsToSave.filterProperty('fileName', fName);
  140. var type = App.config.getConfigTagFromFileName(fName);
  141. desired_config.push(this.createDesiredConfig(type, tagVersion, properties, serviceConfigNote, isNotDefaultGroup));
  142. }
  143. }, this);
  144. }
  145. return desired_config;
  146. },
  147. /**
  148. * for some file names we have a restriction
  149. * and can't save them, in this this method will return false
  150. * @param fName
  151. * @returns {boolean}
  152. */
  153. allowSaveSite: function(fName) {
  154. switch (fName) {
  155. case 'mapred-queue-acls.xml':
  156. return false;
  157. case 'core-site.xml':
  158. return ['HDFS', 'GLUSTERFS'].contains(this.get('content.serviceName'));
  159. default :
  160. return true;
  161. }
  162. },
  163. /**
  164. * generating common JSON object for desired config
  165. * @param {string} type - file name without '.xml'
  166. * @param {string} tagVersion - version + timestamp
  167. * @param {App.ConfigProperty[]} properties - array of properties from model
  168. * @param {string} serviceConfigNote
  169. * @param {boolean} [isNotDefaultGroup=false]
  170. * @returns {{type: string, tag: string, properties: {}, properties_attributes: {}|undefined, service_config_version_note: string|undefined}}
  171. */
  172. createDesiredConfig: function(type, tagVersion, properties, serviceConfigNote, isNotDefaultGroup) {
  173. Em.assert('type and tagVersion should be defined', type && tagVersion);
  174. var desired_config = {
  175. "type": type,
  176. "tag": tagVersion,
  177. "properties": {}
  178. };
  179. if (!isNotDefaultGroup) {
  180. desired_config.service_config_version_note = serviceConfigNote || "";
  181. }
  182. var attributes = { final: {} };
  183. if (Em.isArray(properties)) {
  184. properties.forEach(function(property) {
  185. if (property.get('isRequiredByAgent')) {
  186. desired_config.properties[property.get('name')] = this.formatValueBeforeSave(property);
  187. /**
  188. * add is final value
  189. */
  190. if (property.get('isFinal')) {
  191. attributes.final[property.get('name')] = "true";
  192. }
  193. }
  194. }, this);
  195. }
  196. if (Object.keys(attributes.final).length) {
  197. desired_config.properties_attributes = attributes;
  198. }
  199. return desired_config;
  200. },
  201. /**
  202. * format value before save performs some changing of values
  203. * according to the rules that includes heapsizeException trimming and some custom rules
  204. * @param {App.ConfigProperty} property
  205. * @returns {string}
  206. */
  207. formatValueBeforeSave: function(property) {
  208. var name = property.get('name');
  209. var value = property.get('value');
  210. //TODO check for core-site
  211. if (this.get('heapsizeRegExp').test(name) && !this.get('heapsizeException').contains(name) && !(value).endsWith("m")) {
  212. return value += "m";
  213. }
  214. if (typeof property.get('value') === "boolean") {
  215. return property.get('value').toString();
  216. }
  217. switch (name) {
  218. case 'storm.zookeeper.servers':
  219. if (Object.prototype.toString.call(value) === '[object Array]' ) {
  220. return JSON.stringify(value).replace(/"/g, "'");
  221. } else {
  222. return value;
  223. }
  224. break;
  225. default:
  226. return App.config.trimProperty(property, true);
  227. }
  228. },
  229. /**
  230. * overriden in controller
  231. */
  232. doPUTClusterConfigurationSites: Em.K,
  233. /**
  234. * overriden in controller
  235. */
  236. putConfigGroupChanges: Em.K
  237. });