step3_controller.js 7.3 KB

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