step9_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.InstallerStep9Controller = Em.ArrayController.extend({
  20. name: 'installerStep9Controller',
  21. content: [],
  22. progress: '0',
  23. isStepCompleted: false,
  24. isSubmitDisabled: function () {
  25. return !this.get('isStepCompleted');
  26. }.property('isStepCompleted'),
  27. mockHostData: require('data/mock/step9_hosts'),
  28. pollData_1: require('data/mock/step9_pollData_1'),
  29. pollData_2: require('data/mock/step9_pollData_2'),
  30. pollDataCounter: 0,
  31. status: function () {
  32. if (this.everyProperty('status', 'success')) {
  33. return 'success';
  34. } else if (this.someProperty('status', 'failed')) {
  35. return 'failed';
  36. } else if (this.someProperty('status', 'warning')) {
  37. return 'warning';
  38. } else {
  39. return 'info';
  40. }
  41. }.property('@each.status'),
  42. navigateStep: function () {
  43. this.loadStep();
  44. //TODO: uncomment following line after the hook up with the API call
  45. if (App.db.getClusterStatus().isCompleted === false) {
  46. //this.startPolling();
  47. } else {
  48. this.set('isStepCompleted', true);
  49. this.set('progress', '100');
  50. }
  51. },
  52. clearStep: function () {
  53. this.clear();
  54. this.set('status', 'info');
  55. this.set('progress', '0');
  56. this.set('isStepCompleted', false);
  57. },
  58. loadStep: function () {
  59. console.log("TRACE: Loading step9: Install, Start and Test");
  60. this.clearStep();
  61. this.renderHosts(this.loadHosts());
  62. },
  63. loadHosts: function () {
  64. var hostInfo = [];
  65. hostInfo = App.db.getHosts();
  66. var hosts = new Ember.Set();
  67. for (var index in hostInfo) {
  68. hosts.add(hostInfo[index]);
  69. console.log("TRACE: host name is: " + hostInfo[index].name);
  70. }
  71. return hosts.filterProperty('bootStatus', 'success');
  72. },
  73. renderHosts: function (hostsInfo) {
  74. var self = this;
  75. hostsInfo.forEach(function (_hostInfo) {
  76. var hostInfo = App.HostInfo.create({
  77. name: _hostInfo.name,
  78. status: _hostInfo.status,
  79. message: _hostInfo.message,
  80. progress: _hostInfo.progress
  81. });
  82. console.log('pushing ' + hostInfo.name);
  83. self.content.pushObject(hostInfo);
  84. });
  85. },
  86. onSuccessPerHost: function (actions, contentHost) {
  87. if (actions.everyProperty('status', 'completed')) {
  88. contentHost.set('status', 'success');
  89. }
  90. },
  91. onWarningPerHost: function (actions, contentHost) {
  92. if (actions.findProperty('status', 'failed') || actions.findProperty('status', 'aborted')) {
  93. contentHost.set('status', 'warning');
  94. this.set('status', 'warning');
  95. }
  96. },
  97. onInProgressPerHost: function (actions, contentHost) {
  98. var runningAction = actions.findProperty('status', 'inprogress');
  99. if (runningAction !== null && runningAction !== undefined) {
  100. contentHost.set('message', runningAction.message);
  101. }
  102. },
  103. progressPerHost: function (actions, contentHost) {
  104. var totalProgress = 0;
  105. var actionsPerHost = actions.length;
  106. var completedActions = actions.filterProperty('status', 'completed').length
  107. + actions.filterProperty('status', 'failed').length +
  108. actions.filterProperty('status', 'aborted').length;
  109. var progress = (completedActions / actionsPerHost) * 100;
  110. console.log('INFO: progressPerHost is: ' + progress);
  111. contentHost.set('progress', progress.toString());
  112. return progress;
  113. },
  114. isSuccess: function (polledData) {
  115. return polledData.everyProperty('status', 'success');
  116. },
  117. isStepFailed: function (polledData) {
  118. var self = this;
  119. var result = false;
  120. polledData.forEach(function (_polledData) {
  121. var successFactor = _polledData.sf;
  122. var actionsPerRole = polledData.filterProperty('role', _polledData.role);
  123. var actionsFailed = actionsPerRole.filterProperty('status', 'failed');
  124. var actionsAborted = actionsPerRole.filterProperty('status', 'aborted');
  125. if ((((actionsFailed.length + actionsAborted.length) / actionsPerRole.length) * 100) <= successFactor) {
  126. console.log('TRACE: Entering success factor and result is failed');
  127. result = true;
  128. }
  129. });
  130. return result;
  131. },
  132. getFailedHostsForFailedRoles: function (polledData) {
  133. var hostArr = new Ember.Set();
  134. polledData.forEach(function (_polledData) {
  135. var successFactor = _polledData.sf;
  136. var actionsPerRole = polledData.filterProperty('role', _polledData.role);
  137. var actionsFailed = actionsPerRole.filterProperty('status', 'failed');
  138. var actionsAborted = actionsPerRole.filterProperty('status', 'aborted');
  139. if ((((actionsFailed.length + actionsAborted.length) / actionsPerRole.length) * 100) <= successFactor) {
  140. actionsFailed.forEach(function (_actionFailed) {
  141. hostArr.add(_actionFailed.name);
  142. });
  143. actionsAborted.forEach(function (_actionFailed) {
  144. hostArr.add(_actionFailed.name);
  145. });
  146. }
  147. });
  148. return hostArr;
  149. },
  150. setHostsStatus: function (hosts, status) {
  151. var self = this;
  152. hosts.forEach(function (_host) {
  153. var host = self.findProperty('name', _host);
  154. host.set('status', status);
  155. });
  156. },
  157. // polling from ui stops only when no action has 'pending', 'queued' or 'inprogress' status
  158. finishStep: function (polledData) {
  159. var self = this;
  160. if (!polledData.someProperty('status', 'pending') && !polledData.someProperty('status', 'queued') && !polledData.someProperty('status', 'inprogress')) {
  161. this.set('progress', '100');
  162. if (this.isSuccess(polledData)) {
  163. this.set('status', 'success');
  164. } else {
  165. if (this.isStepFailed(polledData)) {
  166. self.set('status', 'failed');
  167. this.setHostsStatus(this.getFailedHostsForFailedRoles(polledData), 'failed');
  168. }
  169. }
  170. this.set('isStepCompleted', true);
  171. }
  172. },
  173. parseHostInfo: function (polledData) {
  174. console.log('TRACE: Entering host info function');
  175. var self = this;
  176. var result = false;
  177. var totalProgress = 0;
  178. this.forEach(function (_content) {
  179. var actions = polledData.filterProperty('name', _content.name);
  180. if (actions.length === 0) {
  181. alert('For testing with mockData follow the sequence: hit referesh,"mockData btn", "pollData btn", again "pollData btn"');
  182. //exit();
  183. }
  184. if (actions !== null && actions !== undefined && actions.length !== 0) {
  185. this.onSuccessPerHost(actions, _content); // every action should be a success
  186. this.onWarningPerHost(actions, _content); // any action should be a faliure
  187. this.onInProgressPerHost(actions, _content); // current running action for a host
  188. totalProgress = totalProgress + self.progressPerHost(actions, _content);
  189. }
  190. }, this);
  191. totalProgress = totalProgress / this.content.length;
  192. this.set('progress', totalProgress.toString());
  193. console.log("INFO: right now the progress is: " + this.get('progress'));
  194. this.finishStep(polledData);
  195. return this.get('isStepCompleted');
  196. },
  197. retry: function () {
  198. if (this.get('isSubmitDisabled')) {
  199. return;
  200. }
  201. this.clear();
  202. this.renderHosts(this.loadHosts());
  203. //this.startPolling();
  204. },
  205. startPolling: function () {
  206. this.set('isSubmitDisabled', true);
  207. this.doPolling();
  208. },
  209. doPolling: function () {
  210. var self = this;
  211. $.ajax({
  212. type: 'GET',
  213. url: '/ambari_server/api/polling',
  214. async: false,
  215. timeout: 5000,
  216. success: function (data) {
  217. console.log("TRACE: In success function for the GET bootstrap call");
  218. var result = self.parseHostInfo(data);
  219. if (result !== true) {
  220. window.setTimeout(self.doPolling, 3000);
  221. } else {
  222. self.stopPolling();
  223. }
  224. },
  225. error: function () {
  226. console.log("ERROR");
  227. self.stopPolling();
  228. },
  229. statusCode: {
  230. 404: function () {
  231. console.log("URI not found.");
  232. }
  233. },
  234. dataType: 'application/json'
  235. });
  236. },
  237. stopPolling: function () {
  238. //TODO: uncomment following line after the hook up with the API call
  239. // this.set('isStepCompleted',true);
  240. },
  241. saveHostInfoToDb: function () {
  242. var hostInfo = App.db.getHosts();
  243. for (var index in hostInfo) {
  244. hostInfo[index].status = "pending";
  245. if (this.someProperty('name', hostInfo[index].name)) {
  246. var host = this.findProperty('name', hostInfo[index].name);
  247. hostInfo[index].status = host.status;
  248. hostInfo[index].message = host.message;
  249. hostInfo[index].progress = host.progress;
  250. }
  251. console.log("TRACE: host name is: " + hostInfo[index].name);
  252. }
  253. App.db.setHosts(hostInfo);
  254. },
  255. submit: function () {
  256. if (!this.get('isSubmitDisabled')) {
  257. this.saveHostInfoToDb();
  258. App.db.setClusterStatus({status: this.get('status'), isCompleted: true});
  259. App.get('router').transitionTo('step10');
  260. }
  261. },
  262. back: function () {
  263. if (!this.get('isSubmitDisabled')) {
  264. App.router.send('back');
  265. }
  266. },
  267. hostLogPopup: function (event) {
  268. App.ModalPopup.show({
  269. header: Em.I18n.t('installer.step3.hostLog.popup.header'),
  270. onPrimary: function () {
  271. this.hide();
  272. },
  273. bodyClass: Ember.View.extend({
  274. templateName: require('templates/installer/step3HostLogPopup')
  275. })
  276. });
  277. },
  278. mockBtn: function () {
  279. this.set('isSubmitDisabled', false);
  280. this.clear();
  281. var hostInfo = this.mockHostData;
  282. this.renderHosts(hostInfo);
  283. },
  284. pollBtn: function () {
  285. this.set('isSubmitDisabled', false);
  286. var data1 = this.pollData_1;
  287. var data2 = this.pollData_2;
  288. if ((this.get('pollDataCounter') / 2) === 0) {
  289. console.log("TRACE: In pollBtn function data1");
  290. var counter = parseInt(this.get('pollDataCounter')) + 1;
  291. this.set('pollDataCounter', counter.toString());
  292. this.parseHostInfo(data1);
  293. } else {
  294. console.log("TRACE: In pollBtn function data2");
  295. var counter = parseInt(this.get('pollDataCounter')) + 1;
  296. this.set('pollDataCounter', counter.toString());
  297. this.parseHostInfo(data2);
  298. }
  299. }
  300. });