wizardProgressPageController.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /**
  20. * Mixin for wizard controller for showing command progress on wizard pages
  21. * This should
  22. * @type {Ember.Mixin}
  23. */
  24. App.wizardProgressPageControllerMixin = Em.Mixin.create({
  25. controllerName: '',
  26. clusterDeployState: 'WIZARD_DEPLOY',
  27. status: 'IN_PROGRESS',
  28. tasks: [],
  29. commands: [],
  30. currentRequestIds: [], //todo: replace with using requestIds from tasks
  31. logs: [],
  32. currentTaskId: null,
  33. POLL_INTERVAL: 4000,
  34. isSubmitDisabled: true,
  35. isBackButtonDisabled: true,
  36. /**
  37. * tasksMessagesPrefix should be overloaded by any controller including the mixin
  38. */
  39. tasksMessagesPrefix: '',
  40. loadStep: function () {
  41. this.clearStep();
  42. this.initializeTasks();
  43. this.loadTasks();
  44. this.addObserver('tasks.@each.status', this, 'onTaskStatusChange');
  45. this.onTaskStatusChange();
  46. },
  47. clearStep: function () {
  48. this.set('isSubmitDisabled', true);
  49. this.set('isBackButtonDisabled', true);
  50. this.set('tasks', []);
  51. this.set('currentRequestIds', []);
  52. },
  53. initializeTasks: function () {
  54. var commands = this.get('commands');
  55. var currentStep = App.router.get(this.get('content.controllerName') + '.currentStep');
  56. var tasksMessagesPrefix = this.get('tasksMessagesPrefix');
  57. for (var i = 0; i < commands.length; i++) {
  58. this.get('tasks').pushObject(Ember.Object.create({
  59. title: Em.I18n.t(tasksMessagesPrefix + currentStep + '.task' + i + '.title'),
  60. status: 'PENDING',
  61. id: i,
  62. command: commands[i],
  63. showRetry: false,
  64. showRollback: false,
  65. name: Em.I18n.t(tasksMessagesPrefix + currentStep + '.task' + i + '.title'),
  66. displayName: Em.I18n.t(tasksMessagesPrefix + currentStep + '.task' + i + '.title'),
  67. progress: 0,
  68. isRunning: false,
  69. requestIds: []
  70. }));
  71. }
  72. },
  73. loadTasks: function () {
  74. var self = this;
  75. var loadedStatuses = this.get('content.tasksStatuses');
  76. var loadedRequestIds = this.get('content.tasksRequestIds');
  77. if (loadedStatuses && loadedStatuses.length === this.get('tasks').length) {
  78. this.get('tasks').forEach(function (task, i) {
  79. self.setTaskStatus(task.get('id'), loadedStatuses[i]);
  80. self.setRequestIds(task.get('id'), loadedRequestIds[i]);
  81. });
  82. if (loadedStatuses.contains('IN_PROGRESS')) {
  83. var curTaskId = this.get('tasks')[loadedStatuses.indexOf('IN_PROGRESS')].get('id');
  84. this.set('currentRequestIds', this.get('content.requestIds'));
  85. this.set('currentTaskId', curTaskId);
  86. this.doPolling();
  87. } else if (loadedStatuses.contains('QUEUED')) {
  88. var curTaskId = this.get('tasks')[loadedStatuses.indexOf('QUEUED')].get('id');
  89. this.set('currentTaskId', curTaskId);
  90. this.runTask(curTaskId);
  91. }
  92. }
  93. },
  94. setTaskStatus: function (taskId, status) {
  95. this.get('tasks').findProperty('id', taskId).set('status', status);
  96. },
  97. setRequestIds: function (taskId, requestIds) {
  98. this.get('tasks').findProperty('id', taskId).set('requestIds', requestIds);
  99. },
  100. retryTask: function () {
  101. var task = this.get('tasks').findProperty('status', 'FAILED');
  102. task.set('showRetry', false);
  103. task.set('showRollback', false);
  104. task.set('status', 'PENDING');
  105. },
  106. onTaskStatusChange: function () {
  107. var statuses = this.get('tasks').mapProperty('status');
  108. var tasksRequestIds = this.get('tasks').mapProperty('requestIds');
  109. var requestIds = this.get('currentRequestIds');
  110. // save task info
  111. App.router.get(this.get('content.controllerName')).saveTasksStatuses(statuses);
  112. App.router.get(this.get('content.controllerName')).saveTasksRequestIds(tasksRequestIds);
  113. App.router.get(this.get('content.controllerName')).saveRequestIds(requestIds);
  114. // call saving of cluster status asynchronous
  115. // synchronous executing cause problems in Firefox
  116. App.clusterStatus.setClusterStatus({
  117. clusterName: App.router.getClusterName(),
  118. clusterState: this.get('clusterDeployState'),
  119. wizardControllerName: this.get('content.controllerName'),
  120. localdb: App.db.data
  121. }, {successCallback: this.statusChangeCallback, sender: this});
  122. },
  123. /**
  124. * Method that called after saving persist data to server.
  125. * Switch task according its status.
  126. */
  127. statusChangeCallback: function () {
  128. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  129. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  130. if (nextTask) {
  131. this.set('status', 'IN_PROGRESS');
  132. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  133. this.set('currentTaskId', nextTask.get('id'));
  134. this.runTask(nextTask.get('id'));
  135. } else {
  136. this.set('status', 'COMPLETED');
  137. this.set('isSubmitDisabled', false);
  138. this.set('isBackButtonDisabled', false);
  139. }
  140. } else if (this.get('tasks').someProperty('status', 'FAILED')) {
  141. this.set('status', 'FAILED');
  142. this.set('isBackButtonDisabled', false);
  143. this.get('tasks').findProperty('status', 'FAILED').set('showRetry', true);
  144. if (App.supports.autoRollbackHA) {
  145. this.get('tasks').findProperty('status', 'FAILED').set('showRollback', true);
  146. }
  147. }
  148. this.get('tasks').filterProperty('status', 'COMPLETED').setEach('showRetry', false);
  149. this.get('tasks').filterProperty('status', 'COMPLETED').setEach('showRollback', false);
  150. },
  151. /**
  152. * Run command of appropriate task
  153. */
  154. runTask: function (taskId) {
  155. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  156. },
  157. onTaskError: function () {
  158. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  159. },
  160. onTaskCompleted: function () {
  161. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  162. },
  163. /**
  164. * check whether component installed on specified hosts
  165. * @param componentName
  166. * @param hostNames
  167. * @return {$.ajax}
  168. */
  169. checkInstalledComponents: function (componentName, hostNames) {
  170. return App.ajax.send({
  171. name: 'host_component.installed.on_hosts',
  172. sender: this,
  173. data: {
  174. componentName: componentName,
  175. hostNames: hostNames.join(',')
  176. },
  177. success: 'checkInstalledComponentsSuccessCallback'
  178. });
  179. },
  180. checkInstalledComponentsSuccessCallback: function (data, opt, params) {
  181. installedComponents = data.items;
  182. },
  183. createComponent: function (componentName, hostName, serviceName) {
  184. var hostNames = (Array.isArray(hostName)) ? hostName : [hostName];
  185. var self = this;
  186. this.checkInstalledComponents(componentName, hostNames).complete(function () {
  187. var result = [];
  188. hostNames.forEach(function (hostName) {
  189. result.push({
  190. componentName: componentName,
  191. hostName: hostName,
  192. hasComponent: installedComponents.someProperty('HostRoles.host_name', hostName)
  193. });
  194. });
  195. result.forEach(function (host, index, array) {
  196. if (!host.hasComponent) {
  197. App.ajax.send({
  198. name: 'admin.high_availability.create_component',
  199. sender: this,
  200. data: {
  201. hostName: host.hostName,
  202. componentName: host.componentName,
  203. serviceName: serviceName,
  204. taskNum: array.length
  205. },
  206. success: 'onCreateComponent',
  207. error: 'onCreateComponentError'
  208. });
  209. } else {
  210. // Simulates format returned from ajax.send
  211. this.onCreateComponent(null, null, {hostName: host.hostName, componentName: host.componentName, taskNum: array.length});
  212. }
  213. }, self)
  214. });
  215. },
  216. onCreateComponent: function () {
  217. var hostName = arguments[2].hostName;
  218. var componentName = arguments[2].componentName;
  219. var taskNum = arguments[2].taskNum;
  220. var serviceName = arguments[2].serviceName;
  221. this.updateComponent(componentName, hostName, serviceName, "Install", taskNum);
  222. },
  223. onCreateComponentError: function (error) {
  224. if (error.responseText.indexOf('org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException') !== -1) {
  225. this.onCreateComponent();
  226. } else {
  227. this.onTaskError();
  228. }
  229. },
  230. updateComponent: function (componentName, hostName, serviceName, context, taskNum) {
  231. if (!(hostName instanceof Array)) {
  232. hostName = [hostName];
  233. }
  234. var state = context.toLowerCase() == "start" ? "STARTED" : "INSTALLED";
  235. for (var i = 0; i < hostName.length; i++) {
  236. App.ajax.send({
  237. name: 'common.host.host_component.update',
  238. sender: this,
  239. data: {
  240. context: context + " " + App.format.role(componentName),
  241. hostName: hostName[i],
  242. serviceName: serviceName,
  243. componentName: componentName,
  244. taskNum: taskNum || hostName.length,
  245. HostRoles: {
  246. state: state
  247. }
  248. },
  249. success: 'startPolling',
  250. error: 'onTaskError'
  251. });
  252. }
  253. },
  254. startPolling: function (data) {
  255. if (data) {
  256. this.get('currentRequestIds').push(data.Requests.id);
  257. var tasksCount = arguments[2].taskNum || 1;
  258. if (tasksCount === this.get('currentRequestIds').length) {
  259. this.setRequestIds(this.get('currentTaskId'), this.get('currentRequestIds'));
  260. this.doPolling();
  261. }
  262. } else {
  263. this.onTaskCompleted();
  264. }
  265. },
  266. doPolling: function () {
  267. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  268. var requestIds = this.get('currentRequestIds');
  269. this.set('logs', []);
  270. for (var i = 0; i < requestIds.length; i++) {
  271. App.ajax.send({
  272. name: 'admin.high_availability.polling',
  273. sender: this,
  274. data: {
  275. requestId: requestIds[i]
  276. },
  277. success: 'parseLogs',
  278. error: 'onTaskError'
  279. });
  280. }
  281. },
  282. parseLogs: function (logs) {
  283. this.get('logs').pushObject(logs.tasks);
  284. if (this.get('currentRequestIds').length === this.get('logs').length) {
  285. var tasks = [];
  286. this.get('logs').forEach(function (logs) {
  287. tasks.pushObjects(logs);
  288. }, this);
  289. var self = this;
  290. var currentTaskId = this.get('currentTaskId');
  291. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  292. this.set('currentRequestIds', []);
  293. if (tasks.someProperty('Tasks.status', 'FAILED') || tasks.someProperty('Tasks.status', 'TIMEDOUT') || tasks.someProperty('Tasks.status', 'ABORTED')) {
  294. this.setTaskStatus(currentTaskId, 'FAILED');
  295. } else {
  296. this.setTaskStatus(currentTaskId, 'COMPLETED');
  297. }
  298. } else {
  299. var actionsPerHost = tasks.length;
  300. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  301. + tasks.filterProperty('Tasks.status', 'FAILED').length
  302. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  303. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  304. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  305. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  306. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  307. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  308. window.setTimeout(function () {
  309. self.doPolling()
  310. }, self.POLL_INTERVAL);
  311. }
  312. }
  313. },
  314. showHostProgressPopup: function (event) {
  315. var popupTitle = event.contexts[0].title;
  316. var requestIds = event.contexts[0].requestIds;
  317. var hostProgressPopupController = App.router.get('highAvailabilityProgressPopupController');
  318. hostProgressPopupController.initPopup(popupTitle, requestIds, this, true);
  319. },
  320. done: function () {
  321. if (!this.get('isSubmitDisabled')) {
  322. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  323. App.router.send('next');
  324. }
  325. },
  326. back: function () {
  327. if (!this.get('isBackButtonDisabled')) {
  328. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  329. App.router.send('back');
  330. }
  331. }
  332. });