progress_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. var installedComponents = [];
  20. App.HighAvailabilityProgressPageController = App.HighAvailabilityWizardController.extend({
  21. name: 'highAvailabilityProgressPageController',
  22. status: 'IN_PROGRESS',
  23. clusterDeployState: 'HIGH_AVAILABILITY_DEPLOY',
  24. tasks: [],
  25. commands: [],
  26. currentRequestIds: [], //todo: replace with using requestIds from tasks
  27. logs: [],
  28. currentTaskId: null,
  29. POLL_INTERVAL: 4000,
  30. isSubmitDisabled: true,
  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('currentRequestIds', []);
  45. },
  46. initializeTasks: function () {
  47. console.warn('func: initializeTasks');
  48. var commands = this.get('commands');
  49. var currentStep = App.router.get('highAvailabilityWizardController.currentStep');
  50. for (var i = 0; i < commands.length; i++) {
  51. this.get('tasks').pushObject(Ember.Object.create({
  52. title: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  53. status: 'PENDING',
  54. id: i,
  55. command: commands[i],
  56. showRetry: false,
  57. showRollback: false,
  58. name: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  59. displayName: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  60. progress: 0,
  61. isRunning: false,
  62. requestIds: []
  63. }));
  64. }
  65. },
  66. loadTasks: function () {
  67. console.warn('func: loadTasks');
  68. var self = this;
  69. var loadedStatuses = this.get('content.tasksStatuses');
  70. var loadedRequestIds = this.get('content.tasksRequestIds');
  71. if (loadedStatuses && loadedStatuses.length === this.get('tasks').length) {
  72. this.get('tasks').forEach(function (task, i) {
  73. self.setTaskStatus(task.get('id'), loadedStatuses[i]);
  74. self.setRequestIds(task.get('id'), loadedRequestIds[i]);
  75. });
  76. if (loadedStatuses.contains('IN_PROGRESS')) {
  77. var curTaskId = this.get('tasks')[loadedStatuses.indexOf('IN_PROGRESS')].get('id');
  78. this.set('currentRequestIds', this.get('content.requestIds'));
  79. this.set('currentTaskId', curTaskId);
  80. this.doPolling();
  81. } else if (loadedStatuses.contains('QUEUED')) {
  82. var curTaskId = this.get('tasks')[loadedStatuses.indexOf('QUEUED')].get('id');
  83. this.set('currentTaskId', curTaskId);
  84. this.runTask(curTaskId);
  85. }
  86. }
  87. },
  88. setTaskStatus: function (taskId, status) {
  89. console.warn('func: setTaskStatus');
  90. this.get('tasks').findProperty('id', taskId).set('status', status);
  91. },
  92. setRequestIds: function (taskId, requestIds) {
  93. this.get('tasks').findProperty('id', taskId).set('requestIds', requestIds);
  94. },
  95. retryTask: function () {
  96. console.warn('func: retryTask');
  97. var task = this.get('tasks').findProperty('status', 'FAILED');
  98. task.set('showRetry', false);
  99. task.set('showRollback', false);
  100. task.set('status', 'PENDING');
  101. },
  102. manualRollback: function () {
  103. console.warn('func: manualRollback');
  104. App.ModalPopup.show({
  105. header: Em.I18n.t('admin.highAvailability.confirmRollbackHeader'),
  106. primary: Em.I18n.t('yes'),
  107. showCloseButton: false,
  108. onPrimary: function () {
  109. var controller = App.router.get('highAvailabilityWizardController');
  110. controller.clearTasksData();
  111. controller.clearStorageData();
  112. controller.setCurrentStep('1');
  113. App.router.get('updateController').set('isWorking', true);
  114. App.clusterStatus.setClusterStatus({
  115. clusterName: App.router.get('content.cluster.name'),
  116. clusterState: 'DEFAULT',
  117. wizardControllerName: App.router.get('highAvailabilityRollbackController.name'),
  118. localdb: App.db.data
  119. });
  120. this.hide();
  121. App.router.transitionTo('main.admin.index');
  122. location.reload();
  123. },
  124. secondary: Em.I18n.t('no'),
  125. onSecondary: function () {
  126. this.hide();
  127. },
  128. bodyClass: Ember.View.extend({
  129. template: Ember.Handlebars.compile(Em.I18n.t('admin.highAvailability.confirmManualRollbackBody'))
  130. })
  131. });
  132. },
  133. rollback: function () {
  134. console.warn('func: rollback');
  135. var task = this.get('tasks').findProperty('status', 'FAILED');
  136. App.router.get(this.get('content.controllerName')).saveFailedTask(task);
  137. App.ModalPopup.show({
  138. header: Em.I18n.t('admin.highAvailability.confirmRollbackHeader'),
  139. primary: Em.I18n.t('common.confirm'),
  140. showCloseButton: false,
  141. onPrimary: function () {
  142. App.router.get('highAvailabilityWizardController').clearTasksData();
  143. App.router.transitionTo('main.admin.highAvailabilityRollback');
  144. this.hide();
  145. },
  146. secondary: Em.I18n.t('common.cancel'),
  147. body: Em.I18n.t('admin.highAvailability.confirmRollbackBody')
  148. });
  149. },
  150. onTaskStatusChange: function () {
  151. console.warn('func: onTaskStatusChange1');
  152. var statuses = this.get('tasks').mapProperty('status');
  153. var tasksRequestIds = this.get('tasks').mapProperty('requestIds');
  154. var requestIds = this.get('currentRequestIds');
  155. console.warn('func: onTaskStatusChange5', statuses, tasksRequestIds, requestIds);
  156. // save task info
  157. App.router.get(this.get('content.controllerName')).saveTasksStatuses(statuses);
  158. App.router.get(this.get('content.controllerName')).saveTasksRequestIds(tasksRequestIds);
  159. App.router.get(this.get('content.controllerName')).saveRequestIds(requestIds);
  160. // call saving of cluster status asynchronous
  161. // synchronous executing cause problems in Firefox
  162. App.clusterStatus.setClusterStatus({
  163. clusterName: this.get('content.cluster.name'),
  164. clusterState: this.get('clusterDeployState'),
  165. wizardControllerName: this.get('content.controllerName'),
  166. localdb: App.db.data
  167. }, { async: true, success: 'statusChangeCallback', sender: this });
  168. },
  169. /**
  170. * Method that called after saving persist data to server.
  171. * Switch task according its status.
  172. */
  173. statusChangeCallback: function () {
  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. },
  198. /**
  199. * Run command of appropriate task
  200. */
  201. runTask: function (taskId) {
  202. console.warn('func: runTask', taskId);
  203. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  204. },
  205. onTaskError: function () {
  206. console.warn('func: onTaskError');
  207. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  208. },
  209. onTaskCompleted: function () {
  210. console.warn('func: onTaskCompleted');
  211. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  212. },
  213. /**
  214. * check whether component installed on specified hosts
  215. * @param componentName
  216. * @param hostNames
  217. * @return {Array}
  218. */
  219. checkInstalledComponents: function (componentName, hostNames) {
  220. var result = [];
  221. App.ajax.send({
  222. name: 'host_component.installed.on_hosts',
  223. sender: this,
  224. data: {
  225. componentName: componentName,
  226. hostNames: hostNames.join(',')
  227. },
  228. success: 'checkInstalledComponentsSuccessCallback'
  229. });
  230. hostNames.forEach(function (hostName) {
  231. result.push({
  232. componentName: componentName,
  233. hostName: hostName,
  234. hasComponent: installedComponents.someProperty('HostRoles.host_name', hostName)
  235. });
  236. });
  237. return result;
  238. },
  239. checkInstalledComponentsSuccessCallback: function (data, opt, params) {
  240. installedComponents = data.items;
  241. },
  242. createComponent: function (componentName, hostName) {
  243. var hostNames = (Array.isArray(hostName)) ? hostName : [hostName];
  244. this.checkInstalledComponents(componentName, hostNames).forEach(function (host, index, array) {
  245. if (!host.hasComponent) {
  246. App.ajax.send({
  247. name: 'admin.high_availability.create_component',
  248. sender: this,
  249. data: {
  250. hostName: host.hostName,
  251. componentName: host.componentName,
  252. taskNum: array.length
  253. },
  254. success: 'onCreateComponent',
  255. error: 'onCreateComponentError'
  256. });
  257. } else {
  258. // Simulates format returned from ajax.send
  259. this.onCreateComponent(null, null, {hostName: host.hostName, componentName: host.componentName, taskNum: array.length});
  260. }
  261. }, this)
  262. },
  263. onCreateComponent: function () {
  264. console.warn('func: onCreateComponent');
  265. var hostName = arguments[2].hostName;
  266. var componentName = arguments[2].componentName;
  267. var taskNum = arguments[2].taskNum;
  268. this.installComponent(componentName, hostName, taskNum);
  269. },
  270. onCreateComponentError: function (error) {
  271. if (error.responseText.indexOf('org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException') !== -1) {
  272. this.onCreateComponent();
  273. } else {
  274. this.onTaskError();
  275. }
  276. },
  277. installComponent: function (componentName, hostName, taskNum) {
  278. console.warn('func: installComponent');
  279. if (!(hostName instanceof Array)) {
  280. hostName = [hostName];
  281. }
  282. for (var i = 0; i < hostName.length; i++) {
  283. App.ajax.send({
  284. name: 'admin.high_availability.install_component',
  285. sender: this,
  286. data: {
  287. hostName: hostName[i],
  288. componentName: componentName,
  289. displayName: App.format.role(componentName),
  290. taskNum: taskNum || hostName.length
  291. },
  292. success: 'startPolling',
  293. error: 'onTaskError'
  294. });
  295. }
  296. },
  297. startComponent: function (componentName, hostName) {
  298. console.warn('func: startComponent');
  299. if (!(hostName instanceof Array)) {
  300. hostName = [hostName];
  301. }
  302. for (var i = 0; i < hostName.length; i++) {
  303. App.ajax.send({
  304. name: 'admin.high_availability.start_component',
  305. sender: this,
  306. data: {
  307. hostName: hostName[i],
  308. componentName: componentName,
  309. displayName: App.format.role(componentName),
  310. taskNum: hostName.length
  311. },
  312. success: 'startPolling',
  313. error: 'onTaskError'
  314. });
  315. }
  316. },
  317. stopComponent: function (componentName, hostName) {
  318. console.warn('func: stopComponent');
  319. if (!(hostName instanceof Array)) {
  320. hostName = [hostName];
  321. }
  322. for (var i = 0; i < hostName.length; i++) {
  323. App.ajax.send({
  324. name: 'admin.high_availability.stop_component',
  325. sender: this,
  326. data: {
  327. hostName: hostName[i],
  328. componentName: componentName,
  329. displayName: App.format.role(componentName),
  330. taskNum: hostName.length
  331. },
  332. success: 'startPolling',
  333. error: 'onTaskError'
  334. });
  335. }
  336. },
  337. startPolling: function (data) {
  338. if (data) {
  339. console.warn('func: startPolling1');
  340. this.get('currentRequestIds').push(data.Requests.id);
  341. var tasksCount = arguments[2].taskNum || 1;
  342. if (tasksCount === this.get('currentRequestIds').length) {
  343. this.setRequestIds(this.get('currentTaskId'), this.get('currentRequestIds'));
  344. console.warn('func: startPolling2');
  345. this.doPolling();
  346. }
  347. } else {
  348. console.warn('func: startPolling3');
  349. this.onTaskCompleted();
  350. }
  351. },
  352. doPolling: function () {
  353. console.warn('func: doPolling');
  354. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  355. var requestIds = this.get('currentRequestIds');
  356. this.set('logs', []);
  357. for (var i = 0; i < requestIds.length; i++) {
  358. App.ajax.send({
  359. name: 'admin.high_availability.polling',
  360. sender: this,
  361. data: {
  362. requestId: requestIds[i]
  363. },
  364. success: 'parseLogs',
  365. error: 'onTaskError'
  366. });
  367. }
  368. },
  369. parseLogs: function (logs) {
  370. console.warn('func: parseLogs');
  371. this.get('logs').pushObject(logs.tasks);
  372. if (this.get('currentRequestIds').length === this.get('logs').length) {
  373. var tasks = [];
  374. this.get('logs').forEach(function (logs) {
  375. tasks.pushObjects(logs);
  376. }, this);
  377. var self = this;
  378. var currentTaskId = this.get('currentTaskId');
  379. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  380. this.set('currentRequestIds', []);
  381. if (tasks.someProperty('Tasks.status', 'FAILED') || tasks.someProperty('Tasks.status', 'TIMEDOUT') || tasks.someProperty('Tasks.status', 'ABORTED')) {
  382. this.setTaskStatus(currentTaskId, 'FAILED');
  383. } else {
  384. this.setTaskStatus(currentTaskId, 'COMPLETED');
  385. }
  386. } else {
  387. var actionsPerHost = tasks.length;
  388. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  389. + tasks.filterProperty('Tasks.status', 'FAILED').length
  390. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  391. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  392. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  393. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  394. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  395. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  396. window.setTimeout(function () {
  397. self.doPolling()
  398. }, self.POLL_INTERVAL);
  399. }
  400. }
  401. },
  402. showHostProgressPopup: function (event) {
  403. var popupTitle = event.contexts[0].title;
  404. var requestIds = event.contexts[0].requestIds;
  405. var hostProgressPopupController = App.router.get('highAvailabilityProgressPopupController');
  406. hostProgressPopupController.initPopup(popupTitle, requestIds, this);
  407. },
  408. done: function () {
  409. if (!this.get('isSubmitDisabled')) {
  410. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  411. App.router.send('next');
  412. }
  413. }
  414. });