cluster_controller.js 14 KB

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