serverValidator.js 10 KB

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