step3_controller.js 7.8 KB

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