step3_controller.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.getBootStatus() === false) {
  40. this.startBootstrap();
  41. }
  42. },
  43. clearStep: function () {
  44. this.hosts.clear();
  45. },
  46. loadStep: function () {
  47. console.log("TRACE: Loading step3: Confirm Hosts");
  48. this.clearStep();
  49. var hosts = this.loadHosts();
  50. // hosts.setEach('bootStatus', 'pending');
  51. this.renderHosts(hosts);
  52. },
  53. /* Loads the hostinfo from localStorage on the insertion of view. It's being called from view */
  54. loadHosts: function () {
  55. var hostInfo = [];
  56. debugger;
  57. hostInfo = this.get('content.hostsInfo');
  58. var hosts = new Ember.Set();
  59. for (var index in hostInfo) {
  60. hosts.add(hostInfo[index]);
  61. console.log("TRACE: host name is: " + hostInfo[index].name);
  62. }
  63. return hosts;
  64. },
  65. /* Renders the set of passed hosts */
  66. renderHosts: function (hostsInfo) {
  67. var self = this;
  68. hostsInfo.forEach(function (_hostInfo) {
  69. var hostInfo = App.HostInfo.create({
  70. name: _hostInfo.name,
  71. bootStatus: _hostInfo.bootStatus,
  72. isChecked: false
  73. });
  74. console.log('pushing ' + hostInfo.name);
  75. self.hosts.pushObject(hostInfo);
  76. });
  77. },
  78. /**
  79. * Parses and updates the content, and governs the possibility
  80. * of the next doBootstrap (polling) call.
  81. * Returns true if polling should stop (no hosts are in "pending" state); false otherwise
  82. */
  83. parseHostInfo: function (hostsFromServer, hostsFromContent) {
  84. var result = true; // default value as true implies
  85. hostsFromServer.forEach(function (_hostFromServer) {
  86. var host = hostsFromContent.findProperty('name', _hostFromServer.name);
  87. if (host !== null && host !== undefined) { // check if hostname extracted from REST API data matches any hostname in content
  88. host.set('bootStatus', _hostFromServer.status);
  89. host.set('cpu', _hostFromServer.cpu);
  90. host.set('memory', _hostFromServer.memory);
  91. }
  92. });
  93. // if the data rendered by REST API has no hosts or no hosts are in "pending" state, polling will stop
  94. return this.hosts.length == 0 || !this.hosts.someProperty('bootStatus', 'pending');
  95. },
  96. /* Returns the current set of visible hosts on view (All, Succeeded, Failed) */
  97. visibleHosts: function () {
  98. if (this.get('category') === 'Success') {
  99. return (this.hosts.filterProperty('bootStatus', 'success'));
  100. } else if (this.get('category') === 'Error') {
  101. return (this.hosts.filterProperty('bootStatus', 'error'));
  102. } else { // if (this.get('category') === 'All Hosts')
  103. return this.hosts;
  104. }
  105. }.property('category', 'hosts.@each.bootStatus'),
  106. removeHosts: function (hosts) {
  107. var self = this;
  108. App.ModalPopup.show({
  109. header: Em.I18n.t('installer.step3.hosts.remove.popup.header'),
  110. onPrimary: function () {
  111. App.router.send('removeHosts', hosts);
  112. self.hosts.removeObjects(hosts);
  113. this.hide();
  114. },
  115. body: Em.I18n.t('installer.step3.hosts.remove.popup.body')
  116. });
  117. },
  118. /* Removes a single element on the trash icon click. Called from View */
  119. removeHost: function (hostInfo) {
  120. this.removeHosts([hostInfo]);
  121. },
  122. removeSelectedHosts: function () {
  123. if (!this.get('noHostsSelected')) {
  124. var selectedHosts = this.get('visibleHosts').filterProperty('isChecked', true);
  125. selectedHosts.forEach(function (_hostInfo) {
  126. console.log('Removing: ' + _hostInfo.name);
  127. });
  128. this.removeHosts(selectedHosts);
  129. }
  130. },
  131. retryHosts: function (hosts) {
  132. var self = this;
  133. App.ModalPopup.show({
  134. header: Em.I18n.t('installer.step3.hosts.retry.popup.header'),
  135. onPrimary: function () {
  136. hosts.forEach(function (_host) {
  137. console.log('Retrying: ' + _host.name);
  138. });
  139. //TODO: uncomment below code to hookup with @GET bootstrap API
  140. /*
  141. self.set('bootHosts',selectedHosts);
  142. self.doBootstrap();
  143. */
  144. this.hide();
  145. },
  146. body: Em.I18n.t('installer.step3.hosts.retry.popup.body')
  147. });
  148. },
  149. startBootstrap: function () {
  150. this.set('isSubmitDisabled', true);
  151. this.set('bootHosts', this.get('content'));
  152. this.doBootstrap();
  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. startBootstrap: function () {
  164. this.set('isSubmitDisabled', true);
  165. this.set('bootHosts', this.get('hosts'));
  166. this.doBootstrap();
  167. },
  168. doBootstrap: function () {
  169. var self = this;
  170. var url = '/api/bootstrap';
  171. $.ajax({
  172. type: 'GET',
  173. url: url,
  174. timeout: 5000,
  175. success: function (data) {
  176. console.log("TRACE: In success function for the GET bootstrap call");
  177. var result = self.parseHostInfo(data, this.get('bootHosts'));
  178. window.setTimeout(self.doBootstrap, 3000);
  179. },
  180. error: function () {
  181. console.log("ERROR");
  182. self.stopBootstrap();
  183. },
  184. statusCode: require('data/statusCodes')
  185. });
  186. },
  187. stopBootstrap: function () {
  188. //TODO: uncomment following line after the hook up with the API call
  189. // this.set('isSubmitDisabled',false);
  190. },
  191. submit: function () {
  192. if (!this.get('isSubmitDisabled')) {
  193. this.set('content.hostsInfo', this.get('hosts'));
  194. App.router.send('next');
  195. }
  196. },
  197. hostLogPopup: function (event) {
  198. App.ModalPopup.show({
  199. header: Em.I18n.t('installer.step3.hostLog.popup.header'),
  200. onPrimary: function () {
  201. this.hide();
  202. },
  203. bodyClass: Ember.View.extend({
  204. templateName: require('templates/wizard/step3HostLogPopup')
  205. })
  206. });
  207. },
  208. // TODO: dummy button. Remove this after the hook up with actual REST API.
  209. mockBtn: function () {
  210. this.set('isSubmitDisabled', false);
  211. this.hosts.clear();
  212. var hostInfo = this.mockData;
  213. this.renderHosts(hostInfo);
  214. },
  215. pollBtn: function () {
  216. if (this.get('isSubmitDisabled')) {
  217. return;
  218. }
  219. var hosts = this.get('visibleHosts');
  220. var selectedHosts = hosts.filterProperty('isChecked', true);
  221. selectedHosts.forEach(function (_host) {
  222. console.log('Retrying: ' + _host.name);
  223. });
  224. var mockHosts = this.mockRetryData;
  225. mockHosts.forEach(function (_host) {
  226. console.log('Retrying: ' + _host.name);
  227. });
  228. if (this.parseHostInfo(mockHosts, selectedHosts)) {
  229. // this.saveHostInfoToDb();
  230. }
  231. }
  232. });