progress_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. }else if (loadedStauses.contains('QUEUED')){
  80. this.set('currentTaskId', loadedStauses.indexOf('QUEUED'));
  81. this.runTask(loadedStauses.indexOf('QUEUED'));
  82. }
  83. }
  84. },
  85. setTaskStatus: function (taskId, status) {
  86. this.get('tasks').findProperty('id', taskId).set('status', status);
  87. },
  88. restoreTaskLog: function (taskId, log) {
  89. this.get('tasks').findProperty('id', taskId).set('hosts', log);
  90. },
  91. setTaskLogs: function (taskId, tasks) {
  92. var hosts = [];
  93. var uniqHosts = tasks.mapProperty('Tasks.host_name').uniq();
  94. uniqHosts.forEach(function (host) {
  95. var curHostTasks = tasks.filterProperty('Tasks.host_name', host);
  96. hosts.push(
  97. {
  98. name: host,
  99. publicName: host,
  100. logTasks: curHostTasks
  101. }
  102. );
  103. });
  104. this.get('tasks').findProperty('id', taskId).set('hosts', hosts);
  105. this.set('serviceTimestamp', new Date().getTime());
  106. },
  107. retryTask: function () {
  108. var task = this.get('tasks').findProperty('status', 'FAILED');
  109. task.set('showRetry', false);
  110. task.set('showRollback', false);
  111. task.set('status', 'PENDING');
  112. },
  113. manualRollback: function () {
  114. App.ModalPopup.show({
  115. header: Em.I18n.t('admin.highAvailability.confirmRollbackHeader'),
  116. primary: Em.I18n.t('yes'),
  117. showCloseButton: false,
  118. onPrimary: function () {
  119. var controller = App.router.get('highAvailabilityWizardController');
  120. controller.clearTasksData();
  121. controller.clearStorageData();
  122. controller.setCurrentStep('1');
  123. App.router.get('updateController').set('isWorking', true);
  124. App.clusterStatus.setClusterStatus({
  125. clusterName: App.router.get('content.cluster.name'),
  126. clusterState: 'HIGH_AVAILABILITY_DISABLED',
  127. wizardControllerName: App.router.get('highAvailabilityRollbackController.name'),
  128. localdb: App.db.data
  129. });
  130. this.hide();
  131. App.router.transitionTo('main.admin.index');
  132. location.reload();
  133. },
  134. secondary : Em.I18n.t('no'),
  135. onSecondary: function(){
  136. this.hide();
  137. },
  138. bodyClass: Ember.View.extend({
  139. template: Ember.Handlebars.compile( Em.I18n.t('admin.highAvailability.confirmManualRollbackBody'))
  140. })
  141. });
  142. },
  143. rollback: function () {
  144. var task = this.get('tasks').findProperty('status', 'FAILED');
  145. App.router.get(this.get('content.controllerName')).saveFailedTask(task);
  146. App.ModalPopup.show({
  147. header: Em.I18n.t('admin.highAvailability.confirmRollbackHeader'),
  148. primary: Em.I18n.t('common.confirm'),
  149. showCloseButton: false,
  150. onPrimary: function () {
  151. App.router.get('highAvailabilityWizardController').clearTasksData();
  152. App.router.transitionTo('main.admin.highAvailabilityRollback');
  153. this.hide();
  154. },
  155. secondary : Em.I18n.t('common.cancel'),
  156. onSecondary: function(){
  157. this.hide();
  158. },
  159. body: Em.I18n.t('admin.highAvailability.confirmRollbackBody')
  160. });
  161. },
  162. onTaskStatusChange: function () {
  163. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  164. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  165. if (nextTask) {
  166. this.set('status', 'IN_PROGRESS');
  167. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  168. this.set('currentTaskId', nextTask.get('id'));
  169. this.runTask(nextTask.get('id'));
  170. } else {
  171. this.set('status', 'COMPLETED');
  172. this.set('isSubmitDisabled', false);
  173. }
  174. } else if (this.get('tasks').someProperty('status', 'FAILED')) {
  175. this.set('status', 'FAILED');
  176. this.get('tasks').findProperty('status', 'FAILED').set('showRetry', true);
  177. if(App.supports.autoRollbackHA){
  178. this.get('tasks').findProperty('status', 'FAILED').set('showRollback', true);
  179. }
  180. }
  181. var statuses = this.get('tasks').mapProperty('status');
  182. var logs = this.get('tasks').mapProperty('hosts');
  183. var requestIds = this.get('currentRequestIds');
  184. App.router.get(this.get('content.controllerName')).saveTasksStatuses(statuses);
  185. App.router.get(this.get('content.controllerName')).saveRequestIds(requestIds);
  186. App.router.get(this.get('content.controllerName')).saveLogs(logs);
  187. App.clusterStatus.setClusterStatus({
  188. clusterName: this.get('content.cluster.name'),
  189. clusterState: this.get('clusterDeployState'),
  190. wizardControllerName: this.get('content.controllerName'),
  191. localdb: App.db.data
  192. });
  193. },
  194. /*
  195. run command of appropriate task
  196. */
  197. runTask: function (taskId) {
  198. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  199. },
  200. onTaskError: function () {
  201. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  202. },
  203. onTaskCompleted: function () {
  204. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  205. },
  206. createComponent: function (componentName, hostName) {
  207. if (!(hostName instanceof Array)) {
  208. hostName = [hostName];
  209. }
  210. var hostComponents = [];
  211. for (var i = 0; i < hostName.length; i++) {
  212. hostComponents = App.HostComponent.find().filterProperty('componentName', componentName);
  213. if (!hostComponents.length || !hostComponents.mapProperty('host.hostName').contains(hostName[i])) {
  214. App.ajax.send({
  215. name: 'admin.high_availability.create_component',
  216. sender: this,
  217. data: {
  218. hostName: hostName[i],
  219. componentName: componentName,
  220. taskNum: hostName.length
  221. },
  222. success: 'onCreateComponent',
  223. error: 'onTaskError'
  224. });
  225. } else {
  226. var taskNum = hostName.length;
  227. this.installComponent(componentName, hostName[i], taskNum);
  228. }
  229. }
  230. },
  231. onCreateComponent: function () {
  232. var hostName = arguments[2].hostName;
  233. var componentName = arguments[2].componentName;
  234. var taskNum = arguments[2].taskNum;
  235. this.installComponent(componentName, hostName, taskNum);
  236. },
  237. installComponent: function (componentName, hostName, taskNum) {
  238. if (!(hostName instanceof Array)) {
  239. hostName = [hostName];
  240. }
  241. for (var i = 0; i < hostName.length; i++) {
  242. App.ajax.send({
  243. name: 'admin.high_availability.install_component',
  244. sender: this,
  245. data: {
  246. hostName: hostName[i],
  247. componentName: componentName,
  248. displayName: App.format.role(componentName),
  249. taskNum: taskNum || hostName.length
  250. },
  251. success: 'startPolling',
  252. error: 'onTaskError'
  253. });
  254. }
  255. },
  256. startComponent: function (componentName, hostName) {
  257. if (!(hostName instanceof Array)) {
  258. hostName = [hostName];
  259. }
  260. for (var i = 0; i < hostName.length; i++) {
  261. App.ajax.send({
  262. name: 'admin.high_availability.start_component',
  263. sender: this,
  264. data: {
  265. hostName: hostName[i],
  266. componentName: componentName,
  267. displayName: App.format.role(componentName),
  268. taskNum: hostName.length
  269. },
  270. success: 'startPolling',
  271. error: 'onTaskError'
  272. });
  273. }
  274. },
  275. startPolling: function (data) {
  276. if (data) {
  277. this.get('currentRequestIds').push(data.Requests.id);
  278. var tasksCount = arguments[2].taskNum || 1;
  279. if (tasksCount === this.get('currentRequestIds').length) {
  280. this.doPolling();
  281. }
  282. } else {
  283. this.onTaskCompleted();
  284. }
  285. },
  286. doPolling: function () {
  287. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  288. var requestIds = this.get('currentRequestIds');
  289. for (var i = 0; i < requestIds.length; i++) {
  290. App.ajax.send({
  291. name: 'admin.high_availability.polling',
  292. sender: this,
  293. data: {
  294. requestId: requestIds[i]
  295. },
  296. success: 'parseLogs',
  297. error: 'onTaskError'
  298. });
  299. }
  300. },
  301. parseLogs: function (logs) {
  302. this.get('logs').push(logs.tasks);
  303. if (this.get('currentRequestIds').length === this.get('logs').length) {
  304. var tasks = [];
  305. this.get('logs').forEach(function (logs) {
  306. tasks.pushObjects(logs);
  307. }, this);
  308. var self = this;
  309. var currentTaskId = this.get('currentTaskId');
  310. this.setTaskLogs(currentTaskId, tasks);
  311. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  312. this.set('currentRequestIds', []);
  313. if (tasks.someProperty('Tasks.status', 'FAILED') || tasks.someProperty('status', 'TIMEDOUT') || tasks.someProperty('status', 'ABORTED')) {
  314. this.setTaskStatus(currentTaskId, 'FAILED');
  315. } else {
  316. this.setTaskStatus(currentTaskId, 'COMPLETED');
  317. }
  318. } else {
  319. var actionsPerHost = tasks.length;
  320. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  321. + tasks.filterProperty('Tasks.status', 'FAILED').length
  322. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  323. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  324. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  325. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  326. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  327. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  328. window.setTimeout(function () {
  329. self.doPolling()
  330. }, self.POLL_INTERVAL);
  331. }
  332. this.set('logs', []);
  333. }
  334. },
  335. done: function () {
  336. if (!this.get('isSubmitDisabled')) {
  337. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  338. App.router.send('next');
  339. }
  340. }
  341. });