progress_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.HighAvailabilityProgressPageController = App.HighAvailabilityWizardController.extend({
  20. name: 'highAvailabilityProgressPageController',
  21. status: 'IN_PROGRESS',
  22. tasks: [],
  23. commands: [],
  24. currentRequestIds: [],
  25. logs: [],
  26. currentTaskId: null,
  27. POLL_INTERVAL: 4000,
  28. isSubmitDisabled: true,
  29. serviceTimestamp: null,
  30. isRollback: false,
  31. loadStep: function () {
  32. this.clearStep();
  33. this.loadTasks();
  34. this.addObserver('tasks.@each.status', this, 'onTaskStatusChange');
  35. this.onTaskStatusChange();
  36. },
  37. clearStep: function () {
  38. this.set('isSubmitDisabled', true);
  39. this.set('tasks', []);
  40. this.set('logs', []);
  41. this.set('currentRequestIds', []);
  42. var commands = this.get('commands');
  43. var currentStep = App.router.get('highAvailabilityWizardController.currentStep');
  44. for (var i = 0; i < commands.length; i++) {
  45. this.get('tasks').pushObject(Ember.Object.create({
  46. title: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  47. status: 'PENDING',
  48. id: i,
  49. command: commands[i],
  50. showRetry: false,
  51. showRollback: false,
  52. name: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  53. displayName: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  54. progress: 0,
  55. isRunning: false,
  56. hosts: []
  57. }));
  58. }
  59. },
  60. services: function(){
  61. return this.get('tasks');
  62. }.property('tasks'),
  63. loadTasks: function () {
  64. var loadedStauses = this.get('content.tasksStatuses');
  65. var loadedLogs = this.get('content.logs');
  66. if (loadedStauses && loadedLogs && loadedStauses.length === this.get('tasks').length) {
  67. for (var i = 0; i < loadedStauses.length; i++) {
  68. this.setTaskStatus(i, loadedStauses[i]);
  69. this.restoreTaskLog(i, loadedLogs[i]);
  70. }
  71. if (loadedStauses.contains('IN_PROGRESS')) {
  72. this.set('currentRequestIds', this.get('content.requestIds'));
  73. this.set('currentTaskId', loadedStauses.indexOf('IN_PROGRESS'));
  74. this.doPolling();
  75. }
  76. }
  77. },
  78. setTaskStatus: function (taskId, status) {
  79. this.get('tasks').findProperty('id', taskId).set('status', status);
  80. },
  81. restoreTaskLog: function (taskId, log) {
  82. this.get('tasks').findProperty('id', taskId).set('hosts', log);
  83. },
  84. setTaskLogs: function (taskId, tasks) {
  85. var hosts = [];
  86. var uniqHosts = tasks.mapProperty('Tasks.host_name').uniq();
  87. uniqHosts.forEach(function (host) {
  88. var curHostTasks = tasks.filterProperty('Tasks.host_name', host);
  89. hosts.push(
  90. {
  91. name: host,
  92. publicName: host,
  93. logTasks: curHostTasks
  94. }
  95. );
  96. });
  97. this.get('tasks').findProperty('id', taskId).set('hosts', hosts);
  98. this.set('serviceTimestamp', new Date().getTime());
  99. },
  100. retryTask: function () {
  101. var task = this.get('tasks').findProperty('status', 'FAILED');
  102. task.set('showRetry', false);
  103. task.set('showRollback', false);
  104. task.set('status', 'PENDING');
  105. },
  106. rollback: function () {
  107. var task = this.get('tasks').findProperty('status', 'FAILED');
  108. App.router.get(this.get('content.controllerName')).saveFailedTask(task);
  109. App.ModalPopup.show({
  110. header: Em.I18n.t('admin.highAvailability.confirmRollbackHeader'),
  111. primary: Em.I18n.t('common.confirm'),
  112. showCloseButton: false,
  113. onPrimary: function () {
  114. App.router.transitionTo('main.admin.highAvailabilityRollback');
  115. this.hide();
  116. },
  117. secondary : Em.I18n.t('common.cancel'),
  118. onSecondary: function(){
  119. this.hide();
  120. },
  121. body: Em.I18n.t('admin.highAvailability.confirmRollbackBody')
  122. });
  123. },
  124. onTaskStatusChange: function () {
  125. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  126. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  127. if (nextTask) {
  128. this.set('status', 'IN_PROGRESS');
  129. this.runTask(nextTask.get('id'));
  130. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  131. this.set('currentTaskId', nextTask.get('id'));
  132. } else {
  133. this.set('status', 'COMPLETED');
  134. this.set('isSubmitDisabled', false);
  135. }
  136. } else if (this.get('tasks').someProperty('status', 'FAILED')) {
  137. this.set('status', 'FAILED');
  138. this.get('tasks').findProperty('status', 'FAILED').set('showRetry', true);
  139. this.get('tasks').findProperty('status', 'FAILED').set('showRollback', true);
  140. }
  141. var statuses = this.get('tasks').mapProperty('status');
  142. var logs = this.get('tasks').mapProperty('hosts');
  143. var requestIds = this.get('currentRequestIds');
  144. App.router.get(this.get('content.controllerName')).saveTasksStatuses(statuses);
  145. App.router.get(this.get('content.controllerName')).saveRequestIds(requestIds);
  146. App.router.get(this.get('content.controllerName')).saveLogs(logs);
  147. App.clusterStatus.setClusterStatus({
  148. clusterName: this.get('content.cluster.name'),
  149. clusterState: 'HIGH_AVAILABILITY_DEPLOY',
  150. wizardControllerName: this.get('content.controllerName'),
  151. localdb: App.db.data
  152. });
  153. },
  154. /*
  155. run command of appropriate task
  156. */
  157. runTask: function (taskId) {
  158. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  159. },
  160. onTaskError: function () {
  161. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  162. },
  163. onTaskCompleted: function () {
  164. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  165. },
  166. createComponent: function (componentName, hostName) {
  167. if (!(hostName instanceof Array)) {
  168. hostName = [hostName];
  169. }
  170. var hostComponents = [];
  171. for (var i = 0; i < hostName.length; i++) {
  172. hostComponents = App.HostComponent.find().filterProperty('componentName', componentName);
  173. if (!hostComponents.length || !hostComponents.mapProperty('host.hostName').contains(hostName[i])) {
  174. App.ajax.send({
  175. name: 'admin.high_availability.create_component',
  176. sender: this,
  177. data: {
  178. hostName: hostName[i],
  179. componentName: componentName,
  180. taskNum: hostName.length
  181. },
  182. success: 'onCreateComponent',
  183. error: 'onTaskError'
  184. });
  185. } else {
  186. var taskNum = hostName.length;
  187. this.installComponent(componentName, hostName[i], taskNum);
  188. }
  189. }
  190. },
  191. onCreateComponent: function () {
  192. var hostName = arguments[2].hostName;
  193. var componentName = arguments[2].componentName;
  194. var taskNum = arguments[2].taskNum;
  195. this.installComponent(componentName, hostName, taskNum);
  196. },
  197. installComponent: function (componentName, hostName, taskNum) {
  198. if (!(hostName instanceof Array)) {
  199. hostName = [hostName];
  200. }
  201. for (var i = 0; i < hostName.length; i++) {
  202. App.ajax.send({
  203. name: 'admin.high_availability.install_component',
  204. sender: this,
  205. data: {
  206. hostName: hostName[i],
  207. componentName: componentName,
  208. displayName: App.format.role(componentName),
  209. taskNum: taskNum || hostName.length
  210. },
  211. success: 'startPolling',
  212. error: 'onTaskError'
  213. });
  214. }
  215. },
  216. startComponent: function (componentName, hostName) {
  217. if (!(hostName instanceof Array)) {
  218. hostName = [hostName];
  219. }
  220. for (var i = 0; i < hostName.length; i++) {
  221. App.ajax.send({
  222. name: 'admin.high_availability.start_component',
  223. sender: this,
  224. data: {
  225. hostName: hostName[i],
  226. componentName: componentName,
  227. displayName: App.format.role(componentName),
  228. taskNum: hostName.length
  229. },
  230. success: 'startPolling',
  231. error: 'onTaskError'
  232. });
  233. }
  234. },
  235. startPolling: function (data) {
  236. if (data) {
  237. this.get('currentRequestIds').push(data.Requests.id);
  238. var tasksCount = arguments[2].taskNum || 1;
  239. if (tasksCount === this.get('currentRequestIds').length) {
  240. this.doPolling();
  241. }
  242. } else {
  243. this.onTaskCompleted();
  244. }
  245. },
  246. doPolling: function () {
  247. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  248. var requestIds = this.get('currentRequestIds');
  249. for (var i = 0; i < requestIds.length; i++) {
  250. App.ajax.send({
  251. name: 'admin.high_availability.polling',
  252. sender: this,
  253. data: {
  254. requestId: requestIds[i]
  255. },
  256. success: 'parseLogs',
  257. error: 'onTaskError'
  258. });
  259. }
  260. },
  261. parseLogs: function (logs) {
  262. this.get('logs').push(logs.tasks);
  263. if (this.get('currentRequestIds').length === this.get('logs').length) {
  264. var tasks = [];
  265. this.get('logs').forEach(function (logs) {
  266. tasks.pushObjects(logs);
  267. }, this);
  268. var self = this;
  269. var currentTaskId = this.get('currentTaskId');
  270. this.setTaskLogs(currentTaskId, tasks);
  271. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  272. this.set('currentRequestIds', []);
  273. if (tasks.someProperty('Tasks.status', 'FAILED') || tasks.someProperty('status', 'TIMEDOUT') || tasks.someProperty('status', 'ABORTED')) {
  274. this.setTaskStatus(currentTaskId, 'FAILED');
  275. } else {
  276. this.setTaskStatus(currentTaskId, 'COMPLETED');
  277. }
  278. } else {
  279. var actionsPerHost = tasks.length;
  280. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  281. + tasks.filterProperty('Tasks.status', 'FAILED').length
  282. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  283. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  284. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  285. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  286. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  287. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  288. window.setTimeout(function () {
  289. self.doPolling()
  290. }, self.POLL_INTERVAL);
  291. }
  292. this.set('logs', []);
  293. }
  294. },
  295. done: function () {
  296. if (!this.get('isSubmitDisabled')) {
  297. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  298. App.router.send('next');
  299. }
  300. }
  301. });