preload_requests_chain.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. /**
  20. * Mixin with chain of the methods for initial configs loading
  21. * Used in the service configs controller
  22. * Entry point - <code>loadClusterEnvSite</code>
  23. * Chain:
  24. * - loadClusterEnvSite
  25. * |- (on success callback)
  26. * loadServiceConfigVersions
  27. * |- (on success callback)
  28. * loadSelectedVersion
  29. * |- (on complete callback)
  30. * loadServiceTagsAndGroups
  31. * @type {Ember.Mixin}
  32. */
  33. App.PreloadRequestsChainMixin = Em.Mixin.create({
  34. /**
  35. * @type {Function}
  36. */
  37. trackRequest: Em.required(Function),
  38. /**
  39. * @type {Function}
  40. */
  41. isVersionDefault: Em.required(Function),
  42. /**
  43. * load all tag versions of cluster-env site
  44. * @returns {$.ajax}
  45. * @method loadClusterEnvSite
  46. */
  47. loadClusterEnvSite: function () {
  48. return App.ajax.send({
  49. name: 'config.cluster_env_site',
  50. sender: this,
  51. success: 'loadClusterEnvSiteSuccess'
  52. });
  53. },
  54. /**
  55. * Success-callback for loadClusterEnvSite
  56. * @param data
  57. * @private
  58. * @method loadClusterEnvSiteSuccess
  59. */
  60. loadClusterEnvSiteSuccess: function (data) {
  61. // find the latest tag version
  62. var maxVersion = Math.max.apply(this, data.items.mapProperty('version'));
  63. this.set('clusterEnvTagVersion', data.items.findProperty('version', maxVersion).tag);
  64. this.trackRequest(this.loadServiceConfigVersions());
  65. },
  66. /**
  67. * get service config versions of current service
  68. * @return {$.ajax}
  69. * @private
  70. * @method loadServiceConfigVersions
  71. */
  72. loadServiceConfigVersions: function () {
  73. return App.ajax.send({
  74. name: 'service.serviceConfigVersions.get',
  75. data: {
  76. serviceName: this.get('content.serviceName')
  77. },
  78. sender: this,
  79. success: 'loadServiceConfigVersionsSuccess',
  80. error: 'loadServiceConfigVersionsError'
  81. })
  82. },
  83. /**
  84. * success callback for loadServiceConfigVersions
  85. * load service config versions to model
  86. * set currentDefaultVersion
  87. * @param data
  88. * @param opt
  89. * @param params
  90. * @private
  91. * @method loadServiceConfigVersionsSuccess
  92. */
  93. loadServiceConfigVersionsSuccess: function (data, opt, params) {
  94. App.serviceConfigVersionsMapper.map(data);
  95. this.set('currentDefaultVersion', data.items.filterProperty('group_id', -1).findProperty('is_current').service_config_version);
  96. if (this.get('preSelectedConfigVersion')) {
  97. this.loadSelectedVersion(this.get('preSelectedConfigVersion.version'));
  98. } else {
  99. this.loadSelectedVersion();
  100. }
  101. },
  102. /**
  103. * error callback of loadServiceConfigVersions()
  104. * override defaultCallback
  105. * @private
  106. * @method loadServiceConfigVersionsError
  107. */
  108. loadServiceConfigVersionsError: Em.K,
  109. /**
  110. * get selected service config version
  111. * In case selected version is undefined then take currentDefaultVersion
  112. * @param version
  113. * @param switchToGroup
  114. * @method loadSelectedVersion
  115. */
  116. loadSelectedVersion: function (version, switchToGroup) {
  117. var self = this;
  118. this.set('versionLoaded', false);
  119. version = version || this.get('currentDefaultVersion');
  120. //version of non-default group require properties from current version of default group to correctly display page
  121. var versions = (this.isVersionDefault(version)) ? [version] : [this.get('currentDefaultVersion'), version];
  122. switchToGroup = (this.isVersionDefault(version) && !switchToGroup) ? this.get('configGroups').findProperty('isDefault') : switchToGroup;
  123. if (self.get('dataIsLoaded') && switchToGroup) {
  124. this.set('selectedConfigGroup', switchToGroup);
  125. }
  126. var data = {
  127. serviceName: this.get('content.serviceName'),
  128. serviceConfigVersions: versions
  129. };
  130. if (App.get('supports.enhancedConfigs') && this.get('dependentServiceNames.length')) {
  131. data.additionalParams = '|service_name.in(' + this.get('dependentServiceNames') + ')&is_current=true';
  132. }
  133. this.trackRequest(App.ajax.send({
  134. name: 'service.serviceConfigVersions.get.multiple',
  135. sender: this,
  136. data: data,
  137. success: 'loadSelectedVersionSuccess'
  138. }).complete(function (xhr) {
  139. if (xhr.statusText === 'abort') return;
  140. if (self.get('dataIsLoaded')) {
  141. self.onConfigGroupChange();
  142. } else {
  143. self.loadServiceTagsAndGroups();
  144. }
  145. }));
  146. },
  147. /**
  148. * load config groups of service
  149. * and dependent services
  150. * @private
  151. * @method loadServiceTagsAndGroups
  152. */
  153. loadServiceTagsAndGroups: function () {
  154. this.trackRequest(App.ajax.send({
  155. name: 'config.tags_and_groups',
  156. sender: this,
  157. data: {
  158. serviceName: this.get('content.serviceName'),
  159. urlParams: "&config_groups/ConfigGroup/tag.in(" + this.get('servicesToLoad').join(',') + ')'
  160. },
  161. success: 'loadServiceConfigsSuccess'
  162. }));
  163. },
  164. /**
  165. * set cluster to site tag map
  166. * @param data
  167. * @param opt
  168. * @param params
  169. * @private
  170. * @method loadSelectedVersionSuccess
  171. */
  172. loadSelectedVersionSuccess: function (data, opt, params) {
  173. var serviceConfigsDef = this.get('serviceConfigs').filter(function(serviceConfig) {
  174. return this.get('servicesToLoad').contains(serviceConfig.get('serviceName'));
  175. }, this);
  176. var siteToTagMap = {};
  177. var configTypesRendered = [];
  178. serviceConfigsDef.forEach(function(s) {
  179. configTypesRendered = configTypesRendered.concat(Object.keys(s.get('configTypesRendered')));
  180. });
  181. var selectedVersion = params.serviceConfigVersions.length > 1 ? params.serviceConfigVersions[1] : params.serviceConfigVersions[0];
  182. var configurations = [];
  183. configTypesRendered.forEach(function (siteName) {
  184. data.items.forEach(function (item) {
  185. if (item.group_id == -1) {
  186. configurations = item.configurations;
  187. if (item.configurations.someProperty('type', siteName)) {
  188. siteToTagMap[siteName] = item.configurations.findProperty('type', siteName).tag;
  189. } else if (!siteToTagMap[siteName]) {
  190. siteToTagMap[siteName] = 'version1';
  191. }
  192. } else {
  193. //set config tags of non-default config group to load overrides from selected version
  194. this.loadedGroupToOverrideSiteToTagMap[item.group_name] = {};
  195. item.configurations.forEach(function (config) {
  196. this.loadedGroupToOverrideSiteToTagMap[item.group_name][config.type] = config.tag;
  197. }, this)
  198. }
  199. }, this)
  200. }, this);
  201. App.router.get('configurationController').saveToDB(configurations);
  202. // add cluster-env tag
  203. siteToTagMap['cluster-env'] = this.get('clusterEnvTagVersion');
  204. this.loadedClusterSiteToTagMap = siteToTagMap;
  205. this.set('selectedVersion', selectedVersion);
  206. //reset map if selected current version of default group
  207. if (this.get('isCurrentSelected') && selectedVersion === this.get('currentDefaultVersion')) {
  208. this.loadedGroupToOverrideSiteToTagMap = {};
  209. }
  210. },
  211. /**
  212. * Success-callback for loadServiceTagsAndGroups
  213. * @param {object} data
  214. * @param {object} opt
  215. * @param {object} params
  216. * @private
  217. * @method loadServiceConfigsSuccess
  218. */
  219. loadServiceConfigsSuccess: function (data, opt, params) {
  220. this.setConfigGroups(data, opt, params);
  221. },
  222. /**
  223. * @param {object} data
  224. * @param {object} opt
  225. * @param {object} params
  226. * @private
  227. * @method setConfigGroups
  228. */
  229. setConfigGroups: function (data, opt, params) {
  230. var serviceName = this.get('content.serviceName');
  231. var displayName = this.get('content.displayName');
  232. var selectedConfigGroup;
  233. var defaultHosts = App.get('allHostNames');
  234. //parse loaded config groups
  235. var configGroups = [];
  236. if (data && data.config_groups && data.config_groups.length) {
  237. data.config_groups.forEach(function (item) {
  238. item = item.ConfigGroup;
  239. if (item.tag === this.get('content.serviceName')) {
  240. var groupHosts = item.hosts.mapProperty('host_name');
  241. var newConfigGroup = App.ConfigGroup.create({
  242. id: item.id,
  243. name: item.group_name,
  244. description: item.description,
  245. isDefault: false,
  246. parentConfigGroup: null,
  247. service: App.Service.find().findProperty('serviceName', item.tag),
  248. hosts: groupHosts,
  249. configSiteTags: []
  250. });
  251. for (var i = 0; i < groupHosts.length; i++) {
  252. defaultHosts = defaultHosts.without(groupHosts[i]);
  253. }
  254. item.desired_configs.forEach(function (config) {
  255. newConfigGroup.configSiteTags.push(App.ConfigSiteTag.create({
  256. site: config.type,
  257. tag: config.tag
  258. }));
  259. }, this);
  260. // select default selected group for hosts page
  261. if (!selectedConfigGroup && this.get('isHostsConfigsPage') && newConfigGroup.get('hosts').contains(this.get('host.hostName')) && this.get('content.serviceName') === item.tag) {
  262. selectedConfigGroup = newConfigGroup;
  263. }
  264. configGroups.push(newConfigGroup);
  265. } else if (this.get('dependentServiceNames').contains(item.tag)) {
  266. /**
  267. * Load config groups for services that has dependent properties.
  268. * If user change properties that have dependencies in not default config group
  269. * user should pick to which config group Ambari should save these properties
  270. * @type {App.ConfigGroup}
  271. */
  272. var newDependentConfigGroup = App.ConfigGroup.create({
  273. id: item.id,
  274. name: item.group_name,
  275. description: item.description,
  276. isDefault: false,
  277. parentConfigGroup: null,
  278. service: App.Service.find().findProperty('serviceName', item.tag),
  279. hosts: item.hosts.mapProperty('host_name')
  280. });
  281. item.desired_configs.forEach(function (config) {
  282. newDependentConfigGroup.configSiteTags.push(App.ConfigSiteTag.create({
  283. site: config.type,
  284. tag: config.tag
  285. }));
  286. }, this);
  287. if (!this.get('dependentConfigGroups').findProperty('name', item.group_name)) {
  288. this.get('dependentConfigGroups').push(newDependentConfigGroup);
  289. }
  290. }
  291. }, this);
  292. }
  293. this.get('dependentServiceNames').forEach(function(serviceName) {
  294. if (serviceName !== this.get('content.serviceName')) {
  295. var service = App.Service.find().findProperty('serviceName', serviceName);
  296. /**
  297. * default groups for dependent services
  298. * @type {App.ConfigGroup}
  299. */
  300. var defaultConfigGroup = App.ConfigGroup.create({
  301. name: service.get('displayName') + " Default",
  302. description: "Default cluster level " + serviceName + " configuration",
  303. isDefault: true,
  304. hosts: [],
  305. parentConfigGroup: null,
  306. service: service,
  307. serviceName: serviceName,
  308. configSiteTags: []
  309. });
  310. if (!this.get('dependentConfigGroups').findProperty('name', defaultConfigGroup.get('name'))) {
  311. this.get('dependentConfigGroups').push(defaultConfigGroup);
  312. }
  313. }
  314. }, this);
  315. this.set('configGroups', configGroups);
  316. var defaultConfigGroup = App.ConfigGroup.create({
  317. name: displayName + " Default",
  318. description: "Default cluster level " + serviceName + " configuration",
  319. isDefault: true,
  320. hosts: defaultHosts,
  321. parentConfigGroup: null,
  322. service: this.get('content'),
  323. serviceName: serviceName,
  324. configSiteTags: []
  325. });
  326. if (!selectedConfigGroup) {
  327. selectedConfigGroup = configGroups.findProperty('name', this.get('preSelectedConfigVersion.groupName')) || defaultConfigGroup;
  328. }
  329. this.get('configGroups').sort(function (configGroupA, configGroupB) {
  330. return (configGroupA.name > configGroupB.name);
  331. });
  332. this.get('configGroups').unshift(defaultConfigGroup);
  333. this.set('selectedConfigGroup', selectedConfigGroup);
  334. this.set('preSelectedConfigVersion', null);
  335. }
  336. });