serverValidator.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. * defines if we use validation and recommendation on wizards
  23. * depend on this flag some properties will be taken from different places
  24. * @type {boolean}
  25. */
  26. isWizard: function() {
  27. return this.get('wizardController') && ['addServiceController', 'installerController'].contains(this.get('wizardController.name'));
  28. }.property('wizardController.name'),
  29. /**
  30. * @type {boolean} set true if at least one config has error
  31. */
  32. configValidationError: false,
  33. /**
  34. * @type {boolean} set true if at least one config has warning
  35. */
  36. configValidationWarning: false,
  37. /**
  38. * @type {boolean} set true if at least one config has warning
  39. */
  40. configValidationFailed: false,
  41. /**
  42. * @type {object[]} contains additional message about validation errors
  43. */
  44. configValidationGlobalMessage: [],
  45. /**
  46. * recommendation configs loaded from server
  47. * (used only during install)
  48. * @type {Object}
  49. */
  50. recommendationsConfigs: null,
  51. /**
  52. * array of cluster-env configs
  53. * used for validation request
  54. * @type {Array}
  55. */
  56. clusterEnvConfigs: [],
  57. /**
  58. * by default loads data from model otherwise must be overridden as computed property
  59. * refer to \assets\data\stacks\HDP-2.1\recommendations_configs.json to learn structure
  60. * (shouldn't contain configurations filed)
  61. * @type {Object}
  62. */
  63. hostNames: function() {
  64. return this.get('isWizard')
  65. ? Object.keys(this.get('content.hosts'))
  66. : App.get('allHostNames');
  67. }.property('isWizard', 'content.hosts', 'App.allHostNames'),
  68. /**
  69. * by default loads data from model otherwise must be overridden as computed property
  70. * @type {Array} - of strings (serviceNames)
  71. */
  72. serviceNames: function() {
  73. // When editing a service we validate only that service's configs.
  74. // However, we should pass the IDs of services installed, or else,
  75. // default value calculations will alter.
  76. return this.get('isWizard') ? this.get('allSelectedServiceNames') : App.Service.find().mapProperty('serviceName');
  77. }.property('isWizard', 'allSelectedServiceNames'),
  78. /**
  79. * by default loads data from model otherwise must be overridden as computed property
  80. * can be used for service|host configs pages
  81. * @type {Array} of strings (hostNames)
  82. */
  83. hostGroups: function() {
  84. return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(App.get('allHostNames'));
  85. }.property('content.recommendationsHostGroups', 'App.allHostNames'),
  86. /**
  87. * controller that is child of this mixin has to contain stepConfigs
  88. * @type {Array}
  89. */
  90. stepConfigs: null,
  91. serverSideValidation: function () {
  92. var deferred = $.Deferred();
  93. this.set('configValidationFailed', false);
  94. this.set('configValidationGlobalMessage', []);
  95. if (this.get('configValidationFailed')) {
  96. this.warnUser(deferred);
  97. } else {
  98. this.runServerSideValidation(deferred);
  99. }
  100. return deferred;
  101. },
  102. getAllHostsWithComponents: function() {
  103. return App.ajax.send({
  104. sender: this,
  105. name: 'common.hosts.all',
  106. data: {
  107. urlParams: 'fields=HostRoles/component_name,HostRoles/host_name'
  108. }
  109. });
  110. },
  111. /**
  112. * @method serverSideValidation
  113. * send request to validate configs
  114. * @returns {*}
  115. */
  116. runServerSideValidation: function (deferred) {
  117. var self = this;
  118. var recommendations = this.get('hostGroups');
  119. var stepConfigs = this.get('stepConfigs');
  120. return this.getBlueprintConfigurations().done(function(blueprintConfigurations){
  121. recommendations.blueprint.configurations = blueprintConfigurations;
  122. return App.ajax.send({
  123. name: 'config.validations',
  124. sender: self,
  125. data: {
  126. stackVersionUrl: App.get('stackVersionURL'),
  127. hosts: self.get('hostNames'),
  128. services: self.get('serviceNames'),
  129. validate: 'configurations',
  130. recommendations: recommendations
  131. },
  132. success: 'validationSuccess',
  133. error: 'validationError'
  134. }).complete(function () {
  135. self.warnUser(deferred);
  136. });
  137. });
  138. },
  139. /**
  140. * Return JSON for blueprint configurations
  141. * @returns {*}
  142. */
  143. getBlueprintConfigurations: function () {
  144. var dfd = $.Deferred();
  145. var stepConfigs = this.get('stepConfigs');
  146. // check if we have configs from 'cluster-env', if not, then load them, as they are mandatory for validation request
  147. if (!stepConfigs.findProperty('serviceName', 'MISC')) {
  148. this.getClusterEnvConfigsForValidation().done(function(clusterEnvConfigs){
  149. stepConfigs = stepConfigs.concat(Em.Object.create({
  150. serviceName: 'MISC',
  151. configs: clusterEnvConfigs
  152. }));
  153. dfd.resolve(blueprintUtils.buildConfigsJSON(stepConfigs));
  154. });
  155. } else {
  156. dfd.resolve(blueprintUtils.buildConfigsJSON(stepConfigs));
  157. }
  158. return dfd.promise();
  159. },
  160. getClusterEnvConfigsForValidation: function () {
  161. var dfd = $.Deferred();
  162. App.ajax.send({
  163. name: 'config.cluster_env_site',
  164. sender: this,
  165. error: 'validationError'
  166. }).done(function (data) {
  167. App.router.get('configurationController').getConfigsByTags([{
  168. siteName: data.items[data.items.length - 1].type,
  169. tagName: data.items[data.items.length - 1].tag
  170. }]).done(function (clusterEnvConfigs) {
  171. var configsObject = clusterEnvConfigs[0].properties;
  172. var configsArray = [];
  173. for (var property in configsObject) {
  174. if (configsObject.hasOwnProperty(property)) {
  175. configsArray.push(Em.Object.create({
  176. name: property,
  177. value: configsObject[property],
  178. filename: 'cluster-env.xml'
  179. }));
  180. }
  181. }
  182. dfd.resolve(configsArray);
  183. });
  184. });
  185. return dfd.promise();
  186. },
  187. /**
  188. * @method validationSuccess
  189. * success callback after getting response from server
  190. * go through the step configs and set warn and error messages
  191. * @param data
  192. */
  193. validationSuccess: function(data) {
  194. var self = this;
  195. var checkedProperties = [];
  196. var globalWarning = [];
  197. self.set('configValidationError', false);
  198. self.set('configValidationWarning', false);
  199. data.resources.forEach(function(r) {
  200. r.items.forEach(function(item){
  201. if (item.type == "configuration") {
  202. self.get('stepConfigs').forEach(function(service) {
  203. service.get('configs').forEach(function(property) {
  204. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  205. if (item.level == "ERROR") {
  206. self.set('configValidationError', true);
  207. property.set('errorMessage', item.message);
  208. property.set('error', true);
  209. } else if (item.level == "WARN") {
  210. self.set('configValidationWarning', true);
  211. property.set('warnMessage', item.message);
  212. property.set('warn', true);
  213. }
  214. // store property data to detect WARN or ERROR messages for missed property
  215. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  216. }
  217. });
  218. });
  219. // check if error or warn message detected for property that absent in step configs
  220. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  221. var message = {
  222. propertyName: item['config-name'],
  223. filename: item['config-type'],
  224. warnMessage: item.message
  225. };
  226. if (item['config-type'] === "" && item['config-name'] === "") {
  227. //service-independent validation
  228. message.isGeneral = true;
  229. } else {
  230. message.serviceName = App.StackService.find().filter(function(service) {
  231. return !!service.get('configTypes')[item['config-type']];
  232. })[0].get('displayName')
  233. }
  234. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  235. globalWarning.push(message);
  236. }
  237. }
  238. });
  239. });
  240. self.set('configValidationGlobalMessage', globalWarning);
  241. },
  242. validationError: function (jqXHR, ajaxOptions, error, opt) {
  243. this.set('configValidationFailed', true);
  244. },
  245. /**
  246. * warn user if some errors or warning were
  247. * in setting up configs otherwise go to the nex operation
  248. * @param deferred
  249. * @returns {*}
  250. */
  251. warnUser: function(deferred) {
  252. var self = this;
  253. if (this.get('configValidationFailed')) {
  254. return App.ModalPopup.show({
  255. header: Em.I18n.t('installer.step7.popup.validation.failed.header'),
  256. primary: Em.I18n.t('common.proceedAnyway'),
  257. primaryClass: 'btn-danger',
  258. marginBottom: 200,
  259. onPrimary: function () {
  260. this.hide();
  261. deferred.resolve();
  262. },
  263. onSecondary: function () {
  264. this.hide();
  265. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  266. },
  267. onClose: function () {
  268. this.hide();
  269. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  270. },
  271. body: Em.I18n.t('installer.step7.popup.validation.request.failed.body')
  272. });
  273. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  274. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  275. var stepConfigs = self.get('name') === 'mainServiceInfoConfigsController'
  276. ? [self.get('selectedService')]
  277. : self.get('stepConfigs');
  278. var configsWithErrors = stepConfigs.some(function (step) {
  279. return step.get('configs').some(function(c) {
  280. return c.get('isVisible') && !c.get('hiddenBySection') && (c.get('warn') || c.get('error'));
  281. })
  282. });
  283. if (configsWithErrors) {
  284. return App.ModalPopup.show({
  285. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  286. classNames: ['sixty-percent-width-modal','modal-full-width'],
  287. primary: Em.I18n.t('common.proceedAnyway'),
  288. primaryClass: 'btn-danger',
  289. marginBottom: 200,
  290. onPrimary: function () {
  291. this.hide();
  292. deferred.resolve();
  293. },
  294. onSecondary: function () {
  295. this.hide();
  296. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  297. },
  298. onClose: function () {
  299. this.hide();
  300. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  301. },
  302. bodyClass: Em.View.extend({
  303. controller: self,
  304. templateName: require('templates/common/modal_popups/config_recommendation_popup'),
  305. serviceConfigs: stepConfigs
  306. })
  307. });
  308. } else {
  309. deferred.resolve();
  310. }
  311. } else {
  312. deferred.resolve();
  313. }
  314. }
  315. });