progress_controller.js 10 KB

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