progress_controller.js 11 KB

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