serverValidator.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 extended controller doesn't support recommendations ignore this call but keep promise chain
  104. if (!this.get('isControllerSupportsEnhancedConfigs')) {
  105. return $.Deferred().resolve().promise();
  106. }
  107. return App.ajax.send({
  108. 'name': 'config.recommendations',
  109. 'sender': this,
  110. 'data': {
  111. stackVersionUrl: App.get('stackVersionURL'),
  112. dataToSend: {
  113. recommend: 'configurations',
  114. hosts: this.get('hostNames'),
  115. services: this.get('serviceNames'),
  116. recommendations: this.get('hostGroups')
  117. }
  118. },
  119. 'success': 'loadRecommendationsSuccess',
  120. 'error': 'loadRecommendationsError'
  121. });
  122. },
  123. /**
  124. * @method loadRecommendationsSuccess
  125. * success callback after loading recommendations
  126. * (used only during install)
  127. * @param data
  128. */
  129. loadRecommendationsSuccess: function(data) {
  130. if (!data) {
  131. console.warn('error while loading default config values');
  132. }
  133. this._saveRecommendedValues(data);
  134. var configObject = data.resources[0].recommendations.blueprint.configurations;
  135. if (configObject) this.updateInitialValue(configObject);
  136. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  137. },
  138. loadRecommendationsError: function(jqXHR, ajaxOptions, error, opt) {
  139. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  140. console.error('Load recommendations failed');
  141. },
  142. serverSideValidation: function () {
  143. var deferred = $.Deferred();
  144. var self = this;
  145. this.set('configValidationFailed', false);
  146. this.set('configValidationGlobalMessage', []);
  147. if (this.get('configValidationFailed')) {
  148. this.warnUser(deferred);
  149. } else {
  150. if (this.get('isInstaller')) {
  151. this.runServerSideValidation(deferred);
  152. } else {
  153. // on Service Configs page we need to load all hosts with componnets
  154. this.getAllHostsWithComponents().then(function(data) {
  155. self.set('content.recommendationsHostGroups', blueprintUtils.generateHostGroups(App.get('allHostNames'), self.mapHostsToComponents(data.items)));
  156. self.runServerSideValidation(deferred);
  157. });
  158. }
  159. }
  160. return deferred;
  161. },
  162. getAllHostsWithComponents: function() {
  163. return App.ajax.send({
  164. sender: this,
  165. name: 'common.hosts.all',
  166. data: {
  167. urlParams: 'fields=HostRoles/component_name,HostRoles/host_name'
  168. }
  169. });
  170. },
  171. /**
  172. * Generate array similar to App.HostComponent which will be used to
  173. * create blueprint hostGroups object as well.
  174. *
  175. * @param {Object[]} jsonData
  176. * @returns {Em.Object[]}
  177. */
  178. mapHostsToComponents: function(jsonData) {
  179. var result = [];
  180. jsonData.forEach(function(item) {
  181. result.push(Em.Object.create({
  182. componentName: Em.get(item, 'HostRoles.component_name'),
  183. hostName: Em.get(item, 'HostRoles.host_name')
  184. }));
  185. });
  186. return result;
  187. },
  188. /**
  189. * @method serverSideValidation
  190. * send request to validate configs
  191. * @returns {*}
  192. */
  193. runServerSideValidation: function(deferred) {
  194. var self = this;
  195. var recommendations = this.get('hostGroups');
  196. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  197. return App.ajax.send({
  198. name: 'config.validations',
  199. sender: this,
  200. data: {
  201. stackVersionUrl: App.get('stackVersionURL'),
  202. hosts: this.get('hostNames'),
  203. services: this.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. * @method validationSuccess
  215. * success callback after getting responce from server
  216. * go through the step configs and set warn and error messages
  217. * @param data
  218. */
  219. validationSuccess: function(data) {
  220. var self = this;
  221. var checkedProperties = [];
  222. var globalWarning = [];
  223. self.set('configValidationError', false);
  224. self.set('configValidationWarning', false);
  225. data.resources.forEach(function(r) {
  226. r.items.forEach(function(item){
  227. if (item.type == "configuration") {
  228. self.get('stepConfigs').forEach(function(service) {
  229. service.get('configs').forEach(function(property) {
  230. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  231. if (item.level == "ERROR") {
  232. self.set('configValidationError', true);
  233. property.set('errorMessage', item.message);
  234. property.set('error', true);
  235. } else if (item.level == "WARN") {
  236. self.set('configValidationWarning', true);
  237. property.set('warnMessage', item.message);
  238. property.set('warn', true);
  239. }
  240. // store property data to detect WARN or ERROR messages for missed property
  241. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  242. }
  243. });
  244. });
  245. // check if error or warn message detected for property that absent in step configs
  246. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  247. var message = {
  248. propertyName: item['config-name'],
  249. filename: item['config-type'],
  250. warnMessage: item.message
  251. };
  252. if (item['config-type'] === "" && item['config-name'] === "") {
  253. //service-independent validation
  254. message.isGeneral = true;
  255. } else {
  256. message.serviceName = App.StackService.find().filter(function(service) {
  257. return !!service.get('configTypes')[item['config-type']];
  258. })[0].get('displayName')
  259. }
  260. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  261. globalWarning.push(message);
  262. }
  263. }
  264. });
  265. });
  266. self.set('configValidationGlobalMessage', globalWarning);
  267. },
  268. validationError: function (jqXHR, ajaxOptions, error, opt) {
  269. this.set('configValidationFailed', true);
  270. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  271. console.error('config validation failed');
  272. },
  273. /**
  274. * warn user if some errors or warning were
  275. * in seting up configs otherwise go to the nex operation
  276. * @param deferred
  277. * @returns {*}
  278. */
  279. warnUser: function(deferred) {
  280. var self = this;
  281. if (this.get('configValidationFailed')) {
  282. deferred.reject();
  283. return App.showAlertPopup(Em.I18n.t('installer.step7.popup.validation.failed.header'), Em.I18n.t('installer.step7.popup.validation.request.failed.body'));
  284. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  285. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  286. return App.ModalPopup.show({
  287. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  288. classNames: ['sixty-percent-width-modal','modal-full-width'],
  289. primary: Em.I18n.t('common.proceedAnyway'),
  290. primaryClass: 'btn-danger',
  291. marginBottom: 200,
  292. onPrimary: function () {
  293. this.hide();
  294. deferred.resolve();
  295. },
  296. onSecondary: function () {
  297. this.hide();
  298. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  299. },
  300. onClose: function () {
  301. this.hide();
  302. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  303. },
  304. bodyClass: Em.View.extend({
  305. controller: self,
  306. templateName: require('templates/common/modal_popups/config_recommendation_popup')
  307. })
  308. });
  309. } else {
  310. deferred.resolve();
  311. }
  312. }
  313. });