step3_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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, index) {
  241. // Change name of first host for test mode.
  242. if (App.testMode === true) {
  243. if (index == 0) {
  244. _host.set('name', 'localhost.localdomain');
  245. }
  246. }
  247. if (jsonData.items.someProperty('Hosts.host_name', _host.name)) {
  248. if (_host.get('bootStatus') != 'REGISTERED') {
  249. _host.set('bootStatus', 'REGISTERED');
  250. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistration with the server succeeded.');
  251. }
  252. } else if (_host.get('bootStatus') == 'FAILED') {
  253. // ignore FAILED hosts
  254. } else {
  255. // there are some hosts that are not REGISTERED or FAILED
  256. // we need to keep polling
  257. allRegistered = false;
  258. if (_host.get('bootStatus') != 'REGISTERING') {
  259. _host.set('bootStatus', 'REGISTERING');
  260. currentBootLog = _host.get('bootLog') != null ? _host.get('bootLog') : '';
  261. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistering with the server...');
  262. }
  263. }
  264. }, this);
  265. if (allRegistered) {
  266. self.getHostInfo();
  267. } else if (self.get('maxRegistrationAttempts') - self.get('registrationAttempts') >= 0) {
  268. self.set('registrationAttempts', self.get('registrationAttempts') + 1);
  269. window.setTimeout(function () {
  270. self.isHostsRegistered();
  271. }, 3000);
  272. } else {
  273. // maxed out on registration attempts. mark all REGISTERING hosts to FAILED
  274. hosts.filterProperty('bootStatus', 'REGISTERING').forEach(function (_host) {
  275. _host.set('bootStatus', 'FAILED');
  276. _host.set('bootLog', (_host.get('bootLog') != null ? _host.get('bootLog') : '') + '\nRegistration with the server failed.');
  277. });
  278. self.getHostInfo();
  279. }
  280. },
  281. error: function () {
  282. console.log('Error: Getting registered host information from the server');
  283. },
  284. statusCode: require('data/statusCodes')
  285. });
  286. },
  287. registerErrPopup: function (header, message) {
  288. App.ModalPopup.show({
  289. header: header,
  290. secondary: false,
  291. onPrimary: function () {
  292. this.hide();
  293. },
  294. bodyClass: Ember.View.extend({
  295. template: Ember.Handlebars.compile(['<p>{{view.message}}</p>'].join('\n')),
  296. message: message
  297. })
  298. });
  299. },
  300. /**
  301. * Get disk info and cpu count of booted hosts from server
  302. */
  303. getHostInfo: function () {
  304. var self = this;
  305. var kbPerGb = 1024;
  306. var hosts = this.get('bootHosts');
  307. var url = App.testMode ? '/data/wizard/bootstrap/single_host_information.json' : App.apiPrefix + '/hosts?fields=Hosts/total_mem,Hosts/cpu_count';
  308. var method = 'GET';
  309. $.ajax({
  310. type: 'GET',
  311. url: url,
  312. contentType: 'application/json',
  313. timeout: App.timeout,
  314. success: function (data) {
  315. var jsonData;
  316. if (App.testMode) {
  317. jsonData = data;
  318. } else {
  319. jsonData = jQuery.parseJSON(data);
  320. }
  321. hosts.forEach(function (_host) {
  322. var host = jsonData.items.findProperty('Hosts.host_name', _host.name);
  323. if (host) {
  324. _host.cpu = host.Hosts.cpu_count;
  325. _host.memory = ((parseInt(host.Hosts.total_mem))).toFixed(2);
  326. console.log("The value of memory is: " + _host.memory);
  327. }
  328. });
  329. self.set('bootHosts', hosts);
  330. console.log("The value of hosts: " + JSON.stringify(hosts));
  331. self.stopRegistration();
  332. },
  333. error: function () {
  334. console.log('INFO: Getting host information(cpu_count and total_mem) from the server failed');
  335. self.registerErrPopup(Em.I18n.t('installer.step3.hostInformation.popup.header'), Em.I18n.t('installer.step3.hostInformation.popup.body'));
  336. },
  337. statusCode: require('data/statusCodes')
  338. });
  339. },
  340. stopRegistration: function () {
  341. this.set('isSubmitDisabled', false);
  342. },
  343. submit: function () {
  344. if (!this.get('isSubmitDisabled')) {
  345. this.set('content.hostsInfo', this.get('bootHosts'));
  346. App.router.send('next');
  347. }
  348. },
  349. hostLogPopup: function (event, context) {
  350. var host = event.context;
  351. App.ModalPopup.show({
  352. header: Em.I18n.t('installer.step3.hostLog.popup.header').format(host.get('name')),
  353. secondary: null,
  354. onPrimary: function () {
  355. this.hide();
  356. },
  357. bodyClass: Ember.View.extend({
  358. templateName: require('templates/wizard/step3_host_log_popup'),
  359. host: host,
  360. didInsertElement: function () {
  361. var self = this;
  362. var button = $(this.get('element')).find('.textTrigger');
  363. button.click(function () {
  364. if(self.get('isTextArea')){
  365. $(this).text('click to highlight');
  366. } else {
  367. $(this).text('press CTRL+C');
  368. }
  369. self.set('isTextArea', !self.get('isTextArea'));
  370. });
  371. $(this.get('element')).find('.content-area').mouseenter(
  372. function () {
  373. var element = $(this);
  374. element.css('border', '1px solid #dcdcdc');
  375. button.css('visibility', 'visible');
  376. }).mouseleave(
  377. function () {
  378. var element = $(this);
  379. element.css('border', 'none');
  380. button.css('visibility', 'hidden');
  381. })
  382. },
  383. isTextArea: false,
  384. textArea: Em.TextArea.extend({
  385. didInsertElement: function(){
  386. var element = $(this.get('element'));
  387. element.width($(this.get('parentView').get('element')).width() - 10);
  388. element.height($(this.get('parentView').get('element')).height());
  389. element.select();
  390. element.css('resize', 'none');
  391. },
  392. readOnly: true,
  393. value: function(){
  394. return this.get('content');
  395. }.property('content')
  396. })
  397. })
  398. });
  399. },
  400. // TODO: dummy button. Remove this after the hook up with actual REST API.
  401. mockBtn: function () {
  402. this.set('isSubmitDisabled', false);
  403. this.hosts.clear();
  404. var hostInfo = this.mockData;
  405. this.renderHosts(hostInfo);
  406. },
  407. pollBtn: function () {
  408. if (this.get('isSubmitDisabled')) {
  409. return;
  410. }
  411. var hosts = this.get('visibleHosts');
  412. var selectedHosts = hosts.filterProperty('isChecked', true);
  413. selectedHosts.forEach(function (_host) {
  414. console.log('Retrying: ' + _host.name);
  415. });
  416. var mockHosts = this.mockRetryData;
  417. mockHosts.forEach(function (_host) {
  418. console.log('Retrying: ' + _host.name);
  419. });
  420. if (this.parseHostInfo(mockHosts, selectedHosts)) {
  421. // this.saveHostInfoToDb();
  422. }
  423. }
  424. });