]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
a04b4577365867f37b3bdd2b5b0fad83208b2fcf
[ceph-ci.git] /
1 import { Component, OnDestroy, OnInit } from '@angular/core';
2
3 import { Observable, ReplaySubject, Subscription } from 'rxjs';
4 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
5
6 import { Icons } from '~/app/shared/enum/icons.enum';
7 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
8 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
9 import { Permission } from '~/app/shared/models/permissions';
10 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
11 import { UpgradeService } from '~/app/shared/api/upgrade.service';
12 import { NotificationService } from '~/app/shared/services/notification.service';
13 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
14 import { SummaryService } from '~/app/shared/services/summary.service';
15 import { ExecutingTask } from '~/app/shared/models/executing-task';
16 import { shareReplay, switchMap, tap } from 'rxjs/operators';
17 import { Router } from '@angular/router';
18 import { RefreshIntervalService } from '~/app/shared/services/refresh-interval.service';
19 import { UpgradeStatusInterface } from '~/app/shared/models/upgrade.interface';
20
21 @Component({
22   selector: 'cd-upgrade-progress',
23   templateUrl: './upgrade-progress.component.html',
24   styleUrls: ['./upgrade-progress.component.scss']
25 })
26 export class UpgradeProgressComponent implements OnInit, OnDestroy {
27   permission: Permission;
28   icons = Icons;
29   modalRef: NgbModalRef;
30   interval = new Subscription();
31   executingTask: ExecutingTask;
32
33   upgradeStatus$: Observable<UpgradeStatusInterface>;
34   subject = new ReplaySubject<UpgradeStatusInterface>();
35
36   constructor(
37     private authStorageService: AuthStorageService,
38     private upgradeService: UpgradeService,
39     private notificationService: NotificationService,
40     private modalService: ModalCdsService,
41     private summaryService: SummaryService,
42     private router: Router,
43     private refreshIntervalService: RefreshIntervalService
44   ) {
45     this.permission = this.authStorageService.getPermissions().configOpt;
46   }
47
48   ngOnInit() {
49     this.upgradeStatus$ = this.subject.pipe(
50       switchMap(() => this.upgradeService.status()),
51       tap((status: UpgradeStatusInterface) => {
52         if (!status.in_progress) {
53           this.router.navigate(['/upgrade']);
54         }
55       }),
56       shareReplay(1)
57     );
58
59     this.interval = this.refreshIntervalService.intervalData$.subscribe(() => {
60       this.fetchStatus();
61     });
62
63     this.summaryService.subscribe((summary) => {
64       this.executingTask = summary.executing_tasks.filter((tasks) =>
65         tasks.name.includes('progress/Upgrade')
66       )[0];
67     });
68   }
69
70   pauseUpgrade() {
71     this.upgradeService.pause().subscribe({
72       error: (error) => {
73         this.notificationService.show(
74           NotificationType.error,
75           $localize`Failed to pause the upgrade`,
76           error
77         );
78       },
79       complete: () => {
80         this.notificationService.show(NotificationType.success, $localize`The upgrade is paused`);
81         this.fetchStatus();
82       }
83     });
84   }
85
86   fetchStatus() {
87     this.subject.next();
88   }
89
90   resumeUpgrade(modal = false) {
91     this.upgradeService.resume().subscribe({
92       error: (error) => {
93         this.notificationService.show(
94           NotificationType.error,
95           $localize`Failed to resume the upgrade`,
96           error
97         );
98       },
99       complete: () => {
100         this.fetchStatus();
101         this.notificationService.show(NotificationType.success, $localize`Upgrade is resumed`);
102         if (modal) {
103           this.modalRef.close();
104         }
105       }
106     });
107   }
108
109   stopUpgradeModal() {
110     // pause the upgrade meanwhile we get stop confirmation from user
111     this.pauseUpgrade();
112     this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
113       itemDescription: 'Upgrade',
114       actionDescription: 'stop',
115       submitAction: () => this.stopUpgrade(),
116       callBackAtionObservable: () => this.resumeUpgrade(true)
117     });
118   }
119
120   stopUpgrade() {
121     this.modalRef.close();
122     this.upgradeService.stop().subscribe({
123       error: (error) => {
124         this.notificationService.show(
125           NotificationType.error,
126           $localize`Failed to stop the upgrade`,
127           error
128         );
129       },
130       complete: () => {
131         this.notificationService.show(NotificationType.success, $localize`The upgrade is stopped`);
132         this.router.navigate(['/upgrade']);
133       }
134     });
135   }
136
137   ngOnDestroy() {
138     this.interval?.unsubscribe();
139   }
140 }