step3_controller.js 36 KB

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