config.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. var isReadOnly = config.isSecureConfig && App.get('isKerberosEnabled');
  241. config.isReconfigurable = !isReadOnly;
  242. config.isOverridable = !isReadOnly;
  243. },
  244. /**
  245. * This method sets default values for config property
  246. * These property values has the lowest priority and can be overridden be stack/UI
  247. * config property but is used when such properties are absent in stack/UI configs
  248. * @param {string} name
  249. * @param {string} serviceName
  250. * @param {string} fileName
  251. * @param {boolean} definedInStack
  252. * @param {Object} [coreObject]
  253. * @returns {Object}
  254. */
  255. createDefaultConfig: function(name, serviceName, fileName, definedInStack, coreObject) {
  256. var tpl = {
  257. /** core properties **/
  258. id: this.configId(name, fileName),
  259. name: name,
  260. filename: fileName,
  261. value: '',
  262. savedValue: null,
  263. isFinal: false,
  264. savedIsFinal: null,
  265. /** UI and Stack properties **/
  266. recommendedValue: null,
  267. recommendedIsFinal: null,
  268. supportsFinal: this.shouldSupportFinal(serviceName, fileName),
  269. supportsAddingForbidden: this.shouldSupportAddingForbidden(serviceName, fileName),
  270. serviceName: serviceName,
  271. displayName: name,
  272. displayType: this.getDefaultDisplayType(coreObject ? coreObject.value : ''),
  273. description: '',
  274. category: this.getDefaultCategory(definedInStack, fileName),
  275. isSecureConfig: this.getIsSecure(name),
  276. showLabel: true,
  277. isVisible: true,
  278. isUserProperty: !definedInStack,
  279. isRequired: definedInStack,
  280. group: null,
  281. isRequiredByAgent: true,
  282. isReconfigurable: true,
  283. unit: null,
  284. hasInitialValue: false,
  285. isOverridable: true,
  286. index: Infinity,
  287. dependentConfigPattern: null,
  288. options: null,
  289. radioName: null,
  290. widgetType: null
  291. };
  292. return Object.keys(coreObject|| {}).length ?
  293. $.extend(tpl, coreObject) : tpl;
  294. },
  295. /**
  296. * This method creates host name properties
  297. * @param serviceName
  298. * @param componentName
  299. * @param value
  300. * @param stackComponent
  301. * @returns Object
  302. */
  303. createHostNameProperty: function(serviceName, componentName, value, stackComponent) {
  304. var hostOrHosts = stackComponent.get('isMultipleAllowed') ? 'hosts' : 'host';
  305. return {
  306. "name": componentName.toLowerCase() + '_' + hostOrHosts,
  307. "displayName": stackComponent.get('displayName') + ' ' + (value.length > 1 ? 'hosts' : 'host'),
  308. "value": value,
  309. "recommendedValue": value,
  310. "description": "The " + hostOrHosts + " that has been assigned to run " + stackComponent.get('displayName'),
  311. "displayType": "component" + hostOrHosts.capitalize(),
  312. "isOverridable": false,
  313. "isRequiredByAgent": false,
  314. "serviceName": serviceName,
  315. "filename": serviceName.toLowerCase() + "-site.xml",
  316. "category": componentName,
  317. "index": 0
  318. }
  319. },
  320. /**
  321. * This method merge properties form <code>stackConfigProperty<code> which are taken from stack
  322. * with <code>UIConfigProperty<code> which are hardcoded on UI
  323. * @param coreObject
  324. * @param stackProperty
  325. * @param preDefined
  326. * @param [propertiesToSkip]
  327. */
  328. mergeStaticProperties: function(coreObject, stackProperty, preDefined, propertiesToSkip) {
  329. propertiesToSkip = propertiesToSkip || ['name', 'filename', 'value', 'savedValue', 'isFinal', 'savedIsFinal'];
  330. for (var k in coreObject) {
  331. if (coreObject.hasOwnProperty(k)) {
  332. if (!propertiesToSkip.contains(k)) {
  333. coreObject[k] = this.getPropertyIfExists(k, coreObject[k], stackProperty, preDefined);
  334. }
  335. }
  336. }
  337. return coreObject;
  338. },
  339. /**
  340. * This method using for merging some properties from two objects
  341. * if property exists in <code>firstPriority<code> result will be it's property
  342. * else if property exists in <code>secondPriority<code> result will be it's property
  343. * otherwise <code>defaultValue<code> will be returned
  344. * @param {String} propertyName
  345. * @param {*} defaultValue=null
  346. * @param {Em.Object|Object} firstPriority
  347. * @param {Em.Object|Object} [secondPriority=null]
  348. * @returns {*}
  349. */
  350. getPropertyIfExists: function(propertyName, defaultValue, firstPriority, secondPriority) {
  351. firstPriority = firstPriority || {};
  352. secondPriority = secondPriority || {};
  353. var fp = Em.get(firstPriority, propertyName);
  354. if (firstPriority && !Em.isNone(fp)) {
  355. return fp;
  356. }
  357. else {
  358. var sp = Em.get(secondPriority, propertyName);
  359. if (secondPriority && !Em.isNone(sp)) {
  360. return sp;
  361. } else {
  362. return defaultValue;
  363. }
  364. }
  365. },
  366. /**
  367. * Get displayType for properties that has not defined value
  368. * @param value
  369. * @returns {string}
  370. */
  371. getDefaultDisplayType: function(value) {
  372. return value && !stringUtils.isSingleLine(value) ? 'multiLine' : 'string';
  373. },
  374. /**
  375. * Get category for properties that has not defined value
  376. * @param stackConfigProperty
  377. * @param fileName
  378. * @returns {string}
  379. */
  380. getDefaultCategory: function(stackConfigProperty, fileName) {
  381. return (stackConfigProperty ? 'Advanced ' : 'Custom ') + this.getConfigTagFromFileName(fileName);
  382. },
  383. /**
  384. * Get isSecureConfig for properties that has not defined value
  385. * @param propertyName
  386. * @returns {boolean}
  387. */
  388. getIsSecure: function(propertyName) {
  389. return !!this.get('secureConfigsMap')[propertyName];
  390. },
  391. /**
  392. * Calculate isEditable rely on controller state selected group and config restriction
  393. * @param {Object} serviceConfigProperty
  394. * @param {Object} selectedConfigGroup
  395. * @param {boolean} canEdit
  396. * @returns {boolean}
  397. */
  398. getIsEditable: function(serviceConfigProperty, selectedConfigGroup, canEdit) {
  399. return canEdit && Em.get(selectedConfigGroup, 'isDefault') && Em.get(serviceConfigProperty, 'isReconfigurable')
  400. },
  401. /**
  402. * format property value depending on displayType
  403. * and one exception for 'kdc_type'
  404. * @param serviceConfigProperty
  405. * @param [originalValue]
  406. * @returns {*}
  407. */
  408. formatPropertyValue: function(serviceConfigProperty, originalValue) {
  409. var value = Em.isNone(originalValue) ? Em.get(serviceConfigProperty, 'value') : originalValue,
  410. displayType = Em.get(serviceConfigProperty, 'displayType') || Em.get(serviceConfigProperty, 'valueAttributes.type'),
  411. category = Em.get(serviceConfigProperty, 'category');
  412. switch (displayType) {
  413. case 'content':
  414. case 'string':
  415. case 'multiLine':
  416. return this.trimProperty({ displayType: displayType, value: value });
  417. break;
  418. case 'directories':
  419. if (['DataNode', 'NameNode'].contains(category)) {
  420. return value.split(',').sort().join(',');//TODO check if this code is used
  421. }
  422. break;
  423. case 'directory':
  424. if (['SNameNode'].contains(category)) {
  425. return value.split(',').sort()[0];//TODO check if this code is used
  426. }
  427. break;
  428. case 'componentHosts':
  429. if (typeof(value) == 'string') {
  430. return value.replace(/\[|]|'|&apos;/g, "").split(',');
  431. }
  432. break;
  433. case 'int':
  434. if (/\d+m$/.test(value) ) {
  435. return value.slice(0, value.length - 1);
  436. } else {
  437. var int = parseInt(value);
  438. return isNaN(int) ? "" : int.toString();
  439. }
  440. break;
  441. case 'float':
  442. var float = parseFloat(value);
  443. return isNaN(float) ? "" : float.toString();
  444. }
  445. if (Em.get(serviceConfigProperty, 'name') === 'kdc_type') {
  446. return App.router.get('mainAdminKerberosController.kdcTypesValues')[value];
  447. }
  448. if ( /^\s+$/.test("" + value)) {
  449. value = " ";
  450. }
  451. return value;
  452. },
  453. /**
  454. *
  455. * @param configs
  456. * @returns {Object[]}
  457. */
  458. sortConfigs: function(configs) {
  459. return configs.sort(function(a, b) {
  460. if (Em.get(a, 'index') > Em.get(b, 'index')) return 1;
  461. if (Em.get(a, 'index') < Em.get(b, 'index')) return -1;
  462. if (Em.get(a, 'name') > Em.get(b, 'index')) return 1;
  463. if (Em.get(a, 'name') < Em.get(b, 'index')) return -1;
  464. return 0;
  465. });
  466. },
  467. /**
  468. *
  469. * @param {string} ifStatement
  470. * @param {Array} serviceConfigs
  471. * @returns {boolean}
  472. */
  473. calculateConfigCondition: function(ifStatement, serviceConfigs) {
  474. // Split `if` statement if it has logical operators
  475. var ifStatementRegex = /(&&|\|\|)/;
  476. var IfConditions = ifStatement.split(ifStatementRegex);
  477. var allConditionResult = [];
  478. IfConditions.forEach(function(_condition){
  479. var condition = _condition.trim();
  480. if (condition === '&&' || condition === '||') {
  481. allConditionResult.push(_condition);
  482. } else {
  483. var splitIfCondition = condition.split('===');
  484. var ifCondition = splitIfCondition[0];
  485. var result = splitIfCondition[1] || "true";
  486. var parseIfConditionVal = ifCondition;
  487. var regex = /\$\{.*?\}/g;
  488. var configStrings = ifCondition.match(regex);
  489. configStrings.forEach(function (_configString) {
  490. var configObject = _configString.substring(2, _configString.length - 1).split("/");
  491. var config = serviceConfigs.filterProperty('filename', configObject[0] + '.xml').findProperty('name', configObject[1]);
  492. if (config) {
  493. var configValue = Em.get(config, 'value');
  494. parseIfConditionVal = parseIfConditionVal.replace(_configString, configValue);
  495. }
  496. }, this);
  497. var conditionResult = window.eval(JSON.stringify(parseIfConditionVal.trim())) === result.trim();
  498. allConditionResult.push(conditionResult);
  499. }
  500. }, this);
  501. return Boolean(window.eval(allConditionResult.join('')));
  502. },
  503. /**
  504. * create new ServiceConfig object by service name
  505. * @param {string} serviceName
  506. * @param {App.ServiceConfigGroup[]} [configGroups]
  507. * @param {App.ServiceConfigProperty[]} [configs]
  508. * @param {Number} [initConfigsLength]
  509. * @return {App.ServiceConfig}
  510. * @method createServiceConfig
  511. */
  512. createServiceConfig: function (serviceName, configGroups, configs, initConfigsLength) {
  513. var preDefinedServiceConfig = App.config.get('preDefinedServiceConfigs').findProperty('serviceName', serviceName);
  514. return App.ServiceConfig.create({
  515. serviceName: preDefinedServiceConfig.get('serviceName'),
  516. displayName: preDefinedServiceConfig.get('displayName'),
  517. configCategories: preDefinedServiceConfig.get('configCategories'),
  518. configs: configs || [],
  519. configGroups: configGroups || [],
  520. initConfigsLength: initConfigsLength || 0
  521. });
  522. },
  523. /**
  524. * GETs all cluster level sites in one call.
  525. *
  526. * @return {$.ajax}
  527. */
  528. loadConfigsByTags: function (tags) {
  529. var urlParams = [];
  530. tags.forEach(function (_tag) {
  531. urlParams.push('(type=' + _tag.siteName + '&tag=' + _tag.tagName + ')');
  532. });
  533. var params = urlParams.join('|');
  534. return App.ajax.send({
  535. name: 'config.on_site',
  536. sender: this,
  537. data: {
  538. params: params
  539. }
  540. });
  541. },
  542. configTypesInfoMap: {},
  543. /**
  544. * Get config types and config type attributes from stack service
  545. *
  546. * @param service
  547. * @return {object}
  548. */
  549. getConfigTypesInfoFromService: function (service) {
  550. var configTypesInfoMap = this.get('configTypesInfoMap');
  551. if (configTypesInfoMap[service]) {
  552. // don't recalculate
  553. return configTypesInfoMap[service];
  554. }
  555. var configTypes = service.get('configTypes');
  556. var configTypesInfo = {
  557. items: [],
  558. supportsFinal: [],
  559. supportsAddingForbidden: []
  560. };
  561. if (configTypes) {
  562. for (var key in configTypes) {
  563. if (configTypes.hasOwnProperty(key)) {
  564. configTypesInfo.items.push(key);
  565. if (configTypes[key].supports && configTypes[key].supports.final === "true") {
  566. configTypesInfo.supportsFinal.push(key);
  567. }
  568. if (configTypes[key].supports && configTypes[key].supports.adding_forbidden === "true"){
  569. configTypesInfo.supportsAddingForbidden.push(key);
  570. }
  571. }
  572. }
  573. }
  574. configTypesInfoMap[service] = configTypesInfo;
  575. this.set('configTypesInfoMap', configTypesInfoMap);
  576. return configTypesInfo;
  577. },
  578. /**
  579. * Create config with non default config group. Some custom config properties
  580. * can be created and assigned to non-default config group.
  581. *
  582. * @param {String} propertyName - name of the property
  583. * @param {Object} config - config info
  584. * @param {Em.Object} group - config group to set
  585. * @param {Boolean} isEditable
  586. * @return {Object}
  587. **/
  588. createCustomGroupConfig: function (propertyName, config, group, isEditable) {
  589. var propertyObject = this.createDefaultConfig(propertyName, group.get('service.serviceName'), this.getOriginalFileName(config.type), false, {
  590. savedValue: config.properties[propertyName],
  591. value: config.properties[propertyName],
  592. group: group,
  593. isEditable: isEditable !== false,
  594. isOverridable: false
  595. });
  596. group.set('switchGroupTextShort', Em.I18n.t('services.service.config_groups.switchGroupTextShort').format(group.get('name')));
  597. group.set('switchGroupTextFull', Em.I18n.t('services.service.config_groups.switchGroupTextFull').format(group.get('name')));
  598. return App.ServiceConfigProperty.create(propertyObject);
  599. },
  600. complexConfigsTemplate: [
  601. {
  602. "name": "capacity-scheduler",
  603. "displayName": "Capacity Scheduler",
  604. "value": "",
  605. "description": "Capacity Scheduler properties",
  606. "displayType": "custom",
  607. "isOverridable": true,
  608. "isRequired": true,
  609. "isVisible": true,
  610. "isReconfigurable": true,
  611. "supportsFinal": false,
  612. "serviceName": "YARN",
  613. "filename": "capacity-scheduler.xml",
  614. "category": "CapacityScheduler"
  615. }
  616. ],
  617. /**
  618. * transform set of configs from file
  619. * into one config with textarea content:
  620. * name=value
  621. * @param {App.ServiceConfigProperty[]} configs
  622. * @param {String} filename
  623. * @param {App.ServiceConfigProperty[]} [configsToSkip=[]]
  624. * @return {*}
  625. */
  626. fileConfigsIntoTextarea: function (configs, filename, configsToSkip) {
  627. var fileConfigs = configs.filterProperty('filename', filename);
  628. var value = '', savedValue = '', recommendedValue = '';
  629. var template = this.get('complexConfigsTemplate').findProperty('filename', filename);
  630. var complexConfig = $.extend({}, template);
  631. if (complexConfig) {
  632. fileConfigs.forEach(function (_config) {
  633. if (!(configsToSkip && configsToSkip.someProperty('name', _config.name))) {
  634. value += _config.name + '=' + _config.value + '\n';
  635. if (!Em.isNone(_config.savedValue)) {
  636. savedValue += _config.name + '=' + _config.savedValue + '\n';
  637. }
  638. if (!Em.isNone(_config.recommendedValue)) {
  639. recommendedValue += _config.name + '=' + _config.recommendedValue + '\n';
  640. }
  641. }
  642. }, this);
  643. var isFinal = fileConfigs.someProperty('isFinal', true);
  644. var savedIsFinal = fileConfigs.someProperty('savedIsFinal', true);
  645. var recommendedIsFinal = fileConfigs.someProperty('recommendedIsFinal', true);
  646. complexConfig.value = value;
  647. if (savedValue) {
  648. complexConfig.savedValue = savedValue;
  649. }
  650. if (recommendedValue) {
  651. complexConfig.recommendedValue = recommendedValue;
  652. }
  653. complexConfig.isFinal = isFinal;
  654. complexConfig.savedIsFinal = savedIsFinal;
  655. complexConfig.recommendedIsFinal = recommendedIsFinal;
  656. configs = configs.filter(function (_config) {
  657. return _config.filename !== filename || (configsToSkip && configsToSkip.someProperty('name', _config.name));
  658. });
  659. configs.push(App.ServiceConfigProperty.create(complexConfig));
  660. }
  661. return configs;
  662. },
  663. /**
  664. * transform one config with textarea content
  665. * into set of configs of file
  666. * @param configs
  667. * @param filename
  668. * @return {*}
  669. */
  670. textareaIntoFileConfigs: function (configs, filename) {
  671. var complexConfigName = this.get('complexConfigsTemplate').findProperty('filename', filename).name;
  672. var configsTextarea = configs.findProperty('name', complexConfigName);
  673. if (configsTextarea && !App.get('testMode')) {
  674. var properties = configsTextarea.get('value').split('\n');
  675. properties.forEach(function (_property) {
  676. var name, value;
  677. if (_property) {
  678. _property = _property.split('=');
  679. name = _property[0];
  680. value = (_property[1]) ? _property[1] : "";
  681. configs.push(Em.Object.create({
  682. name: name,
  683. value: value,
  684. savedValue: value,
  685. serviceName: configsTextarea.get('serviceName'),
  686. filename: filename,
  687. isFinal: configsTextarea.get('isFinal'),
  688. isNotDefaultValue: configsTextarea.get('isNotDefaultValue'),
  689. isRequiredByAgent: configsTextarea.get('isRequiredByAgent'),
  690. group: null
  691. }));
  692. }
  693. });
  694. return configs.without(configsTextarea);
  695. }
  696. return configs;
  697. },
  698. /**
  699. * trim trailing spaces for all properties.
  700. * trim both trailing and leading spaces for host displayType and hive/oozie datebases url.
  701. * for directory or directories displayType format string for further using.
  702. * for password and values with spaces only do nothing.
  703. * @param {Object} property
  704. * @returns {*}
  705. */
  706. trimProperty: function (property) {
  707. var displayType = Em.get(property, 'displayType');
  708. var value = Em.get(property, 'value');
  709. var name = Em.get(property, 'name');
  710. var rez;
  711. switch (displayType) {
  712. case 'directories':
  713. case 'directory':
  714. rez = value.replace(/,/g, ' ').trim().split(/\s+/g).join(',');
  715. break;
  716. case 'host':
  717. rez = value.trim();
  718. break;
  719. case 'password':
  720. break;
  721. default:
  722. if (name == 'javax.jdo.option.ConnectionURL' || name == 'oozie.service.JPAService.jdbc.url') {
  723. rez = value.trim();
  724. }
  725. rez = (typeof value == 'string') ? value.replace(/(\s+$)/g, '') : value;
  726. }
  727. return ((rez == '') || (rez == undefined)) ? value : rez;
  728. },
  729. /**
  730. * Generate minimal config property object used in *_properties.js files.
  731. * Example:
  732. * <code>
  733. * var someProperties = App.config.generateConfigPropertiesByName([
  734. * 'property_1', 'property_2', 'property_3'], { category: 'General', filename: 'myFileName'});
  735. * // someProperties contains Object[]
  736. * [
  737. * {
  738. * name: 'property_1',
  739. * displayName: 'property_1',
  740. * isVisible: true,
  741. * isReconfigurable: true,
  742. * category: 'General',
  743. * filename: 'myFileName'
  744. * },
  745. * .......
  746. * ]
  747. * </code>
  748. * @param {string[]} names
  749. * @param {Object} properties - additional properties which will merge with base object definition
  750. * @returns {object[]}
  751. * @method generateConfigPropertiesByName
  752. */
  753. generateConfigPropertiesByName: function (names, properties) {
  754. return names.map(function (item) {
  755. var baseObj = {
  756. name: item
  757. };
  758. if (properties) return $.extend(baseObj, properties);
  759. else return baseObj;
  760. });
  761. },
  762. /**
  763. * load cluster stack configs from server and run mapper
  764. * @returns {$.ajax}
  765. * @method loadConfigsFromStack
  766. */
  767. loadClusterConfigsFromStack: function () {
  768. return App.ajax.send({
  769. name: 'configs.stack_configs.load.cluster_configs',
  770. sender: this,
  771. data: {
  772. stackVersionUrl: App.get('stackVersionURL')
  773. },
  774. success: 'saveConfigsToModel'
  775. });
  776. },
  777. /**
  778. * load stack configs from server and run mapper
  779. * @param {String[]} [serviceNames=null]
  780. * @returns {$.ajax}
  781. * @method loadConfigsFromStack
  782. */
  783. loadConfigsFromStack: function (serviceNames) {
  784. serviceNames = serviceNames || [];
  785. var name = serviceNames.length > 0 ? 'configs.stack_configs.load.services' : 'configs.stack_configs.load.all';
  786. return App.ajax.send({
  787. name: name,
  788. sender: this,
  789. data: {
  790. stackVersionUrl: App.get('stackVersionURL'),
  791. serviceList: serviceNames.join(',')
  792. },
  793. success: 'saveConfigsToModel'
  794. });
  795. },
  796. /**
  797. * Runs <code>stackConfigPropertiesMapper<code>
  798. * @param {object} data
  799. * @method saveConfigsToModel
  800. */
  801. saveConfigsToModel: function (data) {
  802. App.stackConfigPropertiesMapper.map(data);
  803. },
  804. /**
  805. * Check if config filename supports final attribute
  806. * @param serviceName
  807. * @param filename
  808. * @returns {boolean}
  809. */
  810. shouldSupportFinal: function (serviceName, filename) {
  811. var unsupportedServiceNames = ['MISC', 'Cluster'];
  812. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  813. return false;
  814. } else {
  815. var stackService = App.StackService.find(serviceName);
  816. if (!stackService) {
  817. return false;
  818. }
  819. return !!this.getConfigTypesInfoFromService(stackService).supportsFinal.find(function (configType) {
  820. return filename.startsWith(configType);
  821. });
  822. }
  823. },
  824. shouldSupportAddingForbidden: function(serviceName, filename) {
  825. var unsupportedServiceNames = ['MISC', 'Cluster'];
  826. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  827. return false;
  828. } else {
  829. var stackServiceName = App.StackService.find().findProperty('serviceName', serviceName);
  830. if (!stackServiceName) {
  831. return false;
  832. }
  833. var stackService = App.StackService.find(serviceName);
  834. return !!this.getConfigTypesInfoFromService(stackService).supportsAddingForbidden.find(function (configType) {
  835. return filename.startsWith(configType);
  836. });
  837. }
  838. },
  839. /**
  840. * Remove all ranger-related configs, that should be available only if Ranger is installed
  841. * @param configs - stepConfigs object
  842. */
  843. removeRangerConfigs: function (configs) {
  844. configs.forEach(function (service) {
  845. var filteredConfigs = [];
  846. service.get('configs').forEach(function (config) {
  847. if (!/^ranger-/.test(config.get('filename'))) {
  848. filteredConfigs.push(config);
  849. }
  850. });
  851. service.set('configs', filteredConfigs);
  852. var filteredCategories = [];
  853. service.get('configCategories').forEach(function (category) {
  854. if (!/ranger-/.test(category.get('name'))) {
  855. filteredCategories.push(category);
  856. }
  857. });
  858. service.set('configCategories', filteredCategories);
  859. });
  860. },
  861. /**
  862. * @param {App.ServiceConfigProperty} serviceConfigProperty
  863. * @param {Object} override - plain object with properties that is different from parent SCP
  864. * @param {App.ServiceConfigGroup} configGroup
  865. * @returns {App.ServiceConfigProperty}
  866. */
  867. createOverride: function(serviceConfigProperty, override, configGroup) {
  868. Em.assert('serviceConfigProperty can\' be null', serviceConfigProperty);
  869. Em.assert('configGroup can\' be null', configGroup);
  870. if (Em.isNone(serviceConfigProperty.get('overrides'))) serviceConfigProperty.set('overrides', []);
  871. var newOverride = App.ServiceConfigProperty.create(serviceConfigProperty);
  872. if (!Em.isNone(override)) {
  873. for (var key in override) {
  874. newOverride.set(key, override[key]);
  875. }
  876. }
  877. newOverride.setProperties({
  878. 'isOriginalSCP': false,
  879. 'overrides': null,
  880. 'group': configGroup,
  881. 'parentSCP': serviceConfigProperty
  882. });
  883. serviceConfigProperty.get('overrides').pushObject(newOverride);
  884. serviceConfigProperty.set('overrideValues', serviceConfigProperty.get('overrides').mapProperty('value'));
  885. serviceConfigProperty.set('overrideIsFinalValues', serviceConfigProperty.get('overrides').mapProperty('isFinal'));
  886. newOverride.validate();
  887. return newOverride;
  888. },
  889. /**
  890. * Merge values in "stored" to "base" if name matches, it's a value only merge.
  891. * @param base {Array} Em.Object
  892. * @param stored {Array} Object
  893. */
  894. mergeStoredValue: function(base, stored) {
  895. if (stored) {
  896. base.forEach(function (p) {
  897. var sp = stored.filterProperty("filename", p.filename).findProperty("name", p.name);
  898. if (sp) {
  899. p.set("value", sp.value);
  900. }
  901. });
  902. }
  903. },
  904. /**
  905. * Update config property value based on its current value and list of zookeeper server hosts.
  906. * Used to prevent sort order issues.
  907. * <code>siteConfigs</code> object formatted according server's persist format e.g.
  908. *
  909. * <code>
  910. * {
  911. * 'yarn-site': {
  912. * 'property_name1': 'property_value1'
  913. * 'property_name2': 'property_value2'
  914. * .....
  915. * }
  916. * }
  917. * </code>
  918. *
  919. * @method updateHostsListValue
  920. * @param {Object} siteConfigs - prepared site config object to store
  921. * @param {String} propertyName - name of the property to update
  922. * @param {String} hostsList - list of ZooKeeper Server names to set as config property value
  923. * @return {String} - result value
  924. */
  925. updateHostsListValue: function(siteConfigs, propertyName, hostsList) {
  926. var value = hostsList;
  927. var propertyHosts = (siteConfigs[propertyName] || '').split(',');
  928. var hostsToSet = hostsList.split(',');
  929. if (!Em.isEmpty(siteConfigs[propertyName])) {
  930. var diffLength = propertyHosts.filter(function(hostName) {
  931. return !hostsToSet.contains(hostName);
  932. }).length;
  933. if (diffLength == 0 && propertyHosts.length == hostsToSet.length) {
  934. value = siteConfigs[propertyName];
  935. }
  936. }
  937. siteConfigs[propertyName] = value;
  938. return value;
  939. }
  940. });