cluster_controller.js 13 KB

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