serverValidator.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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, false, null, false, true);
  160. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  161. this.set('recommendationTimeStamp', (new Date).getTime());
  162. },
  163. loadRecommendationsError: function(jqXHR, ajaxOptions, error, opt) {
  164. },
  165. serverSideValidation: function () {
  166. var deferred = $.Deferred();
  167. this.set('configValidationFailed', false);
  168. this.set('configValidationGlobalMessage', []);
  169. if (this.get('configValidationFailed')) {
  170. this.warnUser(deferred);
  171. } else {
  172. this.runServerSideValidation(deferred);
  173. }
  174. return deferred;
  175. },
  176. getAllHostsWithComponents: function() {
  177. return App.ajax.send({
  178. sender: this,
  179. name: 'common.hosts.all',
  180. data: {
  181. urlParams: 'fields=HostRoles/component_name,HostRoles/host_name'
  182. }
  183. });
  184. },
  185. /**
  186. * @method serverSideValidation
  187. * send request to validate configs
  188. * @returns {*}
  189. */
  190. runServerSideValidation: function (deferred) {
  191. var self = this;
  192. var recommendations = this.get('hostGroups');
  193. var services = this.get('services');
  194. var stepConfigs = this.get('stepConfigs');
  195. return this.getBlueprintConfigurations().done(function(blueprintConfigurations){
  196. recommendations.blueprint.configurations = blueprintConfigurations;
  197. return App.ajax.send({
  198. name: 'config.validations',
  199. sender: self,
  200. data: {
  201. stackVersionUrl: App.get('stackVersionURL'),
  202. hosts: self.get('hostNames'),
  203. services: self.get('serviceNames'),
  204. validate: 'configurations',
  205. recommendations: recommendations
  206. },
  207. success: 'validationSuccess',
  208. error: 'validationError'
  209. }).complete(function () {
  210. self.warnUser(deferred);
  211. });
  212. });
  213. },
  214. /**
  215. * Return JSON for blueprint configurations
  216. * @returns {*}
  217. */
  218. getBlueprintConfigurations: function () {
  219. var dfd = $.Deferred();
  220. var services = this.get('services');
  221. var stepConfigs = this.get('stepConfigs');
  222. var allConfigTypes = [];
  223. services.forEach(function (service) {
  224. allConfigTypes = allConfigTypes.concat(Em.keys(service.get('configTypes')))
  225. });
  226. // check if we have configs from 'cluster-env', if not, then load them, as they are mandatory for validation request
  227. if (!allConfigTypes.contains('cluster-env')) {
  228. this.getClusterEnvConfigsForValidation().done(function(clusterEnvConfigs){
  229. services = services.concat(Em.Object.create({
  230. serviceName: 'MISC',
  231. configTypes: {'cluster-env': {}}
  232. }));
  233. stepConfigs = stepConfigs.concat(Em.Object.create({
  234. serviceName: 'MISC',
  235. configs: clusterEnvConfigs
  236. }));
  237. dfd.resolve(blueprintUtils.buildConfigsJSON(services, stepConfigs));
  238. });
  239. } else {
  240. dfd.resolve(blueprintUtils.buildConfigsJSON(services, stepConfigs));
  241. }
  242. return dfd.promise();
  243. },
  244. getClusterEnvConfigsForValidation: function () {
  245. var dfd = $.Deferred();
  246. App.ajax.send({
  247. name: 'config.cluster_env_site',
  248. sender: this,
  249. error: 'validationError'
  250. }).done(function (data) {
  251. App.router.get('configurationController').getConfigsByTags([{
  252. siteName: data.items[data.items.length - 1].type,
  253. tagName: data.items[data.items.length - 1].tag
  254. }]).done(function (clusterEnvConfigs) {
  255. var configsObject = clusterEnvConfigs[0].properties;
  256. var configsArray = [];
  257. for (var property in configsObject) {
  258. if (configsObject.hasOwnProperty(property)) {
  259. configsArray.push(Em.Object.create({
  260. name: property,
  261. value: configsObject[property],
  262. filename: 'cluster-env.xml'
  263. }));
  264. }
  265. }
  266. dfd.resolve(configsArray);
  267. });
  268. });
  269. return dfd.promise();
  270. },
  271. /**
  272. * @method validationSuccess
  273. * success callback after getting response from server
  274. * go through the step configs and set warn and error messages
  275. * @param data
  276. */
  277. validationSuccess: function(data) {
  278. var self = this;
  279. var checkedProperties = [];
  280. var globalWarning = [];
  281. self.set('configValidationError', false);
  282. self.set('configValidationWarning', false);
  283. data.resources.forEach(function(r) {
  284. r.items.forEach(function(item){
  285. if (item.type == "configuration") {
  286. self.get('stepConfigs').forEach(function(service) {
  287. service.get('configs').forEach(function(property) {
  288. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  289. if (item.level == "ERROR") {
  290. self.set('configValidationError', true);
  291. property.set('errorMessage', item.message);
  292. property.set('error', true);
  293. } else if (item.level == "WARN") {
  294. self.set('configValidationWarning', true);
  295. property.set('warnMessage', item.message);
  296. property.set('warn', true);
  297. }
  298. // store property data to detect WARN or ERROR messages for missed property
  299. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  300. }
  301. });
  302. });
  303. // check if error or warn message detected for property that absent in step configs
  304. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  305. var message = {
  306. propertyName: item['config-name'],
  307. filename: item['config-type'],
  308. warnMessage: item.message
  309. };
  310. if (item['config-type'] === "" && item['config-name'] === "") {
  311. //service-independent validation
  312. message.isGeneral = true;
  313. } else {
  314. message.serviceName = App.StackService.find().filter(function(service) {
  315. return !!service.get('configTypes')[item['config-type']];
  316. })[0].get('displayName')
  317. }
  318. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  319. globalWarning.push(message);
  320. }
  321. }
  322. });
  323. });
  324. self.set('configValidationGlobalMessage', globalWarning);
  325. },
  326. validationError: function (jqXHR, ajaxOptions, error, opt) {
  327. this.set('configValidationFailed', true);
  328. },
  329. /**
  330. * warn user if some errors or warning were
  331. * in setting up configs otherwise go to the nex operation
  332. * @param deferred
  333. * @returns {*}
  334. */
  335. warnUser: function(deferred) {
  336. var self = this;
  337. if (this.get('configValidationFailed')) {
  338. return App.ModalPopup.show({
  339. header: Em.I18n.t('installer.step7.popup.validation.failed.header'),
  340. primary: Em.I18n.t('common.proceedAnyway'),
  341. primaryClass: 'btn-danger',
  342. marginBottom: 200,
  343. onPrimary: function () {
  344. this.hide();
  345. deferred.resolve();
  346. },
  347. onSecondary: function () {
  348. this.hide();
  349. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  350. },
  351. onClose: function () {
  352. this.hide();
  353. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  354. },
  355. body: Em.I18n.t('installer.step7.popup.validation.request.failed.body')
  356. });
  357. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  358. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  359. var stepConfigs = self.get('name') === 'mainServiceInfoConfigsController'
  360. ? [self.get('selectedService')]
  361. : self.get('stepConfigs');
  362. var configsWithErrors = stepConfigs.some(function (step) {
  363. return step.get('configs').some(function(c) {
  364. return c.get('isVisible') && !c.get('hiddenBySection') && (c.get('warn') || c.get('error'));
  365. })
  366. });
  367. if (configsWithErrors) {
  368. return App.ModalPopup.show({
  369. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  370. classNames: ['sixty-percent-width-modal','modal-full-width'],
  371. primary: Em.I18n.t('common.proceedAnyway'),
  372. primaryClass: 'btn-danger',
  373. marginBottom: 200,
  374. onPrimary: function () {
  375. this.hide();
  376. deferred.resolve();
  377. },
  378. onSecondary: function () {
  379. this.hide();
  380. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  381. },
  382. onClose: function () {
  383. this.hide();
  384. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  385. },
  386. bodyClass: Em.View.extend({
  387. controller: self,
  388. templateName: require('templates/common/modal_popups/config_recommendation_popup'),
  389. serviceConfigs: stepConfigs
  390. })
  391. });
  392. } else {
  393. deferred.resolve();
  394. }
  395. } else {
  396. deferred.resolve();
  397. }
  398. }
  399. });