progress_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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', App.dateTime());
  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. body: Em.I18n.t('admin.highAvailability.confirmRollbackBody')
  170. });
  171. },
  172. onTaskStatusChange: function () {
  173. console.warn('func: onTaskStatusChange1');
  174. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  175. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  176. if (nextTask) {
  177. console.warn('func: onTaskStatusChange2');
  178. this.set('status', 'IN_PROGRESS');
  179. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  180. this.set('currentTaskId', nextTask.get('id'));
  181. this.runTask(nextTask.get('id'));
  182. } else {
  183. console.warn('func: onTaskStatusChange3');
  184. this.set('status', 'COMPLETED');
  185. this.set('isSubmitDisabled', false);
  186. }
  187. } else if (this.get('tasks').someProperty('status', 'FAILED')) {
  188. console.warn('func: onTaskStatusChange4');
  189. this.set('status', 'FAILED');
  190. this.get('tasks').findProperty('status', 'FAILED').set('showRetry', true);
  191. if(App.supports.autoRollbackHA){
  192. this.get('tasks').findProperty('status', 'FAILED').set('showRollback', true);
  193. }
  194. }
  195. this.get('tasks').filterProperty('status','COMPLETED').setEach('showRetry', false);
  196. this.get('tasks').filterProperty('status','COMPLETED').setEach('showRollback', false);
  197. var statuses = this.get('tasks').mapProperty('status');
  198. var logs = this.get('tasks').mapProperty('hosts');
  199. var requestIds = this.get('currentRequestIds');
  200. console.warn('func: onTaskStatusChange5',statuses, logs, requestIds);
  201. App.router.get(this.get('content.controllerName')).saveTasksStatuses(statuses);
  202. App.router.get(this.get('content.controllerName')).saveRequestIds(requestIds);
  203. App.router.get(this.get('content.controllerName')).saveLogs(logs);
  204. App.clusterStatus.setClusterStatus({
  205. clusterName: this.get('content.cluster.name'),
  206. clusterState: this.get('clusterDeployState'),
  207. wizardControllerName: this.get('content.controllerName'),
  208. localdb: App.db.data
  209. });
  210. },
  211. /*
  212. run command of appropriate task
  213. */
  214. runTask: function (taskId) {
  215. console.warn('func: runTask',taskId);
  216. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  217. },
  218. onTaskError: function () {
  219. console.warn('func: onTaskError');
  220. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  221. },
  222. onTaskCompleted: function () {
  223. console.warn('func: onTaskCompleted');
  224. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  225. },
  226. createComponent: function (componentName, hostName) {
  227. console.warn('func: createComponent');
  228. if (!(hostName instanceof Array)) {
  229. hostName = [hostName];
  230. }
  231. var hostComponents = [];
  232. for (var i = 0; i < hostName.length; i++) {
  233. hostComponents = App.HostComponent.find().filterProperty('componentName', componentName);
  234. if (!hostComponents.length || !hostComponents.mapProperty('host.hostName').contains(hostName[i])) {
  235. App.ajax.send({
  236. name: 'admin.high_availability.create_component',
  237. sender: this,
  238. data: {
  239. hostName: hostName[i],
  240. componentName: componentName,
  241. taskNum: hostName.length
  242. },
  243. success: 'onCreateComponent',
  244. error: 'onCreateComponentError'
  245. });
  246. } else {
  247. // Simulates format returned from ajax.send
  248. this.onCreateComponent(null, null, {hostName: hostName[i], componentName: componentName, taskNum: hostName.length});
  249. }
  250. }
  251. },
  252. onCreateComponent: function () {
  253. console.warn('func: onCreateComponent');
  254. var hostName = arguments[2].hostName;
  255. var componentName = arguments[2].componentName;
  256. var taskNum = arguments[2].taskNum;
  257. this.installComponent(componentName, hostName, taskNum);
  258. },
  259. onCreateComponentError: function (error) {
  260. if (error.responseText.indexOf('org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException') !== -1) {
  261. this.onCreateComponent();
  262. } else {
  263. this.onTaskError();
  264. }
  265. },
  266. installComponent: function (componentName, hostName, taskNum) {
  267. console.warn('func: installComponent');
  268. if (!(hostName instanceof Array)) {
  269. hostName = [hostName];
  270. }
  271. for (var i = 0; i < hostName.length; i++) {
  272. App.ajax.send({
  273. name: 'admin.high_availability.install_component',
  274. sender: this,
  275. data: {
  276. hostName: hostName[i],
  277. componentName: componentName,
  278. displayName: App.format.role(componentName),
  279. taskNum: taskNum || hostName.length
  280. },
  281. success: 'startPolling',
  282. error: 'onTaskError'
  283. });
  284. }
  285. },
  286. startComponent: function (componentName, hostName) {
  287. console.warn('func: startComponent');
  288. if (!(hostName instanceof Array)) {
  289. hostName = [hostName];
  290. }
  291. for (var i = 0; i < hostName.length; i++) {
  292. App.ajax.send({
  293. name: 'admin.high_availability.start_component',
  294. sender: this,
  295. data: {
  296. hostName: hostName[i],
  297. componentName: componentName,
  298. displayName: App.format.role(componentName),
  299. taskNum: hostName.length
  300. },
  301. success: 'startPolling',
  302. error: 'onTaskError'
  303. });
  304. }
  305. },
  306. stopComponent: function (componentName, hostName) {
  307. console.warn('func: stopComponent');
  308. if (!(hostName instanceof Array)) {
  309. hostName = [hostName];
  310. }
  311. for (var i = 0; i < hostName.length; i++) {
  312. App.ajax.send({
  313. name: 'admin.high_availability.stop_component',
  314. sender: this,
  315. data: {
  316. hostName: hostName[i],
  317. componentName: componentName,
  318. displayName: App.format.role(componentName),
  319. taskNum: hostName.length
  320. },
  321. success: 'startPolling',
  322. error: 'onTaskError'
  323. });
  324. }
  325. },
  326. startPolling: function (data) {
  327. if (data) {
  328. console.warn('func: startPolling1');
  329. this.get('currentRequestIds').push(data.Requests.id);
  330. var tasksCount = arguments[2].taskNum || 1;
  331. if (tasksCount === this.get('currentRequestIds').length) {
  332. console.warn('func: startPolling2');
  333. this.doPolling();
  334. }
  335. } else {
  336. console.warn('func: startPolling3');
  337. this.setTaskLogs(this.get('currentTaskId'), []);
  338. this.onTaskCompleted();
  339. }
  340. },
  341. doPolling: function () {
  342. console.warn('func: doPolling');
  343. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  344. var requestIds = this.get('currentRequestIds');
  345. for (var i = 0; i < requestIds.length; i++) {
  346. App.ajax.send({
  347. name: 'admin.high_availability.polling',
  348. sender: this,
  349. data: {
  350. requestId: requestIds[i]
  351. },
  352. success: 'parseLogs',
  353. error: 'onTaskError'
  354. });
  355. }
  356. },
  357. parseLogs: function (logs) {
  358. console.warn('func: parseLogs');
  359. this.get('logs').push(logs.tasks);
  360. if (this.get('currentRequestIds').length === this.get('logs').length) {
  361. var tasks = [];
  362. this.get('logs').forEach(function (logs) {
  363. tasks.pushObjects(logs);
  364. }, this);
  365. var self = this;
  366. var currentTaskId = this.get('currentTaskId');
  367. this.setTaskLogs(currentTaskId, tasks);
  368. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  369. this.set('currentRequestIds', []);
  370. if (tasks.someProperty('Tasks.status', 'FAILED') || tasks.someProperty('Tasks.status', 'TIMEDOUT') || tasks.someProperty('Tasks.status', 'ABORTED')) {
  371. this.setTaskStatus(currentTaskId, 'FAILED');
  372. } else {
  373. this.setTaskStatus(currentTaskId, 'COMPLETED');
  374. }
  375. } else {
  376. var actionsPerHost = tasks.length;
  377. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  378. + tasks.filterProperty('Tasks.status', 'FAILED').length
  379. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  380. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  381. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  382. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  383. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  384. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  385. window.setTimeout(function () {
  386. self.doPolling()
  387. }, self.POLL_INTERVAL);
  388. }
  389. this.set('logs', []);
  390. }
  391. },
  392. done: function () {
  393. if (!this.get('isSubmitDisabled')) {
  394. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  395. App.router.send('next');
  396. }
  397. }
  398. });