cluster_controller.js 14 KB

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