config.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. require('utils/configs_collection');
  20. var stringUtils = require('utils/string_utils');
  21. var configTagFromFileNameMap = {};
  22. App.config = Em.Object.create({
  23. CONFIG_GROUP_NAME_MAX_LENGTH: 18,
  24. /**
  25. * filename exceptions used to support substandard sitenames which don't have "xml" extension
  26. * @type {string[]}
  27. */
  28. filenameExceptions: ['alert_notification'],
  29. preDefinedServiceConfigs: [],
  30. /**
  31. *
  32. * Returns file name version that stored on server.
  33. *
  34. * Example:
  35. * App.config.getOriginalFileName('core-site') // returns core-site.xml
  36. * App.config.getOriginalFileName('zoo.cfg') // returns zoo.cfg
  37. *
  38. * @param {String} fileName
  39. * @method getOriginalFileName
  40. **/
  41. getOriginalFileName: function (fileName) {
  42. if (/\.xml$/.test(fileName)) return fileName;
  43. return this.get('filenameExceptions').contains(fileName) ? fileName : fileName + '.xml';
  44. },
  45. /**
  46. * truncate Config Group name to <CONFIG_GROUP_NAME_MAX_LENGTH> length and paste "..." in the middle
  47. */
  48. truncateGroupName: function (name) {
  49. if (name && name.length > App.config.CONFIG_GROUP_NAME_MAX_LENGTH) {
  50. var middle = Math.floor(App.config.CONFIG_GROUP_NAME_MAX_LENGTH / 2);
  51. name = name.substring(0, middle) + "..." + name.substring(name.length - middle);
  52. }
  53. return name;
  54. },
  55. /**
  56. * Check if Hive installation with new MySQL database created via Ambari is allowed
  57. * @param osFamily
  58. * @returns {boolean}
  59. */
  60. isManagedMySQLForHiveAllowed: function (osFamily) {
  61. var osList = ['redhat5', 'suse11'];
  62. return !osList.contains(osFamily);
  63. },
  64. /**
  65. *
  66. * Returns the configuration tagName from supplied filename
  67. *
  68. * Example:
  69. * App.config.getConfigTagFromFileName('core-site.xml') // returns core-site
  70. * App.config.getConfigTagFromFileName('zoo.cfg') // returns zoo.cfg
  71. *
  72. * @param {String} fileName
  73. * @method getConfigTagFromFileName
  74. **/
  75. getConfigTagFromFileName: function (fileName) {
  76. if (configTagFromFileNameMap[fileName]) {
  77. return configTagFromFileNameMap[fileName];
  78. }
  79. var ret = fileName.endsWith('.xml') ? fileName.slice(0, -4) : fileName;
  80. configTagFromFileNameMap[fileName] = ret;
  81. return ret;
  82. },
  83. /**
  84. *
  85. * @param name
  86. * @param fileName
  87. * @returns {string}
  88. */
  89. configId: function(name, fileName) {
  90. return name + "__" + App.config.getConfigTagFromFileName(fileName);
  91. },
  92. setPreDefinedServiceConfigs: function (isMiscTabToBeAdded) {
  93. var stackServices = App.StackService.find().filterProperty('id');
  94. // Only include services that has configTypes related to them for service configuration page
  95. var servicesWithConfigTypes = stackServices.filter(function (service) {
  96. var configtypes = service.get('configTypes');
  97. return configtypes && !!Object.keys(configtypes).length;
  98. }, this);
  99. var allTabs;
  100. if (isMiscTabToBeAdded) {
  101. var nonServiceTab = require('data/service_configs');
  102. var miscService = nonServiceTab.findProperty('serviceName', 'MISC');
  103. var tagTypes = {};
  104. servicesWithConfigTypes.mapProperty('configTypes').forEach(function (configTypes) {
  105. for (var fileName in configTypes) {
  106. if (fileName.endsWith('-env') && !miscService.get('configTypes')[fileName]) {
  107. tagTypes[fileName] = configTypes[fileName];
  108. }
  109. }
  110. });
  111. miscService.set('configTypes', $.extend(miscService.get('configTypes'), tagTypes));
  112. allTabs = servicesWithConfigTypes.concat(nonServiceTab);
  113. } else {
  114. allTabs = servicesWithConfigTypes;
  115. }
  116. this.set('preDefinedServiceConfigs', allTabs);
  117. },
  118. secureConfigs: require('data/HDP2/secure_mapping'),
  119. secureConfigsMap: function () {
  120. var ret = {};
  121. this.get('secureConfigs').forEach(function (sc) {
  122. ret[sc.name] = true;
  123. });
  124. return ret;
  125. }.property('secureConfigs.[]'),
  126. customStackMapping: require('data/custom_stack_map'),
  127. mapCustomStack: function () {
  128. var
  129. baseStackFolder = App.get('currentStackName'),
  130. singMap = {
  131. "1": ">",
  132. "-1": "<",
  133. "0": "="
  134. };
  135. this.get('customStackMapping').every(function (stack) {
  136. if(stack.stackName == App.get('currentStackName')){
  137. var versionCompare = Em.compare(App.get('currentStackVersionNumber'), stack.stackVersionNumber);
  138. if(singMap[versionCompare+""] === stack.sign){
  139. baseStackFolder = stack.baseStackFolder;
  140. return false;
  141. }
  142. }
  143. return true;
  144. });
  145. return baseStackFolder;
  146. },
  147. allPreDefinedSiteProperties: function() {
  148. var sitePropertiesForCurrentStack = this.preDefinedConfigFile(this.mapCustomStack(), 'site_properties');
  149. if (sitePropertiesForCurrentStack) {
  150. return sitePropertiesForCurrentStack.configProperties;
  151. } else if (App.get('isHadoop23Stack')) {
  152. return require('data/HDP2.3/site_properties').configProperties;
  153. } else if (App.get('isHadoop22Stack')) {
  154. return require('data/HDP2.2/site_properties').configProperties;
  155. } else {
  156. return require('data/HDP2/site_properties').configProperties;
  157. }
  158. }.property('App.isHadoop22Stack', 'App.isHadoop23Stack'),
  159. preDefinedSiteProperties: function () {
  160. var serviceNames = App.StackService.find().mapProperty('serviceName').concat('MISC');
  161. return this.get('allPreDefinedSiteProperties').filter(function(p) {
  162. return serviceNames.contains(p.serviceName);
  163. });
  164. }.property('allPreDefinedSiteProperties'),
  165. /**
  166. * map of <code>preDefinedSiteProperties</code> provide search by index
  167. * @type {object}
  168. */
  169. preDefinedSitePropertiesMap: function () {
  170. var map = {};
  171. this.get('preDefinedSiteProperties').forEach(function (c) {
  172. map[this.configId(c.name, c.filename)] = c;
  173. }, this);
  174. return map;
  175. }.property('preDefinedSiteProperties'),
  176. preDefinedConfigFile: function(folder, file) {
  177. try {
  178. return require('data/{0}/{1}'.format(folder, file));
  179. } catch (err) {
  180. // the file doesn't exist, which might be expected.
  181. }
  182. },
  183. /**
  184. * get service for current config type
  185. * @param {String} configType - config fileName without xml
  186. * @return App.StackService
  187. */
  188. getServiceByConfigType: function(configType) {
  189. return App.StackService.find().find(function(s) {
  190. return Object.keys(s.get('configTypes')).contains(configType);
  191. });
  192. },
  193. serviceByConfigTypeMap: function () {
  194. var ret = {};
  195. App.StackService.find().forEach(function(s) {
  196. Object.keys(s.get('configTypes')).forEach(function (ct) {
  197. ret[ct] = s;
  198. });
  199. });
  200. return ret;
  201. }.property(),
  202. /**
  203. * generates config objects
  204. * @param configGroups
  205. * @param serviceName
  206. * @param selectedConfigGroup
  207. * @param canEdit
  208. * @returns {Array}
  209. */
  210. mergePredefinedWithSaved: function (configGroups, serviceName, selectedConfigGroup, canEdit) {
  211. var configs = [];
  212. configGroups.forEach(function (siteConfig) {
  213. var filename = App.config.getOriginalFileName(siteConfig.type);
  214. var attributes = siteConfig['properties_attributes'] || {};
  215. var finalAttributes = attributes.final || {};
  216. var properties = siteConfig.properties || {};
  217. for (var index in properties) {
  218. var advancedConfig = App.configsCollection.getConfigByName(index, siteConfig.type);
  219. var serviceConfigObj = advancedConfig || this.createDefaultConfig(index, serviceName, filename, false);
  220. this.restrictSecureProperties(serviceConfigObj);
  221. if (serviceConfigObj.isRequiredByAgent !== false) {
  222. var formattedValue = this.formatPropertyValue(serviceConfigObj, properties[index]);
  223. serviceConfigObj.value = serviceConfigObj.savedValue = formattedValue;
  224. serviceConfigObj.isFinal = serviceConfigObj.savedIsFinal = finalAttributes[index] === "true";
  225. serviceConfigObj.isEditable = this.getIsEditable(serviceConfigObj, selectedConfigGroup, canEdit);
  226. serviceConfigObj.isVisible = serviceConfigObj.isVisible !== false || serviceName === 'MISC';
  227. }
  228. var serviceConfigProperty = App.ServiceConfigProperty.create(serviceConfigObj);
  229. serviceConfigProperty.validate();
  230. configs.push(serviceConfigProperty);
  231. }
  232. }, this);
  233. return configs;
  234. },
  235. /**
  236. * put secure properties in read-only mode
  237. * @param {object} config
  238. */
  239. restrictSecureProperties: function (config) {
  240. if (config.isSecureConfig) {
  241. var isReadOnly = App.get('isKerberosEnabled');
  242. config.isReconfigurable = config.isReconfigurable && !isReadOnly;
  243. config.isOverridable = config.isOverridable && !isReadOnly;
  244. }
  245. },
  246. /**
  247. * This method sets default values for config property
  248. * These property values has the lowest priority and can be overridden be stack/UI
  249. * config property but is used when such properties are absent in stack/UI configs
  250. * @param {string} name
  251. * @param {string} serviceName
  252. * @param {string} fileName
  253. * @param {boolean} definedInStack
  254. * @param {Object} [coreObject]
  255. * @returns {Object}
  256. */
  257. createDefaultConfig: function(name, serviceName, fileName, definedInStack, coreObject) {
  258. var tpl = {
  259. /** core properties **/
  260. id: this.configId(name, fileName),
  261. name: name,
  262. filename: fileName,
  263. value: '',
  264. savedValue: null,
  265. isFinal: false,
  266. savedIsFinal: null,
  267. /** UI and Stack properties **/
  268. recommendedValue: null,
  269. recommendedIsFinal: null,
  270. supportsFinal: this.shouldSupportFinal(serviceName, fileName),
  271. supportsAddingForbidden: this.shouldSupportAddingForbidden(serviceName, fileName),
  272. serviceName: serviceName,
  273. displayName: name,
  274. displayType: this.getDefaultDisplayType(coreObject ? coreObject.value : ''),
  275. description: '',
  276. category: this.getDefaultCategory(definedInStack, fileName),
  277. isSecureConfig: this.getIsSecure(name),
  278. showLabel: true,
  279. isVisible: true,
  280. isUserProperty: !definedInStack,
  281. isRequired: definedInStack,
  282. group: null,
  283. isRequiredByAgent: true,
  284. isReconfigurable: true,
  285. unit: null,
  286. hasInitialValue: false,
  287. isOverridable: true,
  288. index: Infinity,
  289. dependentConfigPattern: null,
  290. options: null,
  291. radioName: null,
  292. widgetType: null
  293. };
  294. return Object.keys(coreObject|| {}).length ?
  295. $.extend(tpl, coreObject) : tpl;
  296. },
  297. /**
  298. * This method creates host name properties
  299. * @param serviceName
  300. * @param componentName
  301. * @param value
  302. * @param stackComponent
  303. * @returns Object
  304. */
  305. createHostNameProperty: function(serviceName, componentName, value, stackComponent) {
  306. var hostOrHosts = stackComponent.get('isMultipleAllowed') ? 'hosts' : 'host';
  307. return {
  308. "name": componentName.toLowerCase() + '_' + hostOrHosts,
  309. "displayName": stackComponent.get('displayName') + ' ' + (value.length > 1 ? 'hosts' : 'host'),
  310. "value": value,
  311. "recommendedValue": value,
  312. "description": "The " + hostOrHosts + " that has been assigned to run " + stackComponent.get('displayName'),
  313. "displayType": "component" + hostOrHosts.capitalize(),
  314. "isOverridable": false,
  315. "isRequiredByAgent": false,
  316. "serviceName": serviceName,
  317. "filename": serviceName.toLowerCase() + "-site.xml",
  318. "category": componentName,
  319. "index": 0
  320. }
  321. },
  322. /**
  323. * This method merge properties form <code>stackConfigProperty<code> which are taken from stack
  324. * with <code>UIConfigProperty<code> which are hardcoded on UI
  325. * @param coreObject
  326. * @param stackProperty
  327. * @param preDefined
  328. * @param [propertiesToSkip]
  329. */
  330. mergeStaticProperties: function(coreObject, stackProperty, preDefined, propertiesToSkip) {
  331. propertiesToSkip = propertiesToSkip || ['name', 'filename', 'value', 'savedValue', 'isFinal', 'savedIsFinal'];
  332. for (var k in coreObject) {
  333. if (coreObject.hasOwnProperty(k)) {
  334. if (!propertiesToSkip.contains(k)) {
  335. coreObject[k] = this.getPropertyIfExists(k, coreObject[k], stackProperty, preDefined);
  336. }
  337. }
  338. }
  339. return coreObject;
  340. },
  341. /**
  342. * This method using for merging some properties from two objects
  343. * if property exists in <code>firstPriority<code> result will be it's property
  344. * else if property exists in <code>secondPriority<code> result will be it's property
  345. * otherwise <code>defaultValue<code> will be returned
  346. * @param {String} propertyName
  347. * @param {*} defaultValue=null
  348. * @param {Em.Object|Object} firstPriority
  349. * @param {Em.Object|Object} [secondPriority=null]
  350. * @returns {*}
  351. */
  352. getPropertyIfExists: function(propertyName, defaultValue, firstPriority, secondPriority) {
  353. firstPriority = firstPriority || {};
  354. secondPriority = secondPriority || {};
  355. var fp = Em.get(firstPriority, propertyName);
  356. if (firstPriority && !Em.isNone(fp)) {
  357. return fp;
  358. }
  359. else {
  360. var sp = Em.get(secondPriority, propertyName);
  361. if (secondPriority && !Em.isNone(sp)) {
  362. return sp;
  363. } else {
  364. return defaultValue;
  365. }
  366. }
  367. },
  368. /**
  369. * Get displayType for properties that has not defined value
  370. * @param value
  371. * @returns {string}
  372. */
  373. getDefaultDisplayType: function(value) {
  374. return value && !stringUtils.isSingleLine(value) ? 'multiLine' : 'string';
  375. },
  376. /**
  377. * Get category for properties that has not defined value
  378. * @param stackConfigProperty
  379. * @param fileName
  380. * @returns {string}
  381. */
  382. getDefaultCategory: function(stackConfigProperty, fileName) {
  383. return (stackConfigProperty ? 'Advanced ' : 'Custom ') + this.getConfigTagFromFileName(fileName);
  384. },
  385. /**
  386. * Get isSecureConfig for properties that has not defined value
  387. * @param propertyName
  388. * @returns {boolean}
  389. */
  390. getIsSecure: function(propertyName) {
  391. return !!this.get('secureConfigsMap')[propertyName];
  392. },
  393. /**
  394. * Calculate isEditable rely on controller state selected group and config restriction
  395. * @param {Object} serviceConfigProperty
  396. * @param {Object} selectedConfigGroup
  397. * @param {boolean} canEdit
  398. * @returns {boolean}
  399. */
  400. getIsEditable: function(serviceConfigProperty, selectedConfigGroup, canEdit) {
  401. return canEdit && Em.get(selectedConfigGroup, 'isDefault') && Em.get(serviceConfigProperty, 'isReconfigurable')
  402. },
  403. /**
  404. * format property value depending on displayType
  405. * and one exception for 'kdc_type'
  406. * @param serviceConfigProperty
  407. * @param [originalValue]
  408. * @returns {*}
  409. */
  410. formatPropertyValue: function(serviceConfigProperty, originalValue) {
  411. var value = Em.isNone(originalValue) ? Em.get(serviceConfigProperty, 'value') : originalValue,
  412. displayType = Em.get(serviceConfigProperty, 'displayType') || Em.get(serviceConfigProperty, 'valueAttributes.type'),
  413. category = Em.get(serviceConfigProperty, 'category');
  414. switch (displayType) {
  415. case 'content':
  416. case 'string':
  417. case 'multiLine':
  418. return this.trimProperty({ displayType: displayType, value: value });
  419. break;
  420. case 'directories':
  421. if (['DataNode', 'NameNode'].contains(category)) {
  422. return value.split(',').sort().join(',');//TODO check if this code is used
  423. }
  424. break;
  425. case 'directory':
  426. if (['SNameNode'].contains(category)) {
  427. return value.split(',').sort()[0];//TODO check if this code is used
  428. }
  429. break;
  430. case 'componentHosts':
  431. if (typeof(value) == 'string') {
  432. return value.replace(/\[|]|'|&apos;/g, "").split(',');
  433. }
  434. break;
  435. case 'int':
  436. if (/\d+m$/.test(value) ) {
  437. return value.slice(0, value.length - 1);
  438. } else {
  439. var int = parseInt(value);
  440. return isNaN(int) ? "" : int.toString();
  441. }
  442. break;
  443. case 'float':
  444. var float = parseFloat(value);
  445. return isNaN(float) ? "" : float.toString();
  446. }
  447. if (Em.get(serviceConfigProperty, 'name') === 'kdc_type') {
  448. return App.router.get('mainAdminKerberosController.kdcTypesValues')[value];
  449. }
  450. if ( /^\s+$/.test("" + value)) {
  451. value = " ";
  452. }
  453. return value;
  454. },
  455. /**
  456. *
  457. * @param configs
  458. * @returns {Object[]}
  459. */
  460. sortConfigs: function(configs) {
  461. return configs.sort(function(a, b) {
  462. if (Em.get(a, 'index') > Em.get(b, 'index')) return 1;
  463. if (Em.get(a, 'index') < Em.get(b, 'index')) return -1;
  464. if (Em.get(a, 'name') > Em.get(b, 'index')) return 1;
  465. if (Em.get(a, 'name') < Em.get(b, 'index')) return -1;
  466. return 0;
  467. });
  468. },
  469. /**
  470. *
  471. * @param {string} ifStatement
  472. * @param {Array} serviceConfigs
  473. * @returns {boolean}
  474. */
  475. calculateConfigCondition: function(ifStatement, serviceConfigs) {
  476. // Split `if` statement if it has logical operators
  477. var ifStatementRegex = /(&&|\|\|)/;
  478. var IfConditions = ifStatement.split(ifStatementRegex);
  479. var allConditionResult = [];
  480. IfConditions.forEach(function(_condition){
  481. var condition = _condition.trim();
  482. if (condition === '&&' || condition === '||') {
  483. allConditionResult.push(_condition);
  484. } else {
  485. var splitIfCondition = condition.split('===');
  486. var ifCondition = splitIfCondition[0];
  487. var result = splitIfCondition[1] || "true";
  488. var parseIfConditionVal = ifCondition;
  489. var regex = /\$\{.*?\}/g;
  490. var configStrings = ifCondition.match(regex);
  491. configStrings.forEach(function (_configString) {
  492. var configObject = _configString.substring(2, _configString.length - 1).split("/");
  493. var config = serviceConfigs.filterProperty('filename', configObject[0] + '.xml').findProperty('name', configObject[1]);
  494. if (config) {
  495. var configValue = Em.get(config, 'value');
  496. parseIfConditionVal = parseIfConditionVal.replace(_configString, configValue);
  497. }
  498. }, this);
  499. var conditionResult = window.eval(JSON.stringify(parseIfConditionVal.trim())) === result.trim();
  500. allConditionResult.push(conditionResult);
  501. }
  502. }, this);
  503. return Boolean(window.eval(allConditionResult.join('')));
  504. },
  505. /**
  506. * create new ServiceConfig object by service name
  507. * @param {string} serviceName
  508. * @param {App.ServiceConfigGroup[]} [configGroups]
  509. * @param {App.ServiceConfigProperty[]} [configs]
  510. * @param {Number} [initConfigsLength]
  511. * @return {App.ServiceConfig}
  512. * @method createServiceConfig
  513. */
  514. createServiceConfig: function (serviceName, configGroups, configs, initConfigsLength) {
  515. var preDefinedServiceConfig = App.config.get('preDefinedServiceConfigs').findProperty('serviceName', serviceName);
  516. return App.ServiceConfig.create({
  517. serviceName: preDefinedServiceConfig.get('serviceName'),
  518. displayName: preDefinedServiceConfig.get('displayName'),
  519. configCategories: preDefinedServiceConfig.get('configCategories'),
  520. configs: configs || [],
  521. configGroups: configGroups || [],
  522. initConfigsLength: initConfigsLength || 0
  523. });
  524. },
  525. /**
  526. * GETs all cluster level sites in one call.
  527. *
  528. * @return {$.ajax}
  529. */
  530. loadConfigsByTags: function (tags) {
  531. var urlParams = [];
  532. tags.forEach(function (_tag) {
  533. urlParams.push('(type=' + _tag.siteName + '&tag=' + _tag.tagName + ')');
  534. });
  535. var params = urlParams.join('|');
  536. return App.ajax.send({
  537. name: 'config.on_site',
  538. sender: this,
  539. data: {
  540. params: params
  541. }
  542. });
  543. },
  544. configTypesInfoMap: {},
  545. /**
  546. * Get config types and config type attributes from stack service
  547. *
  548. * @param service
  549. * @return {object}
  550. */
  551. getConfigTypesInfoFromService: function (service) {
  552. var configTypesInfoMap = this.get('configTypesInfoMap');
  553. if (configTypesInfoMap[service]) {
  554. // don't recalculate
  555. return configTypesInfoMap[service];
  556. }
  557. var configTypes = service.get('configTypes');
  558. var configTypesInfo = {
  559. items: [],
  560. supportsFinal: [],
  561. supportsAddingForbidden: []
  562. };
  563. if (configTypes) {
  564. for (var key in configTypes) {
  565. if (configTypes.hasOwnProperty(key)) {
  566. configTypesInfo.items.push(key);
  567. if (configTypes[key].supports && configTypes[key].supports.final === "true") {
  568. configTypesInfo.supportsFinal.push(key);
  569. }
  570. if (configTypes[key].supports && configTypes[key].supports.adding_forbidden === "true"){
  571. configTypesInfo.supportsAddingForbidden.push(key);
  572. }
  573. }
  574. }
  575. }
  576. configTypesInfoMap[service] = configTypesInfo;
  577. this.set('configTypesInfoMap', configTypesInfoMap);
  578. return configTypesInfo;
  579. },
  580. /**
  581. * Create config with non default config group. Some custom config properties
  582. * can be created and assigned to non-default config group.
  583. *
  584. * @param {String} propertyName - name of the property
  585. * @param {Object} config - config info
  586. * @param {Em.Object} group - config group to set
  587. * @param {Boolean} isEditable
  588. * @return {Object}
  589. **/
  590. createCustomGroupConfig: function (propertyName, config, group, isEditable) {
  591. var propertyObject = this.createDefaultConfig(propertyName, group.get('service.serviceName'), this.getOriginalFileName(config.type), false, {
  592. savedValue: config.properties[propertyName],
  593. value: config.properties[propertyName],
  594. group: group,
  595. isEditable: isEditable !== false,
  596. isOverridable: false
  597. });
  598. group.set('switchGroupTextShort', Em.I18n.t('services.service.config_groups.switchGroupTextShort').format(group.get('name')));
  599. group.set('switchGroupTextFull', Em.I18n.t('services.service.config_groups.switchGroupTextFull').format(group.get('name')));
  600. return App.ServiceConfigProperty.create(propertyObject);
  601. },
  602. complexConfigsTemplate: [
  603. {
  604. "name": "capacity-scheduler",
  605. "displayName": "Capacity Scheduler",
  606. "value": "",
  607. "description": "Capacity Scheduler properties",
  608. "displayType": "custom",
  609. "isOverridable": true,
  610. "isRequired": true,
  611. "isVisible": true,
  612. "isReconfigurable": true,
  613. "supportsFinal": false,
  614. "serviceName": "YARN",
  615. "filename": "capacity-scheduler.xml",
  616. "category": "CapacityScheduler"
  617. }
  618. ],
  619. /**
  620. * transform set of configs from file
  621. * into one config with textarea content:
  622. * name=value
  623. * @param {App.ServiceConfigProperty[]} configs
  624. * @param {String} filename
  625. * @param {App.ServiceConfigProperty[]} [configsToSkip=[]]
  626. * @return {*}
  627. */
  628. fileConfigsIntoTextarea: function (configs, filename, configsToSkip) {
  629. var fileConfigs = configs.filterProperty('filename', filename);
  630. var value = '', savedValue = '', recommendedValue = '';
  631. var template = this.get('complexConfigsTemplate').findProperty('filename', filename);
  632. var complexConfig = $.extend({}, template);
  633. if (complexConfig) {
  634. fileConfigs.forEach(function (_config) {
  635. if (!(configsToSkip && configsToSkip.someProperty('name', _config.name))) {
  636. value += _config.name + '=' + _config.value + '\n';
  637. if (!Em.isNone(_config.savedValue)) {
  638. savedValue += _config.name + '=' + _config.savedValue + '\n';
  639. }
  640. if (!Em.isNone(_config.recommendedValue)) {
  641. recommendedValue += _config.name + '=' + _config.recommendedValue + '\n';
  642. }
  643. }
  644. }, this);
  645. var isFinal = fileConfigs.someProperty('isFinal', true);
  646. var savedIsFinal = fileConfigs.someProperty('savedIsFinal', true);
  647. var recommendedIsFinal = fileConfigs.someProperty('recommendedIsFinal', true);
  648. complexConfig.value = value;
  649. if (savedValue) {
  650. complexConfig.savedValue = savedValue;
  651. }
  652. if (recommendedValue) {
  653. complexConfig.recommendedValue = recommendedValue;
  654. }
  655. complexConfig.isFinal = isFinal;
  656. complexConfig.savedIsFinal = savedIsFinal;
  657. complexConfig.recommendedIsFinal = recommendedIsFinal;
  658. configs = configs.filter(function (_config) {
  659. return _config.filename !== filename || (configsToSkip && configsToSkip.someProperty('name', _config.name));
  660. });
  661. configs.push(App.ServiceConfigProperty.create(complexConfig));
  662. }
  663. return configs;
  664. },
  665. /**
  666. * transform one config with textarea content
  667. * into set of configs of file
  668. * @param configs
  669. * @param filename
  670. * @return {*}
  671. */
  672. textareaIntoFileConfigs: function (configs, filename) {
  673. var complexConfigName = this.get('complexConfigsTemplate').findProperty('filename', filename).name;
  674. var configsTextarea = configs.findProperty('name', complexConfigName);
  675. if (configsTextarea && !App.get('testMode')) {
  676. var properties = configsTextarea.get('value').split('\n');
  677. properties.forEach(function (_property) {
  678. var name, value;
  679. if (_property) {
  680. _property = _property.split('=');
  681. name = _property[0];
  682. value = (_property[1]) ? _property[1] : "";
  683. configs.push(Em.Object.create({
  684. name: name,
  685. value: value,
  686. savedValue: value,
  687. serviceName: configsTextarea.get('serviceName'),
  688. filename: filename,
  689. isFinal: configsTextarea.get('isFinal'),
  690. isNotDefaultValue: configsTextarea.get('isNotDefaultValue'),
  691. isRequiredByAgent: configsTextarea.get('isRequiredByAgent'),
  692. group: null
  693. }));
  694. }
  695. });
  696. return configs.without(configsTextarea);
  697. }
  698. return configs;
  699. },
  700. /**
  701. * trim trailing spaces for all properties.
  702. * trim both trailing and leading spaces for host displayType and hive/oozie datebases url.
  703. * for directory or directories displayType format string for further using.
  704. * for password and values with spaces only do nothing.
  705. * @param {Object} property
  706. * @returns {*}
  707. */
  708. trimProperty: function (property) {
  709. var displayType = Em.get(property, 'displayType');
  710. var value = Em.get(property, 'value');
  711. var name = Em.get(property, 'name');
  712. var rez;
  713. switch (displayType) {
  714. case 'directories':
  715. case 'directory':
  716. rez = value.replace(/,/g, ' ').trim().split(/\s+/g).join(',');
  717. break;
  718. case 'host':
  719. rez = value.trim();
  720. break;
  721. case 'password':
  722. break;
  723. default:
  724. if (name == 'javax.jdo.option.ConnectionURL' || name == 'oozie.service.JPAService.jdbc.url') {
  725. rez = value.trim();
  726. }
  727. rez = (typeof value == 'string') ? value.replace(/(\s+$)/g, '') : value;
  728. }
  729. return ((rez == '') || (rez == undefined)) ? value : rez;
  730. },
  731. /**
  732. * Generate minimal config property object used in *_properties.js files.
  733. * Example:
  734. * <code>
  735. * var someProperties = App.config.generateConfigPropertiesByName([
  736. * 'property_1', 'property_2', 'property_3'], { category: 'General', filename: 'myFileName'});
  737. * // someProperties contains Object[]
  738. * [
  739. * {
  740. * name: 'property_1',
  741. * displayName: 'property_1',
  742. * isVisible: true,
  743. * isReconfigurable: true,
  744. * category: 'General',
  745. * filename: 'myFileName'
  746. * },
  747. * .......
  748. * ]
  749. * </code>
  750. * @param {string[]} names
  751. * @param {Object} properties - additional properties which will merge with base object definition
  752. * @returns {object[]}
  753. * @method generateConfigPropertiesByName
  754. */
  755. generateConfigPropertiesByName: function (names, properties) {
  756. return names.map(function (item) {
  757. var baseObj = {
  758. name: item
  759. };
  760. if (properties) return $.extend(baseObj, properties);
  761. else return baseObj;
  762. });
  763. },
  764. /**
  765. * load cluster stack configs from server and run mapper
  766. * @returns {$.ajax}
  767. * @method loadConfigsFromStack
  768. */
  769. loadClusterConfigsFromStack: function () {
  770. return App.ajax.send({
  771. name: 'configs.stack_configs.load.cluster_configs',
  772. sender: this,
  773. data: {
  774. stackVersionUrl: App.get('stackVersionURL')
  775. },
  776. success: 'saveConfigsToModel'
  777. });
  778. },
  779. /**
  780. * load stack configs from server and run mapper
  781. * @param {String[]} [serviceNames=null]
  782. * @returns {$.ajax}
  783. * @method loadConfigsFromStack
  784. */
  785. loadConfigsFromStack: function (serviceNames) {
  786. serviceNames = serviceNames || [];
  787. var name = serviceNames.length > 0 ? 'configs.stack_configs.load.services' : 'configs.stack_configs.load.all';
  788. return App.ajax.send({
  789. name: name,
  790. sender: this,
  791. data: {
  792. stackVersionUrl: App.get('stackVersionURL'),
  793. serviceList: serviceNames.join(',')
  794. },
  795. success: 'saveConfigsToModel'
  796. });
  797. },
  798. /**
  799. * Runs <code>stackConfigPropertiesMapper<code>
  800. * @param {object} data
  801. * @method saveConfigsToModel
  802. */
  803. saveConfigsToModel: function (data) {
  804. App.stackConfigPropertiesMapper.map(data);
  805. },
  806. /**
  807. * Check if config filename supports final attribute
  808. * @param serviceName
  809. * @param filename
  810. * @returns {boolean}
  811. */
  812. shouldSupportFinal: function (serviceName, filename) {
  813. var unsupportedServiceNames = ['MISC', 'Cluster'];
  814. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  815. return false;
  816. } else {
  817. var stackService = App.StackService.find(serviceName);
  818. if (!stackService) {
  819. return false;
  820. }
  821. return !!this.getConfigTypesInfoFromService(stackService).supportsFinal.find(function (configType) {
  822. return filename.startsWith(configType);
  823. });
  824. }
  825. },
  826. shouldSupportAddingForbidden: function(serviceName, filename) {
  827. var unsupportedServiceNames = ['MISC', 'Cluster'];
  828. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  829. return false;
  830. } else {
  831. var stackServiceName = App.StackService.find().findProperty('serviceName', serviceName);
  832. if (!stackServiceName) {
  833. return false;
  834. }
  835. var stackService = App.StackService.find(serviceName);
  836. return !!this.getConfigTypesInfoFromService(stackService).supportsAddingForbidden.find(function (configType) {
  837. return filename.startsWith(configType);
  838. });
  839. }
  840. },
  841. /**
  842. * Remove all ranger-related configs, that should be available only if Ranger is installed
  843. * @param configs - stepConfigs object
  844. */
  845. removeRangerConfigs: function (configs) {
  846. configs.forEach(function (service) {
  847. var filteredConfigs = [];
  848. service.get('configs').forEach(function (config) {
  849. if (!/^ranger-/.test(config.get('filename'))) {
  850. filteredConfigs.push(config);
  851. }
  852. });
  853. service.set('configs', filteredConfigs);
  854. var filteredCategories = [];
  855. service.get('configCategories').forEach(function (category) {
  856. if (!/ranger-/.test(category.get('name'))) {
  857. filteredCategories.push(category);
  858. }
  859. });
  860. service.set('configCategories', filteredCategories);
  861. });
  862. },
  863. /**
  864. * @param {App.ServiceConfigProperty} serviceConfigProperty
  865. * @param {Object} override - plain object with properties that is different from parent SCP
  866. * @param {App.ServiceConfigGroup} configGroup
  867. * @returns {App.ServiceConfigProperty}
  868. */
  869. createOverride: function(serviceConfigProperty, override, configGroup) {
  870. Em.assert('serviceConfigProperty can\' be null', serviceConfigProperty);
  871. Em.assert('configGroup can\' be null', configGroup);
  872. if (Em.isNone(serviceConfigProperty.get('overrides'))) serviceConfigProperty.set('overrides', []);
  873. var newOverride = App.ServiceConfigProperty.create(serviceConfigProperty);
  874. if (!Em.isNone(override)) {
  875. for (var key in override) {
  876. newOverride.set(key, override[key]);
  877. }
  878. }
  879. newOverride.setProperties({
  880. 'isOriginalSCP': false,
  881. 'overrides': null,
  882. 'group': configGroup,
  883. 'parentSCP': serviceConfigProperty
  884. });
  885. serviceConfigProperty.get('overrides').pushObject(newOverride);
  886. serviceConfigProperty.set('overrideValues', serviceConfigProperty.get('overrides').mapProperty('value'));
  887. serviceConfigProperty.set('overrideIsFinalValues', serviceConfigProperty.get('overrides').mapProperty('isFinal'));
  888. newOverride.validate();
  889. return newOverride;
  890. },
  891. /**
  892. * Merge values in "stored" to "base" if name matches, it's a value only merge.
  893. * @param base {Array} Em.Object
  894. * @param stored {Array} Object
  895. */
  896. mergeStoredValue: function(base, stored) {
  897. if (stored) {
  898. base.forEach(function (p) {
  899. var sp = stored.filterProperty("filename", p.filename).findProperty("name", p.name);
  900. if (sp) {
  901. p.set("value", sp.value);
  902. }
  903. });
  904. }
  905. },
  906. /**
  907. * Update config property value based on its current value and list of zookeeper server hosts.
  908. * Used to prevent sort order issues.
  909. * <code>siteConfigs</code> object formatted according server's persist format e.g.
  910. *
  911. * <code>
  912. * {
  913. * 'yarn-site': {
  914. * 'property_name1': 'property_value1'
  915. * 'property_name2': 'property_value2'
  916. * .....
  917. * }
  918. * }
  919. * </code>
  920. *
  921. * @method updateHostsListValue
  922. * @param {Object} siteConfigs - prepared site config object to store
  923. * @param {String} propertyName - name of the property to update
  924. * @param {String} hostsList - list of ZooKeeper Server names to set as config property value
  925. * @return {String} - result value
  926. */
  927. updateHostsListValue: function(siteConfigs, propertyName, hostsList) {
  928. var value = hostsList;
  929. var propertyHosts = (siteConfigs[propertyName] || '').split(',');
  930. var hostsToSet = hostsList.split(',');
  931. if (!Em.isEmpty(siteConfigs[propertyName])) {
  932. var diffLength = propertyHosts.filter(function(hostName) {
  933. return !hostsToSet.contains(hostName);
  934. }).length;
  935. if (diffLength == 0 && propertyHosts.length == hostsToSet.length) {
  936. value = siteConfigs[propertyName];
  937. }
  938. }
  939. siteConfigs[propertyName] = value;
  940. return value;
  941. }
  942. });