progress_controller.js 15 KB

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