config.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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 validator = require('utils/validator');
  22. var configTagFromFileNameMap = {};
  23. App.config = Em.Object.create({
  24. CONFIG_GROUP_NAME_MAX_LENGTH: 18,
  25. /**
  26. * filename exceptions used to support substandard sitenames which don't have "xml" extension
  27. * @type {string[]}
  28. */
  29. filenameExceptions: ['alert_notification'],
  30. preDefinedServiceConfigs: [],
  31. /**
  32. *
  33. * Returns file name version that stored on server.
  34. *
  35. * Example:
  36. * App.config.getOriginalFileName('core-site') // returns core-site.xml
  37. * App.config.getOriginalFileName('zoo.cfg') // returns zoo.cfg
  38. *
  39. * @param {String} fileName
  40. * @method getOriginalFileName
  41. **/
  42. getOriginalFileName: function (fileName) {
  43. if (/\.xml$/.test(fileName)) return fileName;
  44. return this.get('filenameExceptions').contains(fileName) ? fileName : fileName + '.xml';
  45. },
  46. /**
  47. * truncate Config Group name to <CONFIG_GROUP_NAME_MAX_LENGTH> length and paste "..." in the middle
  48. */
  49. truncateGroupName: function (name) {
  50. if (name && name.length > App.config.CONFIG_GROUP_NAME_MAX_LENGTH) {
  51. var middle = Math.floor(App.config.CONFIG_GROUP_NAME_MAX_LENGTH / 2);
  52. name = name.substring(0, middle) + "..." + name.substring(name.length - middle);
  53. }
  54. return name;
  55. },
  56. /**
  57. * Check if Hive installation with new MySQL database created via Ambari is allowed
  58. * @param osFamily
  59. * @returns {boolean}
  60. */
  61. isManagedMySQLForHiveAllowed: function (osFamily) {
  62. var osList = ['redhat5', 'suse11'];
  63. return !osList.contains(osFamily);
  64. },
  65. /**
  66. *
  67. * Returns the configuration tagName from supplied filename
  68. *
  69. * Example:
  70. * App.config.getConfigTagFromFileName('core-site.xml') // returns core-site
  71. * App.config.getConfigTagFromFileName('zoo.cfg') // returns zoo.cfg
  72. *
  73. * @param {String} fileName
  74. * @method getConfigTagFromFileName
  75. **/
  76. getConfigTagFromFileName: function (fileName) {
  77. if (configTagFromFileNameMap[fileName]) {
  78. return configTagFromFileNameMap[fileName];
  79. }
  80. var ret = fileName.endsWith('.xml') ? fileName.slice(0, -4) : fileName;
  81. configTagFromFileNameMap[fileName] = ret;
  82. return ret;
  83. },
  84. /**
  85. *
  86. * @param name
  87. * @param fileName
  88. * @returns {string}
  89. */
  90. configId: function(name, fileName) {
  91. return name + "__" + App.config.getConfigTagFromFileName(fileName);
  92. },
  93. setPreDefinedServiceConfigs: function (isMiscTabToBeAdded) {
  94. var stackServices = App.StackService.find().filterProperty('id');
  95. // Only include services that has configTypes related to them for service configuration page
  96. var servicesWithConfigTypes = stackServices.filter(function (service) {
  97. var configtypes = service.get('configTypes');
  98. return configtypes && !!Object.keys(configtypes).length;
  99. }, this);
  100. var allTabs;
  101. if (isMiscTabToBeAdded) {
  102. var nonServiceTab = require('data/service_configs');
  103. var miscService = nonServiceTab.findProperty('serviceName', 'MISC');
  104. var tagTypes = {};
  105. servicesWithConfigTypes.mapProperty('configTypes').forEach(function (configTypes) {
  106. for (var fileName in configTypes) {
  107. if (fileName.endsWith('-env') && !miscService.get('configTypes')[fileName]) {
  108. tagTypes[fileName] = configTypes[fileName];
  109. }
  110. }
  111. });
  112. miscService.set('configTypes', $.extend(miscService.get('configTypes'), tagTypes));
  113. allTabs = servicesWithConfigTypes.concat(nonServiceTab);
  114. } else {
  115. allTabs = servicesWithConfigTypes;
  116. }
  117. this.set('preDefinedServiceConfigs', allTabs);
  118. },
  119. secureConfigs: require('data/HDP2/secure_mapping'),
  120. secureConfigsMap: function () {
  121. var ret = {};
  122. this.get('secureConfigs').forEach(function (sc) {
  123. ret[sc.name] = true;
  124. });
  125. return ret;
  126. }.property('secureConfigs.[]'),
  127. kerberosIdentities: require('data/HDP2/kerberos_identities').configProperties,
  128. kerberosIdentitiesMap: function() {
  129. var map = {};
  130. this.get('kerberosIdentities').forEach(function (c) {
  131. map[this.configId(c.name, c.filename)] = c;
  132. }, this);
  133. return map;
  134. }.property('kerberosIdentities'),
  135. customStackMapping: require('data/custom_stack_map'),
  136. mapCustomStack: function () {
  137. var
  138. baseStackFolder = App.get('currentStackName'),
  139. singMap = {
  140. "1": ">",
  141. "-1": "<",
  142. "0": "="
  143. };
  144. this.get('customStackMapping').every(function (stack) {
  145. if(stack.stackName == App.get('currentStackName')){
  146. var versionCompare = Em.compare(App.get('currentStackVersionNumber'), stack.stackVersionNumber);
  147. if(singMap[versionCompare+""] === stack.sign){
  148. baseStackFolder = stack.baseStackFolder;
  149. return false;
  150. }
  151. }
  152. return true;
  153. });
  154. return baseStackFolder;
  155. },
  156. allPreDefinedSiteProperties: function() {
  157. var sitePropertiesForCurrentStack = this.preDefinedConfigFile(this.mapCustomStack(), 'site_properties');
  158. if (sitePropertiesForCurrentStack) {
  159. return sitePropertiesForCurrentStack.configProperties;
  160. } else if (App.get('isHadoop23Stack')) {
  161. return require('data/HDP2.3/site_properties').configProperties;
  162. } else {
  163. return require('data/HDP2.2/site_properties').configProperties;
  164. }
  165. }.property('App.isHadoop23Stack'),
  166. preDefinedSiteProperties: function () {
  167. var serviceNames = App.StackService.find().mapProperty('serviceName').concat('MISC');
  168. return this.get('allPreDefinedSiteProperties').filter(function(p) {
  169. return serviceNames.contains(p.serviceName);
  170. });
  171. }.property('allPreDefinedSiteProperties'),
  172. /**
  173. * map of <code>preDefinedSiteProperties</code> provide search by index
  174. * @type {object}
  175. */
  176. preDefinedSitePropertiesMap: function () {
  177. var map = {};
  178. this.get('preDefinedSiteProperties').forEach(function (c) {
  179. map[this.configId(c.name, c.filename)] = c;
  180. }, this);
  181. return map;
  182. }.property('preDefinedSiteProperties'),
  183. preDefinedConfigFile: function(folder, file) {
  184. try {
  185. return require('data/{0}/{1}'.format(folder, file));
  186. } catch (err) {
  187. // the file doesn't exist, which might be expected.
  188. }
  189. },
  190. serviceByConfigTypeMap: function () {
  191. var ret = {};
  192. App.StackService.find().forEach(function(s) {
  193. s.get('configTypeList').forEach(function (ct) {
  194. ret[ct] = s;
  195. });
  196. });
  197. return ret;
  198. }.property(),
  199. /**
  200. * Generate configs collection with Ember or plain config objects
  201. * from config JSON
  202. *
  203. * @param configJSON
  204. * @param useEmberObject
  205. * @returns {Array}
  206. */
  207. getConfigsFromJSON: function(configJSON, useEmberObject) {
  208. var configs = [],
  209. filename = App.config.getOriginalFileName(configJSON.type),
  210. properties = configJSON.properties,
  211. finalAttributes = Em.get(configJSON, 'properties_attributes.final') || {};
  212. for (var index in properties) {
  213. var serviceConfigObj = this.getDefaultConfig(index, filename);
  214. if (serviceConfigObj.isRequiredByAgent !== false) {
  215. serviceConfigObj.value = serviceConfigObj.savedValue = this.formatPropertyValue(serviceConfigObj, properties[index]);
  216. serviceConfigObj.isFinal = serviceConfigObj.savedIsFinal = finalAttributes[index] === "true";
  217. serviceConfigObj.isEditable = serviceConfigObj.isReconfigurable;
  218. }
  219. if (useEmberObject) {
  220. configs.push(App.ServiceConfigProperty.create(serviceConfigObj));
  221. } else {
  222. configs.push(serviceConfigObj);
  223. }
  224. }
  225. return configs;
  226. },
  227. /**
  228. * Get config from configsCollections or
  229. * generate new default config in collection does not contain
  230. * such config
  231. *
  232. * @param name
  233. * @param fileName
  234. * @param coreObject
  235. * @returns {*|Object}
  236. */
  237. getDefaultConfig: function(name, fileName, coreObject) {
  238. var cfg = App.configsCollection.getConfigByName(name, fileName) ||
  239. App.config.createDefaultConfig(name, fileName, false);
  240. if (Em.typeOf(coreObject) === 'object') {
  241. Em.setProperties(cfg, coreObject);
  242. }
  243. return cfg;
  244. },
  245. /**
  246. * This method sets default values for config property
  247. * These property values has the lowest priority and can be overridden be stack/UI
  248. * config property but is used when such properties are absent in stack/UI configs
  249. * @param {string} name
  250. * @param {string} fileName
  251. * @param {boolean} definedInStack
  252. * @param {Object} [coreObject]
  253. * @returns {Object}
  254. */
  255. createDefaultConfig: function(name, fileName, definedInStack, coreObject) {
  256. var service = this.get('serviceByConfigTypeMap')[App.config.getConfigTagFromFileName(fileName)];
  257. var serviceName = service ? service.get('serviceName') : 'MISC';
  258. var tpl = {
  259. /** core properties **/
  260. id: this.configId(name, fileName),
  261. name: name,
  262. filename: this.getOriginalFileName(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. * Get component name from config name string
  324. *
  325. * @param configName
  326. * @returns {string}
  327. */
  328. getComponentName: function(configName) {
  329. var match = configName.match(/^(.*)_host[s]?$/) || [],
  330. component = match[1];
  331. return component ? component.toUpperCase() : "";
  332. },
  333. /**
  334. * This method merge properties form <code>stackConfigProperty<code> which are taken from stack
  335. * with <code>UIConfigProperty<code> which are hardcoded on UI
  336. * @param coreObject
  337. * @param stackProperty
  338. * @param preDefined
  339. * @param [propertiesToSkip]
  340. */
  341. mergeStaticProperties: function(coreObject, stackProperty, preDefined, propertiesToSkip) {
  342. propertiesToSkip = propertiesToSkip || ['name', 'filename', 'value', 'savedValue', 'isFinal', 'savedIsFinal'];
  343. for (var k in coreObject) {
  344. if (coreObject.hasOwnProperty(k)) {
  345. if (!propertiesToSkip.contains(k)) {
  346. coreObject[k] = this.getPropertyIfExists(k, coreObject[k], stackProperty, preDefined);
  347. }
  348. }
  349. }
  350. return coreObject;
  351. },
  352. /**
  353. * This method using for merging some properties from two objects
  354. * if property exists in <code>firstPriority<code> result will be it's property
  355. * else if property exists in <code>secondPriority<code> result will be it's property
  356. * otherwise <code>defaultValue<code> will be returned
  357. * @param {String} propertyName
  358. * @param {*} defaultValue=null
  359. * @param {Em.Object|Object} firstPriority
  360. * @param {Em.Object|Object} [secondPriority=null]
  361. * @returns {*}
  362. */
  363. getPropertyIfExists: function(propertyName, defaultValue, firstPriority, secondPriority) {
  364. firstPriority = firstPriority || {};
  365. secondPriority = secondPriority || {};
  366. var fp = Em.get(firstPriority, propertyName);
  367. if (firstPriority && !Em.isNone(fp)) {
  368. return fp;
  369. }
  370. else {
  371. var sp = Em.get(secondPriority, propertyName);
  372. if (secondPriority && !Em.isNone(sp)) {
  373. return sp;
  374. } else {
  375. return defaultValue;
  376. }
  377. }
  378. },
  379. /**
  380. * Get displayType for properties that has not defined value
  381. * @param value
  382. * @returns {string}
  383. */
  384. getDefaultDisplayType: function(value) {
  385. return value && !stringUtils.isSingleLine(value) ? 'multiLine' : 'string';
  386. },
  387. /**
  388. * Get category for properties that has not defined value
  389. * @param stackConfigProperty
  390. * @param fileName
  391. * @returns {string}
  392. */
  393. getDefaultCategory: function(stackConfigProperty, fileName) {
  394. return (stackConfigProperty ? 'Advanced ' : 'Custom ') + this.getConfigTagFromFileName(fileName);
  395. },
  396. /**
  397. * Get isSecureConfig for properties that has not defined value
  398. * @param propertyName
  399. * @returns {boolean}
  400. */
  401. getIsSecure: function(propertyName) {
  402. return !!this.get('secureConfigsMap')[propertyName];
  403. },
  404. /**
  405. * Returns description, formatted if needed
  406. *
  407. * @param {String} description
  408. * @param {String} displayType
  409. * @returns {String}
  410. */
  411. getDescription: function(description, displayType) {
  412. var additionalDescription = Em.I18n.t('services.service.config.password.additionalDescription');
  413. if ('password' === displayType) {
  414. if (description && !description.contains(additionalDescription)) {
  415. return description + '<br />' + additionalDescription;
  416. } else {
  417. return additionalDescription;
  418. }
  419. }
  420. return description
  421. },
  422. /**
  423. * Get view class based on display type of config
  424. *
  425. * @param displayType
  426. * @param dependentConfigPattern
  427. * @param unit
  428. * @returns {*}
  429. */
  430. getViewClass: function (displayType, dependentConfigPattern, unit) {
  431. switch (displayType) {
  432. case 'checkbox':
  433. case 'boolean':
  434. return dependentConfigPattern ? App.ServiceConfigCheckboxWithDependencies : App.ServiceConfigCheckbox;
  435. case 'password':
  436. return App.ServiceConfigPasswordField;
  437. case 'combobox':
  438. return App.ServiceConfigComboBox;
  439. case 'radio button':
  440. return App.ServiceConfigRadioButtons;
  441. case 'directories':
  442. return App.ServiceConfigTextArea;
  443. case 'directory':
  444. return App.ServiceConfigTextField;
  445. case 'content':
  446. return App.ServiceConfigTextAreaContent;
  447. case 'multiLine':
  448. return App.ServiceConfigTextArea;
  449. case 'custom':
  450. return App.ServiceConfigBigTextArea;
  451. case 'componentHost':
  452. return App.ServiceConfigMasterHostView;
  453. case 'label':
  454. return App.ServiceConfigLabelView;
  455. case 'componentHosts':
  456. return App.ServiceConfigComponentHostsView;
  457. case 'supportTextConnection':
  458. return App.checkConnectionView;
  459. case 'capacityScheduler':
  460. return App.CapacitySceduler;
  461. default:
  462. return unit ? App.ServiceConfigTextFieldWithUnit : App.ServiceConfigTextField;
  463. }
  464. },
  465. /**
  466. * Returns validator function based on config type
  467. *
  468. * @param displayType
  469. * @returns {Function}
  470. */
  471. getValidator: function (displayType) {
  472. switch (displayType) {
  473. case 'checkbox':
  474. case 'custom':
  475. return function () {
  476. return ''
  477. };
  478. case 'int':
  479. return function (value) {
  480. return !validator.isValidInt(value) && !validator.isConfigValueLink(value)
  481. ? Em.I18n.t('errorMessage.config.number.integer') : '';
  482. };
  483. case 'float':
  484. return function (value) {
  485. return !validator.isValidFloat(value) && !validator.isConfigValueLink(value)
  486. ? Em.I18n.t('errorMessage.config.number.float') : '';
  487. };
  488. case 'directories':
  489. case 'directory':
  490. return function (value, name) {
  491. if (App.config.isDirHeterogeneous(name)) {
  492. if (!validator.isValidDataNodeDir(value)) return Em.I18n.t('errorMessage.config.directory.heterogeneous');
  493. } else {
  494. if (!validator.isValidDir(value)) return Em.I18n.t('errorMessage.config.directory.default');
  495. }
  496. if (!validator.isAllowedDir(value)) {
  497. return Em.I18n.t('errorMessage.config.directory.allowed');
  498. }
  499. return validator.isNotTrimmedRight(value) ? Em.I18n.t('errorMessage.config.spaces.trailing') : '';
  500. };
  501. case 'email':
  502. return function (value) {
  503. return !validator.isValidEmail(value) ? Em.I18n.t('errorMessage.config.mail') : '';
  504. };
  505. case 'supportTextConnection':
  506. case 'host':
  507. return function (value) {
  508. return validator.isNotTrimmed(value) ? Em.I18n.t('errorMessage.config.spaces.trim') : '';
  509. };
  510. case 'password':
  511. return function (value, name, retypedPassword) {
  512. return value !== retypedPassword ? Em.I18n.t('errorMessage.config.password') : '';
  513. };
  514. case 'user':
  515. case 'database':
  516. case 'db_user':
  517. return function (value) {
  518. return !validator.isValidDbName(value) ? Em.I18n.t('errorMessage.config.user') : '';
  519. };
  520. default:
  521. return function (value, name) {
  522. if (['javax.jdo.option.ConnectionURL', 'oozie.service.JPAService.jdbc.url'].contains(name)
  523. && !validator.isConfigValueLink(value) && validator.isConfigValueLink(value)) {
  524. return Em.I18n.t('errorMessage.config.spaces.trim');
  525. } else {
  526. return validator.isNotTrimmedRight(value) ? Em.I18n.t('errorMessage.config.spaces.trailing') : '';
  527. }
  528. };
  529. }
  530. },
  531. /**
  532. * Defines if config support heterogeneous devices
  533. *
  534. * @param {string} name
  535. * @returns {boolean}
  536. */
  537. isDirHeterogeneous: function(name) {
  538. return ['dfs.datanode.data.dir'].contains(name);
  539. },
  540. /**
  541. * format property value depending on displayType
  542. * and one exception for 'kdc_type'
  543. * @param serviceConfigProperty
  544. * @param [originalValue]
  545. * @returns {*}
  546. */
  547. formatPropertyValue: function(serviceConfigProperty, originalValue) {
  548. var value = Em.isNone(originalValue) ? Em.get(serviceConfigProperty, 'value') : originalValue,
  549. displayType = Em.get(serviceConfigProperty, 'displayType') || Em.get(serviceConfigProperty, 'valueAttributes.type');
  550. if (Em.get(serviceConfigProperty, 'name') === 'kdc_type') {
  551. return App.router.get('mainAdminKerberosController.kdcTypesValues')[value];
  552. }
  553. if ( /^\s+$/.test("" + value)) {
  554. return " ";
  555. }
  556. switch (displayType) {
  557. case 'int':
  558. if (/\d+m$/.test(value) ) {
  559. return value.slice(0, value.length - 1);
  560. } else {
  561. var int = parseInt(value);
  562. return isNaN(int) ? "" : int.toString();
  563. }
  564. case 'float':
  565. var float = parseFloat(value);
  566. return isNaN(float) ? "" : float.toString();
  567. case 'componentHosts':
  568. if (typeof(value) == 'string') {
  569. return value.replace(/\[|]|'|&apos;/g, "").split(',');
  570. }
  571. return value;
  572. case 'content':
  573. case 'string':
  574. case 'multiLine':
  575. case 'directories':
  576. case 'directory':
  577. return this.trimProperty({ displayType: displayType, value: value });
  578. default:
  579. return value;
  580. }
  581. },
  582. /**
  583. * Format float value
  584. *
  585. * @param {*} value
  586. * @returns {string|*}
  587. */
  588. formatValue: function(value) {
  589. return validator.isValidFloat(value) ? parseFloat(value).toString() : value;
  590. },
  591. /**
  592. * Get step config by file name
  593. *
  594. * @param stepConfigs
  595. * @param fileName
  596. * @returns {Object|null}
  597. */
  598. getStepConfigForProperty: function (stepConfigs, fileName) {
  599. return stepConfigs.find(function (s) {
  600. return s.get('configTypes').contains(App.config.getConfigTagFromFileName(fileName));
  601. });
  602. },
  603. /**
  604. *
  605. * @param configs
  606. * @returns {Object[]}
  607. */
  608. sortConfigs: function(configs) {
  609. return configs.sort(function(a, b) {
  610. if (Em.get(a, 'index') > Em.get(b, 'index')) return 1;
  611. if (Em.get(a, 'index') < Em.get(b, 'index')) return -1;
  612. if (Em.get(a, 'name') > Em.get(b, 'index')) return 1;
  613. if (Em.get(a, 'name') < Em.get(b, 'index')) return -1;
  614. return 0;
  615. });
  616. },
  617. /**
  618. * create new ServiceConfig object by service name
  619. * @param {string} serviceName
  620. * @param {App.ServiceConfigGroup[]} [configGroups]
  621. * @param {App.ServiceConfigProperty[]} [configs]
  622. * @param {Number} [initConfigsLength]
  623. * @return {App.ServiceConfig}
  624. * @method createServiceConfig
  625. */
  626. createServiceConfig: function (serviceName, configGroups, configs, initConfigsLength) {
  627. var preDefinedServiceConfig = App.config.get('preDefinedServiceConfigs').findProperty('serviceName', serviceName);
  628. return App.ServiceConfig.create({
  629. serviceName: preDefinedServiceConfig.get('serviceName'),
  630. displayName: preDefinedServiceConfig.get('displayName'),
  631. configCategories: preDefinedServiceConfig.get('configCategories'),
  632. configs: configs || [],
  633. configGroups: configGroups || [],
  634. initConfigsLength: initConfigsLength || 0
  635. });
  636. },
  637. /**
  638. * GETs all cluster level sites in one call.
  639. *
  640. * @return {$.ajax}
  641. */
  642. loadConfigsByTags: function (tags) {
  643. var urlParams = [];
  644. tags.forEach(function (_tag) {
  645. urlParams.push('(type=' + _tag.siteName + '&tag=' + _tag.tagName + ')');
  646. });
  647. var params = urlParams.join('|');
  648. return App.ajax.send({
  649. name: 'config.on_site',
  650. sender: this,
  651. data: {
  652. params: params
  653. }
  654. });
  655. },
  656. configTypesInfoMap: {},
  657. /**
  658. * Get config types and config type attributes from stack service
  659. *
  660. * @param service
  661. * @return {object}
  662. */
  663. getConfigTypesInfoFromService: function (service) {
  664. var configTypesInfoMap = this.get('configTypesInfoMap');
  665. if (configTypesInfoMap[service]) {
  666. // don't recalculate
  667. return configTypesInfoMap[service];
  668. }
  669. var configTypes = service.get('configTypes');
  670. var configTypesInfo = {
  671. items: [],
  672. supportsFinal: [],
  673. supportsAddingForbidden: []
  674. };
  675. if (configTypes) {
  676. for (var key in configTypes) {
  677. if (configTypes.hasOwnProperty(key)) {
  678. configTypesInfo.items.push(key);
  679. if (configTypes[key].supports && configTypes[key].supports.final === "true") {
  680. configTypesInfo.supportsFinal.push(key);
  681. }
  682. if (configTypes[key].supports && configTypes[key].supports.adding_forbidden === "true"){
  683. configTypesInfo.supportsAddingForbidden.push(key);
  684. }
  685. }
  686. }
  687. }
  688. configTypesInfoMap[service] = configTypesInfo;
  689. this.set('configTypesInfoMap', configTypesInfoMap);
  690. return configTypesInfo;
  691. },
  692. /**
  693. *
  694. * @param configs
  695. */
  696. addYarnCapacityScheduler: function(configs) {
  697. var value = '', savedValue = '', recommendedValue = '',
  698. excludedConfigs = App.config.getPropertiesFromTheme('YARN');
  699. var connectedConfigs = configs.filter(function(config) {
  700. return !excludedConfigs.contains(App.config.configId(config.get('name'), config.get('filename'))) && (config.get('filename') === 'capacity-scheduler.xml');
  701. });
  702. var names = connectedConfigs.mapProperty('name');
  703. connectedConfigs.forEach(function (config) {
  704. value += config.get('name') + '=' + config.get('value') + '\n';
  705. if (!Em.isNone(config.get('savedValue'))) {
  706. savedValue += config.get('name') + '=' + config.get('savedValue') + '\n';
  707. }
  708. if (!Em.isNone(config.get('recommendedValue'))) {
  709. recommendedValue += config.get('name') + '=' + config.get('recommendedValue') + '\n';
  710. }
  711. }, this);
  712. var isFinal = connectedConfigs.someProperty('isFinal', true);
  713. var savedIsFinal = connectedConfigs.someProperty('savedIsFinal', true);
  714. var recommendedIsFinal = connectedConfigs.someProperty('recommendedIsFinal', true);
  715. var cs = App.config.createDefaultConfig('capacity-scheduler', 'capacity-scheduler.xml', true, {
  716. 'value': value,
  717. 'serviceName': 'YARN',
  718. 'savedValue': savedValue || null,
  719. 'recommendedValue': recommendedValue || null,
  720. 'isFinal': isFinal,
  721. 'savedIsFinal': savedIsFinal,
  722. 'recommendedIsFinal': recommendedIsFinal,
  723. 'category': 'CapacityScheduler',
  724. 'displayName': 'Capacity Scheduler',
  725. 'description': 'Capacity Scheduler properties',
  726. 'displayType': 'capacityScheduler'
  727. });
  728. configs = configs.filter(function(c) {
  729. return !(names.contains(c.get('name')) && (c.get('filename') === 'capacity-scheduler.xml'));
  730. });
  731. configs.push(App.ServiceConfigProperty.create(cs));
  732. return configs;
  733. },
  734. /**
  735. *
  736. * @param serviceName
  737. * @returns {Array}
  738. */
  739. getPropertiesFromTheme: function (serviceName) {
  740. var properties = [];
  741. App.Tab.find().forEach(function (t) {
  742. if (!t.get('isAdvanced') && t.get('serviceName') === serviceName) {
  743. t.get('sections').forEach(function (s) {
  744. s.get('subSections').forEach(function (ss) {
  745. properties = properties.concat(ss.get('configProperties'));
  746. });
  747. });
  748. }
  749. }, this);
  750. return properties;
  751. },
  752. /**
  753. * transform one config with textarea content
  754. * into set of configs of file
  755. * @param configs
  756. * @param filename
  757. * @return {*}
  758. */
  759. textareaIntoFileConfigs: function (configs, filename) {
  760. var configsTextarea = configs.findProperty('name', 'capacity-scheduler');
  761. if (configsTextarea && !App.get('testMode')) {
  762. var properties = configsTextarea.get('value').split('\n');
  763. properties.forEach(function (_property) {
  764. var name, value;
  765. if (_property) {
  766. _property = _property.split('=');
  767. name = _property[0];
  768. value = (_property[1]) ? _property[1] : "";
  769. configs.push(Em.Object.create({
  770. name: name,
  771. value: value,
  772. savedValue: value,
  773. serviceName: configsTextarea.get('serviceName'),
  774. filename: filename,
  775. isFinal: configsTextarea.get('isFinal'),
  776. isNotDefaultValue: configsTextarea.get('isNotDefaultValue'),
  777. isRequiredByAgent: configsTextarea.get('isRequiredByAgent'),
  778. group: null
  779. }));
  780. }
  781. });
  782. return configs.without(configsTextarea);
  783. }
  784. return configs;
  785. },
  786. /**
  787. * trim trailing spaces for all properties.
  788. * trim both trailing and leading spaces for host displayType and hive/oozie datebases url.
  789. * for directory or directories displayType format string for further using.
  790. * for password and values with spaces only do nothing.
  791. * @param {Object} property
  792. * @returns {*}
  793. */
  794. trimProperty: function (property) {
  795. var displayType = Em.get(property, 'displayType');
  796. var value = Em.get(property, 'value');
  797. var name = Em.get(property, 'name');
  798. var rez;
  799. switch (displayType) {
  800. case 'directories':
  801. case 'directory':
  802. rez = value.replace(/,/g, ' ').trim().split(/\s+/g).join(',');
  803. break;
  804. case 'host':
  805. rez = value.trim();
  806. break;
  807. case 'password':
  808. break;
  809. default:
  810. if (name == 'javax.jdo.option.ConnectionURL' || name == 'oozie.service.JPAService.jdbc.url') {
  811. rez = value.trim();
  812. }
  813. rez = (typeof value == 'string') ? value.replace(/(\s+$)/g, '') : value;
  814. }
  815. return ((rez == '') || (rez == undefined)) ? value : rez;
  816. },
  817. /**
  818. * Generate minimal config property object used in *_properties.js files.
  819. * Example:
  820. * <code>
  821. * var someProperties = App.config.generateConfigPropertiesByName([
  822. * 'property_1', 'property_2', 'property_3'], { category: 'General', filename: 'myFileName'});
  823. * // someProperties contains Object[]
  824. * [
  825. * {
  826. * name: 'property_1',
  827. * displayName: 'property_1',
  828. * isVisible: true,
  829. * isReconfigurable: true,
  830. * category: 'General',
  831. * filename: 'myFileName'
  832. * },
  833. * .......
  834. * ]
  835. * </code>
  836. * @param {string[]} names
  837. * @param {Object} properties - additional properties which will merge with base object definition
  838. * @returns {object[]}
  839. * @method generateConfigPropertiesByName
  840. */
  841. generateConfigPropertiesByName: function (names, properties) {
  842. return names.map(function (item) {
  843. var baseObj = {
  844. name: item
  845. };
  846. if (properties) return $.extend(baseObj, properties);
  847. else return baseObj;
  848. });
  849. },
  850. /**
  851. * load cluster stack configs from server and run mapper
  852. * @returns {$.ajax}
  853. * @method loadConfigsFromStack
  854. */
  855. loadClusterConfigsFromStack: function () {
  856. return App.ajax.send({
  857. name: 'configs.stack_configs.load.cluster_configs',
  858. sender: this,
  859. data: {
  860. stackVersionUrl: App.get('stackVersionURL')
  861. },
  862. success: 'saveConfigsToModel'
  863. });
  864. },
  865. /**
  866. * load stack configs from server and run mapper
  867. * @param {String[]} [serviceNames=null]
  868. * @returns {$.ajax}
  869. * @method loadConfigsFromStack
  870. */
  871. loadConfigsFromStack: function (serviceNames) {
  872. serviceNames = serviceNames || [];
  873. var name = serviceNames.length > 0 ? 'configs.stack_configs.load.services' : 'configs.stack_configs.load.all';
  874. return App.ajax.send({
  875. name: name,
  876. sender: this,
  877. data: {
  878. stackVersionUrl: App.get('stackVersionURL'),
  879. serviceList: serviceNames.join(',')
  880. },
  881. success: 'saveConfigsToModel'
  882. });
  883. },
  884. /**
  885. * Runs <code>stackConfigPropertiesMapper<code>
  886. * @param {object} data
  887. * @method saveConfigsToModel
  888. */
  889. saveConfigsToModel: function (data) {
  890. App.stackConfigPropertiesMapper.map(data);
  891. },
  892. /**
  893. * Check if config filename supports final attribute
  894. * @param serviceName
  895. * @param filename
  896. * @returns {boolean}
  897. */
  898. shouldSupportFinal: function (serviceName, filename) {
  899. var unsupportedServiceNames = ['MISC', 'Cluster'];
  900. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  901. return false;
  902. } else {
  903. var stackService = App.StackService.find(serviceName);
  904. if (!stackService) {
  905. return false;
  906. }
  907. return !!this.getConfigTypesInfoFromService(stackService).supportsFinal.find(function (configType) {
  908. return filename.startsWith(configType);
  909. });
  910. }
  911. },
  912. shouldSupportAddingForbidden: function(serviceName, filename) {
  913. var unsupportedServiceNames = ['MISC', 'Cluster'];
  914. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  915. return false;
  916. } else {
  917. var stackServiceName = App.StackService.find().findProperty('serviceName', serviceName);
  918. if (!stackServiceName) {
  919. return false;
  920. }
  921. var stackService = App.StackService.find(serviceName);
  922. return !!this.getConfigTypesInfoFromService(stackService).supportsAddingForbidden.find(function (configType) {
  923. return filename.startsWith(configType);
  924. });
  925. }
  926. },
  927. /**
  928. * Remove all ranger-related configs, that should be available only if Ranger is installed
  929. * @param configs - stepConfigs object
  930. */
  931. removeRangerConfigs: function (configs) {
  932. configs.forEach(function (service) {
  933. var filteredConfigs = [];
  934. service.get('configs').forEach(function (config) {
  935. if (!/^ranger-/.test(config.get('filename'))) {
  936. filteredConfigs.push(config);
  937. }
  938. });
  939. service.set('configs', filteredConfigs);
  940. var filteredCategories = [];
  941. service.get('configCategories').forEach(function (category) {
  942. if (!/ranger-/.test(category.get('name'))) {
  943. filteredCategories.push(category);
  944. }
  945. });
  946. service.set('configCategories', filteredCategories);
  947. });
  948. },
  949. /**
  950. * Create config with non default config group. Some custom config properties
  951. * can be created and assigned to non-default config group.
  952. *
  953. * @param {Em.Object} override - config value
  954. * @param {Em.Object} configGroup - config group to set
  955. * @return {Object}
  956. **/
  957. createCustomGroupConfig: function (override, configGroup) {
  958. App.assertObject(override);
  959. App.assertEmberObject(configGroup);
  960. var newOverride = App.ServiceConfigProperty.create(this.createDefaultConfig(override.propertyName, override.filename, false, override));
  961. newOverride.setProperties({
  962. 'isOriginalSCP': false,
  963. 'overrides': null,
  964. 'group': configGroup,
  965. 'parentSCP': null
  966. });
  967. if (!configGroup.get('properties.length')) {
  968. configGroup.set('properties', Em.A([]));
  969. }
  970. configGroup.set('properties', configGroup.get('properties').concat(newOverride));
  971. return newOverride;
  972. },
  973. /**
  974. * @param {App.ServiceConfigProperty} serviceConfigProperty
  975. * @param {Object} override - plain object with properties that is different from parent SCP
  976. * @param {App.ServiceConfigGroup} configGroup
  977. * @returns {App.ServiceConfigProperty}
  978. */
  979. createOverride: function(serviceConfigProperty, override, configGroup) {
  980. App.assertObject(serviceConfigProperty);
  981. App.assertEmberObject(configGroup);
  982. if (Em.isNone(serviceConfigProperty.get('overrides'))) serviceConfigProperty.set('overrides', []);
  983. var newOverride = App.ServiceConfigProperty.create(serviceConfigProperty);
  984. newOverride.setProperties({ 'savedValue': null, 'savedIsFinal': null });
  985. if (!Em.isNone(override)) {
  986. for (var key in override) {
  987. newOverride.set(key, override[key]);
  988. }
  989. }
  990. newOverride.setProperties({
  991. 'isOriginalSCP': false,
  992. 'overrides': null,
  993. 'group': configGroup,
  994. 'parentSCP': serviceConfigProperty
  995. });
  996. if (!configGroup.get('properties.length')) {
  997. configGroup.set('properties', Em.A([]));
  998. }
  999. configGroup.set('properties', configGroup.get('properties').concat(newOverride));
  1000. serviceConfigProperty.get('overrides').pushObject(newOverride);
  1001. var savedOverrides = serviceConfigProperty.get('overrides').filterProperty('savedValue');
  1002. serviceConfigProperty.set('overrideValues', savedOverrides.mapProperty('savedValue'));
  1003. serviceConfigProperty.set('overrideIsFinalValues', savedOverrides.mapProperty('savedIsFinal'));
  1004. return newOverride;
  1005. },
  1006. /**
  1007. * Merge values in "stored" to "base" if name matches, it's a value only merge.
  1008. * @param base {Array} Em.Object
  1009. * @param stored {Array} Object
  1010. */
  1011. mergeStoredValue: function(base, stored) {
  1012. if (stored) {
  1013. base.forEach(function (p) {
  1014. var sp = stored.filterProperty("filename", p.filename).findProperty("name", p.name);
  1015. if (sp) {
  1016. p.set("value", sp.value);
  1017. }
  1018. });
  1019. }
  1020. },
  1021. /**
  1022. * Helper method to get property from the <code>stepConfigs</code>
  1023. *
  1024. * @param {String} name - config property name
  1025. * @param {String} fileName - config property filename
  1026. * @param {Object[]} stepConfigs
  1027. * @return {App.ServiceConfigProperty|Boolean} - App.ServiceConfigProperty instance or <code>false</code> when property not found
  1028. */
  1029. findConfigProperty: function(stepConfigs, name, fileName) {
  1030. if (!name && !fileName) return false;
  1031. if (stepConfigs && stepConfigs.length) {
  1032. return stepConfigs.mapProperty('configs').filter(function(item) {
  1033. return item.length;
  1034. }).reduce(function(p, c) {
  1035. if (p) {
  1036. return p.concat(c);
  1037. }
  1038. }).filterProperty('filename', fileName).findProperty('name', name);
  1039. }
  1040. return false;
  1041. },
  1042. /**
  1043. * Update config property value based on its current value and list of zookeeper server hosts.
  1044. * Used to prevent sort order issues.
  1045. * <code>siteConfigs</code> object formatted according server's persist format e.g.
  1046. *
  1047. * <code>
  1048. * {
  1049. * 'yarn-site': {
  1050. * 'property_name1': 'property_value1'
  1051. * 'property_name2': 'property_value2'
  1052. * .....
  1053. * }
  1054. * }
  1055. * </code>
  1056. *
  1057. * @method updateHostsListValue
  1058. * @param {Object} siteConfigs - prepared site config object to store
  1059. * @param {String} propertyName - name of the property to update
  1060. * @param {String} hostsList - list of ZooKeeper Server names to set as config property value
  1061. * @return {String} - result value
  1062. */
  1063. updateHostsListValue: function(siteConfigs, propertyName, hostsList) {
  1064. var value = hostsList;
  1065. var propertyHosts = (siteConfigs[propertyName] || '').split(',');
  1066. var hostsToSet = hostsList.split(',');
  1067. if (!Em.isEmpty(siteConfigs[propertyName])) {
  1068. var diffLength = propertyHosts.filter(function(hostName) {
  1069. return !hostsToSet.contains(hostName);
  1070. }).length;
  1071. if (diffLength == 0 && propertyHosts.length == hostsToSet.length) {
  1072. value = siteConfigs[propertyName];
  1073. }
  1074. }
  1075. siteConfigs[propertyName] = value;
  1076. return value;
  1077. }
  1078. });