step3_controller.js 15 KB

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