step3_controller.js 16 KB

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