serverValidator.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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') || !App.get('supports.serverRecommendValidate')) {
  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. if (!App.get('supports.serverRecommendValidate')) {
  125. deferred.resolve();
  126. } else {
  127. this.set('configValidationFailed', false);
  128. if (this.get('configValidationFailed')) {
  129. this.warnUser(deferred);
  130. } else {
  131. this.runServerSideValidation(deferred);
  132. }
  133. }
  134. return deferred;
  135. },
  136. /**
  137. * @method serverSideValidation
  138. * send request to validate configs
  139. * @returns {*}
  140. */
  141. runServerSideValidation: function(deferred) {
  142. var self = this;
  143. var recommendations = this.get('hostGroups');
  144. recommendations.blueprint.configurations = blueprintUtils.buildConfisJSON(this.get('services'), this.get('stepConfigs'));
  145. var serviceNames = this.get('serviceNames');
  146. if (!self.get('isInstaller')) {
  147. // When editing a service we validate only that service's configs.
  148. // However, we should pass the IDs of services installed, or else,
  149. // default value calculations will alter.
  150. serviceNames = App.Service.find().mapProperty('serviceName');
  151. }
  152. return App.ajax.send({
  153. name: 'config.validations',
  154. sender: this,
  155. data: {
  156. stackVersionUrl: App.get('stackVersionURL'),
  157. hosts: this.get('hostNames'),
  158. services: serviceNames,
  159. validate: 'configurations',
  160. recommendations: recommendations
  161. },
  162. success: 'validationSuccess',
  163. error: 'validationError'
  164. }).complete(function() {
  165. self.warnUser(deferred);
  166. });
  167. },
  168. /**
  169. * @method validationSuccess
  170. * success callback after getting responce from server
  171. * go through the step configs and set warn and error messages
  172. * @param data
  173. */
  174. validationSuccess: function(data) {
  175. var self = this;
  176. self.set('configValidationError', false);
  177. self.set('configValidationWarning', false);
  178. data.resources.forEach(function(r) {
  179. r.items.forEach(function(item){
  180. if (item.type == "configuration") {
  181. self.get('stepConfigs').forEach(function(service) {
  182. service.get('configs').forEach(function(property) {
  183. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  184. if (item.level == "ERROR") {
  185. self.set('configValidationError', true);
  186. property.set('errorMessage', item.message);
  187. property.set('error', true);
  188. } else if (item.level == "WARN") {
  189. self.set('configValidationWarning', true);
  190. property.set('warnMessage', item.message);
  191. property.set('warn', true);
  192. }
  193. }
  194. });
  195. })
  196. }
  197. });
  198. });
  199. },
  200. validationError: function (jqXHR, ajaxOptions, error, opt) {
  201. this.set('configValidationFailed', true);
  202. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  203. console.error('config validation failed');
  204. },
  205. /**
  206. * warn user if some errors or warning were
  207. * in seting up configs otherwise go to the nex operation
  208. * @param deferred
  209. * @returns {*}
  210. */
  211. warnUser: function(deferred) {
  212. var self = this;
  213. if (this.get('configValidationFailed')) {
  214. deferred.reject();
  215. return App.showAlertPopup(Em.I18n.t('installer.step7.popup.validation.failed.header'), Em.I18n.t('installer.step7.popup.validation.request.failed.body'));
  216. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  217. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  218. return App.ModalPopup.show({
  219. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  220. classNames: ['sixty-percent-width-modal'],
  221. primary: Em.I18n.t('common.proceedAnyway'),
  222. onPrimary: function () {
  223. this.hide();
  224. deferred.resolve();
  225. },
  226. onSecondary: function () {
  227. this.hide();
  228. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  229. },
  230. bodyClass: Em.View.extend({
  231. controller: self,
  232. templateName: require('templates/common/configs/config_recommendation_popup')
  233. })
  234. });
  235. } else {
  236. deferred.resolve();
  237. }
  238. }
  239. });