@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 467 lines 15.4 KB
Raw
1 import * as compute from "@distilled.cloud/gcp/compute_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
13 import type { Providers } from "../Providers.ts";
14 import { waitZoneOperation } from "./operations.ts";
15
16 const DEFAULT_ZONE = "us-central1-a";
17
18 export type InstanceGroupManagerResizeRequestDuration = compute.Duration;
19
20 export type InstanceGroupManagerResizeRequestProps = {
21 /**
22 * Resize request name (RFC1035, 1-63 chars). If omitted, a unique name
23 * is generated from the stack, stage, and logical id. Changing it
24 * replaces the request.
25 */
26 resizeRequestName?: string;
27 /**
28 * Zone of the managed instance group (e.g. `us-central1-a`). Immutable.
29 * @default "us-central1-a"
30 */
31 zone?: string;
32 /**
33 * Managed instance group name or URL. Immutable — changing it replaces
34 * the request.
35 */
36 instanceGroupManager: string;
37 /**
38 * Number of VMs to create. The group's target size increases by this
39 * amount. Immutable.
40 */
41 resizeBy: number;
42 /**
43 * Optional description. Resize requests have no labels field — Alchemy
44 * ownership is stored in a `[alchemy …]` prefix for `list` / nuke.
45 */
46 description?: string;
47 /**
48 * How long the created VMs run; they are deleted when it elapses. Compute
49 * requires it on insert. Immutable.
50 */
51 requestedRunDuration: InstanceGroupManagerResizeRequestDuration;
52 };
53
54 export type InstanceGroupManagerResizeRequest = Resource<
55 "GCP.Compute.InstanceGroupManagerResizeRequest",
56 InstanceGroupManagerResizeRequestProps,
57 {
58 /** Resize request name. */
59 resizeRequestName: string;
60 /** Project id. */
61 project: string;
62 /** Zone short name. */
63 zone: string;
64 /** Managed instance group name. */
65 instanceGroupManager: string;
66 /** Number of VMs requested. */
67 resizeBy: number | undefined;
68 /** User description with the Alchemy ownership prefix stripped. */
69 description: string | undefined;
70 /** Requested run duration. */
71 requestedRunDuration: InstanceGroupManagerResizeRequestDuration | undefined;
72 /** Current state (`ACCEPTED`, `SUCCEEDED`, …). */
73 state: string | undefined;
74 /** Server-assigned numeric id. */
75 resizeRequestId: string | undefined;
76 /** Server-defined URL. */
77 selfLink: string | undefined;
78 /** RFC3339 creation timestamp. */
79 creationTimestamp: string | undefined;
80 },
81 never,
82 Providers
83 >;
84
85 /**
86 * A zonal queued resize request on a managed instance group.
87 *
88 * Resize requests provision additional MIG VMs immediately or by queueing
89 * until capacity is available (typically GPU / queued-provisioning
90 * machine types). Name, zone, MIG, `resizeBy`, and run duration replace
91 * the request — there is no in-place update. Compute has no labels
92 * field, so Alchemy stamps ownership into the description.
93 *
94 * Delete cancels an `ACCEPTED` / `CREATING` request first, then removes
95 * the record.
96 *
97 * ### Creating a Resize Request
98 * **Example:** Queue one extra VM
99 * ```typescript
100 * const template = yield* GCP.Compute.InstanceTemplate("web", {});
101 * const group = yield* GCP.Compute.InstanceGroupManager("web", {
102 * instanceTemplate: template.templateName,
103 * targetSize: 0,
104 * });
105 * const request = yield* GCP.Compute.InstanceGroupManagerResizeRequest(
106 * "burst",
107 * {
108 * instanceGroupManager: group.managerName,
109 * zone: group.zone,
110 * resizeBy: 1,
111 * requestedRunDuration: { seconds: "3600" },
112 * },
113 * );
114 * ```
115 *
116 * @resource
117 * @category Compute
118 */
119 export const InstanceGroupManagerResizeRequest = Resource<InstanceGroupManagerResizeRequest>(
120 "GCP.Compute.InstanceGroupManagerResizeRequest",
121 );
122
123 export class InstanceGroupManagerResizeRequestNotResolved extends Data.TaggedError(
124 "GCP.Compute.InstanceGroupManagerResizeRequestNotResolved",
125 )<{
126 resizeRequestName: string;
127 instanceGroupManager: string;
128 zone: string;
129 }> {}
130
131 const lastSegment = (value: string | undefined): string => {
132 if (value === undefined || value.length === 0) return "";
133 const parts = value.replace(/\/+$/, "").split("/");
134 return parts[parts.length - 1] || value;
135 };
136
137 const zoneOf = (value: string | undefined): string => lastSegment(value) || DEFAULT_ZONE;
138
139 const rfc1035 = (name: string): string => {
140 let next = name
141 .toLowerCase()
142 .replace(/[^a-z0-9-]/g, "-")
143 .replace(/-+/g, "-")
144 .replace(/^-+/, "")
145 .replace(/-+$/, "");
146 if (!/^[a-z]/.test(next)) {
147 next = `r${next}`;
148 }
149 next = next.slice(0, 63).replace(/-+$/, "");
150 return next.length > 0 ? next : "resizerequest";
151 };
152
153 const toName = (id: string, name: string | undefined, existing?: string) =>
154 Effect.gen(function* () {
155 if (name !== undefined) return name;
156 if (existing !== undefined) return existing;
157 return rfc1035(
158 yield* createPhysicalName({
159 id,
160 maxLength: 63,
161 lowercase: true,
162 }),
163 );
164 });
165
166 const encodeDescription = (
167 labels: Record<string, string>,
168 description: string | undefined,
169 ): string => {
170 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
171 return description ? `${marker}\n${description}` : marker;
172 };
173
174 const parseDescription = (
175 description: string | undefined,
176 ): {
177 labels: Record<string, string>;
178 description: string | undefined;
179 } => {
180 if (!description?.startsWith("[alchemy ")) {
181 return { labels: {}, description };
182 }
183 const end = description.indexOf("]");
184 if (end < 0) return { labels: {}, description };
185 const labels: Record<string, string> = {};
186 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
187 const eq = part.indexOf("=");
188 if (eq > 0) {
189 labels[part.slice(0, eq)] = part.slice(eq + 1);
190 }
191 }
192 const rest = description.slice(end + 1).replace(/^\n/, "");
193 return { labels, description: rest.length > 0 ? rest : undefined };
194 };
195
196 const toAttrs = (
197 request: compute.InstanceGroupManagerResizeRequest,
198 project: string,
199 instanceGroupManager: string,
200 ): InstanceGroupManagerResizeRequest["Attributes"] => {
201 const parsed = parseDescription(request.description);
202 return {
203 resizeRequestName: request.name ?? request.id ?? "",
204 project,
205 zone: zoneOf(request.zone),
206 instanceGroupManager: lastSegment(instanceGroupManager),
207 resizeBy: request.resizeBy,
208 description: parsed.description,
209 requestedRunDuration: request.requestedRunDuration,
210 state: request.state,
211 resizeRequestId: request.id,
212 selfLink: request.selfLink,
213 creationTimestamp: request.creationTimestamp,
214 };
215 };
216
217 const getByName = (
218 project: string,
219 zone: string,
220 instanceGroupManager: string,
221 resizeRequest: string,
222 ) =>
223 compute
224 .getInstanceGroupManagerResizeRequests({
225 project,
226 zone,
227 instanceGroupManager,
228 resizeRequest,
229 })
230 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
231
232 const awaitResource = (
233 project: string,
234 zone: string,
235 instanceGroupManager: string,
236 resizeRequestName: string,
237 ) =>
238 getByName(project, zone, instanceGroupManager, resizeRequestName).pipe(
239 Effect.repeat({
240 schedule: Schedule.spaced("1 second"),
241 until: (item) => item !== undefined,
242 times: 8,
243 }),
244 );
245
246 const terminalStates = new Set(["SUCCEEDED", "FAILED", "CANCELLED"]);
247
248 export const InstanceGroupManagerResizeRequestProvider = () =>
249 Provider.succeed(InstanceGroupManagerResizeRequest, {
250 stables: [
251 "resizeRequestName",
252 "project",
253 "zone",
254 "instanceGroupManager",
255 "resizeBy",
256 "resizeRequestId",
257 "selfLink",
258 "creationTimestamp",
259 ],
260
261 diff: Effect.fn(function* ({ news, olds, output }) {
262 if (!isResolved(news)) return undefined;
263 const previousName = olds?.resizeRequestName ?? output?.resizeRequestName;
264 const nextName = news.resizeRequestName;
265 const previousZone = zoneOf(olds?.zone ?? output?.zone);
266 const nextZone = zoneOf(news.zone ?? output?.zone);
267 const previousMig = lastSegment(olds?.instanceGroupManager ?? output?.instanceGroupManager);
268 const nextMig = lastSegment(news.instanceGroupManager);
269 const previousBy = olds?.resizeBy ?? output?.resizeBy;
270 const nextBy = news.resizeBy;
271 if (
272 (previousName !== undefined && nextName !== undefined && previousName !== nextName) ||
273 previousZone !== nextZone ||
274 previousMig !== nextMig ||
275 (previousBy !== undefined && previousBy !== nextBy)
276 ) {
277 return {
278 action: "replace" as const,
279 deleteFirst:
280 previousName !== undefined &&
281 nextName !== undefined &&
282 previousName === nextName &&
283 previousZone === nextZone &&
284 previousMig === nextMig,
285 };
286 }
287 return undefined;
288 }),
289
290 read: Effect.fn(function* ({ id, olds, output }) {
291 const env = yield* GcpEnvironment.current;
292 const resizeRequestName = yield* toName(
293 id,
294 olds?.resizeRequestName,
295 output?.resizeRequestName,
296 );
297 const zone = zoneOf(olds?.zone ?? output?.zone);
298 const instanceGroupManager = lastSegment(
299 olds?.instanceGroupManager ?? output?.instanceGroupManager,
300 );
301 if (!instanceGroupManager) return undefined;
302 const existing = yield* getByName(env.project, zone, instanceGroupManager, resizeRequestName);
303 if (existing === undefined) return undefined;
304 const attrs = toAttrs(existing, env.project, instanceGroupManager);
305 const { labels } = parseDescription(existing.description);
306 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
307 }),
308
309 list: () =>
310 Effect.gen(function* () {
311 const env = yield* GcpEnvironment.current;
312 const pages = yield* compute.aggregatedListInstanceGroupManagers
313 .pages({
314 project: env.project,
315 maxResults: 500,
316 returnPartialSuccess: true,
317 })
318 .pipe(Stream.runCollect);
319 const managers = Array.from(pages).flatMap((page) =>
320 Object.entries(page.items ?? {}).flatMap(([scope, scoped]) => {
321 if (!scope.startsWith("zones/")) return [];
322 const zone = scope.slice("zones/".length);
323 return (scoped?.instanceGroupManagers ?? [])
324 .filter((manager) => manager.name)
325 .map((manager) => ({
326 zone,
327 name: manager.name ?? "",
328 }));
329 }),
330 );
331 const perManager = yield* Effect.forEach(
332 managers,
333 ({ zone, name }) =>
334 compute.listInstanceGroupManagerResizeRequests
335 .items({
336 project: env.project,
337 zone,
338 instanceGroupManager: name,
339 maxResults: 500,
340 })
341 .pipe(
342 Stream.filter((request) => {
343 const { labels } = parseDescription(request.description);
344 return Object.keys(labels).some((key) => key.startsWith("alchemy-"));
345 }),
346 Stream.map((request) => toAttrs(request, env.project, name)),
347 Stream.runCollect,
348 Effect.map((chunk) => Array.from(chunk)),
349 Effect.catchTag("NotFound", () => Effect.succeed([])),
350 ),
351 { concurrency: 4 },
352 );
353 return perManager.flat();
354 }),
355
356 reconcile: Effect.fn(function* ({ id, news, output }) {
357 const env = yield* GcpEnvironment.current;
358 const resizeRequestName = yield* toName(
359 id,
360 news.resizeRequestName,
361 output?.resizeRequestName,
362 );
363 const zone = zoneOf(news.zone ?? output?.zone);
364 const instanceGroupManager = lastSegment(news.instanceGroupManager);
365 const ownership = yield* createInternalLabels(id);
366 const description = encodeDescription(ownership, news.description);
367
368 let current = yield* getByName(env.project, zone, instanceGroupManager, resizeRequestName);
369
370 if (current === undefined) {
371 yield* compute
372 .insertInstanceGroupManagerResizeRequests({
373 project: env.project,
374 zone,
375 instanceGroupManager,
376 body: {
377 name: resizeRequestName,
378 description,
379 resizeBy: news.resizeBy,
380 requestedRunDuration: news.requestedRunDuration,
381 },
382 })
383 .pipe(
384 Effect.flatMap((operation) =>
385 waitZoneOperation(env.project, zone, operation, {
386 ignore: ["RESOURCE_ALREADY_EXISTS"],
387 }),
388 ),
389 Effect.catchTag("Conflict", () => Effect.undefined),
390 );
391 current = yield* awaitResource(env.project, zone, instanceGroupManager, resizeRequestName);
392 }
393
394 if (current === undefined) {
395 return yield* new InstanceGroupManagerResizeRequestNotResolved({
396 resizeRequestName,
397 instanceGroupManager,
398 zone,
399 });
400 }
401
402 return toAttrs(current, env.project, instanceGroupManager);
403 }),
404
405 delete: Effect.fn(function* ({ output }) {
406 const env = yield* GcpEnvironment.current;
407 const zone = zoneOf(output.zone);
408 const instanceGroupManager = lastSegment(output.instanceGroupManager);
409 const existing = yield* getByName(
410 env.project,
411 zone,
412 instanceGroupManager,
413 output.resizeRequestName,
414 );
415 if (existing === undefined) return;
416
417 if (!terminalStates.has(existing.state ?? "")) {
418 const cancelled = yield* compute
419 .cancelInstanceGroupManagerResizeRequests({
420 project: env.project,
421 zone,
422 instanceGroupManager,
423 resizeRequest: output.resizeRequestName,
424 })
425 .pipe(
426 Effect.catchTags({
427 NotFound: () => Effect.undefined,
428 BadRequest: () => Effect.undefined,
429 Conflict: () => Effect.undefined,
430 }),
431 );
432 if (cancelled !== undefined) {
433 yield* waitZoneOperation(env.project, zone, cancelled).pipe(
434 Effect.catchTag("NotFound", () => Effect.void),
435 );
436 }
437 yield* getByName(env.project, zone, instanceGroupManager, output.resizeRequestName).pipe(
438 Effect.repeat({
439 schedule: Schedule.spaced("1 second"),
440 until: (item) => item === undefined || terminalStates.has(item.state ?? ""),
441 times: 10,
442 }),
443 );
444 }
445
446 const operation = yield* compute
447 .deleteInstanceGroupManagerResizeRequests({
448 project: env.project,
449 zone,
450 instanceGroupManager,
451 resizeRequest: output.resizeRequestName,
452 })
453 .pipe(
454 Effect.retry({
455 while: (error) => error._tag === "Conflict",
456 schedule: Schedule.spaced("2 seconds"),
457 times: 8,
458 }),
459 Effect.catchTag("NotFound", () => Effect.undefined),
460 );
461 if (operation !== undefined) {
462 yield* waitZoneOperation(env.project, zone, operation).pipe(
463 Effect.catchTag("NotFound", () => Effect.void),
464 );
465 }
466 }),
467 });