serverValidator.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. loadAdditionalinfo: function() {
  40. return !(this.get('content.hosts') && this.get('content.hosts').length);
  41. }.property('content.hosts'),
  42. /**
  43. * by default loads data from model otherwise must be overridden as computed property
  44. * refer to \assets\data\stacks\HDP-2.1\recommendations_configs.json to learn structure
  45. * (shouldn't contain configurations filed)
  46. * @type {Object}
  47. */
  48. hostNames: function() {
  49. return this.get('content.hosts')
  50. ? Object.keys(this.get('content.hosts'))
  51. : this.get('allHostNames');
  52. }.property('content.hosts', 'allHostNames'),
  53. allHostNames: [],
  54. /**
  55. * by default loads data from model otherwise must be overridden as computed property
  56. * @type {Array} - of strings (serviceNames)
  57. */
  58. serviceNames: function() {
  59. return this.get('content.serviceName')
  60. ? [this.get('content.serviceName')]
  61. : App.StackService.find().filter(function(s){return s.get('isSelected') || s.get('isInstalled')}).mapProperty('serviceName');
  62. }.property('content.serviceName'),
  63. /**
  64. * by default loads data from model otherwise must be overridden as computed property
  65. * filter services that support server validation and concat with misc configs if Installer or current service
  66. * @type {Array} - of objects (services)
  67. */
  68. services: function() {
  69. return this.get('content.serviceName')
  70. ? [App.StackService.find(this.get('content.serviceName'))]
  71. : App.StackService.find().filter(function(s){
  72. return (s.get('isSelected') || s.get('isInstalled'))
  73. }).concat(require("data/service_configs"));
  74. }.property('content.serviceName'),
  75. /**
  76. * by default loads data from model otherwise must be overridden as computed property
  77. * can be used for service|host configs pages
  78. * @type {Array} of strings (hostNames)
  79. */
  80. hostGroups: function() {
  81. return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(this.get('hostNames'), App.HostComponent.find());
  82. }.property('content.recommendationsHostGroups', 'hostNames'),
  83. /**
  84. * controller that is child of this mixis has to contain stepConfigs
  85. * @type {Array}
  86. */
  87. stepConfigs: null,
  88. /**
  89. * @method loadServerSideConfigsRecommendations
  90. * laod recommendations from server
  91. * (used only during install)
  92. * @returns {*}
  93. */
  94. loadServerSideConfigsRecommendations: function() {
  95. if (this.get('recommendationsConfigs') || !App.get('supports.serverRecommendValidate')) {
  96. return $.Deferred().resolve();
  97. }
  98. return App.ajax.send({
  99. 'name': 'wizard.step7.loadrecommendations.configs',
  100. 'sender': this,
  101. 'data': {
  102. stackVersionUrl: App.get('stackVersionURL'),
  103. hosts: this.get('hostNames'),
  104. services: this.get('serviceNames'),
  105. recommendations: this.get('hostGroups')
  106. },
  107. 'success': 'loadRecommendationsSuccess',
  108. 'error': 'loadRecommendationsError'
  109. });
  110. },
  111. /**
  112. * @method loadRecommendationsSuccess
  113. * success callback after loading recommendations
  114. * (used only during install)
  115. * @param data
  116. */
  117. loadRecommendationsSuccess: function(data) {
  118. if (!data) {
  119. console.warn('error while loading default config values');
  120. }
  121. this.set("recommendationsConfigs", Em.get(data.resources[0] , "recommendations.blueprint.configurations"));
  122. },
  123. loadRecommendationsError: function() {
  124. console.error('Load recommendations failed');
  125. },
  126. serverSideValidation: function() {
  127. var deferred = $.Deferred();
  128. if (!App.get('supports.serverRecommendValidate')) {
  129. deferred.resolve();
  130. } else {
  131. this.set('configValidationFailed', false);
  132. if (this.get('loadAdditionalinfo')) {
  133. var self = this;
  134. this.getHostNames().always(function() {
  135. if (self.get('configValidationFailed')) {
  136. self.warnUser(deferred);
  137. } else {
  138. self.runServerSideValidation(deferred);
  139. }
  140. });
  141. } else {
  142. this.runServerSideValidation(deferred);
  143. }
  144. }
  145. return deferred;
  146. },
  147. getHostNames: function() {
  148. var self = this;
  149. if (self.get('isInstaller')) {
  150. // In installer wizard 'hosts.all' AJAX will not work cause cluster haven't been created yet
  151. var hosts = [];
  152. for (var host in self.get('content.hosts')) {
  153. hosts.push(host);
  154. }
  155. self.set("allHostNames", hosts);
  156. var deferred = $.Deferred();
  157. deferred.resolve();
  158. return deferred;
  159. } else {
  160. return App.ajax.send({
  161. name: 'hosts.all',
  162. sender: self,
  163. success: 'getHostNamesSuccess',
  164. error: 'getHostNamesError'
  165. });
  166. }
  167. },
  168. getHostNamesSuccess: function(data) {
  169. this.set("allHostNames", data.items.mapProperty("Hosts.host_name"));
  170. },
  171. getHostNamesError: function() {
  172. this.set('configValidationFailed', true);
  173. console.error('failed to load hostNames');
  174. },
  175. /**
  176. * @method serverSideValidation
  177. * send request to validate configs
  178. * @returns {*}
  179. */
  180. runServerSideValidation: function(deferred) {
  181. var self = this;
  182. var recommendations = this.get('hostGroups');
  183. recommendations.blueprint.configurations = blueprintUtils.buildConfisJSON(this.get('services'), this.get('stepConfigs'));
  184. return App.ajax.send({
  185. name: 'config.validations',
  186. sender: this,
  187. data: {
  188. stackVersionUrl: App.get('stackVersionURL'),
  189. hosts: this.get('hostNames'),
  190. services: this.get('serviceNames'),
  191. validate: 'configurations',
  192. recommendations: recommendations
  193. },
  194. success: 'validationSuccess',
  195. error: 'validationError'
  196. }).complete(function() {
  197. self.warnUser(deferred);
  198. });
  199. },
  200. /**
  201. * @method validationSuccess
  202. * success callback after getting responce from server
  203. * go through the step configs and set warn and error messages
  204. * @param data
  205. */
  206. validationSuccess: function(data) {
  207. var self = this;
  208. self.set('configValidationError', false);
  209. self.set('configValidationWarning', false);
  210. data.resources.forEach(function(r) {
  211. r.items.forEach(function(item){
  212. if (item.type == "configuration") {
  213. self.get('stepConfigs').forEach(function(service) {
  214. service.get('configs').forEach(function(property) {
  215. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  216. if (item.level == "ERROR") {
  217. self.set('configValidationError', true);
  218. property.set('errorMessage', item.message);
  219. property.set('error', true);
  220. } else if (item.level == "WARN") {
  221. self.set('configValidationWarning', true);
  222. property.set('warnMessage', item.message);
  223. property.set('warn', true);
  224. }
  225. }
  226. });
  227. })
  228. }
  229. });
  230. });
  231. },
  232. validationError: function() {
  233. this.set('configValidationFailed', true);
  234. console.error('config validation failed');
  235. },
  236. /**
  237. * warn user if some errors or warning were
  238. * in seting up configs otherwise go to the nex operation
  239. * @param deferred
  240. * @returns {*}
  241. */
  242. warnUser: function(deferred) {
  243. var self = this;
  244. if (this.get('configValidationFailed')) {
  245. this.set('isSubmitDisabled', false);
  246. this.set("isApplyingChanges", false);
  247. deferred.reject();
  248. return App.showAlertPopup(Em.I18n.t('installer.step7.popup.validation.failed.header'), Em.I18n.t('installer.step7.popup.validation.request.failed.body'));
  249. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  250. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  251. this.set('isSubmitDisabled', true);
  252. this.set("isApplyingChanges", false);
  253. return App.showConfirmationPopup(function () {
  254. self.set('isSubmitDisabled', false);
  255. self.set("isApplyingChanges", true);
  256. deferred.resolve();
  257. }, Em.I18n.t('installer.step7.popup.validation.warning.body'));
  258. } else {
  259. deferred.resolve();
  260. }
  261. }
  262. });