serverValidator.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * @type {bool} set true if at leasst one config has error
  23. */
  24. configValidationError: false,
  25. /**
  26. * @type {bool} set true if at leasst one config has warning
  27. */
  28. configValidationWarning: false,
  29. /**
  30. * @type {bool} set true if at leasst one config has warning
  31. */
  32. configValidationFailed: false,
  33. /**
  34. * recommendation configs loaded from server
  35. * (used only during install)
  36. * @type {Object}
  37. */
  38. recommendationsConfigs: null,
  39. /**
  40. * by default loads data from model otherwise must be overridden as computed property
  41. * refer to \assets\data\stacks\HDP-2.1\recommendations_configs.json to learn structure
  42. * (shouldn't contain configurations filed)
  43. * @type {Object}
  44. */
  45. hostNames: function() {
  46. return this.get('content.hosts')
  47. ? Object.keys(this.get('content.hosts'))
  48. : App.get('allHostNames');
  49. }.property('content.hosts', 'App.allHostNames'),
  50. allHostNames: [],
  51. /**
  52. * by default loads data from model otherwise must be overridden as computed property
  53. * @type {Array} - of strings (serviceNames)
  54. */
  55. serviceNames: function() {
  56. return this.get('content.serviceName') ? [this.get('content.serviceName')] : this.get('allSelectedServiceNames');
  57. }.property('content.serviceName', 'allSelectedServiceNames.@each'),
  58. /**
  59. * by default loads data from model otherwise must be overridden as computed property
  60. * filter services that support server validation and concat with misc configs if Installer or current service
  61. * @type {Array} - of objects (services)
  62. */
  63. services: function() {
  64. return this.get('content.serviceName')
  65. ? [App.StackService.find(this.get('content.serviceName'))]
  66. : this.get('content.services').filter(function(s){
  67. return (s.get('isSelected') || s.get('isInstalled'))
  68. }).concat(require("data/service_configs"));
  69. }.property('content.serviceName', 'content.services', 'content.services.@each.isSelected', 'content.services.@each.isInstalled', 'content.stacks.@each.isSelected'),
  70. /**
  71. * by default loads data from model otherwise must be overridden as computed property
  72. * can be used for service|host configs pages
  73. * @type {Array} of strings (hostNames)
  74. */
  75. hostGroups: function() {
  76. return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(this.get('hostNames'), App.HostComponent.find());
  77. }.property('content.recommendationsHostGroups', 'hostNames'),
  78. /**
  79. * controller that is child of this mixis has to contain stepConfigs
  80. * @type {Array}
  81. */
  82. stepConfigs: null,
  83. /**
  84. * @method loadServerSideConfigsRecommendations
  85. * laod recommendations from server
  86. * (used only during install)
  87. * @returns {*}
  88. */
  89. loadServerSideConfigsRecommendations: function() {
  90. if (this.get('recommendationsConfigs')) {
  91. return $.Deferred().resolve();
  92. }
  93. return App.ajax.send({
  94. 'name': 'wizard.step7.loadrecommendations.configs',
  95. 'sender': this,
  96. 'data': {
  97. stackVersionUrl: App.get('stackVersionURL'),
  98. hosts: this.get('hostNames'),
  99. services: this.get('serviceNames'),
  100. recommendations: this.get('hostGroups')
  101. },
  102. 'success': 'loadRecommendationsSuccess',
  103. 'error': 'loadRecommendationsError'
  104. });
  105. },
  106. /**
  107. * @method loadRecommendationsSuccess
  108. * success callback after loading recommendations
  109. * (used only during install)
  110. * @param data
  111. */
  112. loadRecommendationsSuccess: function(data) {
  113. if (!data) {
  114. console.warn('error while loading default config values');
  115. }
  116. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  117. },
  118. loadRecommendationsError: function(jqXHR, ajaxOptions, error, opt) {
  119. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  120. console.error('Load recommendations failed');
  121. },
  122. serverSideValidation: function () {
  123. var deferred = $.Deferred();
  124. this.set('configValidationFailed', false);
  125. if (this.get('configValidationFailed')) {
  126. this.warnUser(deferred);
  127. } else {
  128. this.runServerSideValidation(deferred);
  129. }
  130. return deferred;
  131. },
  132. /**
  133. * @method serverSideValidation
  134. * send request to validate configs
  135. * @returns {*}
  136. */
  137. runServerSideValidation: function(deferred) {
  138. var self = this;
  139. var recommendations = this.get('hostGroups');
  140. recommendations.blueprint.configurations = blueprintUtils.buildConfisJSON(this.get('services'), this.get('stepConfigs'));
  141. var serviceNames = this.get('serviceNames');
  142. if (!self.get('isInstaller')) {
  143. // When editing a service we validate only that service's configs.
  144. // However, we should pass the IDs of services installed, or else,
  145. // default value calculations will alter.
  146. serviceNames = App.Service.find().mapProperty('serviceName');
  147. }
  148. return App.ajax.send({
  149. name: 'config.validations',
  150. sender: this,
  151. data: {
  152. stackVersionUrl: App.get('stackVersionURL'),
  153. hosts: this.get('hostNames'),
  154. services: serviceNames,
  155. validate: 'configurations',
  156. recommendations: recommendations
  157. },
  158. success: 'validationSuccess',
  159. error: 'validationError'
  160. }).complete(function() {
  161. self.warnUser(deferred);
  162. });
  163. },
  164. /**
  165. * @method validationSuccess
  166. * success callback after getting responce from server
  167. * go through the step configs and set warn and error messages
  168. * @param data
  169. */
  170. validationSuccess: function(data) {
  171. var self = this;
  172. self.set('configValidationError', false);
  173. self.set('configValidationWarning', false);
  174. data.resources.forEach(function(r) {
  175. r.items.forEach(function(item){
  176. if (item.type == "configuration") {
  177. self.get('stepConfigs').forEach(function(service) {
  178. service.get('configs').forEach(function(property) {
  179. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  180. if (item.level == "ERROR") {
  181. self.set('configValidationError', true);
  182. property.set('errorMessage', item.message);
  183. property.set('error', true);
  184. } else if (item.level == "WARN") {
  185. self.set('configValidationWarning', true);
  186. property.set('warnMessage', item.message);
  187. property.set('warn', true);
  188. }
  189. }
  190. });
  191. })
  192. }
  193. });
  194. });
  195. },
  196. validationError: function (jqXHR, ajaxOptions, error, opt) {
  197. this.set('configValidationFailed', true);
  198. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  199. console.error('config validation failed');
  200. },
  201. /**
  202. * warn user if some errors or warning were
  203. * in seting up configs otherwise go to the nex operation
  204. * @param deferred
  205. * @returns {*}
  206. */
  207. warnUser: function(deferred) {
  208. var self = this;
  209. if (this.get('configValidationFailed')) {
  210. deferred.reject();
  211. return App.showAlertPopup(Em.I18n.t('installer.step7.popup.validation.failed.header'), Em.I18n.t('installer.step7.popup.validation.request.failed.body'));
  212. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  213. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  214. return App.ModalPopup.show({
  215. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  216. classNames: ['sixty-percent-width-modal'],
  217. primary: Em.I18n.t('common.proceedAnyway'),
  218. onPrimary: function () {
  219. this.hide();
  220. deferred.resolve();
  221. },
  222. onSecondary: function () {
  223. this.hide();
  224. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  225. },
  226. bodyClass: Em.View.extend({
  227. controller: self,
  228. templateName: require('templates/common/configs/config_recommendation_popup')
  229. })
  230. });
  231. } else {
  232. deferred.resolve();
  233. }
  234. }
  235. });