serverValidator.js 9.0 KB

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