progress_controller.js 11 KB

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