serverValidator.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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(jqXHR, ajaxOptions, error, opt) {
  124. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  125. console.error('Load recommendations failed');
  126. },
  127. serverSideValidation: function() {
  128. var deferred = $.Deferred();
  129. if (!App.get('supports.serverRecommendValidate')) {
  130. deferred.resolve();
  131. } else {
  132. this.set('configValidationFailed', false);
  133. if (this.get('loadAdditionalinfo')) {
  134. var self = this;
  135. this.getHostNames().always(function() {
  136. if (self.get('configValidationFailed')) {
  137. self.warnUser(deferred);
  138. } else {
  139. self.runServerSideValidation(deferred);
  140. }
  141. });
  142. } else {
  143. this.runServerSideValidation(deferred);
  144. }
  145. }
  146. return deferred;
  147. },
  148. getHostNames: function() {
  149. var self = this;
  150. if (self.get('isInstaller')) {
  151. // In installer wizard 'hosts.all' AJAX will not work cause cluster haven't been created yet
  152. var hosts = [];
  153. for (var host in self.get('content.hosts')) {
  154. hosts.push(host);
  155. }
  156. self.set("allHostNames", hosts);
  157. var deferred = $.Deferred();
  158. deferred.resolve();
  159. return deferred;
  160. } else {
  161. return App.ajax.send({
  162. name: 'hosts.all',
  163. sender: self,
  164. success: 'getHostNamesSuccess',
  165. error: 'getHostNamesError'
  166. });
  167. }
  168. },
  169. getHostNamesSuccess: function(data) {
  170. this.set("allHostNames", data.items.mapProperty("Hosts.host_name"));
  171. },
  172. getHostNamesError: function() {
  173. this.set('configValidationFailed', true);
  174. console.error('failed to load hostNames');
  175. },
  176. /**
  177. * @method serverSideValidation
  178. * send request to validate configs
  179. * @returns {*}
  180. */
  181. runServerSideValidation: function(deferred) {
  182. var self = this;
  183. var recommendations = this.get('hostGroups');
  184. recommendations.blueprint.configurations = blueprintUtils.buildConfisJSON(this.get('services'), this.get('stepConfigs'));
  185. return App.ajax.send({
  186. name: 'config.validations',
  187. sender: this,
  188. data: {
  189. stackVersionUrl: App.get('stackVersionURL'),
  190. hosts: this.get('hostNames'),
  191. services: this.get('serviceNames'),
  192. validate: 'configurations',
  193. recommendations: recommendations
  194. },
  195. success: 'validationSuccess',
  196. error: 'validationError'
  197. }).complete(function() {
  198. self.warnUser(deferred);
  199. });
  200. },
  201. /**
  202. * @method validationSuccess
  203. * success callback after getting responce from server
  204. * go through the step configs and set warn and error messages
  205. * @param data
  206. */
  207. validationSuccess: function(data) {
  208. var self = this;
  209. self.set('configValidationError', false);
  210. self.set('configValidationWarning', false);
  211. data.resources.forEach(function(r) {
  212. r.items.forEach(function(item){
  213. if (item.type == "configuration") {
  214. self.get('stepConfigs').forEach(function(service) {
  215. service.get('configs').forEach(function(property) {
  216. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  217. if (item.level == "ERROR") {
  218. self.set('configValidationError', true);
  219. property.set('errorMessage', item.message);
  220. property.set('error', true);
  221. } else if (item.level == "WARN") {
  222. self.set('configValidationWarning', true);
  223. property.set('warnMessage', item.message);
  224. property.set('warn', true);
  225. }
  226. }
  227. });
  228. })
  229. }
  230. });
  231. });
  232. },
  233. validationError: function (jqXHR, ajaxOptions, error, opt) {
  234. this.set('configValidationFailed', true);
  235. App.ajax.defaultErrorHandler(jqXHR, opt.url, opt.method, jqXHR.status);
  236. console.error('config validation failed');
  237. },
  238. /**
  239. * warn user if some errors or warning were
  240. * in seting up configs otherwise go to the nex operation
  241. * @param deferred
  242. * @returns {*}
  243. */
  244. warnUser: function(deferred) {
  245. var self = this;
  246. if (this.get('configValidationFailed')) {
  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. return App.ModalPopup.show({
  252. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  253. primary: Em.I18n.t('common.proceedAnyway'),
  254. onPrimary: function () {
  255. this.hide();
  256. deferred.resolve();
  257. },
  258. onSecondary: function () {
  259. this.hide();
  260. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  261. },
  262. bodyClass: Em.View.extend({
  263. controller: self,
  264. templateName: require('templates/common/configs/config_recommendation_popup')
  265. })
  266. });
  267. } else {
  268. deferred.resolve();
  269. }
  270. }
  271. });