serverValidator.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. return App.ajax.send({
  104. 'name': 'config.recommendations',
  105. 'sender': this,
  106. 'data': {
  107. stackVersionUrl: App.get('stackVersionURL'),
  108. dataToSend: {
  109. recommend: 'configurations',
  110. hosts: this.get('hostNames'),
  111. services: this.get('serviceNames'),
  112. recommendations: this.get('hostGroups')
  113. }
  114. },
  115. 'success': 'loadRecommendationsSuccess',
  116. 'error': 'loadRecommendationsError'
  117. });
  118. },
  119. /**
  120. * @method loadRecommendationsSuccess
  121. * success callback after loading recommendations
  122. * (used only during install)
  123. * @param data
  124. */
  125. loadRecommendationsSuccess: function(data) {
  126. if (!data) {
  127. console.warn('error while loading default config values');
  128. }
  129. this._saveRecommendedValues(data);
  130. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  131. },
  132. loadRecommendationsError: function(jqXHR, ajaxOptions, error, opt) {
  133. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  134. console.error('Load recommendations failed');
  135. },
  136. serverSideValidation: function () {
  137. var deferred = $.Deferred();
  138. this.set('configValidationFailed', false);
  139. this.set('configValidationGlobalMessage', []);
  140. if (this.get('configValidationFailed')) {
  141. this.warnUser(deferred);
  142. } else {
  143. this.runServerSideValidation(deferred);
  144. }
  145. return deferred;
  146. },
  147. /**
  148. * @method serverSideValidation
  149. * send request to validate configs
  150. * @returns {*}
  151. */
  152. runServerSideValidation: function(deferred) {
  153. var self = this;
  154. var recommendations = this.get('hostGroups');
  155. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  156. return App.ajax.send({
  157. name: 'config.validations',
  158. sender: this,
  159. data: {
  160. stackVersionUrl: App.get('stackVersionURL'),
  161. hosts: this.get('hostNames'),
  162. services: this.get('serviceNames'),
  163. validate: 'configurations',
  164. recommendations: recommendations
  165. },
  166. success: 'validationSuccess',
  167. error: 'validationError'
  168. }).complete(function() {
  169. self.warnUser(deferred);
  170. });
  171. },
  172. /**
  173. * @method validationSuccess
  174. * success callback after getting responce from server
  175. * go through the step configs and set warn and error messages
  176. * @param data
  177. */
  178. validationSuccess: function(data) {
  179. var self = this;
  180. var checkedProperties = [];
  181. var globalWarning = [];
  182. self.set('configValidationError', false);
  183. self.set('configValidationWarning', false);
  184. data.resources.forEach(function(r) {
  185. r.items.forEach(function(item){
  186. if (item.type == "configuration") {
  187. self.get('stepConfigs').forEach(function(service) {
  188. service.get('configs').forEach(function(property) {
  189. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  190. if (item.level == "ERROR") {
  191. self.set('configValidationError', true);
  192. property.set('errorMessage', item.message);
  193. property.set('error', true);
  194. } else if (item.level == "WARN") {
  195. self.set('configValidationWarning', true);
  196. property.set('warnMessage', item.message);
  197. property.set('warn', true);
  198. }
  199. // store property data to detect WARN or ERROR messages for missed property
  200. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  201. }
  202. });
  203. });
  204. // check if error or warn message detected for property that absent in step configs
  205. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  206. var message = {
  207. propertyName: item['config-name'],
  208. filename: item['config-type'],
  209. warnMessage: item.message
  210. };
  211. if (item['config-type'] === "" && item['config-name'] === "") {
  212. //service-independent validation
  213. message.isGeneral = true;
  214. } else {
  215. message.serviceName = App.StackService.find().filter(function(service) {
  216. return !!service.get('configTypes')[item['config-type']];
  217. })[0].get('displayName')
  218. }
  219. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  220. globalWarning.push(message);
  221. }
  222. }
  223. });
  224. });
  225. self.set('configValidationGlobalMessage', globalWarning);
  226. },
  227. validationError: function (jqXHR, ajaxOptions, error, opt) {
  228. this.set('configValidationFailed', true);
  229. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  230. console.error('config validation failed');
  231. },
  232. /**
  233. * warn user if some errors or warning were
  234. * in seting up configs otherwise go to the nex operation
  235. * @param deferred
  236. * @returns {*}
  237. */
  238. warnUser: function(deferred) {
  239. var self = this;
  240. if (this.get('configValidationFailed')) {
  241. deferred.reject();
  242. return App.showAlertPopup(Em.I18n.t('installer.step7.popup.validation.failed.header'), Em.I18n.t('installer.step7.popup.validation.request.failed.body'));
  243. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  244. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  245. return App.ModalPopup.show({
  246. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  247. classNames: ['sixty-percent-width-modal','modal-full-width'],
  248. primary: Em.I18n.t('common.proceedAnyway'),
  249. primaryClass: 'btn-danger',
  250. marginBottom: 200,
  251. onPrimary: function () {
  252. this.hide();
  253. deferred.resolve();
  254. },
  255. onSecondary: function () {
  256. this.hide();
  257. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  258. },
  259. onClose: function () {
  260. this.hide();
  261. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  262. },
  263. bodyClass: Em.View.extend({
  264. controller: self,
  265. templateName: require('templates/common/modal_popups/config_recommendation_popup')
  266. })
  267. });
  268. } else {
  269. deferred.resolve();
  270. }
  271. }
  272. });