step3_controller.js 7.1 KB

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