serverValidator.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. * filter services that support server validation and concat with misc configs if Installer or current service
  81. * @type {Array} - of objects (services)
  82. */
  83. services: function() {
  84. var stackServices = App.StackService.find().filter(function(s) {
  85. return this.get('serviceNames').contains(s.get('serviceName'));
  86. }, this);
  87. return this.get('isWizard') ? stackServices.concat(require("data/service_configs")) : stackServices;
  88. }.property('serviceNames'),
  89. /**
  90. * by default loads data from model otherwise must be overridden as computed property
  91. * can be used for service|host configs pages
  92. * @type {Array} of strings (hostNames)
  93. */
  94. hostGroups: function() {
  95. return this.get('content.recommendationsHostGroups') || blueprintUtils.generateHostGroups(App.get('allHostNames'));
  96. }.property('content.recommendationsHostGroups', 'App.allHostNames'),
  97. /**
  98. * controller that is child of this mixin has to contain stepConfigs
  99. * @type {Array}
  100. */
  101. stepConfigs: null,
  102. /**
  103. * @method loadServerSideConfigsRecommendations
  104. * load recommendations from server
  105. * (used only during install)
  106. * @returns {*}
  107. */
  108. loadServerSideConfigsRecommendations: function() {
  109. /**
  110. * if extended controller doesn't support recommendations or recommendations has been already loaded
  111. * ignore this call but keep promise chain
  112. */
  113. if (!this.get('isControllerSupportsEnhancedConfigs') || !Em.isNone(this.get('recommendationsConfigs'))) {
  114. return $.Deferred().resolve().promise();
  115. }
  116. var recommendations = this.get('hostGroups');
  117. // send user's input based on stored configurations
  118. recommendations.blueprint.configurations = blueprintUtils.buildConfigsJSON(this.get('services'), this.get('stepConfigs'));
  119. // include cluster-env site to recommendations call
  120. var miscService = this.get('services').findProperty('serviceName', 'MISC');
  121. if (miscService) {
  122. var miscConfigs = blueprintUtils.buildConfigsJSON([miscService], [this.get('stepConfigs').findProperty('serviceName', 'MISC')]);
  123. var clusterEnv = App.permit(miscConfigs, 'cluster-env');
  124. if (!App.isEmptyObject(clusterEnv)) {
  125. $.extend(recommendations.blueprint.configurations, clusterEnv);
  126. }
  127. /** add user properties from misc tabs to proper filename **/
  128. this.get('stepConfigs').findProperty('serviceName', 'MISC').get('configs').forEach(function(config) {
  129. var tag = App.config.getConfigTagFromFileName(config.get('filename'));
  130. if (recommendations.blueprint.configurations[tag] && tag != 'cluster-env') {
  131. recommendations.blueprint.configurations[tag].properties[config.get('name')] = config.get('value');
  132. }
  133. })
  134. }
  135. this.set('initialConfigValues', recommendations.blueprint.configurations);
  136. return App.ajax.send({
  137. 'name': 'config.recommendations',
  138. 'sender': this,
  139. 'data': {
  140. stackVersionUrl: App.get('stackVersionURL'),
  141. dataToSend: {
  142. recommend: 'configurations',
  143. hosts: this.get('hostNames'),
  144. services: this.get('serviceNames'),
  145. recommendations: recommendations
  146. }
  147. },
  148. 'success': 'loadRecommendationsSuccess',
  149. 'error': 'loadRecommendationsError'
  150. });
  151. },
  152. serverSideValidation: function () {
  153. var deferred = $.Deferred();
  154. this.set('configValidationFailed', false);
  155. this.set('configValidationGlobalMessage', []);
  156. if (this.get('configValidationFailed')) {
  157. this.warnUser(deferred);
  158. } else {
  159. this.runServerSideValidation(deferred);
  160. }
  161. return deferred;
  162. },
  163. getAllHostsWithComponents: function() {
  164. return App.ajax.send({
  165. sender: this,
  166. name: 'common.hosts.all',
  167. data: {
  168. urlParams: 'fields=HostRoles/component_name,HostRoles/host_name'
  169. }
  170. });
  171. },
  172. /**
  173. * @method serverSideValidation
  174. * send request to validate configs
  175. * @returns {*}
  176. */
  177. runServerSideValidation: function (deferred) {
  178. var self = this;
  179. var recommendations = this.get('hostGroups');
  180. var services = this.get('services');
  181. var stepConfigs = this.get('stepConfigs');
  182. return this.getBlueprintConfigurations().done(function(blueprintConfigurations){
  183. recommendations.blueprint.configurations = blueprintConfigurations;
  184. return App.ajax.send({
  185. name: 'config.validations',
  186. sender: self,
  187. data: {
  188. stackVersionUrl: App.get('stackVersionURL'),
  189. hosts: self.get('hostNames'),
  190. services: self.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. /**
  202. * Return JSON for blueprint configurations
  203. * @returns {*}
  204. */
  205. getBlueprintConfigurations: function () {
  206. var dfd = $.Deferred();
  207. var services = this.get('services');
  208. var stepConfigs = this.get('stepConfigs');
  209. var allConfigTypes = [];
  210. services.forEach(function (service) {
  211. allConfigTypes = allConfigTypes.concat(Em.keys(service.get('configTypes')))
  212. });
  213. // check if we have configs from 'cluster-env', if not, then load them, as they are mandatory for validation request
  214. if (!allConfigTypes.contains('cluster-env')) {
  215. this.getClusterEnvConfigsForValidation().done(function(clusterEnvConfigs){
  216. services = services.concat(Em.Object.create({
  217. serviceName: 'MISC',
  218. configTypes: {'cluster-env': {}}
  219. }));
  220. stepConfigs = stepConfigs.concat(Em.Object.create({
  221. serviceName: 'MISC',
  222. configs: clusterEnvConfigs
  223. }));
  224. dfd.resolve(blueprintUtils.buildConfigsJSON(services, stepConfigs));
  225. });
  226. } else {
  227. dfd.resolve(blueprintUtils.buildConfigsJSON(services, stepConfigs));
  228. }
  229. return dfd.promise();
  230. },
  231. getClusterEnvConfigsForValidation: function () {
  232. var dfd = $.Deferred();
  233. App.ajax.send({
  234. name: 'config.cluster_env_site',
  235. sender: this,
  236. error: 'validationError'
  237. }).done(function (data) {
  238. App.router.get('configurationController').getConfigsByTags([{
  239. siteName: data.items[data.items.length - 1].type,
  240. tagName: data.items[data.items.length - 1].tag
  241. }]).done(function (clusterEnvConfigs) {
  242. var configsObject = clusterEnvConfigs[0].properties;
  243. var configsArray = [];
  244. for (var property in configsObject) {
  245. if (configsObject.hasOwnProperty(property)) {
  246. configsArray.push(Em.Object.create({
  247. name: property,
  248. value: configsObject[property],
  249. filename: 'cluster-env.xml'
  250. }));
  251. }
  252. }
  253. dfd.resolve(configsArray);
  254. });
  255. });
  256. return dfd.promise();
  257. },
  258. /**
  259. * @method validationSuccess
  260. * success callback after getting response from server
  261. * go through the step configs and set warn and error messages
  262. * @param data
  263. */
  264. validationSuccess: function(data) {
  265. var self = this;
  266. var checkedProperties = [];
  267. var globalWarning = [];
  268. self.set('configValidationError', false);
  269. self.set('configValidationWarning', false);
  270. data.resources.forEach(function(r) {
  271. r.items.forEach(function(item){
  272. if (item.type == "configuration") {
  273. self.get('stepConfigs').forEach(function(service) {
  274. service.get('configs').forEach(function(property) {
  275. if ((property.get('filename') == item['config-type'] + '.xml') && (property.get('name') == item['config-name'])) {
  276. if (item.level == "ERROR") {
  277. self.set('configValidationError', true);
  278. property.set('errorMessage', item.message);
  279. property.set('error', true);
  280. } else if (item.level == "WARN") {
  281. self.set('configValidationWarning', true);
  282. property.set('warnMessage', item.message);
  283. property.set('warn', true);
  284. }
  285. // store property data to detect WARN or ERROR messages for missed property
  286. if (["ERROR", "WARN"].contains(item.level)) checkedProperties.push(item['config-type'] + '/' + item['config-name']);
  287. }
  288. });
  289. });
  290. // check if error or warn message detected for property that absent in step configs
  291. if (["ERROR", "WARN"].contains(item.level) && !checkedProperties.contains(item['config-type'] + '/' + item['config-name'])) {
  292. var message = {
  293. propertyName: item['config-name'],
  294. filename: item['config-type'],
  295. warnMessage: item.message
  296. };
  297. if (item['config-type'] === "" && item['config-name'] === "") {
  298. //service-independent validation
  299. message.isGeneral = true;
  300. } else {
  301. message.serviceName = App.StackService.find().filter(function(service) {
  302. return !!service.get('configTypes')[item['config-type']];
  303. })[0].get('displayName')
  304. }
  305. self.set(item.level == 'WARN' ? 'configValidationWarning' : 'configValidationError', true);
  306. globalWarning.push(message);
  307. }
  308. }
  309. });
  310. });
  311. self.set('configValidationGlobalMessage', globalWarning);
  312. },
  313. validationError: function (jqXHR, ajaxOptions, error, opt) {
  314. this.set('configValidationFailed', true);
  315. },
  316. /**
  317. * warn user if some errors or warning were
  318. * in setting up configs otherwise go to the nex operation
  319. * @param deferred
  320. * @returns {*}
  321. */
  322. warnUser: function(deferred) {
  323. var self = this;
  324. if (this.get('configValidationFailed')) {
  325. return App.ModalPopup.show({
  326. header: Em.I18n.t('installer.step7.popup.validation.failed.header'),
  327. primary: Em.I18n.t('common.proceedAnyway'),
  328. primaryClass: 'btn-danger',
  329. marginBottom: 200,
  330. onPrimary: function () {
  331. this.hide();
  332. deferred.resolve();
  333. },
  334. onSecondary: function () {
  335. this.hide();
  336. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  337. },
  338. onClose: function () {
  339. this.hide();
  340. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  341. },
  342. body: Em.I18n.t('installer.step7.popup.validation.request.failed.body')
  343. });
  344. } else if (this.get('configValidationWarning') || this.get('configValidationError')) {
  345. // Motivation: for server-side validation warnings and EVEN errors allow user to continue wizard
  346. var stepConfigs = self.get('name') === 'mainServiceInfoConfigsController'
  347. ? [self.get('selectedService')]
  348. : self.get('stepConfigs');
  349. var configsWithErrors = stepConfigs.some(function (step) {
  350. return step.get('configs').some(function(c) {
  351. return c.get('isVisible') && !c.get('hiddenBySection') && (c.get('warn') || c.get('error'));
  352. })
  353. });
  354. if (configsWithErrors) {
  355. return App.ModalPopup.show({
  356. header: Em. I18n.t('installer.step7.popup.validation.warning.header'),
  357. classNames: ['sixty-percent-width-modal','modal-full-width'],
  358. primary: Em.I18n.t('common.proceedAnyway'),
  359. primaryClass: 'btn-danger',
  360. marginBottom: 200,
  361. onPrimary: function () {
  362. this.hide();
  363. deferred.resolve();
  364. },
  365. onSecondary: function () {
  366. this.hide();
  367. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  368. },
  369. onClose: function () {
  370. this.hide();
  371. deferred.reject("invalid_configs"); // message used to differentiate types of rejections.
  372. },
  373. bodyClass: Em.View.extend({
  374. controller: self,
  375. templateName: require('templates/common/modal_popups/config_recommendation_popup'),
  376. serviceConfigs: stepConfigs
  377. })
  378. });
  379. } else {
  380. deferred.resolve();
  381. }
  382. } else {
  383. deferred.resolve();
  384. }
  385. }
  386. });