step3_controller.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. retryHost: function (hostInfo) {
  150. this.retryHosts([hostInfo]);
  151. },
  152. retrySelectedHosts: function () {
  153. if (!this.get('noHostsSelected')) {
  154. var selectedHosts = this.get('visibleHosts').filterProperty('isChecked', true);
  155. this.retryHosts(selectedHosts);
  156. }
  157. },
  158. startBootstrap: function () {
  159. // this.set('isSubmitDisabled', true);
  160. this.set('bootHosts', this.get('hosts'));
  161. this.doBootstrap();
  162. },
  163. doBootstrap: function () {
  164. var self = this;
  165. var url = '/api/bootstrap';
  166. $.ajax({
  167. type: 'GET',
  168. url: url,
  169. timeout: 5000,
  170. success: function (data) {
  171. console.log("TRACE: In success function for the GET bootstrap call");
  172. var result = self.parseHostInfo(data, this.get('bootHosts'));
  173. window.setTimeout(self.doBootstrap, 3000);
  174. },
  175. error: function () {
  176. console.log("ERROR");
  177. self.stopBootstrap();
  178. },
  179. statusCode: require('data/statusCodes')
  180. });
  181. },
  182. stopBootstrap: function () {
  183. //TODO: uncomment following line after the hook up with the API call
  184. // this.set('isSubmitDisabled',false);
  185. },
  186. submit: function () {
  187. if (!this.get('isSubmitDisabled')) {
  188. this.set('content.hostsInfo', this.get('hosts'));
  189. App.router.send('next');
  190. }
  191. },
  192. hostLogPopup: function (event) {
  193. App.ModalPopup.show({
  194. header: Em.I18n.t('installer.step3.hostLog.popup.header'),
  195. onPrimary: function () {
  196. this.hide();
  197. },
  198. bodyClass: Ember.View.extend({
  199. templateName: require('templates/wizard/step3HostLogPopup')
  200. })
  201. });
  202. },
  203. // TODO: dummy button. Remove this after the hook up with actual REST API.
  204. mockBtn: function () {
  205. this.set('isSubmitDisabled', false);
  206. this.hosts.clear();
  207. var hostInfo = this.mockData;
  208. this.renderHosts(hostInfo);
  209. },
  210. pollBtn: function () {
  211. if (this.get('isSubmitDisabled')) {
  212. return;
  213. }
  214. var hosts = this.get('visibleHosts');
  215. var selectedHosts = hosts.filterProperty('isChecked', true);
  216. selectedHosts.forEach(function (_host) {
  217. console.log('Retrying: ' + _host.name);
  218. });
  219. var mockHosts = this.mockRetryData;
  220. mockHosts.forEach(function (_host) {
  221. console.log('Retrying: ' + _host.name);
  222. });
  223. if (this.parseHostInfo(mockHosts, selectedHosts)) {
  224. // this.saveHostInfoToDb();
  225. }
  226. }
  227. });