step3_controller.js 6.7 KB

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