serverValidator.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. var blueprintUtils = require('utils/blueprint');
  20. App.ServerValidatorMixin = Em.Mixin.create({
  21. /**
  22. * defines if we use validation and recommendation on wizards
  23. * depend on this flag some properties will be taken from different places
  24. * @type {boolean}
  25. */
  26. isWizard: function() {
  27. return this.get('wizardController') && ['addServiceController', 'installerController'].contains(this.get('wizardController.name'));
  28. }.property('wizardController.name'),
  29. /**
  30. * @type {boolean} set true if at least one config has error
  31. */
  32. configValidationError: false,
  33. /**
  34. * @type {boolean} set true if at least one config has warning
  35. */
  36. configValidationWarning: false,
  37. /**
  38. * @type {boolean} set true if at least one config has warning
  39. */
  40. configValidationFailed: false,
  41. /**
  42. * @type {object[]} contains additional message about validation errors
  43. */
  44. configValidationGlobalMessage: [],
  45. /**
  46. * recommendation configs loaded from server
  47. * (used only during install)
  48. * @type {Object}
  49. */
  50. recommendationsConfigs: null,
  51. /**
  52. * array of cluster-env configs
  53. * used for validation request
  54. * @type {Array}
  55. */
  56. clusterEnvConfigs: [],
  57. /**
  58. * by default loads data from model otherwise must be overridden as computed property
  59. * refer to \assets\data\stacks\HDP-2.1\recommendations_configs.json to learn structure
  60. * (shouldn't contain configurations filed)
  61. * @type {Object}
  62. */
  63. hostNames: function() {
  64. return this.get('isWizard')
  65. ? Object.keys(this.get('content.hosts'))
  66. : App.get('allHostNames');
  67. }.property('isWizard', 'content.hosts', 'App.allHostNames'),
  68. /**
  69. * by default loads data from model otherwise must be overridden as computed property
  70. * @type {Array} - of strings (serviceNames)
  71. */
  72. serviceNames: function() {
  73. // When editing a service we validate only that service's configs.
  74. // However, we should pass the IDs of services installed, or else,
  75. // default value calculations will alter.
  76. return this.get('isWizard') ? this.get('allSelectedServiceNames') : App.Service.find().mapProperty('serviceName');
  77. }.property('isWizard', 'allSelectedServiceNames'),
  78. /**
  79. * by default loads data from model otherwise must be overridden as computed property
  80. * filter services that support server validation and concat with misc configs if Installer or current service
  81. * @type {Array} - of objects (services)
  82. */
  83. services: function() {
  84. var stackServices = App.StackService.find().filter(function(s) {
  85. return this.get('serviceNames').contains(s.get('serviceName'));
  86. }, this);
  87. return this.get('isWizard') ? stackServices.concat(require("data/service_configs")) : stackServices;
  88. }.property('serviceNames'),
  89. /**
  90. * by default loads data from model otherwise must be overridden as computed property
  91. * can be used for service|host configs pages
  92. * @type {Array} of strings (hostNames)
  93. */
  94. hostGroups: function() {
  95. return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(App.get('allHostNames'));
  96. }.property('content.recommendationsHostGroups', 'App.allHostNames'),
  97. /**
  98. * controller that is child of this mixin has to contain stepConfigs
  99. * @type {Array}
  100. */
  101. stepConfigs: null,
  102. /**
  103. * @method loadServerSideConfigsRecommendations
  104. * load recommendations from server
  105. * (used only during install)
  106. * @returns {*}
  107. */
  108. loadServerSideConfigsRecommendations: function() {
  109. /**
  110. * if extended controller doesn't support recommendations or recommendations has been already loaded
  111. * ignore this call but keep promise chain
  112. */
  113. if (!this.get('isControllerSupportsEnhancedConfigs') || !Em.isNone(this.get('recommendationsConfigs'))) {
  114. return $.Deferred().resolve().promise();
  115. }
  116. var recommendations = this.get('hostGroups');
  117. // send user's input based on stored configurations
  118. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  119. // include cluster-env site to recommendations call
  120. var miscService = this.get('services').findProperty('serviceName', 'MISC');
  121. if (miscService) {
  122. var miscConfigs = blueprintUtils.buildConfigsJSON([miscService], [this.get('stepConfigs').findProperty('serviceName', 'MISC')]);
  123. var clusterEnv = App.permit(miscConfigs, 'cluster-env');
  124. if (!App.isEmptyObject(clusterEnv)) {
  125. $.extend(recommendations.blueprint.configurations, clusterEnv);
  126. }
  127. /** add user properties from misc tabs to proper filename **/
  128. this.get('stepConfigs').findProperty('serviceName', 'MISC').get('configs').forEach(function(config) {
  129. var tag = App.config.getConfigTagFromFileName(config.get('filename'));
  130. if (recommendations.blueprint.configurations[tag] && tag != 'cluster-env') {
  131. recommendations.blueprint.configurations[tag].properties[config.get('name')] = config.get('value');
  132. }
  133. })
  134. }
  135. this.set('initialConfigValues', recommendations.blueprint.configurations);
  136. return App.ajax.send({
  137. 'name': 'config.recommendations',
  138. 'sender': this,
  139. 'data': {
  140. stackVersionUrl: App.get('stackVersionURL'),
  141. dataToSend: {
  142. recommend: 'configurations',
  143. hosts: this.get('hostNames'),
  144. services: this.get('serviceNames'),
  145. recommendations: recommendations
  146. }
  147. },
  148. 'success': 'loadRecommendationsSuccess',
  149. 'error': 'loadRecommendationsError'
  150. });
  151. },
  152. /**
  153. * @method loadRecommendationsSuccess
  154. * success callback after loading recommendations
  155. * (used only during install)
  156. * @param data
  157. */
  158. loadRecommendationsSuccess: function(data) {
  159. this._saveRecommendedValues(data);
  160. var configObject = data.resources[0].recommendations.blueprint.configurations;
  161. if (configObject) this.updateInitialValue(configObject);
  162. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  163. },
  164. loadRecommendationsError: function(jqXHR, ajaxOptions, error, opt) {
  165. },
  166. serverSideValidation: function () {
  167. var deferred = $.Deferred();
  168. var self = this;
  169. this.set('configValidationFailed', false);
  170. this.set('configValidationGlobalMessage', []);
  171. if (this.get('configValidationFailed')) {
  172. this.warnUser(deferred);
  173. } else {
  174. this.runServerSideValidation(deferred);
  175. }
  176. return deferred;
  177. },
  178. getAllHostsWithComponents: function() {
  179. return App.ajax.send({
  180. sender: this,
  181. name: 'common.hosts.all',
  182. data: {
  183. urlParams: 'fields=HostRoles/component_name,HostRoles/host_name'
  184. }
  185. });
  186. },
  187. /**
  188. * @method serverSideValidation
  189. * send request to validate configs
  190. * @returns {*}
  191. */
  192. runServerSideValidation: function (deferred) {
  193. var self = this;
  194. var recommendations = this.get('hostGroups');
  195. var services = this.get('services');
  196. var stepConfigs = this.get('stepConfigs');
  197. return this.getBlueprintConfigurations().done(function(blueprintConfigurations){
  198. recommendations.blueprint.configurations = blueprintConfigurations;
  199. return App.ajax.send({
  200. name: 'config.validations',
  201. sender: self,
  202. data: {
  203. stackVersionUrl: App.get('stackVersionURL'),
  204. hosts: self.get('hostNames'),
  205. services: self.get('serviceNames'),
  206. validate: 'configurations',
  207. recommendations: recommendations
  208. },
  209. success: 'validationSuccess',
  210. error: 'validationError'
  211. }).complete(function () {
  212. self.warnUser(deferred);
  213. });
  214. });
  215. },
  216. /**
  217. * Return JSON for blueprint configurations
  218. * @returns {*}
  219. */
  220. getBlueprintConfigurations: function () {
  221. var dfd = $.Deferred();
  222. var services = this.get('services');
  223. var stepConfigs = this.get('stepConfigs');
  224. var allConfigTypes = [];
  225. services.forEach(function (service) {
  226. allConfigTypes = allConfigTypes.concat(Em.keys(service.get('configTypes')))
  227. });
  228. // check if we have configs from 'cluster-env', if not, then load them, as they are mandatory for validation request
  229. if (!allConfigTypes.contains('cluster-env')) {
  230. this.getClusterEnvConfigsForValidation().done(function(clusterEnvConfigs){
  231. services = services.concat(Em.Object.create({
  232. serviceName: 'MISC',
  233. configTypes: {'cluster-env': {}}
  234. }));
  235. stepConfigs = stepConfigs.concat(Em.Object.create({
  236. serviceName: 'MISC',
  237. configs: clusterEnvConfigs
  238. }));
  239. dfd.resolve(blueprintUtils.buildConfigsJSON(services, stepConfigs));
  240. });
  241. } else {
  242. dfd.resolve(blueprintUtils.buildConfigsJSON(services, stepConfigs));
  243. }
  244. return dfd.promise();
  245. },
  246. getClusterEnvConfigsForValidation: function () {
  247. var dfd = $.Deferred();
  248. App.ajax.send({
  249. name: 'config.cluster_env_site',
  250. sender: this,
  251. error: 'validationError'
  252. }).done(function (data) {
  253. App.router.get('configurationController').getConfigsByTags([{
  254. siteName: data.items[data.items.length - 1].type,
  255. tagName: data.items[data.items.length - 1].tag
  256. }]).done(function (clusterEnvConfigs) {
  257. var configsObject = clusterEnvConfigs[0].properties;
  258. var configsArray = [];
  259. for (var property in configsObject) {
  260. if (configsObject.hasOwnProperty(property)) {
  261. configsArray.push(Em.Object.create({
  262. name: property,
  263. value: configsObject[property],
  264. filename: 'cluster-env.xml'
  265. }));
  266. }
  267. }
  268. dfd.resolve(configsArray);
  269. });
  270. });
  271. return dfd.promise();
  272. },
  273. /**
  274. * @method validationSuccess
  275. * success callback after getting response from server
  276. * go through the step configs and set warn and error messages
  277. * @param data
  278. */
  279. validationSuccess: function(data) {
  280. var self = this;
  281. var checkedProperties = [];
  282. var globalWarning = [];
  283. self.set('configValidationError', false);
  284. self.set('configValidationWarning', false);
  285. data.resources.forEach(function(r) {
  286. r.items.forEach(function(item){
  287. if (item.type == "configuration") {
  288. self.get('stepConfigs').forEach(function(service) {
  289. service.get('configs').forEach(function(property) {
  290. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  291. if (item.level == "ERROR") {
  292. self.set('configValidationError', true);
  293. property.set('errorMessage', item.message);
  294. property.set('error', true);
  295. } else if (item.level == "WARN") {
  296. self.set('configValidationWarning', true);
  297. property.set('warnMessage', item.message);
  298. property.set('warn', true);
  299. }
  300. // store property data to detect WARN or ERROR messages for missed property
  301. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  302. }
  303. });
  304. });
  305. // check if error or warn message detected for property that absent in step configs
  306. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  307. var message = {
  308. propertyName: item['config-name'],
  309. filename: item['config-type'],
  310. warnMessage: item.message
  311. };
  312. if (item['config-type'] === "" && item['config-name'] === "") {
  313. //service-independent validation
  314. message.isGeneral = true;
  315. } else {
  316. message.serviceName = App.StackService.find().filter(function(service) {
  317. return !!service.get('configTypes')[item['config-type']];
  318. })[0].get('displayName')
  319. }
  320. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  321. globalWarning.push(message);
  322. }
  323. }
  324. });
  325. });
  326. self.set('configValidationGlobalMessage', globalWarning);
  327. },
  328. validationError: function (jqXHR, ajaxOptions, error, opt) {
  329. this.set('configValidationFailed', true);
  330. },
  331. /**
  332. * warn user if some errors or warning were
  333. * in setting up configs otherwise go to the nex operation
  334. * @param deferred
  335. * @returns {*}
  336. */
  337. warnUser: function(deferred) {
  338. var self = this;
  339. if (this.get('configValidationFailed')) {
  340. return App.ModalPopup.show({
  341. header: Em.I18n.t('installer.step7.popup.validation.failed.header'),
  342. primary: Em.I18n.t('common.proceedAnyway'),
  343. primaryClass: 'btn-danger',
  344. marginBottom: 200,
  345. onPrimary: function () {
  346. this.hide();
  347. deferred.resolve();
  348. },
  349. onSecondary: function () {
  350. this.hide();
  351. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  352. },
  353. onClose: function () {
  354. this.hide();
  355. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  356. },
  357. body: Em.I18n.t('installer.step7.popup.validation.request.failed.body')
  358. });
  359. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  360. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  361. var stepConfigs = self.get('name') === 'mainServiceInfoConfigsController'
  362. ? [self.get('selectedService')]
  363. : self.get('stepConfigs');
  364. var configsWithErrors = stepConfigs.some(function (step) {
  365. return step.get('configs').some(function(c) {
  366. return c.get('isVisible') && !c.get('hiddenBySection') && (c.get('warn') || c.get('error'));
  367. })
  368. });
  369. if (configsWithErrors) {
  370. return App.ModalPopup.show({
  371. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  372. classNames: ['sixty-percent-width-modal','modal-full-width'],
  373. primary: Em.I18n.t('common.proceedAnyway'),
  374. primaryClass: 'btn-danger',
  375. marginBottom: 200,
  376. onPrimary: function () {
  377. this.hide();
  378. deferred.resolve();
  379. },
  380. onSecondary: function () {
  381. this.hide();
  382. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  383. },
  384. onClose: function () {
  385. this.hide();
  386. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  387. },
  388. bodyClass: Em.View.extend({
  389. controller: self,
  390. templateName: require('templates/common/modal_popups/config_recommendation_popup'),
  391. serviceConfigs: stepConfigs
  392. })
  393. });
  394. } else {
  395. deferred.resolve();
  396. }
  397. } else {
  398. deferred.resolve();
  399. }
  400. }
  401. });