progress_controller.js 15 KB

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