step3_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. maxRegistrationAttempts: 20,
  25. registrationAttempts: null,
  26. isSubmitDisabled: true,
  27. categories: ['All Hosts', 'Success', 'Installing', 'Registering', 'Failed'],
  28. category: 'All Hosts',
  29. allChecked: false,
  30. onAllChecked: function () {
  31. var hosts = this.get('visibleHosts');
  32. hosts.setEach('isChecked', this.get('allChecked'));
  33. }.observes('allChecked'),
  34. noHostsSelected: function () {
  35. return !(this.hosts.someProperty('isChecked', true));
  36. }.property('hosts.@each.isChecked'),
  37. isRetryDisabled: function () {
  38. return !(this.get('bootHosts').someProperty('bootStatus', 'FAILED') && !this.get('isSubmitDisabled'));
  39. }.property('bootHosts.@each.bootStatus','isSubmitDisabled'),
  40. mockData: require('data/mock/step3_hosts'),
  41. mockRetryData: require('data/mock/step3_pollData'),
  42. navigateStep: function () {
  43. this.loadStep();
  44. if (this.get('content.hosts.manualInstall') !== true) {
  45. if (App.db.getBootStatus() === false) {
  46. this.startBootstrap();
  47. }
  48. } else {
  49. this.set('bootHosts', this.get('hosts'));
  50. if (App.testMode && App.skipBootstrap) {
  51. this.get('bootHosts').setEach('bootStatus', 'REGISTERED');
  52. this.get('bootHosts').setEach('cpu', '2');
  53. this.get('bootHosts').setEach('memory', '2000000');
  54. this.getHostInfo();
  55. } else {
  56. this.isHostsRegistered();
  57. }
  58. }
  59. },
  60. clearStep: function () {
  61. this.hosts.clear();
  62. this.bootHosts.clear();
  63. this.set('isSubmitDisabled', true);
  64. this.set('registrationAttempts', 1);
  65. },
  66. loadStep: function () {
  67. console.log("TRACE: Loading step3: Confirm Hosts");
  68. if (!this.get('hosts').length) {
  69. this.clearStep();
  70. var hosts = this.loadHosts();
  71. // hosts.setEach('bootStatus', 'RUNNING');
  72. this.renderHosts(hosts);
  73. } else {
  74. this.set('isSubmitDisabled', false);
  75. }
  76. },
  77. /* Loads the hostinfo from localStorage on the insertion of view. It's being called from view */
  78. loadHosts: function () {
  79. var hostInfo = [];
  80. hostInfo = this.get('content.hostsInfo');
  81. var hosts = new Ember.Set();
  82. for (var index in hostInfo) {
  83. hosts.add(hostInfo[index]);
  84. console.log("TRACE: host name is: " + hostInfo[index].name);
  85. }
  86. return hosts;
  87. },
  88. /* Renders the set of passed hosts */
  89. renderHosts: function (hostsInfo) {
  90. var self = this;
  91. hostsInfo.forEach(function (_hostInfo) {
  92. var hostInfo = App.HostInfo.create({
  93. name: _hostInfo.name,
  94. bootStatus: _hostInfo.bootStatus,
  95. isChecked: false
  96. });
  97. console.log('pushing ' + hostInfo.name);
  98. self.hosts.pushObject(hostInfo);
  99. });
  100. },
  101. /**
  102. * Parses and updates the content based on bootstrap API response.
  103. * Returns true if polling should continue (some hosts are in "RUNNING" state); false otherwise
  104. */
  105. parseHostInfo: function (hostsStatusFromServer) {
  106. hostsStatusFromServer.forEach(function (_hostStatus) {
  107. var host = this.get('bootHosts').findProperty('name', _hostStatus.hostName);
  108. if (host !== null && host !== undefined) { // check if hostname extracted from REST API data matches any hostname in content
  109. host.set('bootStatus', _hostStatus.status);
  110. host.set('bootLog', _hostStatus.log);
  111. }
  112. }, this);
  113. // if the data rendered by REST API has hosts in "RUNNING" state, polling will continue
  114. return this.get('bootHosts').length != 0 && this.get('bootHosts').someProperty('bootStatus', 'RUNNING');
  115. },
  116. /* Returns the current set of visible hosts on view (All, Succeeded, Failed) */
  117. visibleHosts: function () {
  118. if (this.get('category') === 'Success') {
  119. return (this.hosts.filterProperty('bootStatus', 'REGISTERED'));
  120. } else if (this.get('category') === 'Installing') {
  121. return (this.hosts.filterProperty('bootStatus', 'RUNNING'));
  122. } else if (this.get('category') === 'Registering') {
  123. return (this.hosts.filter(function(host) {
  124. return host.bootStatus == 'DONE' || host.bootStatus == 'REGISTERING';
  125. }));
  126. } else if (this.get('category') === 'Failed') {
  127. return (this.hosts.filterProperty('bootStatus', 'FAILED'));
  128. } else { // if (this.get('category') === 'All Hosts')
  129. return this.hosts;
  130. }
  131. }.property('category', 'hosts.@each.bootStatus'),
  132. removeHosts: function (hosts) {
  133. var self = this;
  134. App.ModalPopup.show({
  135. header: Em.I18n.t('installer.step3.hosts.remove.popup.header'),
  136. onPrimary: function () {
  137. App.router.send('removeHosts', hosts);
  138. self.hosts.removeObjects(hosts);
  139. if (!self.hosts.length) {
  140. self.set('isSubmitDisabled', true);
  141. }
  142. this.hide();
  143. },
  144. body: Em.I18n.t('installer.step3.hosts.remove.popup.body')
  145. });
  146. },
  147. /* Removes a single element on the trash icon click. Called from View */
  148. removeHost: function (hostInfo) {
  149. this.removeHosts([hostInfo]);
  150. },
  151. removeSelectedHosts: function () {
  152. if (!this.get('noHostsSelected')) {
  153. var selectedHosts = this.get('visibleHosts').filterProperty('isChecked', true);
  154. selectedHosts.forEach(function (_hostInfo) {
  155. console.log('Removing: ' + _hostInfo.name);
  156. });
  157. this.removeHosts(selectedHosts);
  158. }
  159. },
  160. retryHost: function (hostInfo) {
  161. this.retryHosts([hostInfo]);
  162. },
  163. retryHosts: function (hosts) {
  164. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('content.hosts.sshKey'), hosts: hosts.mapProperty('name')});
  165. this.numPolls = 0;
  166. this.set('registrationAttempts', null);
  167. if (this.get('content.hosts.manualInstall') !== true) {
  168. var requestId = App.router.get('installerController').launchBootstrap(bootStrapData);
  169. this.set('content.hosts.bootRequestId', requestId);
  170. this.doBootstrap();
  171. } else {
  172. this.isHostsRegistered();
  173. }
  174. },
  175. retrySelectedHosts: function () {
  176. if (!this.get('isRetryDisabled')) {
  177. var selectedHosts = this.get('bootHosts').filterProperty('bootStatus', 'FAILED');
  178. selectedHosts.forEach(function (_host) {
  179. _host.set('bootStatus', 'RUNNING');
  180. _host.set('bootLog', 'Retrying ...');
  181. }, this);
  182. this.retryHosts(selectedHosts);
  183. }
  184. },
  185. numPolls: 0,
  186. startBootstrap: function () {
  187. //this.set('isSubmitDisabled', true); //TODO: uncomment after actual hookup
  188. this.numPolls = 0;
  189. this.set('bootHosts', this.get('hosts'));
  190. this.doBootstrap();
  191. },
  192. doBootstrap: function () {
  193. this.numPolls++;
  194. var self = this;
  195. var url = App.testMode ? '/data/wizard/bootstrap/poll_' + this.numPolls + '.json' : App.apiPrefix + '/bootstrap/' + this.get('content.hosts.bootRequestId');
  196. $.ajax({
  197. type: 'GET',
  198. url: url,
  199. timeout: App.timeout,
  200. success: function (data) {
  201. if (data.hostsStatus !== null) {
  202. // in case of bootstrapping just one host, the server returns an object rather than an array...
  203. if (!(data.hostsStatus instanceof Array)) {
  204. data.hostsStatus = [ data.hostsStatus ];
  205. }
  206. console.log("TRACE: In success function for the GET bootstrap call");
  207. var result = self.parseHostInfo(data.hostsStatus);
  208. if (result) {
  209. window.setTimeout(function () {
  210. self.doBootstrap()
  211. }, 3000);
  212. return;
  213. }
  214. }
  215. console.log('Bootstrap failed');
  216. self.stopBootstrap();
  217. },
  218. error: function () {
  219. console.log('Bootstrap failed');
  220. self.stopBootstrap();
  221. },
  222. statusCode: require('data/statusCodes')
  223. });
  224. },
  225. stopBootstrap: function () {
  226. //TODO: uncomment following line after the hook up with the API call
  227. console.log('stopBootstrap() called');
  228. // this.set('isSubmitDisabled',false);
  229. Ember.run.later(this, function () {
  230. this.startRegistration();
  231. }, 1000);
  232. },
  233. startRegistration: function () {
  234. this.isHostsRegistered();
  235. },
  236. isHostsRegistered: function () {
  237. var self = this;
  238. var hosts = this.get('bootHosts');
  239. var url = App.testMode ? '/data/wizard/bootstrap/single_host_registration.json' : App.apiPrefix + '/hosts';
  240. var method = 'GET';
  241. $.ajax({
  242. type: 'GET',
  243. url: url,
  244. timeout: App.timeout,
  245. success: function (data) {
  246. console.log('registration attempt #' + self.get('registrationAttempts'));
  247. var jsonData = App.testMode ? data : jQuery.parseJSON(data);
  248. if (!jsonData) {
  249. console.log("Error: jsonData is null");
  250. return;
  251. }
  252. // keep polling until all hosts are registered
  253. var allRegistered = true;
  254. hosts.forEach(function (_host, index) {
  255. // Change name of first host for test mode.
  256. if (App.testMode === true) {
  257. if (index == 0) {
  258. _host.set('name', 'localhost.localdomain');
  259. }
  260. }
  261. if (jsonData.items.someProperty('Hosts.host_name', _host.name)) {
  262. if (_host.get('bootStatus') != 'REGISTERED') {
  263. _host.set('bootStatus', 'REGISTERED');
  264. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistration with the server succeeded.');
  265. }
  266. } else if (_host.get('bootStatus') == 'FAILED') {
  267. // ignore FAILED hosts
  268. } else {
  269. // there are some hosts that are not REGISTERED or FAILED
  270. // we need to keep polling
  271. allRegistered = false;
  272. if (_host.get('bootStatus') != 'REGISTERING') {
  273. _host.set('bootStatus', 'REGISTERING');
  274. currentBootLog = _host.get('bootLog') != null ? _host.get('bootLog') : '';
  275. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistering with the server...');
  276. }
  277. }
  278. }, this);
  279. if (allRegistered) {
  280. self.getHostInfo();
  281. } else if (self.get('maxRegistrationAttempts') - self.get('registrationAttempts') >= 0) {
  282. self.set('registrationAttempts', self.get('registrationAttempts') + 1);
  283. window.setTimeout(function () {
  284. self.isHostsRegistered();
  285. }, 3000);
  286. } else {
  287. // maxed out on registration attempts. mark all REGISTERING hosts to FAILED
  288. hosts.filterProperty('bootStatus', 'REGISTERING').forEach(function (_host) {
  289. _host.set('bootStatus', 'FAILED');
  290. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistration with the server failed.');
  291. });
  292. self.getHostInfo();
  293. }
  294. },
  295. error: function () {
  296. console.log('Error: Getting registered host information from the server');
  297. },
  298. statusCode: require('data/statusCodes')
  299. });
  300. },
  301. registerErrPopup: function (header, message) {
  302. App.ModalPopup.show({
  303. header: header,
  304. secondary: false,
  305. onPrimary: function () {
  306. this.hide();
  307. },
  308. bodyClass: Ember.View.extend({
  309. template: Ember.Handlebars.compile(['<p>{{view.message}}</p>'].join('\n')),
  310. message: message
  311. })
  312. });
  313. },
  314. /**
  315. * Get disk info and cpu count of booted hosts from server
  316. */
  317. getHostInfo: function () {
  318. var self = this;
  319. var kbPerGb = 1024;
  320. var hosts = this.get('bootHosts');
  321. var url = App.testMode ? '/data/wizard/bootstrap/single_host_information.json' : App.apiPrefix + '/hosts?fields=Hosts/total_mem,Hosts/cpu_count';
  322. var method = 'GET';
  323. $.ajax({
  324. type: 'GET',
  325. url: url,
  326. contentType: 'application/json',
  327. timeout: App.timeout,
  328. success: function (data) {
  329. var jsonData = App.testMode ? data : jQuery.parseJSON(data);
  330. hosts.forEach(function (_host) {
  331. var host = jsonData.items.findProperty('Hosts.host_name', _host.name);
  332. if (host) {
  333. _host.cpu = host.Hosts.cpu_count;
  334. _host.memory = ((parseInt(host.Hosts.total_mem))).toFixed(2);
  335. console.log("The value of memory is: " + _host.memory);
  336. }
  337. });
  338. self.set('bootHosts', hosts);
  339. console.log("The value of hosts: " + JSON.stringify(hosts));
  340. self.stopRegistration();
  341. },
  342. error: function () {
  343. console.log('INFO: Getting host information(cpu_count and total_mem) from the server failed');
  344. self.registerErrPopup(Em.I18n.t('installer.step3.hostInformation.popup.header'), Em.I18n.t('installer.step3.hostInformation.popup.body'));
  345. },
  346. statusCode: require('data/statusCodes')
  347. });
  348. },
  349. stopRegistration: function () {
  350. this.set('isSubmitDisabled', false);
  351. },
  352. submit: function () {
  353. if (!this.get('isSubmitDisabled')) {
  354. this.set('content.hostsInfo', this.get('bootHosts'));
  355. App.router.send('next');
  356. }
  357. },
  358. hostLogPopup: function (event, context) {
  359. var host = event.context;
  360. App.ModalPopup.show({
  361. header: Em.I18n.t('installer.step3.hostLog.popup.header').format(host.get('name')),
  362. secondary: null,
  363. onPrimary: function () {
  364. this.hide();
  365. },
  366. bodyClass: Ember.View.extend({
  367. templateName: require('templates/wizard/step3_host_log_popup'),
  368. host: host,
  369. didInsertElement: function () {
  370. var self = this;
  371. var button = $(this.get('element')).find('.textTrigger');
  372. button.click(function () {
  373. if (self.get('isTextArea')) {
  374. $(this).text('click to highlight');
  375. } else {
  376. $(this).text('press CTRL+C');
  377. }
  378. self.set('isTextArea', !self.get('isTextArea'));
  379. });
  380. $(this.get('element')).find('.content-area').mouseenter(
  381. function () {
  382. var element = $(this);
  383. element.css('border', '1px solid #dcdcdc');
  384. button.css('visibility', 'visible');
  385. }).mouseleave(
  386. function () {
  387. var element = $(this);
  388. element.css('border', 'none');
  389. button.css('visibility', 'hidden');
  390. })
  391. },
  392. isTextArea: false,
  393. textArea: Em.TextArea.extend({
  394. didInsertElement: function () {
  395. var element = $(this.get('element'));
  396. element.width($(this.get('parentView').get('element')).width() - 10);
  397. element.height($(this.get('parentView').get('element')).height());
  398. element.select();
  399. element.css('resize', 'none');
  400. },
  401. readOnly: true,
  402. value: function () {
  403. return this.get('content');
  404. }.property('content')
  405. })
  406. })
  407. });
  408. },
  409. // TODO: dummy button. Remove this after the hook up with actual REST API.
  410. mockBtn: function () {
  411. this.set('isSubmitDisabled', false);
  412. this.hosts.clear();
  413. var hostInfo = this.mockData;
  414. this.renderHosts(hostInfo);
  415. },
  416. pollBtn: function () {
  417. if (this.get('isSubmitDisabled')) {
  418. return;
  419. }
  420. var hosts = this.get('visibleHosts');
  421. var selectedHosts = hosts.filterProperty('isChecked', true);
  422. selectedHosts.forEach(function (_host) {
  423. console.log('Retrying: ' + _host.name);
  424. });
  425. var mockHosts = this.mockRetryData;
  426. mockHosts.forEach(function (_host) {
  427. console.log('Retrying: ' + _host.name);
  428. });
  429. if (this.parseHostInfo(mockHosts, selectedHosts)) {
  430. // this.saveHostInfoToDb();
  431. }
  432. }
  433. });