step3_controller.js 15 KB

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