step3_controller.js 8.1 KB

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