config.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. * parse Kerberos descriptor
  424. *
  425. * @param kerberosDescriptor
  426. * @returns {{}}
  427. */
  428. parseDescriptor: function(kerberosDescriptor) {
  429. var identitiesMap = {};
  430. Em.get(kerberosDescriptor, 'KerberosDescriptor.kerberos_descriptor.services').forEach(function (service) {
  431. this.parseIdentities(service, identitiesMap);
  432. service.components.forEach(function (component) {
  433. this.parseIdentities(component, identitiesMap);
  434. }, this);
  435. }, this);
  436. return identitiesMap;
  437. },
  438. /**
  439. * Looking for configs identities and add them to <code>identitiesMap<code>
  440. *
  441. * @param item
  442. * @param identitiesMap
  443. */
  444. parseIdentities: function (item, identitiesMap) {
  445. if (item.identities) {
  446. item.identities.forEach(function (identity) {
  447. Em.keys(identity).without('name').forEach(function (item) {
  448. if (identity[item].configuration) {
  449. var cfg = identity[item].configuration.split('/'), name = cfg[1], fileName = cfg[0];
  450. identitiesMap[App.config.configId(name, fileName)] = true;
  451. }
  452. });
  453. });
  454. }
  455. return identitiesMap;
  456. },
  457. /**
  458. * Update description for disabled kerberos configs which are identities
  459. *
  460. * @param description
  461. * @returns {*}
  462. */
  463. kerberosIdentitiesDescription: function(description) {
  464. return (description.endsWith('.') ? description : description + '.') +
  465. Em.I18n.t('services.service.config.secure.additionalDescription');
  466. },
  467. /**
  468. * Get view class based on display type of config
  469. *
  470. * @param displayType
  471. * @param dependentConfigPattern
  472. * @param unit
  473. * @returns {*}
  474. */
  475. getViewClass: function (displayType, dependentConfigPattern, unit) {
  476. switch (displayType) {
  477. case 'checkbox':
  478. case 'boolean':
  479. return dependentConfigPattern ? App.ServiceConfigCheckboxWithDependencies : App.ServiceConfigCheckbox;
  480. case 'password':
  481. return App.ServiceConfigPasswordField;
  482. case 'combobox':
  483. return App.ServiceConfigComboBox;
  484. case 'radio button':
  485. return App.ServiceConfigRadioButtons;
  486. case 'directories':
  487. return App.ServiceConfigTextArea;
  488. case 'directory':
  489. return App.ServiceConfigTextField;
  490. case 'content':
  491. return App.ServiceConfigTextAreaContent;
  492. case 'multiLine':
  493. return App.ServiceConfigTextArea;
  494. case 'custom':
  495. return App.ServiceConfigBigTextArea;
  496. case 'componentHost':
  497. return App.ServiceConfigMasterHostView;
  498. case 'label':
  499. return App.ServiceConfigLabelView;
  500. case 'componentHosts':
  501. return App.ServiceConfigComponentHostsView;
  502. case 'supportTextConnection':
  503. return App.checkConnectionView;
  504. case 'capacityScheduler':
  505. return App.CapacitySceduler;
  506. default:
  507. return unit ? App.ServiceConfigTextFieldWithUnit : App.ServiceConfigTextField;
  508. }
  509. },
  510. /**
  511. * Returns validator function based on config type
  512. *
  513. * @param displayType
  514. * @returns {Function}
  515. */
  516. getValidator: function (displayType) {
  517. switch (displayType) {
  518. case 'checkbox':
  519. case 'custom':
  520. return function () {
  521. return ''
  522. };
  523. case 'int':
  524. return function (value) {
  525. return !validator.isValidInt(value) && !validator.isConfigValueLink(value)
  526. ? Em.I18n.t('errorMessage.config.number.integer') : '';
  527. };
  528. case 'float':
  529. return function (value) {
  530. return !validator.isValidFloat(value) && !validator.isConfigValueLink(value)
  531. ? Em.I18n.t('errorMessage.config.number.float') : '';
  532. };
  533. case 'directories':
  534. case 'directory':
  535. return function (value, name) {
  536. if (App.config.isDirHeterogeneous(name)) {
  537. if (!validator.isValidDataNodeDir(value)) return Em.I18n.t('errorMessage.config.directory.heterogeneous');
  538. } else {
  539. if (!validator.isValidDir(value)) return Em.I18n.t('errorMessage.config.directory.default');
  540. }
  541. if (!validator.isAllowedDir(value)) {
  542. return Em.I18n.t('errorMessage.config.directory.allowed');
  543. }
  544. return validator.isNotTrimmedRight(value) ? Em.I18n.t('errorMessage.config.spaces.trailing') : '';
  545. };
  546. case 'email':
  547. return function (value) {
  548. return !validator.isValidEmail(value) ? Em.I18n.t('errorMessage.config.mail') : '';
  549. };
  550. case 'supportTextConnection':
  551. case 'host':
  552. return function (value) {
  553. return validator.isNotTrimmed(value) ? Em.I18n.t('errorMessage.config.spaces.trim') : '';
  554. };
  555. case 'password':
  556. return function (value, name, retypedPassword) {
  557. return value !== retypedPassword ? Em.I18n.t('errorMessage.config.password') : '';
  558. };
  559. case 'user':
  560. case 'database':
  561. case 'db_user':
  562. return function (value) {
  563. return !validator.isValidDbName(value) ? Em.I18n.t('errorMessage.config.user') : '';
  564. };
  565. default:
  566. return function (value, name) {
  567. if (['javax.jdo.option.ConnectionURL', 'oozie.service.JPAService.jdbc.url'].contains(name)
  568. && !validator.isConfigValueLink(value) && validator.isConfigValueLink(value)) {
  569. return Em.I18n.t('errorMessage.config.spaces.trim');
  570. } else {
  571. return validator.isNotTrimmedRight(value) ? Em.I18n.t('errorMessage.config.spaces.trailing') : '';
  572. }
  573. };
  574. }
  575. },
  576. /**
  577. * Defines if config support heterogeneous devices
  578. *
  579. * @param {string} name
  580. * @returns {boolean}
  581. */
  582. isDirHeterogeneous: function(name) {
  583. return ['dfs.datanode.data.dir'].contains(name);
  584. },
  585. /**
  586. * format property value depending on displayType
  587. * and one exception for 'kdc_type'
  588. * @param serviceConfigProperty
  589. * @param [originalValue]
  590. * @returns {*}
  591. */
  592. formatPropertyValue: function(serviceConfigProperty, originalValue) {
  593. var value = Em.isNone(originalValue) ? Em.get(serviceConfigProperty, 'value') : originalValue,
  594. displayType = Em.get(serviceConfigProperty, 'displayType') || Em.get(serviceConfigProperty, 'valueAttributes.type');
  595. if (Em.get(serviceConfigProperty, 'name') === 'kdc_type') {
  596. return App.router.get('mainAdminKerberosController.kdcTypesValues')[value];
  597. }
  598. if ( /^\s+$/.test("" + value)) {
  599. return " ";
  600. }
  601. switch (displayType) {
  602. case 'int':
  603. if (/\d+m$/.test(value) ) {
  604. return value.slice(0, value.length - 1);
  605. } else {
  606. var int = parseInt(value);
  607. return isNaN(int) ? "" : int.toString();
  608. }
  609. case 'float':
  610. var float = parseFloat(value);
  611. return isNaN(float) ? "" : float.toString();
  612. case 'componentHosts':
  613. if (typeof(value) == 'string') {
  614. return value.replace(/\[|]|'|&apos;/g, "").split(',');
  615. }
  616. return value;
  617. case 'content':
  618. case 'string':
  619. case 'multiLine':
  620. case 'directories':
  621. case 'directory':
  622. return this.trimProperty({ displayType: displayType, value: value });
  623. default:
  624. return value;
  625. }
  626. },
  627. /**
  628. * Format float value
  629. *
  630. * @param {*} value
  631. * @returns {string|*}
  632. */
  633. formatValue: function(value) {
  634. return validator.isValidFloat(value) ? parseFloat(value).toString() : value;
  635. },
  636. /**
  637. * Get step config by file name
  638. *
  639. * @param stepConfigs
  640. * @param fileName
  641. * @returns {Object|null}
  642. */
  643. getStepConfigForProperty: function (stepConfigs, fileName) {
  644. return stepConfigs.find(function (s) {
  645. return s.get('configTypes').contains(App.config.getConfigTagFromFileName(fileName));
  646. });
  647. },
  648. /**
  649. *
  650. * @param configs
  651. * @returns {Object[]}
  652. */
  653. sortConfigs: function(configs) {
  654. return configs.sort(function(a, b) {
  655. if (Em.get(a, 'index') > Em.get(b, 'index')) return 1;
  656. if (Em.get(a, 'index') < Em.get(b, 'index')) return -1;
  657. if (Em.get(a, 'name') > Em.get(b, 'index')) return 1;
  658. if (Em.get(a, 'name') < Em.get(b, 'index')) return -1;
  659. return 0;
  660. });
  661. },
  662. /**
  663. * create new ServiceConfig object by service name
  664. * @param {string} serviceName
  665. * @param {App.ServiceConfigGroup[]} [configGroups]
  666. * @param {App.ServiceConfigProperty[]} [configs]
  667. * @param {Number} [initConfigsLength]
  668. * @return {App.ServiceConfig}
  669. * @method createServiceConfig
  670. */
  671. createServiceConfig: function (serviceName, configGroups, configs, initConfigsLength) {
  672. var preDefinedServiceConfig = App.config.get('preDefinedServiceConfigs').findProperty('serviceName', serviceName);
  673. return App.ServiceConfig.create({
  674. serviceName: preDefinedServiceConfig.get('serviceName'),
  675. displayName: preDefinedServiceConfig.get('displayName'),
  676. configCategories: preDefinedServiceConfig.get('configCategories'),
  677. configs: configs || [],
  678. configGroups: configGroups || [],
  679. initConfigsLength: initConfigsLength || 0
  680. });
  681. },
  682. /**
  683. * GETs all cluster level sites in one call.
  684. *
  685. * @return {$.ajax}
  686. */
  687. loadConfigsByTags: function (tags) {
  688. var urlParams = [];
  689. tags.forEach(function (_tag) {
  690. urlParams.push('(type=' + _tag.siteName + '&tag=' + _tag.tagName + ')');
  691. });
  692. var params = urlParams.join('|');
  693. return App.ajax.send({
  694. name: 'config.on_site',
  695. sender: this,
  696. data: {
  697. params: params
  698. }
  699. });
  700. },
  701. configTypesInfoMap: {},
  702. /**
  703. * Get config types and config type attributes from stack service
  704. *
  705. * @param service
  706. * @return {object}
  707. */
  708. getConfigTypesInfoFromService: function (service) {
  709. var configTypesInfoMap = this.get('configTypesInfoMap');
  710. if (configTypesInfoMap[service]) {
  711. // don't recalculate
  712. return configTypesInfoMap[service];
  713. }
  714. var configTypes = service.get('configTypes');
  715. var configTypesInfo = {
  716. items: [],
  717. supportsFinal: [],
  718. supportsAddingForbidden: []
  719. };
  720. if (configTypes) {
  721. for (var key in configTypes) {
  722. if (configTypes.hasOwnProperty(key)) {
  723. configTypesInfo.items.push(key);
  724. if (configTypes[key].supports && configTypes[key].supports.final === "true") {
  725. configTypesInfo.supportsFinal.push(key);
  726. }
  727. if (configTypes[key].supports && configTypes[key].supports.adding_forbidden === "true"){
  728. configTypesInfo.supportsAddingForbidden.push(key);
  729. }
  730. }
  731. }
  732. }
  733. configTypesInfoMap[service] = configTypesInfo;
  734. this.set('configTypesInfoMap', configTypesInfoMap);
  735. return configTypesInfo;
  736. },
  737. /**
  738. *
  739. * @param configs
  740. */
  741. addYarnCapacityScheduler: function(configs) {
  742. var value = '', savedValue = '', recommendedValue = '',
  743. excludedConfigs = App.config.getPropertiesFromTheme('YARN');
  744. var connectedConfigs = configs.filter(function(config) {
  745. return !excludedConfigs.contains(App.config.configId(config.get('name'), config.get('filename'))) && (config.get('filename') === 'capacity-scheduler.xml');
  746. });
  747. var names = connectedConfigs.mapProperty('name');
  748. connectedConfigs.forEach(function (config) {
  749. value += config.get('name') + '=' + config.get('value') + '\n';
  750. if (!Em.isNone(config.get('savedValue'))) {
  751. savedValue += config.get('name') + '=' + config.get('savedValue') + '\n';
  752. }
  753. if (!Em.isNone(config.get('recommendedValue'))) {
  754. recommendedValue += config.get('name') + '=' + config.get('recommendedValue') + '\n';
  755. }
  756. }, this);
  757. var isFinal = connectedConfigs.someProperty('isFinal', true);
  758. var savedIsFinal = connectedConfigs.someProperty('savedIsFinal', true);
  759. var recommendedIsFinal = connectedConfigs.someProperty('recommendedIsFinal', true);
  760. var cs = App.config.createDefaultConfig('capacity-scheduler', 'capacity-scheduler.xml', true, {
  761. 'value': value,
  762. 'serviceName': 'YARN',
  763. 'savedValue': savedValue || null,
  764. 'recommendedValue': recommendedValue || null,
  765. 'isFinal': isFinal,
  766. 'savedIsFinal': savedIsFinal,
  767. 'recommendedIsFinal': recommendedIsFinal,
  768. 'category': 'CapacityScheduler',
  769. 'displayName': 'Capacity Scheduler',
  770. 'description': 'Capacity Scheduler properties',
  771. 'displayType': 'capacityScheduler'
  772. });
  773. configs = configs.filter(function(c) {
  774. return !(names.contains(c.get('name')) && (c.get('filename') === 'capacity-scheduler.xml'));
  775. });
  776. configs.push(App.ServiceConfigProperty.create(cs));
  777. return configs;
  778. },
  779. /**
  780. *
  781. * @param serviceName
  782. * @returns {Array}
  783. */
  784. getPropertiesFromTheme: function (serviceName) {
  785. var properties = [];
  786. App.Tab.find().forEach(function (t) {
  787. if (!t.get('isAdvanced') && t.get('serviceName') === serviceName) {
  788. t.get('sections').forEach(function (s) {
  789. s.get('subSections').forEach(function (ss) {
  790. properties = properties.concat(ss.get('configProperties'));
  791. });
  792. });
  793. }
  794. }, this);
  795. return properties;
  796. },
  797. /**
  798. * transform one config with textarea content
  799. * into set of configs of file
  800. * @param configs
  801. * @param filename
  802. * @return {*}
  803. */
  804. textareaIntoFileConfigs: function (configs, filename) {
  805. var configsTextarea = configs.findProperty('name', 'capacity-scheduler');
  806. if (configsTextarea && !App.get('testMode')) {
  807. var properties = configsTextarea.get('value').split('\n');
  808. properties.forEach(function (_property) {
  809. var name, value;
  810. if (_property) {
  811. _property = _property.split('=');
  812. name = _property[0];
  813. value = (_property[1]) ? _property[1] : "";
  814. configs.push(Em.Object.create({
  815. name: name,
  816. value: value,
  817. savedValue: value,
  818. serviceName: configsTextarea.get('serviceName'),
  819. filename: filename,
  820. isFinal: configsTextarea.get('isFinal'),
  821. isNotDefaultValue: configsTextarea.get('isNotDefaultValue'),
  822. isRequiredByAgent: configsTextarea.get('isRequiredByAgent'),
  823. group: null
  824. }));
  825. }
  826. });
  827. return configs.without(configsTextarea);
  828. }
  829. return configs;
  830. },
  831. /**
  832. * trim trailing spaces for all properties.
  833. * trim both trailing and leading spaces for host displayType and hive/oozie datebases url.
  834. * for directory or directories displayType format string for further using.
  835. * for password and values with spaces only do nothing.
  836. * @param {Object} property
  837. * @returns {*}
  838. */
  839. trimProperty: function (property) {
  840. var displayType = Em.get(property, 'displayType');
  841. var value = Em.get(property, 'value');
  842. var name = Em.get(property, 'name');
  843. var rez;
  844. switch (displayType) {
  845. case 'directories':
  846. case 'directory':
  847. rez = value.replace(/,/g, ' ').trim().split(/\s+/g).join(',');
  848. break;
  849. case 'host':
  850. rez = value.trim();
  851. break;
  852. case 'password':
  853. break;
  854. default:
  855. if (name == 'javax.jdo.option.ConnectionURL' || name == 'oozie.service.JPAService.jdbc.url') {
  856. rez = value.trim();
  857. }
  858. rez = (typeof value == 'string') ? value.replace(/(\s+$)/g, '') : value;
  859. }
  860. return ((rez == '') || (rez == undefined)) ? value : rez;
  861. },
  862. /**
  863. * Generate minimal config property object used in *_properties.js files.
  864. * Example:
  865. * <code>
  866. * var someProperties = App.config.generateConfigPropertiesByName([
  867. * 'property_1', 'property_2', 'property_3'], { category: 'General', filename: 'myFileName'});
  868. * // someProperties contains Object[]
  869. * [
  870. * {
  871. * name: 'property_1',
  872. * displayName: 'property_1',
  873. * isVisible: true,
  874. * isReconfigurable: true,
  875. * category: 'General',
  876. * filename: 'myFileName'
  877. * },
  878. * .......
  879. * ]
  880. * </code>
  881. * @param {string[]} names
  882. * @param {Object} properties - additional properties which will merge with base object definition
  883. * @returns {object[]}
  884. * @method generateConfigPropertiesByName
  885. */
  886. generateConfigPropertiesByName: function (names, properties) {
  887. return names.map(function (item) {
  888. var baseObj = {
  889. name: item
  890. };
  891. if (properties) return $.extend(baseObj, properties);
  892. else return baseObj;
  893. });
  894. },
  895. /**
  896. * load cluster stack configs from server and run mapper
  897. * @returns {$.ajax}
  898. * @method loadConfigsFromStack
  899. */
  900. loadClusterConfigsFromStack: function () {
  901. return App.ajax.send({
  902. name: 'configs.stack_configs.load.cluster_configs',
  903. sender: this,
  904. data: {
  905. stackVersionUrl: App.get('stackVersionURL')
  906. },
  907. success: 'saveConfigsToModel'
  908. });
  909. },
  910. /**
  911. * load stack configs from server and run mapper
  912. * @param {String[]} [serviceNames=null]
  913. * @returns {$.ajax}
  914. * @method loadConfigsFromStack
  915. */
  916. loadConfigsFromStack: function (serviceNames) {
  917. serviceNames = serviceNames || [];
  918. var name = serviceNames.length > 0 ? 'configs.stack_configs.load.services' : 'configs.stack_configs.load.all';
  919. return App.ajax.send({
  920. name: name,
  921. sender: this,
  922. data: {
  923. stackVersionUrl: App.get('stackVersionURL'),
  924. serviceList: serviceNames.join(',')
  925. },
  926. success: 'saveConfigsToModel'
  927. });
  928. },
  929. /**
  930. * Runs <code>stackConfigPropertiesMapper<code>
  931. * @param {object} data
  932. * @method saveConfigsToModel
  933. */
  934. saveConfigsToModel: function (data) {
  935. App.stackConfigPropertiesMapper.map(data);
  936. },
  937. /**
  938. * Check if config filename supports final attribute
  939. * @param serviceName
  940. * @param filename
  941. * @returns {boolean}
  942. */
  943. shouldSupportFinal: function (serviceName, filename) {
  944. var unsupportedServiceNames = ['MISC', 'Cluster'];
  945. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  946. return false;
  947. } else {
  948. var stackService = App.StackService.find(serviceName);
  949. if (!stackService) {
  950. return false;
  951. }
  952. return !!this.getConfigTypesInfoFromService(stackService).supportsFinal.find(function (configType) {
  953. return filename.startsWith(configType);
  954. });
  955. }
  956. },
  957. shouldSupportAddingForbidden: function(serviceName, filename) {
  958. var unsupportedServiceNames = ['MISC', 'Cluster'];
  959. if (!serviceName || unsupportedServiceNames.contains(serviceName) || !filename) {
  960. return false;
  961. } else {
  962. var stackServiceName = App.StackService.find().findProperty('serviceName', serviceName);
  963. if (!stackServiceName) {
  964. return false;
  965. }
  966. var stackService = App.StackService.find(serviceName);
  967. return !!this.getConfigTypesInfoFromService(stackService).supportsAddingForbidden.find(function (configType) {
  968. return filename.startsWith(configType);
  969. });
  970. }
  971. },
  972. /**
  973. * Remove all ranger-related configs, that should be available only if Ranger is installed
  974. * @param configs - stepConfigs object
  975. */
  976. removeRangerConfigs: function (configs) {
  977. configs.forEach(function (service) {
  978. var filteredConfigs = [];
  979. service.get('configs').forEach(function (config) {
  980. if (!/^ranger-/.test(config.get('filename'))) {
  981. filteredConfigs.push(config);
  982. }
  983. });
  984. service.set('configs', filteredConfigs);
  985. var filteredCategories = [];
  986. service.get('configCategories').forEach(function (category) {
  987. if (!/ranger-/.test(category.get('name'))) {
  988. filteredCategories.push(category);
  989. }
  990. });
  991. service.set('configCategories', filteredCategories);
  992. });
  993. },
  994. /**
  995. * Create config with non default config group. Some custom config properties
  996. * can be created and assigned to non-default config group.
  997. *
  998. * @param {Em.Object} override - config value
  999. * @param {Em.Object} configGroup - config group to set
  1000. * @return {Object}
  1001. **/
  1002. createCustomGroupConfig: function (override, configGroup) {
  1003. App.assertObject(override);
  1004. App.assertEmberObject(configGroup);
  1005. var newOverride = App.ServiceConfigProperty.create(this.createDefaultConfig(override.propertyName, override.filename, false, override));
  1006. newOverride.setProperties({
  1007. 'isOriginalSCP': false,
  1008. 'overrides': null,
  1009. 'group': configGroup,
  1010. 'parentSCP': null
  1011. });
  1012. if (!configGroup.get('properties.length')) {
  1013. configGroup.set('properties', Em.A([]));
  1014. }
  1015. configGroup.set('properties', configGroup.get('properties').concat(newOverride));
  1016. return newOverride;
  1017. },
  1018. /**
  1019. * @param {App.ServiceConfigProperty} serviceConfigProperty
  1020. * @param {Object} override - plain object with properties that is different from parent SCP
  1021. * @param {App.ServiceConfigGroup} configGroup
  1022. * @returns {App.ServiceConfigProperty}
  1023. */
  1024. createOverride: function(serviceConfigProperty, override, configGroup) {
  1025. App.assertObject(serviceConfigProperty);
  1026. App.assertEmberObject(configGroup);
  1027. if (Em.isNone(serviceConfigProperty.get('overrides'))) serviceConfigProperty.set('overrides', []);
  1028. var newOverride = App.ServiceConfigProperty.create(serviceConfigProperty);
  1029. newOverride.setProperties({ 'savedValue': null, 'savedIsFinal': null });
  1030. if (!Em.isNone(override)) {
  1031. for (var key in override) {
  1032. newOverride.set(key, override[key]);
  1033. }
  1034. }
  1035. newOverride.setProperties({
  1036. 'isOriginalSCP': false,
  1037. 'overrides': null,
  1038. 'group': configGroup,
  1039. 'parentSCP': serviceConfigProperty
  1040. });
  1041. if (!configGroup.get('properties.length')) {
  1042. configGroup.set('properties', Em.A([]));
  1043. }
  1044. configGroup.set('properties', configGroup.get('properties').concat(newOverride));
  1045. serviceConfigProperty.get('overrides').pushObject(newOverride);
  1046. var savedOverrides = serviceConfigProperty.get('overrides').filterProperty('savedValue');
  1047. serviceConfigProperty.set('overrideValues', savedOverrides.mapProperty('savedValue'));
  1048. serviceConfigProperty.set('overrideIsFinalValues', savedOverrides.mapProperty('savedIsFinal'));
  1049. return newOverride;
  1050. },
  1051. /**
  1052. * Merge values in "stored" to "base" if name matches, it's a value only merge.
  1053. * @param base {Array} Em.Object
  1054. * @param stored {Array} Object
  1055. */
  1056. mergeStoredValue: function(base, stored) {
  1057. if (stored) {
  1058. base.forEach(function (p) {
  1059. var sp = stored.filterProperty("filename", p.filename).findProperty("name", p.name);
  1060. if (sp) {
  1061. p.set("value", sp.value);
  1062. }
  1063. });
  1064. }
  1065. },
  1066. /**
  1067. * Helper method to get property from the <code>stepConfigs</code>
  1068. *
  1069. * @param {String} name - config property name
  1070. * @param {String} fileName - config property filename
  1071. * @param {Object[]} stepConfigs
  1072. * @return {App.ServiceConfigProperty|Boolean} - App.ServiceConfigProperty instance or <code>false</code> when property not found
  1073. */
  1074. findConfigProperty: function(stepConfigs, name, fileName) {
  1075. if (!name && !fileName) return false;
  1076. if (stepConfigs && stepConfigs.length) {
  1077. return stepConfigs.mapProperty('configs').filter(function(item) {
  1078. return item.length;
  1079. }).reduce(function(p, c) {
  1080. if (p) {
  1081. return p.concat(c);
  1082. }
  1083. }).filterProperty('filename', fileName).findProperty('name', name);
  1084. }
  1085. return false;
  1086. },
  1087. /**
  1088. * Update config property value based on its current value and list of zookeeper server hosts.
  1089. * Used to prevent sort order issues.
  1090. * <code>siteConfigs</code> object formatted according server's persist format e.g.
  1091. *
  1092. * <code>
  1093. * {
  1094. * 'yarn-site': {
  1095. * 'property_name1': 'property_value1'
  1096. * 'property_name2': 'property_value2'
  1097. * .....
  1098. * }
  1099. * }
  1100. * </code>
  1101. *
  1102. * @method updateHostsListValue
  1103. * @param {Object} siteConfigs - prepared site config object to store
  1104. * @param {String} propertyName - name of the property to update
  1105. * @param {String} hostsList - list of ZooKeeper Server names to set as config property value
  1106. * @return {String} - result value
  1107. */
  1108. updateHostsListValue: function(siteConfigs, propertyName, hostsList) {
  1109. var value = hostsList;
  1110. var propertyHosts = (siteConfigs[propertyName] || '').split(',');
  1111. var hostsToSet = hostsList.split(',');
  1112. if (!Em.isEmpty(siteConfigs[propertyName])) {
  1113. var diffLength = propertyHosts.filter(function(hostName) {
  1114. return !hostsToSet.contains(hostName);
  1115. }).length;
  1116. if (diffLength == 0 && propertyHosts.length == hostsToSet.length) {
  1117. value = siteConfigs[propertyName];
  1118. }
  1119. }
  1120. siteConfigs[propertyName] = value;
  1121. return value;
  1122. }
  1123. });