serverValidator.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * by default loads data from model otherwise must be overridden as computed property
  53. * refer to \assets\data\stacks\HDP-2.1\recommendations_configs.json to learn structure
  54. * (shouldn't contain configurations filed)
  55. * @type {Object}
  56. */
  57. hostNames: function() {
  58. return this.get('isWizard')
  59. ? Object.keys(this.get('content.hosts'))
  60. : App.get('allHostNames');
  61. }.property('isWizard', 'content.hosts', 'App.allHostNames'),
  62. /**
  63. * by default loads data from model otherwise must be overridden as computed property
  64. * @type {Array} - of strings (serviceNames)
  65. */
  66. serviceNames: function() {
  67. // When editing a service we validate only that service's configs.
  68. // However, we should pass the IDs of services installed, or else,
  69. // default value calculations will alter.
  70. return this.get('isWizard') ? this.get('allSelectedServiceNames') : App.Service.find().mapProperty('serviceName');
  71. }.property('isWizard', 'allSelectedServiceNames'),
  72. /**
  73. * by default loads data from model otherwise must be overridden as computed property
  74. * filter services that support server validation and concat with misc configs if Installer or current service
  75. * @type {Array} - of objects (services)
  76. */
  77. services: function() {
  78. var stackServices = App.StackService.find().filter(function(s) {
  79. return this.get('serviceNames').contains(s.get('serviceName'));
  80. }, this);
  81. return this.get('isWizard') ? stackServices.concat(require("data/service_configs")) : stackServices;
  82. }.property('serviceNames'),
  83. /**
  84. * by default loads data from model otherwise must be overridden as computed property
  85. * can be used for service|host configs pages
  86. * @type {Array} of strings (hostNames)
  87. */
  88. hostGroups: function() {
  89. return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(App.get('allHostNames'));
  90. }.property('content.recommendationsHostGroups', 'App.allHostNames'),
  91. /**
  92. * controller that is child of this mixin has to contain stepConfigs
  93. * @type {Array}
  94. */
  95. stepConfigs: null,
  96. /**
  97. * @method loadServerSideConfigsRecommendations
  98. * load recommendations from server
  99. * (used only during install)
  100. * @returns {*}
  101. */
  102. loadServerSideConfigsRecommendations: function() {
  103. var self = this;
  104. // if extended controller doesn't support recommendations ignore this call but keep promise chain
  105. if (!this.get('isControllerSupportsEnhancedConfigs')) {
  106. return $.Deferred().resolve().promise();
  107. }
  108. var recommendations = this.get('hostGroups');
  109. // send user's input based on stored configurations
  110. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  111. // include cluster-env site to recommendations call
  112. var miscService = this.get('services').findProperty('serviceName', 'MISC');
  113. if (miscService) {
  114. var miscConfigs = blueprintUtils.buildConfigsJSON([miscService], [this.get('stepConfigs').findProperty('serviceName', 'MISC')]);
  115. var clusterEnv = App.permit(miscConfigs, 'cluster-env');
  116. if (!App.isEmptyObject(clusterEnv)) {
  117. $.extend(recommendations.blueprint.configurations, clusterEnv);
  118. }
  119. /** add user properties from misc tabs to proper filename **/
  120. this.get('stepConfigs').findProperty('serviceName', 'MISC').get('configs').forEach(function(config) {
  121. var tag = App.config.getConfigTagFromFileName(config.get('filename'));
  122. if (recommendations.blueprint.configurations[tag] && tag != 'cluster-env') {
  123. recommendations.blueprint.configurations[tag].properties[config.get('name')] = config.get('value');
  124. }
  125. })
  126. }
  127. return App.ajax.send({
  128. 'name': 'config.recommendations',
  129. 'sender': this,
  130. 'data': {
  131. stackVersionUrl: App.get('stackVersionURL'),
  132. dataToSend: {
  133. recommend: 'configurations',
  134. hosts: this.get('hostNames'),
  135. services: this.get('serviceNames'),
  136. recommendations: recommendations
  137. }
  138. },
  139. 'success': 'loadRecommendationsSuccess',
  140. 'error': 'loadRecommendationsError'
  141. });
  142. },
  143. /**
  144. * @method loadRecommendationsSuccess
  145. * success callback after loading recommendations
  146. * (used only during install)
  147. * @param data
  148. */
  149. loadRecommendationsSuccess: function(data) {
  150. if (!data) {
  151. console.warn('error while loading default config values');
  152. }
  153. this._saveRecommendedValues(data);
  154. var configObject = data.resources[0].recommendations.blueprint.configurations;
  155. if (configObject) this.updateInitialValue(configObject);
  156. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  157. },
  158. loadRecommendationsError: function(jqXHR, ajaxOptions, error, opt) {
  159. console.error("ERROR: Unable to determine recommendations for configs: ", jqXHR, ajaxOptions, error, opt);
  160. },
  161. serverSideValidation: function () {
  162. var deferred = $.Deferred();
  163. var self = this;
  164. this.set('configValidationFailed', false);
  165. this.set('configValidationGlobalMessage', []);
  166. if (this.get('configValidationFailed')) {
  167. this.warnUser(deferred);
  168. } else {
  169. if (this.get('isInstaller')) {
  170. this.runServerSideValidation(deferred);
  171. } else {
  172. // on Service Configs page we need to load all hosts with componnets
  173. this.getAllHostsWithComponents().then(function(data) {
  174. self.set('content.recommendationsHostGroups', blueprintUtils.generateHostGroups(App.get('allHostNames'), self.mapHostsToComponents(data.items)));
  175. self.runServerSideValidation(deferred);
  176. });
  177. }
  178. }
  179. return deferred;
  180. },
  181. getAllHostsWithComponents: function() {
  182. return App.ajax.send({
  183. sender: this,
  184. name: 'common.hosts.all',
  185. data: {
  186. urlParams: 'fields=HostRoles/component_name,HostRoles/host_name'
  187. }
  188. });
  189. },
  190. /**
  191. * Generate array similar to App.HostComponent which will be used to
  192. * create blueprint hostGroups object as well.
  193. *
  194. * @param {Object[]} jsonData
  195. * @returns {Em.Object[]}
  196. */
  197. mapHostsToComponents: function(jsonData) {
  198. var result = [];
  199. jsonData.forEach(function(item) {
  200. result.push(Em.Object.create({
  201. componentName: Em.get(item, 'HostRoles.component_name'),
  202. hostName: Em.get(item, 'HostRoles.host_name')
  203. }));
  204. });
  205. return result;
  206. },
  207. /**
  208. * @method serverSideValidation
  209. * send request to validate configs
  210. * @returns {*}
  211. */
  212. runServerSideValidation: function(deferred) {
  213. var self = this;
  214. var recommendations = this.get('hostGroups');
  215. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  216. return App.ajax.send({
  217. name: 'config.validations',
  218. sender: this,
  219. data: {
  220. stackVersionUrl: App.get('stackVersionURL'),
  221. hosts: this.get('hostNames'),
  222. services: this.get('serviceNames'),
  223. validate: 'configurations',
  224. recommendations: recommendations
  225. },
  226. success: 'validationSuccess',
  227. error: 'validationError'
  228. }).complete(function() {
  229. self.warnUser(deferred);
  230. });
  231. },
  232. /**
  233. * @method validationSuccess
  234. * success callback after getting responce from server
  235. * go through the step configs and set warn and error messages
  236. * @param data
  237. */
  238. validationSuccess: function(data) {
  239. var self = this;
  240. var checkedProperties = [];
  241. var globalWarning = [];
  242. self.set('configValidationError', false);
  243. self.set('configValidationWarning', false);
  244. data.resources.forEach(function(r) {
  245. r.items.forEach(function(item){
  246. if (item.type == "configuration") {
  247. self.get('stepConfigs').forEach(function(service) {
  248. service.get('configs').forEach(function(property) {
  249. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  250. if (item.level == "ERROR") {
  251. self.set('configValidationError', true);
  252. property.set('errorMessage', item.message);
  253. property.set('error', true);
  254. } else if (item.level == "WARN") {
  255. self.set('configValidationWarning', true);
  256. property.set('warnMessage', item.message);
  257. property.set('warn', true);
  258. }
  259. // store property data to detect WARN or ERROR messages for missed property
  260. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  261. }
  262. });
  263. });
  264. // check if error or warn message detected for property that absent in step configs
  265. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  266. var message = {
  267. propertyName: item['config-name'],
  268. filename: item['config-type'],
  269. warnMessage: item.message
  270. };
  271. if (item['config-type'] === "" && item['config-name'] === "") {
  272. //service-independent validation
  273. message.isGeneral = true;
  274. } else {
  275. message.serviceName = App.StackService.find().filter(function(service) {
  276. return !!service.get('configTypes')[item['config-type']];
  277. })[0].get('displayName')
  278. }
  279. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  280. globalWarning.push(message);
  281. }
  282. }
  283. });
  284. });
  285. self.set('configValidationGlobalMessage', globalWarning);
  286. },
  287. validationError: function (jqXHR, ajaxOptions, error, opt) {
  288. this.set('configValidationFailed', true);
  289. console.error('Config validation failed: ', jqXHR, ajaxOptions, error, opt);
  290. },
  291. /**
  292. * warn user if some errors or warning were
  293. * in setting up configs otherwise go to the nex operation
  294. * @param deferred
  295. * @returns {*}
  296. */
  297. warnUser: function(deferred) {
  298. var self = this;
  299. if (this.get('configValidationFailed')) {
  300. console.error("Config validation failed. Going ahead with saving of configs");
  301. return App.ModalPopup.show({
  302. header: Em.I18n.t('installer.step7.popup.validation.failed.header'),
  303. primary: Em.I18n.t('common.proceedAnyway'),
  304. primaryClass: 'btn-danger',
  305. marginBottom: 200,
  306. onPrimary: function () {
  307. this.hide();
  308. deferred.resolve();
  309. },
  310. onSecondary: function () {
  311. this.hide();
  312. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  313. },
  314. onClose: function () {
  315. this.hide();
  316. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  317. },
  318. body: Em.I18n.t('installer.step7.popup.validation.request.failed.body')
  319. });
  320. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  321. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  322. return App.ModalPopup.show({
  323. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  324. classNames: ['sixty-percent-width-modal','modal-full-width'],
  325. primary: Em.I18n.t('common.proceedAnyway'),
  326. primaryClass: 'btn-danger',
  327. marginBottom: 200,
  328. onPrimary: function () {
  329. this.hide();
  330. deferred.resolve();
  331. },
  332. onSecondary: function () {
  333. this.hide();
  334. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  335. },
  336. onClose: function () {
  337. this.hide();
  338. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  339. },
  340. bodyClass: Em.View.extend({
  341. controller: self,
  342. templateName: require('templates/common/modal_popups/config_recommendation_popup')
  343. })
  344. });
  345. } else {
  346. deferred.resolve();
  347. }
  348. }
  349. });