step3_controller.js 13 KB

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