-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathproject-service.ts
More file actions
216 lines (182 loc) · 7.28 KB
/
project-service.ts
File metadata and controls
216 lines (182 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import BaseAPIService from './api-service';
import { environmentStore } from '$lib/stores/environment.store.svelte';
import type { Project, ProjectStatusCounts } from '$lib/types/project.type';
import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';
import { transformPaginationParams } from '$lib/utils/params.util';
import { m } from '$lib/paraglide/messages';
export class ProjectService extends BaseAPIService {
async getProjects(options?: SearchPaginationSortRequest): Promise<Paginated<Project>> {
const envId = await environmentStore.getCurrentEnvironmentId();
// Map projectStatus back to status for the API
const apiOptions = options ? { ...options } : undefined;
if (apiOptions?.filters && 'projectStatus' in apiOptions.filters) {
apiOptions.filters = { ...apiOptions.filters };
apiOptions.filters.status = apiOptions.filters.projectStatus;
delete apiOptions.filters.projectStatus;
}
const params = transformPaginationParams(apiOptions);
const res = await this.api.get(`/environments/${envId}/projects`, { params });
return res.data;
}
deployProject(projectId: string): Promise<Project>;
deployProject(projectId: string, onLine: (data: any) => void): Promise<Project>;
async deployProject(projectId: string, onLine?: (data: any) => void): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
const url = `/api/environments/${envId}/projects/${projectId}/up`;
const res = await fetch(url, { method: 'POST' });
if (!res.ok || !res.body) {
throw new Error(m.progress_deploy_failed_to_start({ status: String(res.status) }));
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
let obj: any;
try {
obj = JSON.parse(trimmed);
} catch {
continue;
}
onLine?.(obj);
if (obj?.error) {
throw new Error(typeof obj.error === 'string' ? obj.error : obj.error?.message || m.progress_deploy_failed());
}
}
}
// The deploy stream doesn't return the project object; fetch fresh details.
return this.getProject(projectId);
}
async downProject(projectName: string): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
return this.handleResponse(this.api.post(`/environments/${envId}/projects/${projectName}/down`));
}
async createProject(projectName: string, composeContent: string, envContent?: string): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
const payload = {
name: projectName,
composeContent,
envContent
};
return this.handleResponse(this.api.post(`/environments/${envId}/projects`, payload));
}
async getProject(projectId: string): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
const response = await this.handleResponse<{ project?: Project; success?: boolean }>(
this.api.get(`/environments/${envId}/projects/${projectId}`)
);
return response.project ? response.project : (response as Project);
}
async getProjectStatusCounts(): Promise<ProjectStatusCounts> {
const envId = await environmentStore.getCurrentEnvironmentId();
const res = await this.api.get(`/environments/${envId}/projects/counts`);
return res.data.data;
}
async updateProject(projectId: string, name: string, composeContent: string, envContent?: string): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
const payload = {
name,
composeContent,
envContent
};
return this.handleResponse(this.api.put(`/environments/${envId}/projects/${projectId}`, payload));
}
async updateProjectIncludeFile(projectId: string, relativePath: string, content: string): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
const payload = {
relativePath,
content
};
return this.handleResponse(this.api.put(`/environments/${envId}/projects/${projectId}/includes`, payload));
}
async restartProject(projectId: string): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
return this.handleResponse(this.api.post(`/environments/${envId}/projects/${projectId}/restart`));
}
async redeployProject(projectName: string): Promise<Project> {
const envId = await environmentStore.getCurrentEnvironmentId();
return this.handleResponse(this.api.post(`/environments/${envId}/projects/${projectName}/redeploy`));
}
private isDownloadingStatus(status?: string): boolean {
if (!status) return false;
const s = status.toLowerCase();
return (
s.includes('downloading') ||
s.includes('extracting') ||
s.includes('pull complete') ||
s.includes('download complete') ||
s.includes('pulling fs layer')
);
}
private async streamProjectPull(projectId: string, onLine?: (data: any) => void): Promise<boolean> {
const envId = await environmentStore.getCurrentEnvironmentId();
const url = `/api/environments/${envId}/projects/${projectId}/pull`;
const res = await fetch(url, { method: 'POST' });
if (!res.ok || !res.body) {
throw new Error(`Failed to start project image pull (${res.status})`);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let pulled = false;
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
try {
const obj = JSON.parse(trimmed);
// Detect if any actual download happened
if (!pulled) {
const status = obj?.status as string | undefined;
const total = obj?.progressDetail?.total as number | undefined;
if (this.isDownloadingStatus(status) || (typeof total === 'number' && total > 0)) {
pulled = true;
}
}
onLine?.(obj);
} catch {
// ignore malformed line
}
}
}
return pulled;
}
pullProjectImages(projectId: string): Promise<void>;
pullProjectImages(projectId: string, onLine: (data: any) => void): Promise<void>;
async pullProjectImages(projectId: string, onLine?: (data: any) => void): Promise<void> {
await this.streamProjectPull(projectId, onLine);
}
async deployProjectMaybePull(
projectId: string,
onPullLine?: (data: any) => void,
onDeployLine?: (data: any) => void
): Promise<{ pulled: boolean; project: Project }> {
const pulled = await this.streamProjectPull(projectId, onPullLine);
const project = onDeployLine ? await this.deployProject(projectId, onDeployLine) : await this.deployProject(projectId);
return { pulled, project };
}
async destroyProject(projectName: string, removeVolumes = false, removeFiles = false): Promise<void> {
const envId = await environmentStore.getCurrentEnvironmentId();
await this.handleResponse(
this.api.delete(`/environments/${envId}/projects/${projectName}/destroy`, {
data: {
removeVolumes,
removeFiles
}
})
);
}
}
export const projectService = new ProjectService();