step3_controller.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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.WizardStep3Controller = Em.Controller.extend({
  20. name: 'wizardStep3Controller',
  21. hosts: [],
  22. content: [],
  23. bootHosts: [],
  24. registrationStartedAt: null,
  25. registrationTimeoutSecs: 120,
  26. stopBootstrap: false,
  27. isSubmitDisabled: true,
  28. categoryObject: Em.Object.extend({
  29. hostsCount: function () {
  30. var category = this;
  31. var hosts = this.get('controller.hosts').filter(function(_host) {
  32. if (_host.get('bootStatus') == category.get('hostsBootStatus')) {
  33. return true;
  34. } else if (_host.get('bootStatus') == 'DONE' && category.get('hostsBootStatus') == 'REGISTERING') {
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. }, this);
  40. return hosts.get('length');
  41. }.property('controller.hosts.@each.bootStatus'), // 'hosts.@each.bootStatus'
  42. label: function () {
  43. return "%@ (%@)".fmt(this.get('value'), this.get('hostsCount'));
  44. }.property('value', 'hostsCount')
  45. }),
  46. getCategory: function(field, value){
  47. return this.get('categories').find(function(item){
  48. return item.get(field) == value;
  49. });
  50. },
  51. categories: function () {
  52. var self = this;
  53. self.categoryObject.reopen({
  54. controller: self,
  55. isActive: function(){
  56. return this.get('controller.category') == this;
  57. }.property('controller.category'),
  58. itemClass: function(){
  59. return this.get('isActive') ? 'active' : '';
  60. }.property('isActive')
  61. });
  62. var categories = [
  63. self.categoryObject.create({value: 'All', hostsCount: function () {
  64. return this.get('controller.hosts.length');
  65. }.property('controller.hosts.length') }),
  66. self.categoryObject.create({value: 'Installing', hostsBootStatus: 'RUNNING'}),
  67. self.categoryObject.create({value: 'Registering', hostsBootStatus: 'REGISTERING'}),
  68. self.categoryObject.create({value: 'Success', hostsBootStatus: 'REGISTERED' }),
  69. self.categoryObject.create({value: 'Fail', hostsBootStatus: 'FAILED', last: true })
  70. ];
  71. this.set('category', categories.get('firstObject'));
  72. return categories;
  73. }.property(),
  74. category: false,
  75. allChecked: false,
  76. onAllChecked: function () {
  77. var hosts = this.get('visibleHosts');
  78. hosts.setEach('isChecked', this.get('allChecked'));
  79. }.observes('allChecked'),
  80. noHostsSelected: function () {
  81. return !(this.hosts.someProperty('isChecked', true));
  82. }.property('hosts.@each.isChecked'),
  83. isRetryDisabled: true,
  84. mockData: require('data/mock/step3_hosts'),
  85. mockRetryData: require('data/mock/step3_pollData'),
  86. navigateStep: function () {
  87. this.loadStep();
  88. if(App.testMode){
  89. this.getHostInfo();
  90. }
  91. if (this.get('content.installOptions.manualInstall') !== true) {
  92. if (!App.db.getBootStatus()) {
  93. this.startBootstrap();
  94. }
  95. } else {
  96. this.set('bootHosts', this.get('hosts'));
  97. if (App.testMode) {
  98. this.get('bootHosts').setEach('bootStatus', 'REGISTERED');
  99. this.get('bootHosts').setEach('cpu', '2');
  100. this.get('bootHosts').setEach('memory', '2000000');
  101. this.set('isSubmitDisabled', false);
  102. } else {
  103. this.set('registrationStartedAt', null);
  104. this.get('bootHosts').setEach('bootStatus', 'DONE');
  105. this.startRegistration();
  106. }
  107. }
  108. },
  109. clearStep: function () {
  110. this.set('stopBootstrap', false);
  111. this.hosts.clear();
  112. this.bootHosts.clear();
  113. App.db.setBootStatus(false);
  114. this.set('isSubmitDisabled', true);
  115. this.set('isRetryDisabled', true);
  116. },
  117. loadStep: function () {
  118. console.log("TRACE: Loading step3: Confirm Hosts");
  119. this.set('registrationStartedAt', null);
  120. this.clearStep();
  121. var hosts = this.loadHosts();
  122. // hosts.setEach('bootStatus', 'RUNNING');
  123. this.renderHosts(hosts);
  124. },
  125. /* Loads the hostinfo from localStorage on the insertion of view. It's being called from view */
  126. loadHosts: function () {
  127. var hostInfo = this.get('content.hosts');
  128. var hosts = new Ember.Set();
  129. for (var index in hostInfo) {
  130. hosts.add(hostInfo[index]);
  131. console.log("TRACE: host name is: " + hostInfo[index].name);
  132. }
  133. return hosts;
  134. },
  135. /* Renders the set of passed hosts */
  136. renderHosts: function (hostsInfo) {
  137. var self = this;
  138. hostsInfo.forEach(function (_hostInfo) {
  139. var hostInfo = App.HostInfo.create({
  140. name: _hostInfo.name,
  141. bootStatus: _hostInfo.bootStatus,
  142. isChecked: false
  143. });
  144. console.log('pushing ' + hostInfo.name);
  145. self.hosts.pushObject(hostInfo);
  146. });
  147. },
  148. /**
  149. * Parses and updates the content based on bootstrap API response.
  150. * Returns true if polling should continue (some hosts are in "RUNNING" state); false otherwise
  151. */
  152. parseHostInfo: function (hostsStatusFromServer) {
  153. hostsStatusFromServer.forEach(function (_hostStatus) {
  154. var host = this.get('bootHosts').findProperty('name', _hostStatus.hostName);
  155. // check if hostname extracted from REST API data matches any hostname in content
  156. // also, make sure that bootStatus modified by isHostsRegistered call does not get overwritten
  157. // since these calls are being made in parallel
  158. if (host && !['REGISTERED', 'REGISTERING'].contains(host.get('bootStatus'))) {
  159. host.set('bootStatus', _hostStatus.status);
  160. host.set('bootLog', _hostStatus.log);
  161. }
  162. }, this);
  163. // if the data rendered by REST API has hosts in "RUNNING" state, polling will continue
  164. return this.get('bootHosts').length != 0 && this.get('bootHosts').someProperty('bootStatus', 'RUNNING');
  165. },
  166. /* Returns the current set of visible hosts on view (All, Succeeded, Failed) */
  167. visibleHosts: function () {
  168. var self = this;
  169. if (this.get('category.hostsBootStatus')) {
  170. return this.hosts.filterProperty('bootStatus', self.get('category.hostsBootStatus'));
  171. } else { // if (this.get('category') === 'All Hosts')
  172. return this.hosts;
  173. }
  174. }.property('category', 'hosts.@each.bootStatus'),
  175. removeHosts: function (hosts) {
  176. var self = this;
  177. App.ModalPopup.show({
  178. header: Em.I18n.t('installer.step3.hosts.remove.popup.header'),
  179. onPrimary: function () {
  180. App.router.send('removeHosts', hosts);
  181. self.hosts.removeObjects(hosts);
  182. if (!self.hosts.length) {
  183. self.set('isSubmitDisabled', true);
  184. }
  185. this.hide();
  186. },
  187. body: Em.I18n.t('installer.step3.hosts.remove.popup.body')
  188. });
  189. },
  190. /* Removes a single element on the trash icon click. Called from View */
  191. removeHost: function (hostInfo) {
  192. this.removeHosts([hostInfo]);
  193. },
  194. removeSelectedHosts: function () {
  195. if (!this.get('noHostsSelected')) {
  196. var selectedHosts = this.get('visibleHosts').filterProperty('isChecked', true);
  197. selectedHosts.forEach(function (_hostInfo) {
  198. console.log('Removing: ' + _hostInfo.name);
  199. });
  200. this.removeHosts(selectedHosts);
  201. }
  202. },
  203. retryHost: function (hostInfo) {
  204. this.retryHosts([hostInfo]);
  205. },
  206. retryHosts: function (hosts) {
  207. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('content.installOptions.sshKey'), hosts: hosts.mapProperty('name')});
  208. this.numPolls = 0;
  209. if (this.get('content.installOptions.manualInstall') !== true) {
  210. var requestId = App.router.get('installerController').launchBootstrap(bootStrapData);
  211. this.set('content.installOptions.bootRequestId', requestId);
  212. this.set('registrationStartedAt', null);
  213. this.doBootstrap();
  214. } else {
  215. this.set('registrationStartedAt', null);
  216. this.get('bootHosts').setEach('bootStatus', 'DONE');
  217. this.startRegistration();
  218. }
  219. },
  220. retrySelectedHosts: function () {
  221. //to display all hosts
  222. this.set('category', 'All');
  223. if (!this.get('isRetryDisabled')) {
  224. this.set('isRetryDisabled', true);
  225. var selectedHosts = this.get('bootHosts').filterProperty('bootStatus', 'FAILED');
  226. selectedHosts.forEach(function (_host) {
  227. _host.set('bootStatus', 'RUNNING');
  228. _host.set('bootLog', 'Retrying ...');
  229. }, this);
  230. this.retryHosts(selectedHosts);
  231. }
  232. },
  233. numPolls: 0,
  234. startBootstrap: function () {
  235. //this.set('isSubmitDisabled', true); //TODO: uncomment after actual hookup
  236. this.numPolls = 0;
  237. this.set('registrationStartedAt', null);
  238. this.set('bootHosts', this.get('hosts'));
  239. this.get('bootHosts').setEach('bootStatus', 'PENDING');
  240. this.doBootstrap();
  241. },
  242. isInstallInProgress: function(){
  243. var bootStatuses = this.get('bootHosts').getEach('bootStatus');
  244. if(bootStatuses.length &&
  245. (bootStatuses.contains('REGISTERING') ||
  246. bootStatuses.contains('DONE') ||
  247. bootStatuses.contains('RUNNING') ||
  248. bootStatuses.contains('PENDING'))){
  249. return true;
  250. }
  251. return false;
  252. }.property('bootHosts.@each.bootStatus'),
  253. disablePreviousSteps: function(){
  254. if(this.get('isInstallInProgress')){
  255. App.router.get('installerController').setLowerStepsDisable(3);
  256. this.set('isSubmitDisabled', true);
  257. } else {
  258. App.router.get('installerController.isStepDisabled').findProperty('step', 1).set('value', false);
  259. App.router.get('installerController.isStepDisabled').findProperty('step', 2).set('value', false);
  260. }
  261. }.observes('isInstallInProgress'),
  262. doBootstrap: function () {
  263. if (this.get('stopBootstrap')) {
  264. return;
  265. }
  266. this.numPolls++;
  267. var self = this;
  268. var url = App.testMode ? '/data/wizard/bootstrap/poll_' + this.numPolls + '.json' : App.apiPrefix + '/bootstrap/' + this.get('content.installOptions.bootRequestId');
  269. $.ajax({
  270. type: 'GET',
  271. url: url,
  272. timeout: App.timeout,
  273. success: function (data) {
  274. if (data.hostsStatus !== null) {
  275. // in case of bootstrapping just one host, the server returns an object rather than an array, so
  276. // force into an array
  277. if (!(data.hostsStatus instanceof Array)) {
  278. data.hostsStatus = [ data.hostsStatus ];
  279. }
  280. console.log("TRACE: In success function for the GET bootstrap call");
  281. var keepPolling = self.parseHostInfo(data.hostsStatus);
  282. // Single host : if the only hostname is invalid (data.status == 'ERROR')
  283. // Multiple hosts : if one or more hostnames are invalid
  284. // following check will mark the bootStatus as 'FAILED' for the invalid hostname
  285. if (data.status == 'ERROR' || data.hostsStatus.length != self.get('bootHosts').length) {
  286. var hosts = self.get('bootHosts');
  287. for (var i = 0; i < hosts.length; i++) {
  288. var isValidHost = data.hostsStatus.someProperty('hostName', hosts[i].get('name'));
  289. if(hosts[i].get('bootStatus') !== 'REGISTERED'){
  290. if (!isValidHost) {
  291. hosts[i].set('bootStatus', 'FAILED');
  292. hosts[i].set('bootLog', 'Registration with the server failed.');
  293. }
  294. }
  295. }
  296. }
  297. if (data.hostsStatus.someProperty('status', 'DONE') || data.hostsStatus.someProperty('status', 'FAILED')) {
  298. // kicking off registration polls after at least one host has succeeded
  299. self.startRegistration();
  300. }
  301. if (keepPolling) {
  302. window.setTimeout(function () {
  303. self.doBootstrap()
  304. }, 3000);
  305. return;
  306. }
  307. }
  308. },
  309. statusCode: require('data/statusCodes')
  310. }).retry({times: App.maxRetries, timeout: App.timeout}).then(null,
  311. function () {
  312. App.showReloadPopup();
  313. console.log('Bootstrap failed');
  314. }
  315. );
  316. },
  317. /*
  318. stopBootstrap: function () {
  319. console.log('stopBootstrap() called');
  320. Ember.run.later(this, function () {
  321. this.startRegistration();
  322. }, 1000);
  323. },
  324. */
  325. startRegistration: function () {
  326. if (this.get('registrationStartedAt') == null) {
  327. this.set('registrationStartedAt', new Date().getTime());
  328. console.log('registration started at ' + this.get('registrationStartedAt'));
  329. this.isHostsRegistered();
  330. }
  331. },
  332. isHostsRegistered: function () {
  333. if (this.get('stopBootstrap')) {
  334. return;
  335. }
  336. var self = this;
  337. var hosts = this.get('bootHosts');
  338. var url = App.testMode ? '/data/wizard/bootstrap/single_host_registration.json' : App.apiPrefix + '/hosts';
  339. $.ajax({
  340. type: 'GET',
  341. url: url,
  342. timeout: App.timeout,
  343. success: function (data) {
  344. console.log('registration attempt...');
  345. var jsonData = App.testMode ? data : jQuery.parseJSON(data);
  346. if (!jsonData) {
  347. console.log("Error: jsonData is null");
  348. return;
  349. }
  350. // keep polling until all hosts have registered/failed, or registrationTimeout seconds after the last host finished bootstrapping
  351. var stopPolling = true;
  352. hosts.forEach(function (_host, index) {
  353. // Change name of first host for test mode.
  354. if (App.testMode) {
  355. if (index == 0) {
  356. _host.set('name', 'localhost.localdomain');
  357. }
  358. }
  359. // actions to take depending on the host's current bootStatus
  360. // RUNNING - bootstrap is running; leave it alone
  361. // DONE - bootstrap is done; transition to REGISTERING
  362. // REGISTERING - bootstrap is done but has not registered; transition to REGISTERED if host found in polling API result
  363. // REGISTERED - bootstrap and registration is done; leave it alone
  364. // FAILED - either bootstrap or registration failed; leave it alone
  365. console.log(_host.name + ' bootStatus=' + _host.get('bootStatus'));
  366. switch (_host.get('bootStatus')) {
  367. case 'DONE':
  368. _host.set('bootStatus', 'REGISTERING');
  369. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistering with the server...');
  370. // update registration timestamp so that the timeout is computed from the last host that finished bootstrapping
  371. self.set('registrationStartedAt', new Date().getTime());
  372. stopPolling = false;
  373. break;
  374. case 'REGISTERING':
  375. if (jsonData.items.someProperty('Hosts.host_name', _host.name)) {
  376. console.log(_host.name + ' has been registered');
  377. _host.set('bootStatus', 'REGISTERED');
  378. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistration with the server succeeded.');
  379. } else {
  380. console.log(_host.name + ' is registering...');
  381. stopPolling = false;
  382. }
  383. break;
  384. case 'RUNNING':
  385. stopPolling = false;
  386. break;
  387. case 'REGISTERED':
  388. case 'FAILED':
  389. default:
  390. break;
  391. }
  392. }, this);
  393. if (stopPolling) {
  394. self.getHostInfo();
  395. } else if (hosts.someProperty('bootStatus', 'RUNNING') || new Date().getTime() - self.get('registrationStartedAt') < self.get('registrationTimeoutSecs') * 1000) {
  396. // we want to keep polling for registration status if any of the hosts are still bootstrapping (so we check for RUNNING).
  397. window.setTimeout(function () {
  398. self.isHostsRegistered();
  399. }, 3000);
  400. } else {
  401. // registration timed out. mark all REGISTERING hosts to FAILED
  402. console.log('registration timed out');
  403. hosts.filterProperty('bootStatus', 'REGISTERING').forEach(function (_host) {
  404. _host.set('bootStatus', 'FAILED');
  405. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistration with the server failed.');
  406. });
  407. self.getHostInfo();
  408. }
  409. },
  410. statusCode: require('data/statusCodes')
  411. }).retry({times: App.maxRetries, timeout: App.timeout}).then(null, function () {
  412. App.showReloadPopup();
  413. console.log('Error: Getting registered host information from the server');
  414. });
  415. },
  416. registerErrPopup: function (header, message) {
  417. App.ModalPopup.show({
  418. header: header,
  419. secondary: false,
  420. onPrimary: function () {
  421. this.hide();
  422. },
  423. bodyClass: Ember.View.extend({
  424. template: Ember.Handlebars.compile(['<p>{{view.message}}</p>'].join('\n')),
  425. message: message
  426. })
  427. });
  428. },
  429. /**
  430. * Get disk info and cpu count of booted hosts from server
  431. */
  432. getHostInfo: function () {
  433. var self = this;
  434. var kbPerGb = 1024;
  435. var hosts = this.get('bootHosts');
  436. var url = App.testMode ? '/data/wizard/bootstrap/two_hosts_information.json' : App.apiPrefix + '/hosts?fields=Hosts/total_mem,Hosts/cpu_count,Hosts/disk_info,Hosts/last_agent_env';
  437. var method = 'GET';
  438. $.ajax({
  439. type: 'GET',
  440. url: url,
  441. contentType: 'application/json',
  442. timeout: App.timeout,
  443. success: function (data) {
  444. var jsonData = (App.testMode) ? data : jQuery.parseJSON(data);
  445. self.parseWarnings(jsonData);
  446. hosts.forEach(function (_host) {
  447. var host = (App.testMode) ? jsonData.items[0] : jsonData.items.findProperty('Hosts.host_name', _host.name);
  448. if (App.skipBootstrap) {
  449. _host.cpu = 2;
  450. _host.memory = ((parseInt(2000000))).toFixed(2);
  451. _host.disk_info = [{"mountpoint": "/", "type":"ext4"},{"mountpoint": "/grid/0", "type":"ext4"}, {"mountpoint": "/grid/1", "type":"ext4"}, {"mountpoint": "/grid/2", "type":"ext4"}];
  452. } else if (host) {
  453. _host.cpu = host.Hosts.cpu_count;
  454. _host.memory = ((parseInt(host.Hosts.total_mem))).toFixed(2);
  455. _host.disk_info = host.Hosts.disk_info;
  456. console.log("The value of memory is: " + _host.memory);
  457. }
  458. });
  459. self.set('bootHosts', hosts);
  460. console.log("The value of hosts: " + JSON.stringify(hosts));
  461. self.stopRegistration();
  462. },
  463. error: function () {
  464. console.log('INFO: Getting host information(cpu_count and total_mem) from the server failed');
  465. self.registerErrPopup(Em.I18n.t('installer.step3.hostInformation.popup.header'), Em.I18n.t('installer.step3.hostInformation.popup.body'));
  466. },
  467. statusCode: require('data/statusCodes')
  468. });
  469. },
  470. stopRegistration: function () {
  471. this.set('isSubmitDisabled', !this.get('bootHosts').someProperty('bootStatus', 'REGISTERED'));
  472. this.set('isRetryDisabled', !this.get('bootHosts').someProperty('bootStatus', 'FAILED'));
  473. },
  474. selectCategory: function(event, context){
  475. this.set('category', event.context);
  476. },
  477. submit: function () {
  478. if (!this.get('isSubmitDisabled')) {
  479. this.set('content.hosts', this.get('bootHosts'));
  480. App.router.send('next');
  481. }
  482. },
  483. hostLogPopup: function (event, context) {
  484. var host = event.context;
  485. App.ModalPopup.show({
  486. header: Em.I18n.t('installer.step3.hostLog.popup.header').format(host.get('name')),
  487. secondary: null,
  488. onPrimary: function () {
  489. this.hide();
  490. },
  491. bodyClass: Ember.View.extend({
  492. templateName: require('templates/wizard/step3_host_log_popup'),
  493. host: host,
  494. didInsertElement: function () {
  495. var self = this;
  496. var button = $(this.get('element')).find('.textTrigger');
  497. button.click(function () {
  498. if (self.get('isTextArea')) {
  499. $(this).text('click to highlight');
  500. } else {
  501. $(this).text('press CTRL+C');
  502. }
  503. self.set('isTextArea', !self.get('isTextArea'));
  504. });
  505. $(this.get('element')).find('.content-area').mouseenter(
  506. function () {
  507. var element = $(this);
  508. element.css('border', '1px solid #dcdcdc');
  509. button.css('visibility', 'visible');
  510. }).mouseleave(
  511. function () {
  512. var element = $(this);
  513. element.css('border', 'none');
  514. button.css('visibility', 'hidden');
  515. })
  516. },
  517. isTextArea: false,
  518. textArea: Em.TextArea.extend({
  519. didInsertElement: function () {
  520. var element = $(this.get('element'));
  521. element.width($(this.get('parentView').get('element')).width() - 10);
  522. element.height($(this.get('parentView').get('element')).height());
  523. element.select();
  524. element.css('resize', 'none');
  525. },
  526. readOnly: true,
  527. value: function () {
  528. return this.get('content');
  529. }.property('content')
  530. })
  531. })
  532. });
  533. },
  534. /**
  535. * check warnings from server and put it in parsing
  536. */
  537. rerunChecks: function(){
  538. var self = this;
  539. var url = App.testMode ? '/data/wizard/bootstrap/two_hosts_information.json' : App.apiPrefix + '/hosts?fields=Hosts/last_agent_env';
  540. var currentProgress = 0;
  541. var interval = setInterval(function(){
  542. self.set('checksUpdateProgress', Math.ceil((++currentProgress/60)*100))
  543. }, 1000);
  544. setTimeout(function(){
  545. clearInterval(interval);
  546. $.ajax({
  547. type: 'GET',
  548. url: url,
  549. contentType: 'application/json',
  550. timeout: App.timeout,
  551. success: function (data) {
  552. var jsonData = (App.testMode) ? data : jQuery.parseJSON(data);
  553. self.set('checksUpdateProgress', 100);
  554. self.set('checksUpdateStatus', 'SUCCESS');
  555. self.parseWarnings(jsonData);
  556. },
  557. error: function () {
  558. self.set('checksUpdateProgress', 100);
  559. self.set('checksUpdateStatus', 'FAILED');
  560. console.log('INFO: Getting host information(last_agent_env) from the server failed');
  561. },
  562. statusCode: require('data/statusCodes')
  563. })
  564. }, this.get('warningsTimeInterval'));
  565. },
  566. warnings: [],
  567. warningsTimeInterval: 60000,
  568. /**
  569. * check are hosts have any warnings
  570. */
  571. isHostHaveWarnings: function(){
  572. var isWarning = false;
  573. this.get('warnings').forEach(function(warning){
  574. if(!isWarning && (warning.directoriesFiles.someProperty('isWarn', true) ||
  575. warning.packages.someProperty('isWarn', true) ||
  576. warning.processes.someProperty('isWarn', true))){
  577. isWarning = true;
  578. }
  579. }, this);
  580. return isWarning;
  581. }.property('warnings'),
  582. isWarningsBoxVisible: function(){
  583. return (App.testMode) ? true : !this.get('isSubmitDisabled');
  584. }.property('isSubmitDisabled'),
  585. checksUpdateProgress:0,
  586. checksUpdateStatus: null,
  587. /**
  588. * filter data for warnings parse
  589. * is data from host in bootStrap
  590. * @param data
  591. * @return {Object}
  592. */
  593. filterBootHosts: function(data){
  594. var bootHosts = this.get('bootHosts');
  595. var filteredData = {
  596. href: data.href,
  597. items: []
  598. };
  599. bootHosts.forEach(function(bootHost){
  600. data.items.forEach(function(host){
  601. if(host.Hosts.host_name == bootHost.get('name')){
  602. filteredData.items.push(host);
  603. }
  604. })
  605. })
  606. return filteredData;
  607. },
  608. /**
  609. * parse warnings data for each host and total
  610. * @param data
  611. */
  612. parseWarnings: function(data){
  613. data = this.filterBootHosts(data);
  614. var warnings = [];
  615. var totalWarnings = {
  616. hostName: 'All Hosts',
  617. directoriesFiles: [],
  618. packages: [],
  619. processes: []
  620. }
  621. //alphabetical sorting
  622. var sortingFunc = function(a, b){
  623. var a1= a.name, b1= b.name;
  624. if(a1== b1) return 0;
  625. return a1> b1? 1: -1;
  626. }
  627. data.items.forEach(function(host){
  628. var warningsByHost = {
  629. hostName: host.Hosts.host_name,
  630. directoriesFiles: [],
  631. packages: [],
  632. processes: []
  633. };
  634. //render all directories and files for each host
  635. host.Hosts.last_agent_env.paths.forEach(function(path){
  636. var parsedPath = {
  637. name: path.name,
  638. isWarn: (path.type == 'not_exist') ? false : true,
  639. message: (path.type == 'not_exist') ? 'OK' : 'WARN: already exists on host'
  640. }
  641. warningsByHost.directoriesFiles.push(parsedPath);
  642. // parsing total warnings
  643. if(!totalWarnings.directoriesFiles.someProperty('name', parsedPath.name)){
  644. totalWarnings.directoriesFiles.push({
  645. name:parsedPath.name,
  646. isWarn: parsedPath.isWarn,
  647. message: (parsedPath.isWarn) ? 'WARN: already exists on 1 host': 'OK',
  648. warnCount: (parsedPath.isWarn) ? 1 : 0
  649. })
  650. } else if(parsedPath.isWarn){
  651. totalWarnings.directoriesFiles.forEach(function(item, index){
  652. if(item.name == parsedPath.name){
  653. totalWarnings.directoriesFiles[index].isWarn = true;
  654. totalWarnings.directoriesFiles[index].warnCount++;
  655. totalWarnings.directoriesFiles[index].message = 'WARN: already exists on '+ totalWarnings.directoriesFiles[index].warnCount +' hosts';
  656. }
  657. });
  658. }
  659. }, this);
  660. //render all packages for each host
  661. host.Hosts.last_agent_env.rpms.forEach(function(_package){
  662. var parsedPackage = {
  663. name: _package.name,
  664. isWarn: _package.installed,
  665. message: (_package.installed) ? 'WARN: already installed on host' : 'OK'
  666. }
  667. warningsByHost.packages.push(parsedPackage);
  668. // parsing total warnings
  669. if(!totalWarnings.packages.someProperty('name', parsedPackage.name)){
  670. totalWarnings.packages.push({
  671. name:parsedPackage.name,
  672. isWarn: parsedPackage.isWarn,
  673. message: (parsedPackage.isWarn) ? 'WARN: already exists on 1 host': 'OK',
  674. warnCount: (parsedPackage.isWarn) ? 1 : 0
  675. })
  676. } else if(parsedPackage.isWarn){
  677. totalWarnings.packages.forEach(function(item, index){
  678. if(item.name == parsedPackage.name){
  679. totalWarnings.packages[index].isWarn = true;
  680. totalWarnings.packages[index].warnCount++;
  681. totalWarnings.packages[index].message = 'WARN: already exists on '+ totalWarnings.packages[index].warnCount +' hosts';
  682. }
  683. });
  684. }
  685. }, this);
  686. // render all process for each host
  687. host.Hosts.last_agent_env.javaProcs.forEach(function(process){
  688. var parsedProcess = {
  689. user: process.user,
  690. isWarn: process.hadoop,
  691. pid: process.pid,
  692. command: process.command,
  693. shortCommand: (process.command.substr(0, 15)+'...'),
  694. message: (process.hadoop) ? 'WARN: running on host' : 'OK'
  695. }
  696. warningsByHost.processes.push(parsedProcess);
  697. // parsing total warnings
  698. if(!totalWarnings.processes.someProperty('pid', parsedProcess.name)){
  699. totalWarnings.processes.push({
  700. user: process.user,
  701. pid: process.pid,
  702. command: process.command,
  703. shortCommand: (process.command.substr(0, 15)+'...'),
  704. isWarn: parsedProcess.isWarn,
  705. message: (parsedProcess.isWarn) ? 'WARN: running on 1 host': 'OK',
  706. warnCount: (parsedProcess.isWarn) ? 1 : 0
  707. })
  708. } else if(parsedProcess.isWarn){
  709. totalWarnings.processes.forEach(function(item, index){
  710. if(item.pid == parsedProcess.pid){
  711. totalWarnings.processes[index].isWarn = true;
  712. totalWarnings.processes[index].warnCount++;
  713. totalWarnings.processes[index].message = 'WARN: running on '+ totalWarnings.processes[index].warnCount +' hosts';
  714. }
  715. });
  716. }
  717. }, this);
  718. warningsByHost.directoriesFiles.sort(sortingFunc);
  719. warningsByHost.packages.sort(sortingFunc);
  720. warnings.push(warningsByHost);
  721. }, this);
  722. totalWarnings.directoriesFiles.sort(sortingFunc);
  723. totalWarnings.packages.sort(sortingFunc);
  724. warnings.unshift(totalWarnings);
  725. this.set('warnings', warnings);
  726. },
  727. /**
  728. * open popup that contain hosts' warnings
  729. * @param event
  730. */
  731. hostWarningsPopup: function(event){
  732. var self = this;
  733. App.ModalPopup.show({
  734. header: Em.I18n.t('installer.step3.warnings.popup.header'),
  735. secondary: 'Rerun Checks',
  736. primary: 'Close',
  737. onPrimary: function () {
  738. self.set('checksUpdateStatus', null);
  739. this.hide();
  740. },
  741. onClose: function(){
  742. self.set('checksUpdateStatus', null);
  743. this.hide();
  744. },
  745. onSecondary: function() {
  746. self.rerunChecks();
  747. },
  748. footerClass: Ember.View.extend({
  749. template: Ember.Handlebars.compile([
  750. '<div class="update-progress pull-left">',
  751. '{{#if view.isUpdateInProgress}}',
  752. '<div class="progress-info active progress">',
  753. '<div class="bar" {{bindAttr style="view.progressWidth"}}></div></div>',
  754. '{{else}}<label {{bindAttr class="view.updateStatusClass"}}>{{view.updateStatus}}</label>',
  755. '{{/if}}</div>',
  756. '{{#if view.parentView.secondary}}<button type="button" class="btn btn-info" {{bindAttr disabled="view.isUpdateInProgress"}} {{action onSecondary target="view.parentView"}}><i class="icon-repeat"></i>&nbsp;{{view.parentView.secondary}}</button>{{/if}}',
  757. '{{#if view.parentView.primary}}<button type="button" class="btn" {{action onPrimary target="view.parentView"}}>{{view.parentView.primary}}</button>{{/if}}'
  758. ].join('')),
  759. classNames: ['modal-footer', 'host-checks-update'],
  760. progressWidth: function(){
  761. return 'width:'+App.router.get('wizardStep3Controller.checksUpdateProgress')+'%';
  762. }.property('App.router.wizardStep3Controller.checksUpdateProgress'),
  763. isUpdateInProgress: function(){
  764. if((App.router.get('wizardStep3Controller.checksUpdateProgress') > 0) &&
  765. (App.router.get('wizardStep3Controller.checksUpdateProgress') < 100)){
  766. return true;
  767. }
  768. }.property('App.router.wizardStep3Controller.checksUpdateProgress'),
  769. updateStatusClass:function(){
  770. var status = App.router.get('wizardStep3Controller.checksUpdateStatus');
  771. if(status === 'SUCCESS'){
  772. return 'text-success';
  773. } else if(status === 'FAILED'){
  774. return 'text-error';
  775. } else {
  776. return null;
  777. }
  778. }.property('App.router.wizardStep3Controller.checksUpdateStatus'),
  779. updateStatus:function(){
  780. var status = App.router.get('wizardStep3Controller.checksUpdateStatus');
  781. if(status === 'SUCCESS'){
  782. return Em.I18n.t('installer.step3.warnings.updateChecks.success');
  783. } else if(status === 'FAILED'){
  784. return Em.I18n.t('installer.step3.warnings.updateChecks.failed');
  785. } else {
  786. return null;
  787. }
  788. }.property('App.router.wizardStep3Controller.checksUpdateStatus')
  789. }),
  790. bodyClass: Ember.View.extend({
  791. templateName: require('templates/wizard/step3_host_warnings_popup'),
  792. warnings: function(){
  793. return App.router.get('wizardStep3Controller.warnings');
  794. }.property('App.router.wizardStep3Controller.warnings'),
  795. categories: function(){
  796. var categories = this.get('warnings').getEach('hostName');
  797. return categories;
  798. }.property('warnings'),
  799. category: 'All Hosts',
  800. content: function(){
  801. return this.get('warnings').findProperty('hostName', this.get('category'));
  802. }.property('category', 'warnings'),
  803. /**
  804. * generate detailed content to show it in new window
  805. */
  806. contentInDetails: function(){
  807. var content = this.get('content');
  808. var newContent = '';
  809. if(content.hostName == 'All Hosts'){
  810. newContent += '<h4>Warnings across all hosts</h4>';
  811. } else {
  812. newContent += '<h4>Warnings on ' + content.hostName + '</h4>';
  813. }
  814. newContent += '<div>DIRECTORIES AND FILES</div><div>';
  815. content.directoriesFiles.filterProperty('isWarn', true).forEach(function(path){
  816. newContent += path.name + '&nbsp;'
  817. });
  818. if(content.directoriesFiles.filterProperty('isWarn', true).length == 0){
  819. newContent += 'No warnings';
  820. }
  821. newContent += '</div><br/><div>PACKAGES</div><div>';
  822. content.packages.filterProperty('isWarn', true).forEach(function(_package){
  823. newContent += _package.name + '&nbsp;'
  824. });
  825. if(content.packages.filterProperty('isWarn', true).length == 0){
  826. newContent += 'No warnings';
  827. }
  828. newContent += '</div><br/><div>PROCESSES</div><div>';
  829. content.processes.filterProperty('isWarn', true).forEach(function(process, index){
  830. newContent += '(' + content.hostName + ',' + process.pid + ',' + process.user + ')';
  831. newContent += (index != (content.processes.filterProperty('isWarn', true).length-1)) ? ',' : '';
  832. })
  833. if(content.processes.filterProperty('isWarn', true).length == 0){
  834. newContent += 'No warnings';
  835. }
  836. return newContent;
  837. }.property('content'),
  838. /**
  839. * open new browser tab with detailed content
  840. */
  841. openWarningsInDialog: function(){
  842. var newWindow = window.open('', this.get('category')+' warnings');
  843. var newDocument = newWindow.document;
  844. newDocument.write(this.get('contentInDetails'));
  845. newWindow.focus();
  846. }
  847. })
  848. })
  849. },
  850. // TODO: dummy button. Remove this after the hook up with actual REST API.
  851. mockBtn: function () {
  852. this.set('isSubmitDisabled', false);
  853. this.hosts.clear();
  854. var hostInfo = this.mockData;
  855. this.renderHosts(hostInfo);
  856. },
  857. pollBtn: function () {
  858. if (this.get('isSubmitDisabled')) {
  859. return;
  860. }
  861. var hosts = this.get('visibleHosts');
  862. var selectedHosts = hosts.filterProperty('isChecked', true);
  863. selectedHosts.forEach(function (_host) {
  864. console.log('Retrying: ' + _host.name);
  865. });
  866. var mockHosts = this.mockRetryData;
  867. mockHosts.forEach(function (_host) {
  868. console.log('Retrying: ' + _host.name);
  869. });
  870. if (this.parseHostInfo(mockHosts, selectedHosts)) {
  871. // this.saveHostInfoToDb();
  872. }
  873. }
  874. });