step3_controller.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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: ['Hosts', 'Succeeded', 'Failed'],
  25. category: 'Hosts',
  26. allChecked: true,
  27. onAllChecked: function () {
  28. var hosts = this.visibleHosts();
  29. if (this.get('allChecked') === true) {
  30. hosts.setEach('isChecked', true);
  31. } else {
  32. hosts.setEach('isChecked', false);
  33. }
  34. }.observes('allChecked'),
  35. mockData: require('data/mock/step3_hosts'),
  36. mockRetryData: require('data/mock/step3_pollData'),
  37. navigateStep: function () {
  38. if (App.router.get('isFwdNavigation') === true && !App.router.get('backBtnForHigherStep')) {
  39. this.loadStep('pending');
  40. this.startBootstrap();
  41. }
  42. App.router.set('backBtnForHigherStep', false);
  43. },
  44. loadStep: function (bootStatus) {
  45. console.log("TRACE: Loading step3: Confirm Hosts");
  46. this.clear();
  47. var hosts = this.loadHosts();
  48. if(bootStatus === 'pending') {
  49. hosts.setEach('bootStatus','pending');
  50. }
  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 = App.db.getHosts();
  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. });
  72. console.log('pushing ' + hostInfo.name);
  73. self.content.pushObject(hostInfo);
  74. });
  75. },
  76. /* Below function parses and updates the content, and governs the possibility
  77. of the next doBootstrap (polling) call
  78. */
  79. parseHostInfo: function (hostsFrmServer, hostsFrmContent) {
  80. var result = true; // default value as true implies if the data rendered by REST API has no hosts, polling will stop
  81. hostsFrmServer.forEach(function (_hostFrmServer) {
  82. var host = hostsFrmContent.findProperty('name', _hostFrmServer.name);
  83. if (host !== null && host !== undefined) { // check if hostname extracted from REST API data matches any hostname in content
  84. host.set('bootStatus', _hostFrmServer.status);
  85. host.set('cpu', _hostFrmServer.cpu);
  86. host.set('memory', _hostFrmServer.memory);
  87. }
  88. });
  89. result = !this.content.someProperty('bootStatus', 'pending');
  90. return result;
  91. },
  92. /* Below function returns the current set of visible hosts on view (All,succeded,failed) */
  93. visibleHosts: function () {
  94. var result;
  95. if (this.get('category') === 'Succeeded') {
  96. return (this.filterProperty('bootStatus', 'success'));
  97. } else if (this.get('category') === 'Failed') {
  98. return (this.filterProperty('bootStatus', 'error'));
  99. } else if (this.get('category') === 'Hosts') {
  100. return this.content;
  101. }
  102. },
  103. /* Below function removes a single element on the trash icon click. Being called from view */
  104. removeElement: function (hostInfo) {
  105. console.log('TRACE: In removeElement');
  106. var hosts = [hostInfo];
  107. App.db.removeHosts(hosts); // remove from localStorage
  108. this.removeObject(hostInfo); // remove from the content to rerender the view
  109. },
  110. retry: function () {
  111. if (this.get('isSubmitDisabled')) {
  112. return;
  113. }
  114. var hosts = this.visibleHosts();
  115. var selectedHosts = hosts.filterProperty('isChecked', true);
  116. selectedHosts.forEach(function (_host) {
  117. console.log('Retrying: ' + _host.name);
  118. });
  119. //TODO: uncomment below code to hookup with @GET bootstrap API
  120. /*
  121. this.set('bootHosts',selectedHosts);
  122. this.doBootstrap();
  123. */
  124. },
  125. removeHosts: function (hosts) {
  126. App.db.removeHosts(hosts);
  127. this.removeObjects(hosts);
  128. },
  129. removeBtn: function () {
  130. if (this.get('isSubmitDisabled')) {
  131. return;
  132. }
  133. var hostResult = this.visibleHosts();
  134. var selectedHosts = hostResult.filterProperty('isChecked', true);
  135. selectedHosts.forEach(function (_hostInfo) {
  136. console.log('Removing: ' + _hostInfo.name);
  137. });
  138. this.removeHosts(selectedHosts);
  139. },
  140. startBootstrap: function () {
  141. this.set('isSubmitDisabled', true);
  142. this.set('bootHosts', this.get('content'));
  143. this.doBootstrap();
  144. },
  145. doBootstrap: function () {
  146. var self = this;
  147. $.ajax({
  148. type: 'GET',
  149. url: '/ambari_server/api/bootstrap',
  150. async: false,
  151. timeout: 5000,
  152. success: function (data) {
  153. console.log("TRACE: In success function for the GET bootstrap call");
  154. var result = self.parseHostInfo(data, this.get('bootHosts'));
  155. if (result !== true) {
  156. window.setTimeout(self.doBootstrap, 3000);
  157. } else {
  158. self.stopBootstrap();
  159. }
  160. },
  161. error: function () {
  162. console.log("ERROR");
  163. self.stopBootstrap();
  164. },
  165. statusCode: {
  166. 404: function () {
  167. console.log("URI not found.");
  168. }
  169. },
  170. dataType: 'application/json'
  171. });
  172. },
  173. stopBootstrap: function () {
  174. //TODO: uncomment following line after the hook up with the API call
  175. // this.set('isSubmitDisabled',false);
  176. },
  177. saveHostInfoToDb: function () {
  178. var hostInfo = {};
  179. this.forEach(function (_host) {
  180. hostInfo[_host.name] = {
  181. name: _host.name,
  182. cpu: _host.cpu,
  183. memory: _host.memory,
  184. bootStatus: _host.bootStatus
  185. };
  186. });
  187. App.db.setHosts(hostInfo);
  188. },
  189. submit: function () {
  190. if (!this.get('isSubmitDisabled')) {
  191. this.saveHostInfoToDb();
  192. App.router.send('next');
  193. }
  194. },
  195. hostLogPopup: function (event) {
  196. App.ModalPopup.show({
  197. header: Em.I18n.t('installer.step3.hostLog.popup.header'),
  198. onPrimary: function () {
  199. this.hide();
  200. },
  201. bodyClass: Ember.View.extend({
  202. templateName: require('templates/installer/step3HostLogPopup')
  203. })
  204. });
  205. },
  206. // TODO: dummy button. Remove this after the hook up with actual REST API.
  207. mockBtn: function () {
  208. this.set('isSubmitDisabled', false);
  209. this.clear();
  210. var hostInfo = this.mockData;
  211. this.renderHosts(hostInfo);
  212. },
  213. pollBtn: function () {
  214. if (this.get('isSubmitDisabled')) {
  215. return;
  216. }
  217. var hosts = this.visibleHosts();
  218. var selectedHosts = hosts.filterProperty('isChecked', true);
  219. selectedHosts.forEach(function (_host) {
  220. console.log('Retrying: ' + _host.name);
  221. });
  222. var mockHosts = this.mockRetryData;
  223. mockHosts.forEach(function (_host) {
  224. console.log('Retrying: ' + _host.name);
  225. });
  226. if (this.parseHostInfo(mockHosts, selectedHosts)) {
  227. // this.saveHostInfoToDb();
  228. }
  229. }
  230. });