step3_controller.js 16 KB

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