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

main
ts 572 lines 19.5 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 { tagRecord } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import { GcpEnvironment } from "../Environment.ts";
13 import {
14 createInternalLabels,
15 diffLabels,
16 hasAlchemyLabels,
17 stripInternalLabels,
18 toLabels,
19 } from "../Labels.ts";
20 import type { Providers } from "../Providers.ts";
21 import { waitRegionOperation } from "./operations.ts";
22
23 export type RegionDiskProps = {
24 /**
25 * Disk name (RFC1035, 1-63 characters). If omitted, a unique name is
26 * generated from the stack, stage, and logical id. Changing the name
27 * replaces the disk.
28 */
29 diskName?: string;
30 /**
31 * Region the disk lives in (e.g. `us-central1`). Immutable — changing
32 * it replaces the disk. `US-CENTRAL1` is accepted and normalized to
33 * `us-central1`.
34 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
35 */
36 region?: string;
37 /**
38 * Zones the disk is replicated to. Two zones in `region` are required.
39 * Short names (`us-central1-a`) or URLs are accepted. Immutable —
40 * changing them replaces the disk.
41 * @default ["{region}-a", "{region}-b"]
42 */
43 replicaZones?: string[];
44 /**
45 * Persistent disk type (`pd-standard`, `pd-balanced`, `pd-ssd`,
46 * `pd-extreme`, `hyperdisk-balanced`, …). Immutable — changing it
47 * replaces the disk.
48 * @default "pd-standard"
49 */
50 type?: string;
51 /**
52 * Size in GB. Regional persistent disks require at least 200 GB. May
53 * only grow in place; shrinking replaces the disk.
54 * @default 200
55 */
56 sizeGb?: number;
57 /**
58 * Optional description.
59 */
60 description?: string;
61 /**
62 * User labels. Alchemy ownership labels are merged in automatically.
63 */
64 labels?: Record<string, string>;
65 /**
66 * Source image used to create the disk. Immutable — changing it replaces
67 * the disk.
68 */
69 sourceImage?: string;
70 /**
71 * Source snapshot used to create the disk. Immutable — changing it
72 * replaces the disk.
73 */
74 sourceSnapshot?: string;
75 /**
76 * Source disk used to create this disk. Immutable — changing it replaces
77 * the disk.
78 */
79 sourceDisk?: string;
80 /**
81 * CPU architecture (`ARM64` or `X86_64`). Immutable — changing it
82 * replaces the disk.
83 */
84 architecture?: string;
85 /**
86 * Physical block size in bytes (typically `4096`). Immutable — changing
87 * it replaces the disk.
88 */
89 physicalBlockSizeBytes?: number;
90 /**
91 * Provisioned IOPS (Extreme PD / Hyperdisk). Mutable in place.
92 */
93 provisionedIops?: number;
94 /**
95 * Provisioned throughput in MB/s (Hyperdisk). Mutable in place.
96 */
97 provisionedThroughput?: number;
98 /**
99 * Whether confidential compute is enabled. Immutable — changing it
100 * replaces the disk.
101 */
102 enableConfidentialCompute?: boolean;
103 };
104
105 export type RegionDisk = Resource<
106 "GCP.Compute.RegionDisk",
107 RegionDiskProps,
108 {
109 /** Disk name. */
110 diskName: string;
111 /** Server-assigned numeric id. */
112 diskId: string | undefined;
113 /** Project id. */
114 project: string;
115 /** Region (short name, e.g. `us-central1`). */
116 region: string;
117 /** Replica zone short names, sorted. */
118 replicaZones: string[];
119 /** Disk type (short name, e.g. `pd-standard`). */
120 type: string;
121 /** Size in GB. */
122 sizeGb: number;
123 /** Server-reported status (`READY`, `CREATING`, …). */
124 status: string | undefined;
125 /** User labels (Alchemy ownership labels stripped). */
126 labels: Record<string, string>;
127 /** Optional description. */
128 description: string | undefined;
129 /** Compute Engine self-link. */
130 selfLink: string | undefined;
131 /** RFC3339 creation timestamp. */
132 creationTimestamp: string | undefined;
133 /** Source image, if any. */
134 sourceImage: string | undefined;
135 /** Source snapshot, if any. */
136 sourceSnapshot: string | undefined;
137 /** Source disk, if any. */
138 sourceDisk: string | undefined;
139 /** Attached instance URLs. */
140 users: ReadonlyArray<string>;
141 },
142 never,
143 Providers
144 >;
145
146 /**
147 * A regional Compute Engine persistent disk.
148 *
149 * Regional disks replicate across two zones in a region and require a
150 * minimum size of 200 GB. Changing
151 * `region`, `replicaZones`, `type`, `diskName`, `sourceImage`,
152 * `sourceSnapshot`, `sourceDisk`, `architecture`,
153 * `physicalBlockSizeBytes`, or `enableConfidentialCompute` replaces the
154 * disk. Growing `sizeGb` is applied in place via `regionDisks.resize`;
155 * shrinking it replaces the disk.
156 *
157 * ### Creating a RegionDisk
158 * **Example:** Generated name
159 * ```typescript
160 * const disk = yield* GCP.Compute.RegionDisk("data", {});
161 * ```
162 *
163 * **Example:** Explicit name, replica zones, type, size, and labels
164 * ```typescript
165 * const disk = yield* GCP.Compute.RegionDisk("data", {
166 * diskName: "app-data",
167 * region: "us-central1",
168 * replicaZones: ["us-central1-a", "us-central1-b"],
169 * type: "pd-balanced",
170 * sizeGb: 200,
171 * labels: { env: "prod" },
172 * });
173 * ```
174 *
175 * ### Resizing a RegionDisk
176 * **Example:** Grow in place
177 * ```typescript
178 * const disk = yield* GCP.Compute.RegionDisk("data", {
179 * diskName: "app-data",
180 * region: "us-central1",
181 * sizeGb: 250,
182 * });
183 * ```
184 *
185 * @resource
186 * @category Compute
187 */
188 export const RegionDisk = Resource<RegionDisk>("GCP.Compute.RegionDisk");
189
190 export class RegionDiskNotResolved extends Data.TaggedError("GCP.Compute.RegionDiskNotResolved")<{
191 diskName: string;
192 region: string;
193 }> {}
194
195 export class RegionDiskNotReady extends Data.TaggedError("GCP.Compute.RegionDiskNotReady")<{
196 diskName: string;
197 status: string;
198 }> {}
199
200 export class RegionDiskFailed extends Data.TaggedError("GCP.Compute.RegionDiskFailed")<{
201 diskName: string;
202 status: string;
203 }> {}
204
205 export class RegionDiskStillExists extends Data.TaggedError("GCP.Compute.RegionDiskStillExists")<{
206 diskName: string;
207 status: string;
208 }> {}
209
210 const DEFAULT_TYPE = "pd-standard";
211 const DEFAULT_SIZE_GB = 200;
212
213 const lastSegment = (value: string | undefined): string | undefined => {
214 if (value === undefined || value.length === 0) return undefined;
215 const parts = value.split("/");
216 return parts[parts.length - 1] || value;
217 };
218
219 const normalizeRegion = (region: string | undefined, defaultRegion: string) =>
220 (lastSegment(region ?? defaultRegion) ?? defaultRegion).toLowerCase();
221
222 const userLabels = (
223 labels: Record<string, string | undefined> | null | undefined,
224 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
225
226 const parseSizeGb = (value: string | undefined): number => {
227 const parsed = Number(value);
228 return Number.isFinite(parsed) ? parsed : 0;
229 };
230
231 const normalizeReplicaZones = (region: string, zones: readonly string[] | undefined): string[] => {
232 const raw = zones && zones.length > 0 ? zones : [`${region}-a`, `${region}-b`];
233 return [
234 ...new Set(
235 raw.map((zone) => lastSegment(zone)).filter((zone): zone is string => zone !== undefined),
236 ),
237 ].sort();
238 };
239
240 const replicaZoneUrls = (project: string, zones: readonly string[]) =>
241 zones.map((zone) => `projects/${project}/zones/${zone}`);
242
243 const replicaZonesEqual = (left: readonly string[], right: readonly string[]) =>
244 left.length === right.length && left.every((zone, index) => zone === right[index]);
245
246 const toName = (id: string, name: string | undefined, existing?: string) =>
247 Effect.gen(function* () {
248 if (name !== undefined) return name;
249 if (existing !== undefined) return existing;
250 const generated = yield* createPhysicalName({
251 id,
252 maxLength: 63,
253 lowercase: true,
254 });
255 // RFC1035: disk names must start with a letter.
256 return /^[a-z]/.test(generated) ? generated : `d${generated}`.slice(0, 63);
257 });
258
259 const toAttrs = (disk: compute.Disk, project: string) => {
260 const region = (lastSegment(disk.region) ?? "").toLowerCase();
261 return {
262 diskName: disk.name ?? disk.id ?? "",
263 diskId: disk.id,
264 project,
265 region,
266 replicaZones: normalizeReplicaZones(region, disk.replicaZones),
267 type: lastSegment(disk.type) ?? DEFAULT_TYPE,
268 sizeGb: parseSizeGb(disk.sizeGb),
269 status: disk.status,
270 labels: userLabels(disk.labels),
271 description: disk.description,
272 selfLink: disk.selfLink,
273 creationTimestamp: disk.creationTimestamp,
274 sourceImage: disk.sourceImage,
275 sourceSnapshot: disk.sourceSnapshot,
276 sourceDisk: disk.sourceDisk,
277 users: disk.users ?? [],
278 };
279 };
280
281 const getByName = (project: string, region: string, disk: string) =>
282 compute
283 .getRegionDisks({ project, region, disk })
284 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
285
286 const waitDiskReady = (project: string, region: string, diskName: string) =>
287 getByName(project, region, diskName).pipe(
288 Effect.flatMap((disk) =>
289 disk?.status === "FAILED"
290 ? Effect.fail(new RegionDiskFailed({ diskName, status: "FAILED" }))
291 : Effect.succeed(disk),
292 ),
293 Effect.filterOrFail(
294 (disk): disk is compute.Disk => disk !== undefined && disk.status === "READY",
295 (disk) =>
296 new RegionDiskNotReady({
297 diskName,
298 status: disk?.status ?? "MISSING",
299 }),
300 ),
301 Effect.retry({
302 while: (error) => error._tag === "GCP.Compute.RegionDiskNotReady",
303 times: 10,
304 schedule: Schedule.spaced("2 seconds"),
305 }),
306 );
307
308 const waitDiskGone = (project: string, region: string, diskName: string) =>
309 getByName(project, region, diskName).pipe(
310 Effect.flatMap((disk) =>
311 disk === undefined
312 ? Effect.void
313 : Effect.fail(
314 new RegionDiskStillExists({
315 diskName,
316 status: disk.status ?? "UNKNOWN",
317 }),
318 ),
319 ),
320 Effect.retry({
321 while: (error) => error._tag === "GCP.Compute.RegionDiskStillExists",
322 times: 10,
323 schedule: Schedule.spaced("3 seconds"),
324 }),
325 );
326
327 const diskTypeUrl = (project: string, region: string, type: string) =>
328 `projects/${project}/regions/${region}/diskTypes/${type}`;
329
330 export const RegionDiskProvider = () =>
331 Provider.succeed(RegionDisk, {
332 stables: [
333 "diskName",
334 "diskId",
335 "project",
336 "region",
337 "replicaZones",
338 "type",
339 "selfLink",
340 "creationTimestamp",
341 "sourceImage",
342 "sourceSnapshot",
343 "sourceDisk",
344 ],
345
346 diff: Effect.fn(function* ({ news, olds, output }) {
347 if (!isResolved(news)) return undefined;
348 const env = yield* GcpEnvironment.current;
349
350 const previousRegion = normalizeRegion(olds?.region ?? output?.region, env.region);
351 const nextRegion = normalizeRegion(news.region ?? output?.region, env.region);
352 const previousType = lastSegment(olds?.type) ?? lastSegment(output?.type) ?? DEFAULT_TYPE;
353 const nextType = lastSegment(news.type) ?? DEFAULT_TYPE;
354 const previousName = olds?.diskName ?? output?.diskName;
355 const nextName = news.diskName ?? previousName;
356 const previousSize = olds?.sizeGb ?? output?.sizeGb;
357 const nextSize = news.sizeGb ?? DEFAULT_SIZE_GB;
358 const previousReplicas = normalizeReplicaZones(
359 previousRegion,
360 olds?.replicaZones ?? output?.replicaZones,
361 );
362 const nextReplicas = normalizeReplicaZones(nextRegion, news.replicaZones);
363
364 const replace =
365 previousRegion !== nextRegion ||
366 previousType !== nextType ||
367 !replicaZonesEqual(previousReplicas, nextReplicas) ||
368 (previousName !== undefined && nextName !== undefined && previousName !== nextName) ||
369 (olds?.sourceImage ?? undefined) !== (news.sourceImage ?? undefined) ||
370 (olds?.sourceSnapshot ?? undefined) !== (news.sourceSnapshot ?? undefined) ||
371 (olds?.sourceDisk ?? undefined) !== (news.sourceDisk ?? undefined) ||
372 (olds?.architecture ?? undefined) !== (news.architecture ?? undefined) ||
373 (olds?.physicalBlockSizeBytes ?? undefined) !==
374 (news.physicalBlockSizeBytes ?? undefined) ||
375 (olds?.enableConfidentialCompute ?? undefined) !==
376 (news.enableConfidentialCompute ?? undefined) ||
377 (previousSize !== undefined && nextSize < previousSize);
378
379 if (!replace) return undefined;
380 return {
381 action: "replace" as const,
382 deleteFirst:
383 previousName !== undefined && nextName === previousName && previousRegion === nextRegion,
384 };
385 }),
386
387 read: Effect.fn(function* ({ id, olds, output }) {
388 const env = yield* GcpEnvironment.current;
389 const diskName = yield* toName(id, olds?.diskName, output?.diskName);
390 const region = normalizeRegion(olds?.region ?? output?.region, env.region);
391 const existing = yield* getByName(env.project, region, diskName);
392 if (existing === undefined) return undefined;
393 const attrs = toAttrs(existing, env.project);
394 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
395 }),
396
397 list: () =>
398 Effect.gen(function* () {
399 const env = yield* GcpEnvironment.current;
400 const pages = yield* compute.aggregatedListDisks
401 .pages({
402 project: env.project,
403 filter: "labels.alchemy-id:*",
404 returnPartialSuccess: true,
405 maxResults: 500,
406 })
407 .pipe(Stream.runCollect);
408 return Array.from(pages).flatMap((page) =>
409 Object.values(page.items ?? {}).flatMap((scoped) =>
410 (scoped?.disks ?? [])
411 .filter((disk) => disk.region !== undefined)
412 .filter((disk) =>
413 Object.keys(disk.labels ?? {}).some((key) => key.startsWith("alchemy-")),
414 )
415 .map((disk) => toAttrs(disk, env.project)),
416 ),
417 );
418 }),
419
420 reconcile: Effect.fn(function* ({ id, news, output }) {
421 const env = yield* GcpEnvironment.current;
422 const diskName = yield* toName(id, news.diskName, output?.diskName);
423 const region = normalizeRegion(news.region ?? output?.region, env.region);
424 const diskType = lastSegment(news.type) ?? DEFAULT_TYPE;
425 const sizeGb = news.sizeGb ?? DEFAULT_SIZE_GB;
426 const replicaZones = normalizeReplicaZones(region, news.replicaZones);
427 const desiredLabels = {
428 ...toLabels(news.labels),
429 ...(yield* createInternalLabels(id)),
430 };
431
432 let current = yield* getByName(env.project, region, diskName);
433 if (current?.status === "DELETING") {
434 yield* waitDiskGone(env.project, region, diskName);
435 current = undefined;
436 }
437
438 if (current === undefined) {
439 const inserted = yield* compute
440 .insertRegionDisks({
441 project: env.project,
442 region,
443 body: {
444 name: diskName,
445 sizeGb: String(sizeGb),
446 type: diskTypeUrl(env.project, region, diskType),
447 replicaZones: replicaZoneUrls(env.project, replicaZones),
448 description: news.description,
449 labels: desiredLabels,
450 sourceImage: news.sourceImage,
451 sourceSnapshot: news.sourceSnapshot,
452 sourceDisk: news.sourceDisk,
453 architecture: news.architecture,
454 physicalBlockSizeBytes:
455 news.physicalBlockSizeBytes !== undefined
456 ? String(news.physicalBlockSizeBytes)
457 : undefined,
458 provisionedIops:
459 news.provisionedIops !== undefined ? String(news.provisionedIops) : undefined,
460 provisionedThroughput:
461 news.provisionedThroughput !== undefined
462 ? String(news.provisionedThroughput)
463 : undefined,
464 enableConfidentialCompute: news.enableConfidentialCompute,
465 },
466 })
467 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
468 if (inserted !== undefined) {
469 yield* waitRegionOperation(env.project, region, inserted, {
470 ignore: ["RESOURCE_ALREADY_EXISTS"],
471 });
472 }
473 current = yield* waitDiskReady(env.project, region, diskName);
474 }
475
476 if (current === undefined) {
477 return yield* new RegionDiskNotResolved({ diskName, region });
478 }
479
480 const observedSize = parseSizeGb(current.sizeGb);
481 if (sizeGb > observedSize) {
482 const resized = yield* compute.resizeRegionDisks({
483 project: env.project,
484 region,
485 disk: diskName,
486 body: { sizeGb: String(sizeGb) },
487 });
488 yield* waitRegionOperation(env.project, region, resized);
489 current = yield* waitDiskReady(env.project, region, diskName);
490 }
491
492 if (current === undefined) {
493 return yield* new RegionDiskNotResolved({ diskName, region });
494 }
495
496 const observedLabels = tagRecord(current.labels);
497 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
498 if (upsert.length > 0 || removed.length > 0) {
499 const labeled = yield* compute.setLabelsRegionDisks({
500 project: env.project,
501 region,
502 resource: diskName,
503 body: {
504 labels: desiredLabels,
505 labelFingerprint: current.labelFingerprint,
506 },
507 });
508 yield* waitRegionOperation(env.project, region, labeled);
509 current = yield* getByName(env.project, region, diskName);
510 }
511
512 if (current === undefined) {
513 return yield* new RegionDiskNotResolved({ diskName, region });
514 }
515
516 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
517 const iopsChanged =
518 news.provisionedIops !== undefined &&
519 String(news.provisionedIops) !== (current.provisionedIops ?? "");
520 const throughputChanged =
521 news.provisionedThroughput !== undefined &&
522 String(news.provisionedThroughput) !== (current.provisionedThroughput ?? "");
523
524 if (descriptionChanged || iopsChanged || throughputChanged) {
525 const updated = yield* compute.updateRegionDisks({
526 project: env.project,
527 region,
528 disk: diskName,
529 updateMask: [
530 descriptionChanged ? "description" : undefined,
531 iopsChanged ? "provisionedIops" : undefined,
532 throughputChanged ? "provisionedThroughput" : undefined,
533 ]
534 .filter((field): field is string => field !== undefined)
535 .join(","),
536 body: {
537 description: news.description,
538 provisionedIops:
539 news.provisionedIops !== undefined ? String(news.provisionedIops) : undefined,
540 provisionedThroughput:
541 news.provisionedThroughput !== undefined
542 ? String(news.provisionedThroughput)
543 : undefined,
544 },
545 });
546 yield* waitRegionOperation(env.project, region, updated);
547 current = yield* getByName(env.project, region, diskName);
548 }
549
550 if (current === undefined) {
551 return yield* new RegionDiskNotResolved({ diskName, region });
552 }
553
554 return toAttrs(current, env.project);
555 }),
556
557 delete: Effect.fn(function* ({ output }) {
558 const deleted = yield* compute
559 .deleteRegionDisks({
560 project: output.project,
561 region: output.region,
562 disk: output.diskName,
563 })
564 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
565 if (deleted !== undefined) {
566 yield* waitRegionOperation(output.project, output.region, deleted, {
567 ignore: ["RESOURCE_NOT_FOUND"],
568 });
569 }
570 yield* waitDiskGone(output.project, output.region, output.diskName);
571 }),
572 });