step3_controller.js 15 KB

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