step3_controller.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. isSubmitDisabled: false,
  25. categories: ['All Hosts', 'Success', 'Error'],
  26. category: 'All Hosts',
  27. allChecked: false,
  28. onAllChecked: function () {
  29. var hosts = this.get('visibleHosts');
  30. hosts.setEach('isChecked', this.get('allChecked'));
  31. }.observes('allChecked'),
  32. noHostsSelected: function () {
  33. return !(this.hosts.someProperty('isChecked', true));
  34. }.property('hosts.@each.isChecked'),
  35. mockData: require('data/mock/step3_hosts'),
  36. mockRetryData: require('data/mock/step3_pollData'),
  37. navigateStep: function () {
  38. this.loadStep();
  39. if (App.db.getInstallType().installType !== 'manual') {
  40. if (App.db.getBootStatus() === false) {
  41. this.startBootstrap();
  42. }
  43. } else {
  44. // TODO: assume manually bootstrapped hosts are all successful for now
  45. this.get('hosts').forEach(function (_host) {
  46. _host.set('bootStatus', 'DONE');
  47. _host.set('bootLog', 'Success');
  48. });
  49. }
  50. },
  51. clearStep: function () {
  52. this.hosts.clear();
  53. },
  54. loadStep: function () {
  55. console.log("TRACE: Loading step3: Confirm Hosts");
  56. this.clearStep();
  57. var hosts = this.loadHosts();
  58. // hosts.setEach('bootStatus', 'RUNNING');
  59. this.renderHosts(hosts);
  60. },
  61. /* Loads the hostinfo from localStorage on the insertion of view. It's being called from view */
  62. loadHosts: function () {
  63. var hostInfo = [];
  64. hostInfo = this.get('content.hostsInfo');
  65. var hosts = new Ember.Set();
  66. for (var index in hostInfo) {
  67. hosts.add(hostInfo[index]);
  68. console.log("TRACE: host name is: " + hostInfo[index].name);
  69. }
  70. return hosts;
  71. },
  72. /* Renders the set of passed hosts */
  73. renderHosts: function (hostsInfo) {
  74. var self = this;
  75. hostsInfo.forEach(function (_hostInfo) {
  76. var hostInfo = App.HostInfo.create({
  77. name: _hostInfo.name,
  78. bootStatus: _hostInfo.bootStatus,
  79. isChecked: false
  80. });
  81. console.log('pushing ' + hostInfo.name);
  82. self.hosts.pushObject(hostInfo);
  83. });
  84. },
  85. /**
  86. * Parses and updates the content based on bootstrap API response.
  87. * Returns true if polling should continue (some hosts are in "RUNNING" state); false otherwise
  88. */
  89. parseHostInfo: function (hostsStatusFromServer) {
  90. hostsStatusFromServer.forEach(function (_hostStatus) {
  91. var host = this.get('bootHosts').findProperty('name', _hostStatus.hostName);
  92. if (host !== null && host !== undefined) { // check if hostname extracted from REST API data matches any hostname in content
  93. host.set('bootStatus', _hostStatus.status);
  94. host.set('bootLog', _hostStatus.log);
  95. }
  96. }, this);
  97. // if the data rendered by REST API has hosts in "RUNNING" state, polling will continue
  98. return this.get('bootHosts').length != 0 && this.get('bootHosts').someProperty('bootStatus', 'RUNNING');
  99. },
  100. /* Returns the current set of visible hosts on view (All, Succeeded, Failed) */
  101. visibleHosts: function () {
  102. if (this.get('category') === 'Success') {
  103. return (this.hosts.filterProperty('bootStatus', 'DONE'));
  104. } else if (this.get('category') === 'Error') {
  105. return (this.hosts.filterProperty('bootStatus', 'FAILED'));
  106. } else { // if (this.get('category') === 'All Hosts')
  107. return this.hosts;
  108. }
  109. }.property('category', 'hosts.@each.bootStatus'),
  110. removeHosts: function (hosts) {
  111. var self = this;
  112. App.ModalPopup.show({
  113. header: Em.I18n.t('installer.step3.hosts.remove.popup.header'),
  114. onPrimary: function () {
  115. App.router.send('removeHosts', hosts);
  116. self.hosts.removeObjects(hosts);
  117. this.hide();
  118. },
  119. body: Em.I18n.t('installer.step3.hosts.remove.popup.body')
  120. });
  121. },
  122. /* Removes a single element on the trash icon click. Called from View */
  123. removeHost: function (hostInfo) {
  124. this.removeHosts([hostInfo]);
  125. },
  126. removeSelectedHosts: function () {
  127. if (!this.get('noHostsSelected')) {
  128. var selectedHosts = this.get('visibleHosts').filterProperty('isChecked', true);
  129. selectedHosts.forEach(function (_hostInfo) {
  130. console.log('Removing: ' + _hostInfo.name);
  131. });
  132. this.removeHosts(selectedHosts);
  133. }
  134. },
  135. retryHosts: function (hosts) {
  136. var self = this;
  137. App.ModalPopup.show({
  138. header: Em.I18n.t('installer.step3.hosts.retry.popup.header'),
  139. onPrimary: function () {
  140. hosts.forEach(function (_host) {
  141. console.log('Retrying: ' + _host.name);
  142. });
  143. //TODO: uncomment below code to hookup with @GET bootstrap API
  144. /*
  145. self.set('bootHosts',selectedHosts);
  146. self.doBootstrap();
  147. */
  148. this.hide();
  149. },
  150. body: Em.I18n.t('installer.step3.hosts.retry.popup.body')
  151. });
  152. },
  153. retryHost: function (hostInfo) {
  154. this.retryHosts([hostInfo]);
  155. },
  156. retrySelectedHosts: function () {
  157. if (!this.get('noHostsSelected')) {
  158. var selectedHosts = this.get('visibleHosts').filterProperty('isChecked', true);
  159. this.retryHosts(selectedHosts);
  160. }
  161. },
  162. numPolls: 0,
  163. startBootstrap: function () {
  164. //this.set('isSubmitDisabled', true); //TODO: uncomment after actual hookup
  165. this.numPolls = 0;
  166. this.set('bootHosts', this.get('hosts'));
  167. this.doBootstrap();
  168. },
  169. doBootstrap: function () {
  170. this.numPolls++;
  171. var self = this;
  172. var url = App.testMode ? '/data/wizard/bootstrap/poll_' + this.numPolls + '.json' : '/api/bootstrap/1';
  173. $.ajax({
  174. type: 'GET',
  175. url: url,
  176. timeout: 5000,
  177. success: function (data) {
  178. if (data.hostsStatus !== null) {
  179. // in case of bootstrapping just one server, the server returns an object rather than an array...
  180. if (!(data.hostsStatus instanceof Array)) {
  181. data.hostsStatus = [ data.hostsStatus ];
  182. }
  183. console.log("TRACE: In success function for the GET bootstrap call");
  184. var result = self.parseHostInfo(data.hostsStatus);
  185. if (result) {
  186. window.setTimeout(function () { self.doBootstrap() }, 3000);
  187. return;
  188. }
  189. }
  190. console.log('Bootstrap failed');
  191. self.stopBootstrap();
  192. },
  193. error: function () {
  194. console.log('Bootstrap failed');
  195. self.stopBootstrap();
  196. },
  197. statusCode: require('data/statusCodes')
  198. });
  199. },
  200. stopBootstrap: function () {
  201. //TODO: uncomment following line after the hook up with the API call
  202. console.log('stopBootstrap() called');
  203. // this.set('isSubmitDisabled',false);
  204. },
  205. submit: function () {
  206. if (!this.get('isSubmitDisabled')) {
  207. this.set('content.hostsInfo', this.get('hosts'));
  208. App.router.send('next');
  209. }
  210. },
  211. hostLogPopup: function (event, context) {
  212. var host = event.context;
  213. App.ModalPopup.show({
  214. header: Em.I18n.t('installer.step3.hostLog.popup.header').format(host.get('name')),
  215. secondary: null,
  216. onPrimary: function () {
  217. this.hide();
  218. },
  219. bodyClass: Ember.View.extend({
  220. templateName: require('templates/wizard/step3_host_log_popup'),
  221. host: host
  222. })
  223. });
  224. },
  225. // TODO: dummy button. Remove this after the hook up with actual REST API.
  226. mockBtn: function () {
  227. this.set('isSubmitDisabled', false);
  228. this.hosts.clear();
  229. var hostInfo = this.mockData;
  230. this.renderHosts(hostInfo);
  231. },
  232. pollBtn: function () {
  233. if (this.get('isSubmitDisabled')) {
  234. return;
  235. }
  236. var hosts = this.get('visibleHosts');
  237. var selectedHosts = hosts.filterProperty('isChecked', true);
  238. selectedHosts.forEach(function (_host) {
  239. console.log('Retrying: ' + _host.name);
  240. });
  241. var mockHosts = this.mockRetryData;
  242. mockHosts.forEach(function (_host) {
  243. console.log('Retrying: ' + _host.name);
  244. });
  245. if (this.parseHostInfo(mockHosts, selectedHosts)) {
  246. // this.saveHostInfoToDb();
  247. }
  248. }
  249. });