progress_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. }, {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, serviceName) {
  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. serviceName: serviceName,
  253. taskNum: array.length
  254. },
  255. success: 'onCreateComponent',
  256. error: 'onCreateComponentError'
  257. });
  258. } else {
  259. // Simulates format returned from ajax.send
  260. this.onCreateComponent(null, null, {hostName: host.hostName, componentName: host.componentName, taskNum: array.length});
  261. }
  262. }, this)
  263. },
  264. onCreateComponent: function () {
  265. var hostName = arguments[2].hostName;
  266. var componentName = arguments[2].componentName;
  267. var taskNum = arguments[2].taskNum;
  268. var serviceName = arguments[2].serviceName;
  269. this.updateComponent(componentName, hostName, serviceName, "Install", taskNum);
  270. },
  271. onCreateComponentError: function (error) {
  272. if (error.responseText.indexOf('org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException') !== -1) {
  273. this.onCreateComponent();
  274. } else {
  275. this.onTaskError();
  276. }
  277. },
  278. updateComponent: function (componentName, hostName, serviceName, context, taskNum) {
  279. if (!(hostName instanceof Array)) {
  280. hostName = [hostName];
  281. }
  282. var state = context.toLowerCase() == "start" ? "STARTED" : "INSTALLED";
  283. for (var i = 0; i < hostName.length; i++) {
  284. App.ajax.send({
  285. name: 'common.host.host_component.update',
  286. sender: this,
  287. data: {
  288. context: context + " " + App.format.role(componentName),
  289. hostName: hostName[i],
  290. serviceName: serviceName,
  291. componentName: componentName,
  292. taskNum: taskNum || hostName.length,
  293. HostRoles: {
  294. state: state
  295. }
  296. },
  297. success: 'startPolling',
  298. error: 'onTaskError'
  299. });
  300. }
  301. },
  302. startPolling: function (data) {
  303. if (data) {
  304. console.warn('func: startPolling1');
  305. this.get('currentRequestIds').push(data.Requests.id);
  306. var tasksCount = arguments[2].taskNum || 1;
  307. if (tasksCount === this.get('currentRequestIds').length) {
  308. this.setRequestIds(this.get('currentTaskId'), this.get('currentRequestIds'));
  309. console.warn('func: startPolling2');
  310. this.doPolling();
  311. }
  312. } else {
  313. console.warn('func: startPolling3');
  314. this.onTaskCompleted();
  315. }
  316. },
  317. doPolling: function () {
  318. console.warn('func: doPolling');
  319. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  320. var requestIds = this.get('currentRequestIds');
  321. this.set('logs', []);
  322. for (var i = 0; i < requestIds.length; i++) {
  323. App.ajax.send({
  324. name: 'admin.high_availability.polling',
  325. sender: this,
  326. data: {
  327. requestId: requestIds[i]
  328. },
  329. success: 'parseLogs',
  330. error: 'onTaskError'
  331. });
  332. }
  333. },
  334. parseLogs: function (logs) {
  335. console.warn('func: parseLogs');
  336. this.get('logs').pushObject(logs.tasks);
  337. if (this.get('currentRequestIds').length === this.get('logs').length) {
  338. var tasks = [];
  339. this.get('logs').forEach(function (logs) {
  340. tasks.pushObjects(logs);
  341. }, this);
  342. var self = this;
  343. var currentTaskId = this.get('currentTaskId');
  344. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  345. this.set('currentRequestIds', []);
  346. if (tasks.someProperty('Tasks.status', 'FAILED') || tasks.someProperty('Tasks.status', 'TIMEDOUT') || tasks.someProperty('Tasks.status', 'ABORTED')) {
  347. this.setTaskStatus(currentTaskId, 'FAILED');
  348. } else {
  349. this.setTaskStatus(currentTaskId, 'COMPLETED');
  350. }
  351. } else {
  352. var actionsPerHost = tasks.length;
  353. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  354. + tasks.filterProperty('Tasks.status', 'FAILED').length
  355. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  356. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  357. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  358. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  359. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  360. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  361. window.setTimeout(function () {
  362. self.doPolling()
  363. }, self.POLL_INTERVAL);
  364. }
  365. }
  366. },
  367. showHostProgressPopup: function (event) {
  368. var popupTitle = event.contexts[0].title;
  369. var requestIds = event.contexts[0].requestIds;
  370. var hostProgressPopupController = App.router.get('highAvailabilityProgressPopupController');
  371. hostProgressPopupController.initPopup(popupTitle, requestIds, this);
  372. },
  373. done: function () {
  374. if (!this.get('isSubmitDisabled')) {
  375. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  376. App.router.send('next');
  377. }
  378. }
  379. });