cluster_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. if (this.get('isLoaded')) {
  140. this.set('isGangliaUrlLoaded', true);
  141. App.ajax.send({
  142. name: 'hosts.for_quick_links',
  143. sender: this,
  144. data: {
  145. clusterName: App.get('clusterName'),
  146. masterComponents: 'GANGLIA_SERVER',
  147. urlParams: ''
  148. },
  149. success: 'setGangliaUrlSuccessCallback'
  150. });
  151. }
  152. }
  153. }.observes('App.router.updateController.isUpdated', 'dataLoadList.hosts', 'gangliaWebProtocol', 'isLoaded'),
  154. setGangliaUrlSuccessCallback: function (response) {
  155. var url = null;
  156. if (response.items.length > 0) {
  157. url = this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : response.items[0].Hosts.public_host_name) + "/ganglia";
  158. }
  159. this.set('gangliaUrl', url);
  160. this.set('isGangliaUrlLoaded', true);
  161. },
  162. setNagiosUrl: function () {
  163. if (App.testMode) {
  164. return 'http://nagiosserver/nagios';
  165. } else {
  166. // We want live data here
  167. if (this.get('isLoaded')) {
  168. this.set('isNagiosUrlLoaded', false);
  169. App.ajax.send({
  170. name: 'hosts.for_quick_links',
  171. sender: this,
  172. data: {
  173. clusterName: App.get('clusterName'),
  174. masterComponents: 'NAGIOS_SERVER',
  175. urlParams: ''
  176. },
  177. success: 'setNagiosUrlSuccessCallback'
  178. });
  179. }
  180. }
  181. }.observes('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics', 'dataLoadList.hosts', 'nagiosWebProtocol', 'isLoaded'),
  182. setNagiosUrlSuccessCallback: function (response) {
  183. var url = null;
  184. if (response.items.length > 0) {
  185. url = this.get('nagiosWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : response.items[0].Hosts.public_host_name) + "/nagios";
  186. }
  187. this.set('nagiosUrl', url);
  188. this.set('isNagiosUrlLoaded', true);
  189. },
  190. nagiosWebProtocol: function () {
  191. var properties = this.get('ambariProperties');
  192. if (properties && properties.hasOwnProperty('nagios.https') && properties['nagios.https']) {
  193. return "https";
  194. } else {
  195. return "http";
  196. }
  197. }.property('ambariProperties'),
  198. gangliaWebProtocol: function () {
  199. var properties = this.get('ambariProperties');
  200. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  201. return "https";
  202. } else {
  203. return "http";
  204. }
  205. }.property('ambariProperties'),
  206. isNagiosInstalled: function () {
  207. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  208. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  209. isGangliaInstalled: function () {
  210. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  211. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  212. /**
  213. *
  214. * load all data and update load status
  215. */
  216. loadClusterData: function () {
  217. var self = this;
  218. this.loadAmbariProperties();
  219. this.loadAmbariViews();
  220. if (!this.get('clusterName')) {
  221. return;
  222. }
  223. if (this.get('isLoaded')) { // do not load data repeatedly
  224. App.router.get('mainController').startPolling();
  225. return;
  226. }
  227. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  228. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  229. var racksUrl = "/data/racks/racks.json";
  230. var hostsController = App.router.get('mainHostController');
  231. hostsController.set('isCountersUpdating', true);
  232. hostsController.updateStatusCounters();
  233. hostsController.set('isCountersUpdating', false);
  234. App.HttpClient.get(racksUrl, App.racksMapper, {
  235. complete: function (jqXHR, textStatus) {
  236. self.updateLoadStatus('racks');
  237. }
  238. }, function (jqXHR, textStatus) {
  239. self.updateLoadStatus('racks');
  240. });
  241. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  242. complete: function (jqXHR, textStatus) {
  243. self.updateLoadStatus('cluster');
  244. }
  245. }, function (jqXHR, textStatus) {
  246. self.updateLoadStatus('cluster');
  247. });
  248. if (App.testMode) {
  249. self.updateLoadStatus('clusterStatus');
  250. } else {
  251. App.clusterStatus.updateFromServer(true).complete(function () {
  252. self.updateLoadStatus('clusterStatus');
  253. });
  254. }
  255. App.HttpClient.get(usersUrl, App.usersMapper, {
  256. complete: function (jqXHR, textStatus) {
  257. self.updateLoadStatus('users');
  258. }
  259. }, function (jqXHR, textStatus) {
  260. self.updateLoadStatus('users');
  261. });
  262. /**
  263. * Order of loading:
  264. * 1. request for service components supported by stack
  265. * 2. load stack components to model
  266. * 3. request for services
  267. * 4. put services in cache
  268. * 5. request for hosts and host-components (single call)
  269. * 6. request for service metrics
  270. * 7. load host-components to model
  271. * 8. load hosts to model
  272. * 9. load services from cache with metrics to model
  273. * 10. update stale_configs of host-components (depends on App.supports.hostOverrides)
  274. */
  275. this.loadStackServiceComponents(function (data) {
  276. var updater = App.router.get('updateController');
  277. require('utils/component').loadStackServiceComponentModel(data);
  278. self.updateLoadStatus('stackComponents');
  279. updater.updateServices(function () {
  280. self.updateLoadStatus('services');
  281. updater.updateHost(function () {
  282. self.updateLoadStatus('hosts');
  283. });
  284. updater.updateServiceMetric(function () {
  285. if (App.supports.hostOverrides) {
  286. updater.updateComponentConfig(function () {
  287. self.updateLoadStatus('componentConfigs');
  288. });
  289. } else {
  290. self.updateLoadStatus('componentConfigs');
  291. }
  292. updater.updateComponentsState(function () {
  293. self.updateLoadStatus('componentsState');
  294. });
  295. self.updateLoadStatus('serviceMetrics');
  296. });
  297. });
  298. });
  299. },
  300. requestHosts: function (realUrl, callback) {
  301. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  302. var url = this.getUrl(testHostUrl, realUrl);
  303. App.HttpClient.get(url, App.hostsMapper, {
  304. complete: callback
  305. }, callback)
  306. },
  307. loadAmbariViews: function () {
  308. App.ajax.send({
  309. name: 'views.info',
  310. sender: this,
  311. success: 'loadAmbariViewsSuccess'
  312. });
  313. },
  314. loadAmbariViewsSuccess: function (data) {
  315. if (data.items.length) {
  316. App.ajax.send({
  317. name: 'views.instances',
  318. sender: this,
  319. success: 'loadViewInstancesSuccess'
  320. });
  321. }
  322. },
  323. loadViewInstancesSuccess: function (data) {
  324. this.set('ambariViews', []);
  325. var self = this;
  326. data.items.forEach(function (view) {
  327. view.versions.forEach(function (version) {
  328. version.instances.forEach(function (instance) {
  329. var current_instance = Em.Object.create({
  330. iconPath: instance.ViewInstanceInfo.icon_path || "/img/ambari-view-default.png",
  331. label: instance.ViewInstanceInfo.label || version.ViewVersionInfo.label || instance.ViewInstanceInfo.view_name,
  332. visible: instance.ViewInstanceInfo.visible || false,
  333. version: instance.ViewInstanceInfo.version,
  334. description: instance.ViewInstanceInfo.description || Em.I18n.t('views.main.instance.noDescription'),
  335. viewName: instance.ViewInstanceInfo.view_name,
  336. instanceName: instance.ViewInstanceInfo.instance_name,
  337. href: instance.ViewInstanceInfo.context_path
  338. });
  339. self.get('ambariViews').pushObject(current_instance);
  340. }, this);
  341. }, this);
  342. }, this);
  343. },
  344. /**
  345. *
  346. * @param callback
  347. */
  348. loadStackServiceComponents: function (callback) {
  349. var callbackObj = {
  350. loadStackServiceComponentsSuccess: callback
  351. };
  352. App.ajax.send({
  353. name: 'wizard.service_components',
  354. data: {
  355. stackUrl: App.get('stackVersionURL'),
  356. stackVersion: App.get('currentStackVersionNumber'),
  357. async: true
  358. },
  359. sender: callbackObj,
  360. success: 'loadStackServiceComponentsSuccess'
  361. });
  362. },
  363. loadAmbariProperties: function () {
  364. return App.ajax.send({
  365. name: 'ambari.service',
  366. sender: this,
  367. success: 'loadAmbariPropertiesSuccess',
  368. error: 'loadAmbariPropertiesError'
  369. });
  370. },
  371. loadAmbariPropertiesSuccess: function (data) {
  372. console.log('loading ambari properties');
  373. this.set('ambariProperties', data.RootServiceComponents.properties);
  374. },
  375. loadAmbariPropertiesError: function () {
  376. console.warn('can\'t get ambari properties');
  377. },
  378. clusterName: function () {
  379. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  380. }.property('cluster'),
  381. updateClusterData: function () {
  382. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json' : '/data/clusters/cluster.json';
  383. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  384. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  385. complete: function () {
  386. }
  387. });
  388. }
  389. });