-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvironment.ts
266 lines (226 loc) · 7.64 KB
/
environment.ts
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { Gitpod } from '../client';
import { Environment } from '../resources/environments/environments';
import { EnvironmentClass } from '../resources/shared';
/**
* Maintains the current state of an environment and updates it via event stream.
* Uses event emitter pattern for state updates.
*/
export class EnvironmentState {
private environment?: Environment;
private ready: Promise<void>;
private resolveReady!: () => void;
private listeners: Array<(env: Environment) => void> = [];
private shouldStop = false;
private controller: AbortController | undefined;
private updateTask: Promise<void>;
constructor(
private client: Gitpod,
private environmentId: string,
) {
this.ready = new Promise((resolve) => {
this.resolveReady = resolve;
});
this.updateTask = this.startUpdateLoop();
}
async getEnvironment(): Promise<Environment> {
await this.ready;
if (!this.environment) {
throw new Error('Environment not initialized');
}
return this.environment;
}
private async updateEnvironment(): Promise<void> {
try {
const resp = await this.client.environments.retrieve({ environmentId: this.environmentId });
const env = resp.environment;
this.environment = env;
this.resolveReady();
for (const listener of [...this.listeners]) {
try {
listener(env);
} catch (error) {
console.error('Failed to call listener:', error);
}
}
} catch (error) {
console.error('Failed to update environment:', error);
}
}
private async startUpdateLoop(): Promise<void> {
let retryDelay = 1000; // Initial retry delay in milliseconds
const maxDelay = 32000; // Maximum retry delay
await this.updateEnvironment();
while (!this.shouldStop) {
try {
if (this.shouldStop) return;
this.controller = new AbortController();
const eventStream = await this.client.events.watch(
{
environmentId: this.environmentId,
},
{
signal: this.controller.signal,
},
);
retryDelay = 1000; // Reset delay on successful connection
if (this.shouldStop) return;
await this.updateEnvironment();
if (this.shouldStop) return;
try {
for await (const event of eventStream) {
if (this.shouldStop) return;
if (
event.resourceType === 'RESOURCE_TYPE_ENVIRONMENT' &&
event.resourceId === this.environmentId
) {
await this.updateEnvironment();
}
}
} finally {
this.controller?.abort();
this.controller = undefined;
}
} catch (error) {
if (this.shouldStop || (error instanceof Error && error.name === 'AbortError')) {
return;
}
console.error(`Error in event stream, retrying in ${retryDelay}ms:`, error);
await new Promise((resolve) => setTimeout(resolve, retryDelay));
retryDelay = Math.min(retryDelay * 2, maxDelay);
}
}
}
async close(): Promise<void> {
this.shouldStop = true;
this.controller?.abort();
await this.updateTask;
}
async waitUntil<T>(checkFn: (env: Environment) => T | undefined): Promise<T> {
const initialEnv = await this.getEnvironment();
const initialCheck = checkFn(initialEnv);
if (initialCheck !== undefined) {
return initialCheck;
}
return new Promise<T>((resolve) => {
const listener = (env: Environment) => {
const result = checkFn(env);
if (result !== undefined) {
this.listeners = this.listeners.filter((l) => l !== listener);
resolve(result);
}
};
this.listeners.push(listener);
});
}
isRunning(env: Environment): boolean {
if (!env.status) {
return false;
}
if (env.status.failureMessage) {
throw new Error(`Environment ${env.id} failed: ${env.status.failureMessage.join('; ')}`);
} else if (
[
'ENVIRONMENT_PHASE_STOPPING',
'ENVIRONMENT_PHASE_STOPPED',
'ENVIRONMENT_PHASE_DELETING',
'ENVIRONMENT_PHASE_DELETED',
].includes(env.status.phase || '')
) {
throw new Error(`Environment ${env.id} is in unexpected phase: ${env.status.phase}`);
}
return env.status.phase === 'ENVIRONMENT_PHASE_RUNNING';
}
getSshUrl(env: Environment): string | undefined {
return env.status?.environmentUrls?.ssh?.url;
}
getPortUrl(env: Environment, port: number): string | undefined {
return env.status?.environmentUrls?.ports?.find((p) => p.port === port)?.url;
}
checkSshKeyApplied(env: Environment, keyId: string, keyValue: string): boolean {
if (!env.spec?.sshPublicKeys) {
return false;
}
const key = env.spec.sshPublicKeys.find((k) => k.id === keyId);
if (!key) {
throw new Error(`SSH key '${keyId}' not found in environment spec`);
}
if (key.value !== keyValue) {
throw new Error(`SSH key '${keyId}' has incorrect value`);
}
if (!env.status?.sshPublicKeys) {
return false;
}
const keyStatus = env.status.sshPublicKeys.find((ks) => ks.id === keyId);
if (!keyStatus) {
return false;
}
if (keyStatus.phase === 'CONTENT_PHASE_FAILED') {
throw new Error(`SSH key '${keyId}' failed to apply`);
}
return keyStatus.phase === 'CONTENT_PHASE_READY';
}
async waitUntilRunning(): Promise<void> {
await this.waitUntil((env) => (this.isRunning(env) ? true : undefined));
}
async waitForSshUrl(): Promise<string> {
const url = await this.waitUntil((env) => this.getSshUrl(env));
return url;
}
async waitForPortUrl(port: number): Promise<string> {
const url = await this.waitUntil((env) => this.getPortUrl(env, port));
return url;
}
async waitForSshKeyApplied(keyId: string, keyValue: string): Promise<void> {
await this.waitUntil((env) => (this.checkSshKeyApplied(env, keyId, keyValue) ? true : undefined));
}
}
export async function waitForEnvironmentRunning(client: Gitpod, environmentId: string): Promise<void> {
const env = new EnvironmentState(client, environmentId);
try {
await env.waitUntilRunning();
} finally {
await env.close();
}
}
export async function findMostUsedEnvironmentClass(client: Gitpod): Promise<EnvironmentClass | undefined> {
const classUsage = new Map<string, number>();
let envsResp = await client.environments.list({});
while (envsResp) {
for (const env of envsResp.environments) {
const envClass = env.spec?.machine?.class;
if (envClass) {
classUsage.set(envClass, (classUsage.get(envClass) || 0) + 1);
}
}
if (envsResp.pagination?.nextToken) {
envsResp = await client.environments.list({ token: envsResp.pagination.nextToken });
} else {
break;
}
}
const sortedClasses = Array.from(classUsage.entries()).sort(([, a], [, b]) => b - a);
const environmentClassId = sortedClasses[0]?.[0];
if (!environmentClassId) {
return undefined;
}
return findEnvironmentClassById(client, environmentClassId);
}
export async function findEnvironmentClassById(
client: Gitpod,
environmentClassId: string,
): Promise<EnvironmentClass | undefined> {
let classesResp = await client.environments.classes.list({ filter: { canCreateEnvironments: true } });
while (classesResp) {
for (const cls of classesResp.environmentClasses) {
if (cls.id === environmentClassId) {
return cls;
}
}
if (classesResp.pagination?.nextToken) {
classesResp = await client.environments.classes.list({ token: classesResp.pagination.nextToken });
} else {
break;
}
}
return undefined;
}