step3_controller.js 12 KB

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