config.js 36 KB

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