cluster_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. App.ClusterController = Em.Controller.extend({
  20. name: 'clusterController',
  21. isLoaded: false,
  22. ambariProperties: null,
  23. clusterDataLoadedPercent: 'width:0', // 0 to 1
  24. isGangliaUrlLoaded: false,
  25. /**
  26. * Provides the URL to use for Ganglia server. This URL
  27. * is helpful in populating links in UI.
  28. *
  29. * If null is returned, it means GANGLIA service is not installed.
  30. */
  31. gangliaUrl: null,
  32. clusterName: function () {
  33. return App.get('clusterName');
  34. }.property('App.clusterName'),
  35. updateLoadStatus: function (item) {
  36. var loadList = this.get('dataLoadList');
  37. var loaded = true;
  38. var numLoaded = 0;
  39. var loadListLength = 0;
  40. loadList.set(item, true);
  41. for (var i in loadList) {
  42. if (loadList.hasOwnProperty(i)) {
  43. loadListLength++;
  44. if (!loadList[i] && loaded) {
  45. loaded = false;
  46. }
  47. }
  48. // calculate the number of true
  49. if (loadList.hasOwnProperty(i) && loadList[i]) {
  50. numLoaded++;
  51. }
  52. }
  53. this.set('isLoaded', loaded);
  54. this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded / loadListLength * 100)).toString() + '%');
  55. },
  56. dataLoadList: Em.Object.create({
  57. 'hosts': false,
  58. 'serviceMetrics': false,
  59. 'stackComponents': false,
  60. 'services': false,
  61. 'cluster': false,
  62. 'clusterStatus': false,
  63. 'racks': false,
  64. 'componentConfigs': false,
  65. 'componentsState': false,
  66. 'rootService': false,
  67. 'alertDefinitions': false,
  68. 'securityStatus': false
  69. }),
  70. /**
  71. * load cluster name
  72. */
  73. loadClusterName: function (reload) {
  74. var dfd = $.Deferred();
  75. if (App.get('clusterName') && !reload) {
  76. App.set('clusterName', this.get('clusterName'));
  77. dfd.resolve();
  78. } else {
  79. App.ajax.send({
  80. name: 'cluster.load_cluster_name',
  81. sender: this,
  82. success: 'loadClusterNameSuccessCallback',
  83. error: 'loadClusterNameErrorCallback'
  84. }).complete(function () {
  85. if (!App.get('currentStackVersion')) {
  86. App.set('currentStackVersion', App.defaultStackVersion);
  87. }
  88. dfd.resolve();
  89. });
  90. }
  91. return dfd.promise()
  92. },
  93. loadClusterNameSuccessCallback: function (data) {
  94. if (data.items && data.items.length > 0) {
  95. App.set('clusterName', data.items[0].Clusters.cluster_name);
  96. App.set('currentStackVersion', data.items[0].Clusters.version);
  97. }
  98. },
  99. loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
  100. console.log('failed on loading cluster name');
  101. this.set('isLoaded', true);
  102. },
  103. /**
  104. * load current server clock in milli-seconds
  105. */
  106. loadClientServerClockDistance: function () {
  107. var dfd = $.Deferred();
  108. this.getServerClock().done(function () {
  109. dfd.resolve();
  110. });
  111. return dfd.promise();
  112. },
  113. getServerClock: function () {
  114. return App.ajax.send({
  115. name: 'ambari.service.load_server_clock',
  116. sender: this,
  117. success: 'getServerClockSuccessCallback',
  118. error: 'getServerClockErrorCallback'
  119. });
  120. },
  121. getServerClockSuccessCallback: function (data) {
  122. var clientClock = new Date().getTime();
  123. var serverClock = (data.RootServiceComponents.server_clock).toString();
  124. serverClock = serverClock.length < 13 ? serverClock + '000' : serverClock;
  125. App.set('clockDistance', serverClock - clientClock);
  126. App.set('currentServerTime', parseInt(serverClock));
  127. console.log('loading ambari server clock distance');
  128. },
  129. getServerClockErrorCallback: function () {
  130. console.log('Cannot load ambari server clock');
  131. },
  132. getUrl: function (testUrl, url) {
  133. return (App.get('testMode')) ? testUrl : App.get('apiPrefix') + '/clusters/' + App.get('clusterName') + url;
  134. },
  135. setGangliaUrl: function () {
  136. if (App.get('testMode')) {
  137. this.set('gangliaUrl', 'http://gangliaserver/ganglia/?t=yes');
  138. this.set('isGangliaUrlLoaded', true);
  139. } else {
  140. // We want live data here
  141. var gangliaServer = App.HostComponent.find().findProperty('componentName', 'GANGLIA_SERVER');
  142. if (this.get('isLoaded') && gangliaServer) {
  143. this.set('isGangliaUrlLoaded', false);
  144. App.ajax.send({
  145. name: 'hosts.for_quick_links',
  146. sender: this,
  147. data: {
  148. clusterName: App.get('clusterName'),
  149. masterHosts: gangliaServer.get('hostName'),
  150. urlParams: ''
  151. },
  152. success: 'setGangliaUrlSuccessCallback'
  153. });
  154. }
  155. }
  156. }.observes('App.router.updateController.isUpdated', 'dataLoadList.hosts', 'gangliaWebProtocol', 'isLoaded'),
  157. setGangliaUrlSuccessCallback: function (response) {
  158. var url = null;
  159. if (response.items.length > 0) {
  160. url = this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : response.items[0].Hosts.public_host_name) + "/ganglia";
  161. }
  162. this.set('gangliaUrl', url);
  163. this.set('isGangliaUrlLoaded', true);
  164. },
  165. gangliaWebProtocol: function () {
  166. var properties = this.get('ambariProperties');
  167. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  168. return "https";
  169. } else {
  170. return "http";
  171. }
  172. }.property('ambariProperties'),
  173. isGangliaInstalled: function () {
  174. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  175. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  176. /**
  177. * load all data and update load status
  178. */
  179. loadClusterData: function () {
  180. var self = this;
  181. this.getAllHostNames();
  182. this.loadAmbariProperties();
  183. if (!App.get('clusterName')) {
  184. return;
  185. }
  186. if (this.get('isLoaded')) { // do not load data repeatedly
  187. App.router.get('mainController').startPolling();
  188. return;
  189. }
  190. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  191. var racksUrl = "/data/racks/racks.json";
  192. var hostsController = App.router.get('mainHostController');
  193. hostsController.set('isCountersUpdating', true);
  194. hostsController.updateStatusCounters();
  195. App.HttpClient.get(racksUrl, App.racksMapper, {
  196. complete: function (jqXHR, textStatus) {
  197. self.updateLoadStatus('racks');
  198. }
  199. }, function (jqXHR, textStatus) {
  200. self.updateLoadStatus('racks');
  201. });
  202. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  203. complete: function (jqXHR, textStatus) {
  204. self.updateLoadStatus('cluster');
  205. }
  206. }, function (jqXHR, textStatus) {
  207. self.updateLoadStatus('cluster');
  208. });
  209. if (App.get('testMode')) {
  210. self.updateLoadStatus('clusterStatus');
  211. } else {
  212. App.clusterStatus.updateFromServer().complete(function () {
  213. self.updateLoadStatus('clusterStatus');
  214. if (App.get('supports.stackUpgrade')) {
  215. self.restoreUpgradeState();
  216. }
  217. });
  218. }
  219. /**
  220. * Order of loading:
  221. * 1. request for service components supported by stack
  222. * 2. load stack components to model
  223. * 3. request for services
  224. * 4. put services in cache
  225. * 5. request for hosts and host-components (single call)
  226. * 6. request for service metrics
  227. * 7. load host-components to model
  228. * 8. load hosts to model
  229. * 9. load services from cache with metrics to model
  230. * 10. update stale_configs of host-components (depends on App.supports.hostOverrides)
  231. * 11. load root service (Ambari)
  232. * 12. load alert definitions to model
  233. * 13. load unhealthy alert instances
  234. * 14. load security status
  235. */
  236. self.loadStackServiceComponents(function (data) {
  237. data.items.forEach(function (service) {
  238. service.StackServices.is_selected = true;
  239. service.StackServices.is_installed = false;
  240. }, self);
  241. App.stackServiceMapper.mapStackServices(data);
  242. App.config.setPreDefinedServiceConfigs(true);
  243. var updater = App.router.get('updateController');
  244. self.updateLoadStatus('stackComponents');
  245. updater.updateServices(function () {
  246. self.updateLoadStatus('services');
  247. //force clear filters for hosts page to load all data
  248. App.db.setFilterConditions('mainHostController', null);
  249. updater.updateHost(function () {
  250. self.updateLoadStatus('hosts');
  251. });
  252. updater.updateServiceMetric(function () {
  253. updater.updateComponentConfig(function () {
  254. self.updateLoadStatus('componentConfigs');
  255. });
  256. updater.updateComponentsState(function () {
  257. self.updateLoadStatus('componentsState');
  258. });
  259. self.updateLoadStatus('serviceMetrics');
  260. updater.updateAlertGroups(function () {
  261. updater.updateAlertDefinitions(function() {
  262. updater.updateAlertDefinitionSummary(function () {
  263. updater.updateUnhealthyAlertInstances(function () {
  264. self.updateLoadStatus('alertGroups');
  265. self.updateLoadStatus('alertDefinitions');
  266. self.updateLoadStatus('alertInstancesUnhealthy');
  267. });
  268. });
  269. });
  270. });
  271. });
  272. });
  273. self.loadRootService().done(function (data) {
  274. App.rootServiceMapper.map(data);
  275. self.updateLoadStatus('rootService');
  276. });
  277. // load security status
  278. App.router.get('mainAdminKerberosController').getSecurityStatus().always(function() {
  279. self.updateLoadStatus('securityStatus');
  280. });
  281. });
  282. },
  283. /**
  284. * restore upgrade status from local storage
  285. * and make call to get latest status from server
  286. */
  287. restoreUpgradeState: function () {
  288. var dbUpgradeState = App.db.get('MainAdminStackAndUpgrade', 'upgradeState');
  289. if (!Em.isNone(dbUpgradeState)) {
  290. App.set('upgradeState', dbUpgradeState);
  291. }
  292. App.router.get('mainAdminStackAndUpgradeController').initDBProperties();
  293. App.router.get('mainAdminStackAndUpgradeController').loadUpgradeData(true);
  294. },
  295. loadRootService: function () {
  296. return App.ajax.send({
  297. name: 'service.ambari',
  298. sender: this
  299. });
  300. },
  301. requestHosts: function (realUrl, callback) {
  302. var testHostUrl = '/data/hosts/HDP2/hosts.json';
  303. var url = this.getUrl(testHostUrl, realUrl);
  304. App.HttpClient.get(url, App.hostsMapper, {
  305. complete: callback
  306. }, callback)
  307. },
  308. /**
  309. *
  310. * @param callback
  311. */
  312. loadStackServiceComponents: function (callback) {
  313. var callbackObj = {
  314. loadStackServiceComponentsSuccess: callback
  315. };
  316. App.ajax.send({
  317. name: 'wizard.service_components',
  318. data: {
  319. stackUrl: App.get('stackVersionURL'),
  320. stackVersion: App.get('currentStackVersionNumber')
  321. },
  322. sender: callbackObj,
  323. success: 'loadStackServiceComponentsSuccess'
  324. });
  325. },
  326. loadAmbariProperties: function () {
  327. return App.ajax.send({
  328. name: 'ambari.service',
  329. sender: this,
  330. success: 'loadAmbariPropertiesSuccess',
  331. error: 'loadAmbariPropertiesError'
  332. });
  333. },
  334. loadAmbariPropertiesSuccess: function (data) {
  335. console.log('loading ambari properties');
  336. this.set('ambariProperties', data.RootServiceComponents.properties);
  337. },
  338. loadAmbariPropertiesError: function () {
  339. console.warn('can\'t get ambari properties');
  340. },
  341. updateClusterData: function () {
  342. var testUrl = '/data/clusters/HDP2/cluster.json';
  343. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  344. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  345. complete: function () {
  346. }
  347. });
  348. },
  349. /**
  350. *
  351. * @returns {*|Transport|$.ajax|boolean|ServerResponse}
  352. */
  353. getAllHostNames: function () {
  354. return App.ajax.send({
  355. name: 'hosts.all',
  356. sender: this,
  357. success: 'getHostNamesSuccess',
  358. error: 'getHostNamesError'
  359. });
  360. },
  361. getHostNamesSuccess: function (data) {
  362. App.set("allHostNames", data.items.mapProperty("Hosts.host_name"));
  363. },
  364. getHostNamesError: function () {
  365. console.error('failed to load hostNames');
  366. },
  367. /**
  368. * puts kerberos admin credentials in the live cluster session
  369. * and resend ajax request
  370. * @param adminPrincipalValue
  371. * @param adminPasswordValue
  372. * @param ajaxOpt
  373. * @returns {$.ajax}
  374. */
  375. createKerberosAdminSession: function (adminPrincipalValue, adminPasswordValue, ajaxOpt) {
  376. return App.ajax.send({
  377. name: 'common.cluster.update',
  378. sender: this,
  379. data: {
  380. clusterName: App.get('clusterName'),
  381. data: [{
  382. session_attributes: {
  383. kerberos_admin: {principal: adminPrincipalValue, password: adminPasswordValue}
  384. }
  385. }]
  386. }
  387. }).success(function () {
  388. if (ajaxOpt) {
  389. $.ajax(ajaxOpt);
  390. }
  391. });
  392. }
  393. });