progress_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. console.warn('func: loadStep');
  34. this.clearStep();
  35. this.initializeTasks();
  36. this.loadTasks();
  37. this.addObserver('tasks.@each.status', this, 'onTaskStatusChange');
  38. this.onTaskStatusChange();
  39. },
  40. clearStep: function () {
  41. console.warn('func: clearStep');
  42. this.set('isSubmitDisabled', true);
  43. this.set('tasks', []);
  44. this.set('logs', []);
  45. this.set('currentRequestIds', []);
  46. },
  47. initializeTasks: function() {
  48. console.warn('func: initializeTasks');
  49. var commands = this.get('commands');
  50. var currentStep = App.router.get('highAvailabilityWizardController.currentStep');
  51. for (var i = 0; i < commands.length; i++) {
  52. this.get('tasks').pushObject(Ember.Object.create({
  53. title: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  54. status: 'PENDING',
  55. id: i,
  56. command: commands[i],
  57. showRetry: false,
  58. showRollback: false,
  59. name: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  60. displayName: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  61. progress: 0,
  62. isRunning: false,
  63. hosts: []
  64. }));
  65. }
  66. },
  67. services: function(){
  68. return this.get('tasks');
  69. }.property('tasks'),
  70. loadTasks: function () {
  71. console.warn('func: loadTasks');
  72. var self = this;
  73. var loadedStauses = this.get('content.tasksStatuses');
  74. var loadedLogs = this.get('content.logs');
  75. if (loadedStauses && loadedLogs && loadedStauses.length === this.get('tasks').length) {
  76. this.get('tasks').forEach(function(task,i){
  77. self.setTaskStatus(task.get('id'), loadedStauses[i]);
  78. self.restoreTaskLog(task.get('id'), loadedLogs[i]);
  79. });
  80. if (loadedStauses.contains('IN_PROGRESS')) {
  81. var curTaskId = this.get('tasks')[loadedStauses.indexOf('IN_PROGRESS')].get('id');
  82. this.set('currentRequestIds', this.get('content.requestIds'));
  83. this.set('currentTaskId', curTaskId);
  84. this.doPolling();
  85. }else if (loadedStauses.contains('QUEUED')){
  86. var curTaskId = this.get('tasks')[loadedStauses.indexOf('QUEUED')].get('id');
  87. this.set('currentTaskId', curTaskId);
  88. this.runTask(curTaskId);
  89. }
  90. }
  91. },
  92. setTaskStatus: function (taskId, status) {
  93. console.warn('func: setTaskStatus');
  94. this.get('tasks').findProperty('id', taskId).set('status', status);
  95. },
  96. restoreTaskLog: function (taskId, log) {
  97. console.warn('func: restoreTaskLog');
  98. this.get('tasks').findProperty('id', taskId).set('hosts', log);
  99. },
  100. setTaskLogs: function (taskId, tasks) {
  101. console.warn('func: setTaskLogs');
  102. var hosts = [];
  103. var uniqHosts = tasks.mapProperty('Tasks.host_name').uniq();
  104. uniqHosts.forEach(function (host) {
  105. var curHostTasks = tasks.filterProperty('Tasks.host_name', host);
  106. hosts.push(
  107. {
  108. name: host,
  109. publicName: host,
  110. logTasks: curHostTasks
  111. }
  112. );
  113. });
  114. this.get('tasks').findProperty('id', taskId).set('hosts', hosts);
  115. this.set('serviceTimestamp', new Date().getTime());
  116. },
  117. retryTask: function () {
  118. console.warn('func: retryTask');
  119. var task = this.get('tasks').findProperty('status', 'FAILED');
  120. task.set('showRetry', false);
  121. task.set('showRollback', false);
  122. task.set('status', 'PENDING');
  123. },
  124. manualRollback: function () {
  125. console.warn('func: manualRollback');
  126. App.ModalPopup.show({
  127. header: Em.I18n.t('admin.highAvailability.confirmRollbackHeader'),
  128. primary: Em.I18n.t('yes'),
  129. showCloseButton: false,
  130. onPrimary: function () {
  131. var controller = App.router.get('highAvailabilityWizardController');
  132. controller.clearTasksData();
  133. controller.clearStorageData();
  134. controller.setCurrentStep('1');
  135. App.router.get('updateController').set('isWorking', true);
  136. App.clusterStatus.setClusterStatus({
  137. clusterName: App.router.get('content.cluster.name'),
  138. clusterState: 'DEFAULT',
  139. wizardControllerName: App.router.get('highAvailabilityRollbackController.name'),
  140. localdb: App.db.data
  141. });
  142. this.hide();
  143. App.router.transitionTo('main.admin.index');
  144. location.reload();
  145. },
  146. secondary : Em.I18n.t('no'),
  147. onSecondary: function(){
  148. this.hide();
  149. },
  150. bodyClass: Ember.View.extend({
  151. template: Ember.Handlebars.compile( Em.I18n.t('admin.highAvailability.confirmManualRollbackBody'))
  152. })
  153. });
  154. },
  155. rollback: function () {
  156. console.warn('func: rollback');
  157. var task = this.get('tasks').findProperty('status', 'FAILED');
  158. App.router.get(this.get('content.controllerName')).saveFailedTask(task);
  159. App.ModalPopup.show({
  160. header: Em.I18n.t('admin.highAvailability.confirmRollbackHeader'),
  161. primary: Em.I18n.t('common.confirm'),
  162. showCloseButton: false,
  163. onPrimary: function () {
  164. App.router.get('highAvailabilityWizardController').clearTasksData();
  165. App.router.transitionTo('main.admin.highAvailabilityRollback');
  166. this.hide();
  167. },
  168. secondary : Em.I18n.t('common.cancel'),
  169. onSecondary: function(){
  170. this.hide();
  171. },
  172. body: Em.I18n.t('admin.highAvailability.confirmRollbackBody')
  173. });
  174. },
  175. onTaskStatusChange: function () {
  176. console.warn('func: onTaskStatusChange1');
  177. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  178. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  179. if (nextTask) {
  180. console.warn('func: onTaskStatusChange2');
  181. this.set('status', 'IN_PROGRESS');
  182. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  183. this.set('currentTaskId', nextTask.get('id'));
  184. this.runTask(nextTask.get('id'));
  185. } else {
  186. console.warn('func: onTaskStatusChange3');
  187. this.set('status', 'COMPLETED');
  188. this.set('isSubmitDisabled', false);
  189. }
  190. } else if (this.get('tasks').someProperty('status', 'FAILED')) {
  191. console.warn('func: onTaskStatusChange4');
  192. this.set('status', 'FAILED');
  193. this.get('tasks').findProperty('status', 'FAILED').set('showRetry', true);
  194. if(App.supports.autoRollbackHA){
  195. this.get('tasks').findProperty('status', 'FAILED').set('showRollback', true);
  196. }
  197. }
  198. this.get('tasks').filterProperty('status','COMPLETED').setEach('showRetry', false);
  199. this.get('tasks').filterProperty('status','COMPLETED').setEach('showRollback', false);
  200. var statuses = this.get('tasks').mapProperty('status');
  201. var logs = this.get('tasks').mapProperty('hosts');
  202. var requestIds = this.get('currentRequestIds');
  203. console.warn('func: onTaskStatusChange5',statuses, logs, requestIds);
  204. App.router.get(this.get('content.controllerName')).saveTasksStatuses(statuses);
  205. App.router.get(this.get('content.controllerName')).saveRequestIds(requestIds);
  206. App.router.get(this.get('content.controllerName')).saveLogs(logs);
  207. App.clusterStatus.setClusterStatus({
  208. clusterName: this.get('content.cluster.name'),
  209. clusterState: this.get('clusterDeployState'),
  210. wizardControllerName: this.get('content.controllerName'),
  211. localdb: App.db.data
  212. });
  213. },
  214. /*
  215. run command of appropriate task
  216. */
  217. runTask: function (taskId) {
  218. console.warn('func: runTask',taskId);
  219. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  220. },
  221. onTaskError: function () {
  222. console.warn('func: onTaskError');
  223. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  224. },
  225. onTaskCompleted: function () {
  226. console.warn('func: onTaskCompleted');
  227. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  228. },
  229. createComponent: function (componentName, hostName) {
  230. console.warn('func: createComponent');
  231. if (!(hostName instanceof Array)) {
  232. hostName = [hostName];
  233. }
  234. var hostComponents = [];
  235. for (var i = 0; i < hostName.length; i++) {
  236. hostComponents = App.HostComponent.find().filterProperty('componentName', componentName);
  237. if (!hostComponents.length || !hostComponents.mapProperty('host.hostName').contains(hostName[i])) {
  238. App.ajax.send({
  239. name: 'admin.high_availability.create_component',
  240. sender: this,
  241. data: {
  242. hostName: hostName[i],
  243. componentName: componentName,
  244. taskNum: hostName.length
  245. },
  246. success: 'onCreateComponent',
  247. error: 'onTaskError'
  248. });
  249. } else {
  250. var taskNum = hostName.length;
  251. this.installComponent(componentName, hostName[i], taskNum);
  252. }
  253. }
  254. },
  255. onCreateComponent: function () {
  256. console.warn('func: onCreateComponent');
  257. var hostName = arguments[2].hostName;
  258. var componentName = arguments[2].componentName;
  259. var taskNum = arguments[2].taskNum;
  260. this.installComponent(componentName, hostName, taskNum);
  261. },
  262. installComponent: function (componentName, hostName, taskNum) {
  263. console.warn('func: installComponent');
  264. if (!(hostName instanceof Array)) {
  265. hostName = [hostName];
  266. }
  267. for (var i = 0; i < hostName.length; i++) {
  268. App.ajax.send({
  269. name: 'admin.high_availability.install_component',
  270. sender: this,
  271. data: {
  272. hostName: hostName[i],
  273. componentName: componentName,
  274. displayName: App.format.role(componentName),
  275. taskNum: taskNum || hostName.length
  276. },
  277. success: 'startPolling',
  278. error: 'onTaskError'
  279. });
  280. }
  281. },
  282. startComponent: function (componentName, hostName) {
  283. console.warn('func: startComponent');
  284. if (!(hostName instanceof Array)) {
  285. hostName = [hostName];
  286. }
  287. for (var i = 0; i < hostName.length; i++) {
  288. App.ajax.send({
  289. name: 'admin.high_availability.start_component',
  290. sender: this,
  291. data: {
  292. hostName: hostName[i],
  293. componentName: componentName,
  294. displayName: App.format.role(componentName),
  295. taskNum: hostName.length
  296. },
  297. success: 'startPolling',
  298. error: 'onTaskError'
  299. });
  300. }
  301. },
  302. stopComponent: function (componentName, hostName) {
  303. console.warn('func: stopComponent');
  304. if (!(hostName instanceof Array)) {
  305. hostName = [hostName];
  306. }
  307. for (var i = 0; i < hostName.length; i++) {
  308. App.ajax.send({
  309. name: 'admin.high_availability.stop_component',
  310. sender: this,
  311. data: {
  312. hostName: hostName[i],
  313. componentName: componentName,
  314. displayName: App.format.role(componentName),
  315. taskNum: hostName.length
  316. },
  317. success: 'startPolling',
  318. error: 'onTaskError'
  319. });
  320. }
  321. },
  322. startPolling: function (data) {
  323. if (data) {
  324. console.warn('func: startPolling1');
  325. this.get('currentRequestIds').push(data.Requests.id);
  326. var tasksCount = arguments[2].taskNum || 1;
  327. if (tasksCount === this.get('currentRequestIds').length) {
  328. console.warn('func: startPolling2');
  329. this.doPolling();
  330. }
  331. } else {
  332. console.warn('func: startPolling3');
  333. this.onTaskCompleted();
  334. }
  335. },
  336. doPolling: function () {
  337. console.warn('func: doPolling');
  338. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  339. var requestIds = this.get('currentRequestIds');
  340. for (var i = 0; i < requestIds.length; i++) {
  341. App.ajax.send({
  342. name: 'admin.high_availability.polling',
  343. sender: this,
  344. data: {
  345. requestId: requestIds[i]
  346. },
  347. success: 'parseLogs',
  348. error: 'onTaskError'
  349. });
  350. }
  351. },
  352. parseLogs: function (logs) {
  353. console.warn('func: parseLogs');
  354. this.get('logs').push(logs.tasks);
  355. if (this.get('currentRequestIds').length === this.get('logs').length) {
  356. var tasks = [];
  357. this.get('logs').forEach(function (logs) {
  358. tasks.pushObjects(logs);
  359. }, this);
  360. var self = this;
  361. var currentTaskId = this.get('currentTaskId');
  362. this.setTaskLogs(currentTaskId, tasks);
  363. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  364. this.set('currentRequestIds', []);
  365. if (tasks.someProperty('Tasks.status', 'FAILED') || tasks.someProperty('Tasks.status', 'TIMEDOUT') || tasks.someProperty('Tasks.status', 'ABORTED')) {
  366. this.setTaskStatus(currentTaskId, 'FAILED');
  367. } else {
  368. this.setTaskStatus(currentTaskId, 'COMPLETED');
  369. }
  370. } else {
  371. var actionsPerHost = tasks.length;
  372. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  373. + tasks.filterProperty('Tasks.status', 'FAILED').length
  374. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  375. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  376. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  377. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  378. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  379. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  380. window.setTimeout(function () {
  381. self.doPolling()
  382. }, self.POLL_INTERVAL);
  383. }
  384. this.set('logs', []);
  385. }
  386. },
  387. done: function () {
  388. if (!this.get('isSubmitDisabled')) {
  389. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  390. App.router.send('next');
  391. }
  392. }
  393. });