config.js 39 KB

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