step3_controller.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. }
  71. });
  72. result = !this.content.someProperty('status', 'pending');
  73. return result;
  74. },
  75. /* Below function returns the current set of visible hosts on view (All,succeded,failed) */
  76. visibleHosts: function () {
  77. var result;
  78. if (this.get('category') === 'Succeeded') {
  79. return (this.filterProperty('status', 'success'));
  80. } else if (this.get('category') === 'Failed') {
  81. return (this.filterProperty('status', 'error'));
  82. } else if (this.get('category') === 'Hosts') {
  83. return this.content;
  84. }
  85. },
  86. /* Below function removes a single element on the trsah icon click. Being called from view */
  87. removeElement: function (hostInfo) {
  88. console.log('TRACE: In removeElement');
  89. var hosts = [hostInfo];
  90. App.db.removeHosts(hosts); // remove from localStorage
  91. this.removeObject(hostInfo); // remove from the content to rerender the view
  92. },
  93. retry: function () {
  94. if (this.get('isSubmitDisabled')) {
  95. return;
  96. }
  97. var hosts = this.visibleHosts();
  98. var selectedHosts = hosts.filterProperty('isChecked', true);
  99. selectedHosts.forEach(function (_host) {
  100. console.log('Retrying: ' + _host.name);
  101. });
  102. //TODO: uncomment below code to hookup with @GET bootstrap API
  103. /*
  104. this.set('bootHosts',selectedHosts);
  105. this.doBootstrap();
  106. */
  107. //TODO: comment below lines while hooking up with actual @GET bootstrap API
  108. var mockHosts = this.mockRetryData;
  109. mockHosts.forEach(function (_host) {
  110. console.log('Retrying: ' + _host.name);
  111. });
  112. this.parseHostInfo(mockHosts, selectedHosts);
  113. },
  114. removeHosts: function (hosts) {
  115. App.db.removeHosts(hosts);
  116. this.removeObjects(hosts);
  117. },
  118. removeBtn: function() {
  119. if (this.get('isSubmitDisabled')) {
  120. return;
  121. }
  122. var hostResult = this.visibleHosts();
  123. var selectedHosts = hostResult.filterProperty('isChecked', true);
  124. selectedHosts.forEach(function (_hostInfo) {
  125. console.log('Removing: ' + _hostInfo.name);
  126. });
  127. this.removeHosts(selectedHosts);
  128. },
  129. startBootstrap: function () {
  130. this.set('isSubmitDisabled', true);
  131. this.set('bootHosts', this.get('content'));
  132. this.doBootstrap();
  133. },
  134. doBootstrap: function () {
  135. var self = this;
  136. $.ajax({
  137. type: 'GET',
  138. url: '/ambari_server/api/bootstrap',
  139. async: false,
  140. timeout: 5000,
  141. success: function (data) {
  142. console.log("TRACE: In success function for the GET bootstrap call");
  143. var result = self.parseHostInfo(data, this.get('bootHosts'));
  144. if (result !== true) {
  145. window.setTimeout(self.doBootstrap, 3000);
  146. } else {
  147. self.stopBootstrap();
  148. }
  149. },
  150. error: function () {
  151. console.log("ERROR");
  152. self.stopBootstrap();
  153. },
  154. statusCode: {
  155. 404: function () {
  156. console.log("URI not found.");
  157. }
  158. },
  159. dataType: 'application/json'
  160. });
  161. },
  162. stopBootstrap: function () {
  163. //TODO: uncomment following line after the hook up with the API call
  164. // this.set('isSubmitDisabled',false);
  165. },
  166. submit: function () {
  167. if (!this.get('isSubmitDisabled')) {
  168. var erroneousHosts = this.filterProperty('status', 'error');
  169. console.log("INFO: Removing erroneous hosts before moving to step4");
  170. this.removeHosts(erroneousHosts);
  171. App.get('router').transitionTo('step4');
  172. }
  173. },
  174. hostLogPopup: function (event) {
  175. App.ModalPopup.show({
  176. header: Em.I18n.t('installer.step3.hostLog.popup.header'),
  177. onPrimary: function () {
  178. this.hide();
  179. },
  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. });