@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 496 lines 17.5 KB
Raw
1 import * as compute from "@distilled.cloud/gcp/compute_v1";
2 import { unownedUnlessPrevious } 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 { ignoredCodes } from "./internal.ts";
15 import { waitZoneOperation } from "./operations.ts";
16
17 const DEFAULT_ZONE = "us-central1-a";
18 const DEFAULT_MAINTENANCE_POLICY = "DEFAULT";
19 const MAX_NAME_LENGTH = 63;
20
21 export type NodeGroupMaintenancePolicy = compute.NodeGroupMaintenancePolicyEnum | (string & {});
22 export type NodeGroupMaintenanceInterval = compute.NodeGroupMaintenanceIntervalEnum | (string & {});
23 export type NodeGroupAutoscalingPolicy = compute.NodeGroupAutoscalingPolicy;
24 export type NodeGroupMaintenanceWindow = compute.NodeGroupMaintenanceWindow;
25 export type NodeGroupShareSettings = compute.ShareSettings;
26
27 export type NodeGroupProps = {
28 /**
29 * Group name (RFC1035, 1-63 characters). If omitted, a unique name is
30 * generated from the stack, stage, and logical id. Immutable — changing
31 * it replaces the group.
32 */
33 nodeGroupName?: string;
34 /**
35 * Zone the group lives in. Immutable — changing it replaces the group.
36 * `US-CENTRAL1-A` is accepted and normalized to `us-central1-a`.
37 * @default "us-central1-a"
38 */
39 zone?: string;
40 /**
41 * Node template URL or name used to create nodes. Required. Updated in
42 * place via `setNodeTemplate`.
43 */
44 nodeTemplate: string;
45 /**
46 * Initial number of nodes. Required on create; later size changes use
47 * `addNodes` / `deleteNodes` and are not applied from this field.
48 * @default 0
49 */
50 initialNodeCount?: number;
51 /**
52 * Optional description. Node groups have no labels field, so Alchemy
53 * ownership (`alchemy-stack` / `alchemy-stage` / `alchemy-id`) is stored
54 * in a `[alchemy …]` prefix for `list` / nuke. Updated in place via
55 * `patch`.
56 */
57 description?: string;
58 /**
59 * How instances are handled during host maintenance.
60 * @default "DEFAULT"
61 */
62 maintenancePolicy?: NodeGroupMaintenancePolicy;
63 /**
64 * Daily maintenance window start time (`00:00`, `04:00`, …).
65 */
66 maintenanceWindow?: NodeGroupMaintenanceWindow;
67 /**
68 * Planned-maintenance frequency (`AS_NEEDED` or `RECURRENT`).
69 */
70 maintenanceInterval?: NodeGroupMaintenanceInterval;
71 /**
72 * Autoscaling policy. Updated in place via `patch`.
73 */
74 autoscalingPolicy?: NodeGroupAutoscalingPolicy;
75 /**
76 * Share settings for this node group. Updated in place via `patch`.
77 */
78 shareSettings?: NodeGroupShareSettings;
79 };
80
81 export type NodeGroup = Resource<
82 "GCP.Compute.NodeGroup",
83 NodeGroupProps,
84 {
85 /** Group name. */
86 nodeGroupName: string;
87 /** Project id. */
88 project: string;
89 /** Zone short name (`us-central1-a`). */
90 zone: string;
91 /** Node template URL. */
92 nodeTemplate: string | undefined;
93 /** User description with the Alchemy ownership prefix stripped. */
94 description: string | undefined;
95 /** Number of nodes currently in the group. */
96 size: number | undefined;
97 /** Maintenance policy. */
98 maintenancePolicy: string | undefined;
99 /** Maintenance window. */
100 maintenanceWindow: NodeGroupMaintenanceWindow | undefined;
101 /** Maintenance interval. */
102 maintenanceInterval: string | undefined;
103 /** Autoscaling policy. */
104 autoscalingPolicy: NodeGroupAutoscalingPolicy | undefined;
105 /** Share settings. */
106 shareSettings: NodeGroupShareSettings | undefined;
107 /** Group status. */
108 status: string | undefined;
109 /** Optimistic-locking fingerprint. */
110 fingerprint: string | undefined;
111 /** Server-assigned numeric id. */
112 nodeGroupId: string | undefined;
113 /** Resource self-link. */
114 selfLink: string | undefined;
115 /** RFC3339 creation timestamp. */
116 creationTimestamp: string | undefined;
117 /** Resource kind. */
118 kind: string | undefined;
119 },
120 never,
121 Providers
122 >;
123
124 /**
125 * A zonal Compute Engine sole-tenant node group.
126 *
127 * Node groups allocate dedicated physical servers from a node template.
128 * Name and zone are immutable. Description, maintenance policy, window,
129 * interval, autoscaling, and share settings update in place via
130 * `nodeGroups.patch`. The node template is swapped with
131 * `setNodeTemplate`. Compute NodeGroup has no labels field — Alchemy
132 * stamps ownership into the description so nuke can find leaked groups.
133 *
134 * ### Creating a Node Group
135 * **Example:** Generated name from a template
136 * ```typescript
137 * const group = yield* GCP.Compute.NodeGroup("SoleTenant", {
138 * zone: "us-central1-a",
139 * nodeTemplate: template.selfLink,
140 * initialNodeCount: 1,
141 * description: "prod sole tenant",
142 * });
143 * ```
144 *
145 * **Example:** Autoscaled group
146 * ```typescript
147 * const group = yield* GCP.Compute.NodeGroup("SoleTenant", {
148 * nodeTemplate: template.nodeTemplateName,
149 * initialNodeCount: 0,
150 * autoscalingPolicy: { mode: "ON", minNodes: 0, maxNodes: 3 },
151 * });
152 * ```
153 *
154 * @resource
155 * @category Compute
156 */
157 export const NodeGroup = Resource<NodeGroup>("GCP.Compute.NodeGroup");
158
159 export class NodeGroupNotResolved extends Data.TaggedError("GCP.Compute.NodeGroupNotResolved")<{
160 nodeGroupName: string;
161 zone: string;
162 }> {}
163
164 export class NodeGroupStillExists extends Data.TaggedError("GCP.Compute.NodeGroupStillExists")<{
165 nodeGroupName: string;
166 }> {}
167
168 const lastSegment = (value: string | undefined): string => {
169 if (value === undefined || value.length === 0) return "";
170 const trimmed = value.replace(/\/+$/, "");
171 const parts = trimmed.split("/");
172 return parts[parts.length - 1] || trimmed;
173 };
174
175 const normalizeZone = (zone: string | undefined) => lastSegment(zone ?? DEFAULT_ZONE).toLowerCase();
176
177 const regionOfZone = (zone: string) => {
178 const parts = zone.split("-");
179 return parts.length >= 3 ? parts.slice(0, 2).join("-") : zone;
180 };
181
182 const rfc1035 = (name: string): string => {
183 let next = name
184 .toLowerCase()
185 .replace(/[^a-z0-9-]/g, "-")
186 .replace(/-+/g, "-")
187 .replace(/^-+/, "")
188 .replace(/-+$/, "");
189 if (!/^[a-z]/.test(next)) next = `n${next}`;
190 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/, "");
191 return next.length > 0 ? next : "group";
192 };
193
194 const toName = (id: string, name: string | undefined, existing?: string) =>
195 Effect.gen(function* () {
196 if (name !== undefined) return name;
197 if (existing !== undefined) return existing;
198 return rfc1035(
199 yield* createPhysicalName({
200 id,
201 maxLength: MAX_NAME_LENGTH,
202 lowercase: true,
203 }),
204 );
205 });
206
207 const encodeDescription = (
208 labels: Record<string, string>,
209 description: string | undefined,
210 ): string => {
211 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
212 return description ? `${marker}\n${description}` : marker;
213 };
214
215 const parseDescription = (
216 description: string | undefined,
217 ): {
218 labels: Record<string, string>;
219 description: string | undefined;
220 } => {
221 if (!description?.startsWith("[alchemy ")) {
222 return { labels: {}, description };
223 }
224 const end = description.indexOf("]");
225 if (end < 0) return { labels: {}, description };
226 const labels: Record<string, string> = {};
227 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
228 const eq = part.indexOf("=");
229 if (eq > 0) labels[part.slice(0, eq)] = part.slice(eq + 1);
230 }
231 const rest = description.slice(end + 1).replace(/^\n/, "");
232 return { labels, description: rest.length > 0 ? rest : undefined };
233 };
234
235 const hasOwnershipMarker = (description: string | undefined) =>
236 Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));
237
238 const maintenanceOf = (value: string | undefined) =>
239 (value ?? DEFAULT_MAINTENANCE_POLICY).toUpperCase();
240
241 const nodeTemplateUrl = (project: string, zone: string, value: string) => {
242 if (value.includes("/")) return value;
243 return `projects/${project}/regions/${regionOfZone(zone)}/nodeTemplates/${value}`;
244 };
245
246 const sameJson = (left: unknown, right: unknown) =>
247 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
248
249 const toAttrs = (group: compute.NodeGroup, project: string): NodeGroup["Attributes"] => {
250 const parsed = parseDescription(group.description);
251 return {
252 nodeGroupName: group.name ?? "",
253 project,
254 zone: normalizeZone(group.zone),
255 nodeTemplate: group.nodeTemplate,
256 description: parsed.description,
257 size: group.size,
258 maintenancePolicy: group.maintenancePolicy,
259 maintenanceWindow: group.maintenanceWindow,
260 maintenanceInterval: group.maintenanceInterval,
261 autoscalingPolicy: group.autoscalingPolicy,
262 shareSettings: group.shareSettings,
263 status: group.status,
264 fingerprint: group.fingerprint,
265 nodeGroupId: group.id,
266 selfLink: group.selfLink,
267 creationTimestamp: group.creationTimestamp,
268 kind: group.kind,
269 };
270 };
271
272 const getByName = (project: string, zone: string, nodeGroup: string) =>
273 compute
274 .getNodeGroups({ project, zone, nodeGroup })
275 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
276
277 const awaitResource = (project: string, zone: string, nodeGroupName: string) =>
278 getByName(project, zone, nodeGroupName).pipe(
279 Effect.flatMap((group) =>
280 group !== undefined
281 ? Effect.succeed(group)
282 : Effect.fail(new NodeGroupNotResolved({ nodeGroupName, zone })),
283 ),
284 Effect.retry({
285 while: (error) => error._tag === "GCP.Compute.NodeGroupNotResolved",
286 times: 8,
287 schedule: Schedule.spaced("2 seconds"),
288 }),
289 );
290
291 const waitUntilGone = (project: string, zone: string, nodeGroupName: string) =>
292 getByName(project, zone, nodeGroupName).pipe(
293 Effect.flatMap((group) =>
294 group === undefined ? Effect.void : Effect.fail(new NodeGroupStillExists({ nodeGroupName })),
295 ),
296 Effect.retry({
297 while: (error) => error._tag === "GCP.Compute.NodeGroupStillExists",
298 times: 10,
299 schedule: Schedule.spaced("2 seconds"),
300 }),
301 Effect.catchTag("GCP.Compute.NodeGroupStillExists", () => Effect.void),
302 );
303
304 const runOp = <E extends { readonly _tag: string }, R>(
305 project: string,
306 zone: string,
307 nodeGroupName: string,
308 start: Effect.Effect<compute.Operation, E, R>,
309 options?: { ignoreAlreadyExists?: boolean; ignoreNotFound?: boolean },
310 ) =>
311 start.pipe(
312 Effect.flatMap((operation) =>
313 waitZoneOperation(project, zone, operation, {
314 ignore: ignoredCodes(options),
315 }),
316 ),
317 Effect.retry({
318 while: (error) => error._tag === "Conflict",
319 times: 5,
320 schedule: Schedule.spaced("1 second"),
321 }),
322 );
323
324 export const NodeGroupProvider = () =>
325 Provider.succeed(NodeGroup, {
326 stables: ["nodeGroupName", "project", "zone", "nodeGroupId", "selfLink", "creationTimestamp"],
327
328 diff: Effect.fn(function* ({ news, olds, output }) {
329 if (!isResolved(news)) return undefined;
330 const previousName = olds?.nodeGroupName ?? output?.nodeGroupName;
331 const nextName = news.nodeGroupName ?? previousName;
332 const nameChanged =
333 previousName !== undefined && nextName !== undefined && previousName !== nextName;
334 const previousZone = normalizeZone(olds?.zone ?? output?.zone);
335 const nextZone = normalizeZone(news.zone ?? previousZone);
336 if (nameChanged) {
337 return { action: "replace" as const, deleteFirst: false };
338 }
339 if (previousZone !== nextZone) {
340 return { action: "replace" as const, deleteFirst: true };
341 }
342 return undefined;
343 }),
344
345 read: Effect.fn(function* ({ id, olds, output, previous }) {
346 const env = yield* GcpEnvironment.current;
347 const nodeGroupName = yield* toName(id, olds?.nodeGroupName, output?.nodeGroupName);
348 const zone = normalizeZone(olds?.zone ?? output?.zone);
349 const existing = yield* getByName(env.project, zone, nodeGroupName);
350 if (existing === undefined) return undefined;
351 const attrs = toAttrs(existing, env.project);
352 const { labels } = parseDescription(existing.description);
353 return (yield* hasAlchemyLabels(id, labels))
354 ? attrs
355 : unownedUnlessPrevious(attrs, previous, "nodeGroupName");
356 }),
357
358 list: () =>
359 Effect.gen(function* () {
360 const env = yield* GcpEnvironment.current;
361 const pages = yield* compute.aggregatedListNodeGroups
362 .pages({
363 project: env.project,
364 returnPartialSuccess: true,
365 maxResults: 500,
366 })
367 .pipe(Stream.runCollect);
368 return Array.from(pages).flatMap((page) =>
369 Object.values(page.items ?? {}).flatMap((scoped) =>
370 (scoped?.nodeGroups ?? [])
371 .filter((item) => hasOwnershipMarker(item.description))
372 .map((item) => toAttrs(item, env.project)),
373 ),
374 );
375 }),
376
377 reconcile: Effect.fn(function* ({ id, news, output }) {
378 const env = yield* GcpEnvironment.current;
379 const nodeGroupName = yield* toName(id, news.nodeGroupName, output?.nodeGroupName);
380 const zone = normalizeZone(news.zone ?? output?.zone);
381 const ownership = yield* createInternalLabels(id);
382 const desiredDescription = encodeDescription(ownership, news.description);
383 const nodeTemplate = nodeTemplateUrl(env.project, zone, news.nodeTemplate);
384 const maintenancePolicy = maintenanceOf(news.maintenancePolicy);
385 const initialNodeCount = news.initialNodeCount ?? 0;
386
387 let current = yield* getByName(env.project, zone, nodeGroupName);
388
389 if (current === undefined) {
390 yield* compute
391 .insertNodeGroups({
392 project: env.project,
393 zone,
394 initialNodeCount,
395 body: {
396 name: nodeGroupName,
397 description: desiredDescription,
398 nodeTemplate,
399 maintenancePolicy,
400 maintenanceWindow: news.maintenanceWindow,
401 maintenanceInterval: news.maintenanceInterval,
402 autoscalingPolicy: news.autoscalingPolicy,
403 shareSettings: news.shareSettings,
404 },
405 })
406 .pipe(
407 Effect.flatMap((operation) =>
408 waitZoneOperation(env.project, zone, operation, {
409 ignore: ["RESOURCE_ALREADY_EXISTS"],
410 }),
411 ),
412 Effect.catchTag("Conflict", () => Effect.void),
413 );
414 current = yield* awaitResource(env.project, zone, nodeGroupName);
415 }
416
417 if (lastSegment(current.nodeTemplate) !== lastSegment(nodeTemplate)) {
418 yield* runOp(
419 env.project,
420 zone,
421 nodeGroupName,
422 compute.setNodeTemplateNodeGroups({
423 project: env.project,
424 zone,
425 nodeGroup: nodeGroupName,
426 body: { nodeTemplate },
427 }),
428 );
429 current = (yield* getByName(env.project, zone, nodeGroupName)) ?? current;
430 }
431
432 const needsPatch =
433 (current.description ?? "") !== desiredDescription ||
434 maintenanceOf(current.maintenancePolicy) !== maintenancePolicy ||
435 (news.maintenanceInterval !== undefined &&
436 (current.maintenanceInterval ?? "") !== news.maintenanceInterval) ||
437 (news.maintenanceWindow !== undefined &&
438 !sameJson(current.maintenanceWindow, news.maintenanceWindow)) ||
439 (news.autoscalingPolicy !== undefined &&
440 !sameJson(current.autoscalingPolicy, news.autoscalingPolicy)) ||
441 (news.shareSettings !== undefined && !sameJson(current.shareSettings, news.shareSettings));
442
443 if (needsPatch) {
444 const latest = (yield* getByName(env.project, zone, nodeGroupName)) ?? current;
445 yield* runOp(
446 env.project,
447 zone,
448 nodeGroupName,
449 compute.patchNodeGroups({
450 project: env.project,
451 zone,
452 nodeGroup: nodeGroupName,
453 body: {
454 fingerprint: latest.fingerprint,
455 description: desiredDescription,
456 maintenancePolicy,
457 maintenanceWindow: news.maintenanceWindow ?? current.maintenanceWindow,
458 maintenanceInterval: news.maintenanceInterval ?? current.maintenanceInterval,
459 autoscalingPolicy: news.autoscalingPolicy ?? current.autoscalingPolicy,
460 shareSettings: news.shareSettings ?? current.shareSettings,
461 },
462 }),
463 );
464 current = (yield* getByName(env.project, zone, nodeGroupName)) ?? current;
465 }
466
467 return toAttrs(current, env.project);
468 }),
469
470 delete: Effect.fn(function* ({ output }) {
471 if (!output.nodeGroupName) return;
472 const env = yield* GcpEnvironment.current;
473 const project = output.project || env.project;
474 const zone = normalizeZone(output.zone);
475 yield* compute
476 .deleteNodeGroups({
477 project,
478 zone,
479 nodeGroup: output.nodeGroupName,
480 })
481 .pipe(
482 Effect.flatMap((operation) =>
483 waitZoneOperation(project, zone, operation, {
484 ignore: ["RESOURCE_NOT_FOUND"],
485 }),
486 ),
487 Effect.catchTag("NotFound", () => Effect.void),
488 Effect.retry({
489 while: (error) => error._tag === "Conflict",
490 times: 8,
491 schedule: Schedule.spaced("2 seconds"),
492 }),
493 );
494 yield* waitUntilGone(project, zone, output.nodeGroupName);
495 }),
496 });