serverValidator.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. *
  124. * @param changedConfigs
  125. * @returns {$.ajax|null}
  126. */
  127. getRecommendationsForDependencies: function(changedConfigs) {
  128. if (Em.isArray(changedConfigs) && changedConfigs.length > 0) {
  129. var recommendations = this.get('hostGroups');
  130. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  131. var dataToSend = {
  132. recommend: 'configurations',
  133. hosts: this.get('hostNames'),
  134. services: this.get('serviceNames'),
  135. recommendations: recommendations
  136. };
  137. /** TODO uncomment when be will be ready
  138. if (App.get('supports.enhancedConfigs')) {
  139. dataToSend.recommend = 'configuration-dependencies';
  140. dataToSend.changed_configurations = changedConfigs;
  141. }
  142. **/
  143. return App.ajax.send({
  144. name: 'config.recommendations',
  145. sender: this,
  146. data: {
  147. stackVersionUrl: App.get('stackVersionURL'),
  148. dataToSend: dataToSend
  149. },
  150. success: 'dependenciesSuccess',
  151. error: 'dependenciesError'
  152. });
  153. } else {
  154. return null;
  155. }
  156. },
  157. /**
  158. *
  159. * @param data
  160. */
  161. dependenciesSuccess: function(data) {
  162. Em.assert('invalid data', data && data.resources[0] && Em.get(data.resources[0], 'recommendations.blueprint.configurations'));
  163. var configs = data.resources[0].recommendations.blueprint.configurations;
  164. this.loadConfigsToModel(this.get('stepConfigs')[0].get('configs'), this.get('selectedVersion'));
  165. var currentProperties = App.ConfigProperty.find().filterProperty('configVersion.isCurrent').filterProperty('configVersion.groupId', -1);
  166. for (var key in configs) {
  167. for (var propertyName in configs[key].properties) {
  168. var property = currentProperties.findProperty('name', propertyName)
  169. if (property) {
  170. property.set('recommendedValue', configs[key].properties[propertyName]);
  171. }
  172. }
  173. }
  174. var configsToShow = currentProperties.filter(function(p) {
  175. return p.get('recommendedValue') && p.get('recommendedValue') !== p.get('value');
  176. });
  177. if (configsToShow.length > 0) {
  178. App.showDependentConfigsPopup(configsToShow);
  179. }
  180. },
  181. /**
  182. *
  183. * @param jqXHR
  184. * @param ajaxOptions
  185. * @param error
  186. * @param opt
  187. */
  188. dependenciesError: function(jqXHR, ajaxOptions, error, opt) {
  189. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  190. },
  191. /**
  192. * @method loadRecommendationsSuccess
  193. * success callback after loading recommendations
  194. * (used only during install)
  195. * @param data
  196. */
  197. loadRecommendationsSuccess: function(data) {
  198. if (!data) {
  199. console.warn('error while loading default config values');
  200. }
  201. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  202. },
  203. loadRecommendationsError: function(jqXHR, ajaxOptions, error, opt) {
  204. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  205. console.error('Load recommendations failed');
  206. },
  207. serverSideValidation: function () {
  208. var deferred = $.Deferred();
  209. this.set('configValidationFailed', false);
  210. this.set('configValidationGlobalMessage', []);
  211. if (this.get('configValidationFailed')) {
  212. this.warnUser(deferred);
  213. } else {
  214. this.runServerSideValidation(deferred);
  215. }
  216. return deferred;
  217. },
  218. /**
  219. * @method serverSideValidation
  220. * send request to validate configs
  221. * @returns {*}
  222. */
  223. runServerSideValidation: function(deferred) {
  224. var self = this;
  225. var recommendations = this.get('hostGroups');
  226. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  227. return App.ajax.send({
  228. name: 'config.validations',
  229. sender: this,
  230. data: {
  231. stackVersionUrl: App.get('stackVersionURL'),
  232. hosts: this.get('hostNames'),
  233. services: this.get('serviceNames'),
  234. validate: 'configurations',
  235. recommendations: recommendations
  236. },
  237. success: 'validationSuccess',
  238. error: 'validationError'
  239. }).complete(function() {
  240. self.warnUser(deferred);
  241. });
  242. },
  243. /**
  244. * @method validationSuccess
  245. * success callback after getting responce from server
  246. * go through the step configs and set warn and error messages
  247. * @param data
  248. */
  249. validationSuccess: function(data) {
  250. var self = this;
  251. var checkedProperties = [];
  252. var globalWarning = [];
  253. self.set('configValidationError', false);
  254. self.set('configValidationWarning', false);
  255. data.resources.forEach(function(r) {
  256. r.items.forEach(function(item){
  257. if (item.type == "configuration") {
  258. self.get('stepConfigs').forEach(function(service) {
  259. service.get('configs').forEach(function(property) {
  260. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  261. if (item.level == "ERROR") {
  262. self.set('configValidationError', true);
  263. property.set('errorMessage', item.message);
  264. property.set('error', true);
  265. } else if (item.level == "WARN") {
  266. self.set('configValidationWarning', true);
  267. property.set('warnMessage', item.message);
  268. property.set('warn', true);
  269. }
  270. // store property data to detect WARN or ERROR messages for missed property
  271. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  272. }
  273. });
  274. });
  275. // check if error or warn message detected for property that absent in step configs
  276. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  277. var message = {
  278. propertyName: item['config-name'],
  279. filename: item['config-type'],
  280. warnMessage: item.message,
  281. serviceName: App.StackService.find().filter(function(service) {
  282. return !!service.get('configTypes')[item['config-type']];
  283. })[0].get('displayName')
  284. };
  285. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  286. globalWarning.push(message);
  287. }
  288. }
  289. });
  290. });
  291. self.set('configValidationGlobalMessage', globalWarning);
  292. },
  293. validationError: function (jqXHR, ajaxOptions, error, opt) {
  294. this.set('configValidationFailed', true);
  295. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  296. console.error('config validation failed');
  297. },
  298. /**
  299. * warn user if some errors or warning were
  300. * in seting up configs otherwise go to the nex operation
  301. * @param deferred
  302. * @returns {*}
  303. */
  304. warnUser: function(deferred) {
  305. var self = this;
  306. if (this.get('configValidationFailed')) {
  307. deferred.reject();
  308. return App.showAlertPopup(Em.I18n.t('installer.step7.popup.validation.failed.header'), Em.I18n.t('installer.step7.popup.validation.request.failed.body'));
  309. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  310. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  311. return App.ModalPopup.show({
  312. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  313. classNames: ['sixty-percent-width-modal','modal-full-width'],
  314. primary: Em.I18n.t('common.proceedAnyway'),
  315. primaryClass: 'btn-danger',
  316. marginBottom: 200,
  317. onPrimary: function () {
  318. this.hide();
  319. deferred.resolve();
  320. },
  321. onSecondary: function () {
  322. this.hide();
  323. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  324. },
  325. onClose: function () {
  326. this.hide();
  327. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  328. },
  329. bodyClass: Em.View.extend({
  330. controller: self,
  331. templateName: require('templates/common/modal_popups/config_recommendation_popup')
  332. })
  333. });
  334. } else {
  335. deferred.resolve();
  336. }
  337. }
  338. });