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

main
ts 552 lines 17.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 { waitGlobalOperation } from "./operations.ts";
22
23 export type ImageRawDisk = {
24 /** GCS URI of a gzip-compressed tarball (`gs://` or `https://storage.googleapis.com/`). */
25 source: string;
26 /** SHA-1 checksum of the tarball, if known. */
27 sha1Checksum?: string;
28 /**
29 * Container type of the disk image.
30 * @default "TAR"
31 */
32 containerType?: "TAR" | (string & {});
33 };
34
35 export type ImageProps = {
36 /**
37 * Image name (RFC1035, 1-63 characters). If omitted, a unique name is
38 * generated from the stack, stage, and logical id. Changing the name
39 * replaces the image.
40 */
41 imageName?: string;
42 /**
43 * Optional description. Mutable in place via `images.patch`.
44 */
45 description?: string;
46 /**
47 * Image family. Disks can target the family to get the latest
48 * non-deprecated image. Mutable in place via `images.patch`.
49 */
50 family?: string;
51 /**
52 * User labels. Alchemy ownership labels are merged in automatically
53 * and synced via `images.setLabels`.
54 */
55 labels?: Record<string, string>;
56 /**
57 * Source disk URL (`projects/{project}/zones/{zone}/disks/{disk}`).
58 * Immutable — changing it replaces the image. Provide one of
59 * `sourceDisk`, `sourceImage`, `sourceSnapshot`, or `rawDisk`.
60 */
61 sourceDisk?: string;
62 /**
63 * Source image URL (`projects/{project}/global/images/{image}` or
64 * `.../images/family/{family}`). Immutable — changing it replaces
65 * the image.
66 */
67 sourceImage?: string;
68 /**
69 * Source snapshot URL (`projects/{project}/global/snapshots/{snapshot}`).
70 * Immutable — changing it replaces the image.
71 */
72 sourceSnapshot?: string;
73 /**
74 * Raw disk image stored in Cloud Storage. Immutable — changing it
75 * replaces the image.
76 */
77 rawDisk?: ImageRawDisk;
78 /**
79 * Size of the image when restored onto a persistent disk, in GB.
80 * Immutable — changing it replaces the image.
81 */
82 diskSizeGb?: number;
83 /**
84 * Cloud Storage locations for the image (`us-central1`, `us`, …).
85 * Immutable — changing it replaces the image.
86 */
87 storageLocations?: string[];
88 /**
89 * CPU architecture (`ARM64` or `X86_64`). Immutable — changing it
90 * replaces the image.
91 */
92 architecture?: compute.ImageArchitectureEnum | (string & {});
93 /**
94 * License URIs attached to the image. Immutable — changing it replaces
95 * the image.
96 */
97 licenses?: string[];
98 };
99
100 export type Image = Resource<
101 "GCP.Compute.Image",
102 ImageProps,
103 {
104 /** Image name. */
105 imageName: string;
106 /** Server-assigned numeric id. */
107 imageId: string | undefined;
108 /** Project id. */
109 project: string;
110 /** Server-reported status (`PENDING`, `READY`, `FAILED`, …). */
111 status: string | undefined;
112 /** Image family, if set. */
113 family: string | undefined;
114 /** Description. */
115 description: string | undefined;
116 /** User labels (Alchemy ownership labels stripped). */
117 labels: Record<string, string>;
118 /** Source disk URL, if any. */
119 sourceDisk: string | undefined;
120 /** Source image URL, if any. */
121 sourceImage: string | undefined;
122 /** Source snapshot URL, if any. */
123 sourceSnapshot: string | undefined;
124 /** Size in GB when restored onto a disk. */
125 diskSizeGb: string | undefined;
126 /** Architecture, if set. */
127 architecture: string | undefined;
128 /** Storage locations. */
129 storageLocations: string[];
130 /** Compute Engine self-link. */
131 selfLink: string | undefined;
132 /** RFC3339 creation timestamp. */
133 creationTimestamp: string | undefined;
134 /** Archive size in bytes, if reported. */
135 archiveSizeBytes: string | undefined;
136 },
137 never,
138 Providers
139 >;
140
141 /**
142 * A Compute Engine custom image used to create boot disks for VMs.
143 *
144 * Create from a disk, another image, a snapshot, or a GCS tarball.
145 * `family` and `description` update in place; labels are synced via
146 * `setLabels`. Source, size, architecture, licenses, and storage
147 * locations replace the image.
148 *
149 * ### Creating an Image
150 * **Example:** Image from a disk
151 * ```typescript
152 * const image = yield* GCP.Compute.Image("boot", {
153 * sourceDisk: "projects/my-project/zones/us-central1-a/disks/my-disk",
154 * family: "app-boot",
155 * labels: { env: "prod" },
156 * });
157 * ```
158 *
159 * **Example:** Clone a public image family
160 * ```typescript
161 * const image = yield* GCP.Compute.Image("debian", {
162 * sourceImage: "projects/debian-cloud/global/images/family/debian-12",
163 * storageLocations: ["us-central1"],
164 * });
165 * ```
166 *
167 * ### Updating an Image
168 * **Example:** Family, description, and labels
169 * ```typescript
170 * const image = yield* GCP.Compute.Image("boot", {
171 * imageName: "app-boot-v2",
172 * sourceDisk: "projects/my-project/zones/us-central1-a/disks/my-disk",
173 * family: "app-boot",
174 * description: "golden image",
175 * labels: { env: "prod", role: "boot" },
176 * });
177 * ```
178 *
179 * @resource
180 * @category Compute
181 */
182 export const Image = Resource<Image>("GCP.Compute.Image");
183
184 export class ImageNotResolved extends Data.TaggedError("GCP.Compute.ImageNotResolved")<{
185 imageName: string;
186 }> {}
187
188 export class ImagePending extends Data.TaggedError("GCP.Compute.ImagePending")<{
189 imageName: string;
190 status: string;
191 }> {}
192
193 export class ImageFailed extends Data.TaggedError("GCP.Compute.ImageFailed")<{
194 imageName: string;
195 status: string;
196 }> {}
197
198 export class ImageStillExists extends Data.TaggedError("GCP.Compute.ImageStillExists")<{
199 imageName: string;
200 status: string;
201 }> {}
202
203 export class ImageSourceRequired extends Data.TaggedError("GCP.Compute.ImageSourceRequired")<{
204 imageName: string;
205 }> {}
206
207 const MAX_NAME_LENGTH = 63;
208
209 const lastSegment = (value: string | undefined): string => {
210 if (value === undefined || value.length === 0) return "";
211 const parts = value.split("/");
212 return parts[parts.length - 1] || value;
213 };
214
215 const userLabels = (
216 labels: Record<string, string | undefined> | null | undefined,
217 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
218
219 const sameStrings = (left?: readonly string[], right?: readonly string[]) =>
220 JSON.stringify([...(left ?? [])].sort()) === JSON.stringify([...(right ?? [])].sort());
221
222 const toName = (id: string, name: string | undefined, existing?: string) =>
223 Effect.gen(function* () {
224 if (name !== undefined) return name;
225 if (existing !== undefined) return existing;
226 const generated = yield* createPhysicalName({
227 id,
228 maxLength: MAX_NAME_LENGTH,
229 lowercase: true,
230 });
231 return /^[a-z]/.test(generated) ? generated : `i${generated}`.slice(0, MAX_NAME_LENGTH);
232 });
233
234 const toAttrs = (image: compute.Image, project: string) => ({
235 imageName: image.name ?? image.id ?? "",
236 imageId: image.id,
237 project,
238 status: image.status,
239 family: image.family,
240 description: image.description,
241 labels: userLabels(image.labels),
242 sourceDisk: image.sourceDisk,
243 sourceImage: image.sourceImage,
244 sourceSnapshot: image.sourceSnapshot,
245 diskSizeGb: image.diskSizeGb,
246 architecture: image.architecture,
247 storageLocations: image.storageLocations ?? [],
248 selfLink: image.selfLink,
249 creationTimestamp: image.creationTimestamp,
250 archiveSizeBytes: image.archiveSizeBytes,
251 });
252
253 const getByName = (project: string, image: string) =>
254 compute.getImages({ project, image }).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
255
256 const waitUntilReady = (project: string, imageName: string) =>
257 getByName(project, imageName).pipe(
258 Effect.flatMap((image) =>
259 image?.status === "FAILED"
260 ? Effect.fail(new ImageFailed({ imageName, status: "FAILED" }))
261 : Effect.succeed(image),
262 ),
263 Effect.filterOrFail(
264 (image): image is compute.Image => image !== undefined && image.status === "READY",
265 (image) =>
266 new ImagePending({
267 imageName,
268 status: image?.status ?? "MISSING",
269 }),
270 ),
271 Effect.retry({
272 while: (error) => error._tag === "GCP.Compute.ImagePending",
273 times: 10,
274 schedule: Schedule.spaced("5 seconds"),
275 }),
276 );
277
278 const waitUntilGone = (project: string, imageName: string) =>
279 getByName(project, imageName).pipe(
280 Effect.flatMap((image) =>
281 image === undefined
282 ? Effect.void
283 : Effect.fail(
284 new ImageStillExists({
285 imageName,
286 status: image.status ?? "UNKNOWN",
287 }),
288 ),
289 ),
290 Effect.retry({
291 while: (error) => error._tag === "GCP.Compute.ImageStillExists",
292 times: 18,
293 schedule: Schedule.spaced("3 seconds"),
294 }),
295 );
296
297 const insertBody = (
298 imageName: string,
299 news: ImageProps,
300 labels: Record<string, string>,
301 ): compute.Image => ({
302 name: imageName,
303 description: news.description,
304 family: news.family,
305 labels,
306 sourceDisk: news.sourceDisk,
307 sourceImage: news.sourceImage,
308 sourceSnapshot: news.sourceSnapshot,
309 rawDisk: news.rawDisk
310 ? {
311 source: news.rawDisk.source,
312 sha1Checksum: news.rawDisk.sha1Checksum,
313 containerType: news.rawDisk.containerType ?? "TAR",
314 }
315 : undefined,
316 diskSizeGb: news.diskSizeGb !== undefined ? String(news.diskSizeGb) : undefined,
317 storageLocations: news.storageLocations,
318 architecture: news.architecture,
319 licenses: news.licenses,
320 });
321
322 const hasSource = (props: ImageProps) =>
323 props.sourceDisk !== undefined ||
324 props.sourceImage !== undefined ||
325 props.sourceSnapshot !== undefined ||
326 props.rawDisk?.source !== undefined;
327
328 const sourceIdentity = (props: ImageProps): string => {
329 if (props.sourceDisk !== undefined) {
330 return `disk:${lastSegment(props.sourceDisk)}`;
331 }
332 if (props.sourceImage !== undefined) {
333 return `image:${lastSegment(props.sourceImage)}`;
334 }
335 if (props.sourceSnapshot !== undefined) {
336 return `snapshot:${lastSegment(props.sourceSnapshot)}`;
337 }
338 if (props.rawDisk?.source !== undefined) {
339 return `raw:${props.rawDisk.source}:${props.rawDisk.sha1Checksum ?? ""}:${props.rawDisk.containerType ?? "TAR"}`;
340 }
341 return "";
342 };
343
344 const sourceChanged = (olds: ImageProps, news: ImageProps) => {
345 const previous = sourceIdentity(olds);
346 const next = sourceIdentity(news);
347 return previous.length > 0 && next.length > 0 && previous !== next;
348 };
349
350 export const ImageProvider = () =>
351 Provider.succeed(Image, {
352 stables: [
353 "imageName",
354 "imageId",
355 "project",
356 "selfLink",
357 "creationTimestamp",
358 "sourceDisk",
359 "sourceImage",
360 "sourceSnapshot",
361 "architecture",
362 ],
363
364 diff: Effect.fn(function* ({ news, olds, output }) {
365 if (!isResolved(news)) return undefined;
366 const previousName = olds.imageName ?? output?.imageName;
367 const nextName = news.imageName ?? previousName;
368 const nameChanged =
369 previousName !== undefined && nextName !== undefined && previousName !== nextName;
370
371 const previousSize = olds.diskSizeGb ?? Number(output?.diskSizeGb);
372 const sizeChanged =
373 news.diskSizeGb !== undefined &&
374 previousSize !== undefined &&
375 !Number.isNaN(previousSize) &&
376 news.diskSizeGb !== previousSize;
377
378 const architectureChanged =
379 news.architecture !== undefined &&
380 (olds.architecture ?? output?.architecture) !== undefined &&
381 news.architecture !== (olds.architecture ?? output?.architecture);
382
383 const storageChanged =
384 news.storageLocations !== undefined &&
385 !sameStrings(news.storageLocations, olds.storageLocations ?? output?.storageLocations);
386
387 const licensesChanged =
388 news.licenses !== undefined &&
389 olds.licenses !== undefined &&
390 !sameStrings(news.licenses, olds.licenses);
391
392 const replace =
393 nameChanged ||
394 sourceChanged(olds, news) ||
395 sizeChanged ||
396 architectureChanged ||
397 storageChanged ||
398 licensesChanged;
399
400 if (!replace) return undefined;
401 return {
402 action: "replace" as const,
403 deleteFirst:
404 previousName !== undefined && nextName !== undefined && nextName === previousName,
405 };
406 }),
407
408 read: Effect.fn(function* ({ id, olds, output }) {
409 const env = yield* GcpEnvironment.current;
410 const imageName = yield* toName(id, olds?.imageName, output?.imageName);
411 const existing = yield* getByName(env.project, imageName);
412 if (existing === undefined) return undefined;
413 const attrs = toAttrs(existing, env.project);
414 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
415 }),
416
417 list: () =>
418 Effect.gen(function* () {
419 const env = yield* GcpEnvironment.current;
420 return yield* compute.listImages
421 .items({
422 project: env.project,
423 maxResults: 500,
424 filter: "labels.alchemy-id:*",
425 })
426 .pipe(
427 Stream.filter((image) =>
428 Object.keys(image.labels ?? {}).some((key) => key.startsWith("alchemy-")),
429 ),
430 Stream.map((image) => toAttrs(image, env.project)),
431 Stream.runCollect,
432 Effect.map((chunk) => Array.from(chunk)),
433 );
434 }),
435
436 reconcile: Effect.fn(function* ({ id, news, output }) {
437 const env = yield* GcpEnvironment.current;
438 const imageName = yield* toName(id, news.imageName, output?.imageName);
439 const desiredLabels = {
440 ...toLabels(news.labels),
441 ...(yield* createInternalLabels(id)),
442 };
443
444 let current = yield* getByName(env.project, imageName);
445 if (current?.status === "DELETING") {
446 yield* waitUntilGone(env.project, imageName);
447 current = undefined;
448 }
449
450 if (current === undefined) {
451 if (!hasSource(news)) {
452 return yield* new ImageSourceRequired({ imageName });
453 }
454 const inserted = yield* compute
455 .insertImages({
456 project: env.project,
457 body: insertBody(imageName, news, desiredLabels),
458 })
459 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
460 if (inserted !== undefined) {
461 yield* waitGlobalOperation(env.project, inserted, {
462 ignore: ["RESOURCE_ALREADY_EXISTS"],
463 });
464 }
465 current = yield* waitUntilReady(env.project, imageName);
466 }
467
468 if (current === undefined) {
469 return yield* new ImageNotResolved({ imageName });
470 }
471
472 if (current.status !== "READY") {
473 current = yield* waitUntilReady(env.project, imageName);
474 }
475
476 if (current === undefined) {
477 return yield* new ImageNotResolved({ imageName });
478 }
479
480 const familyChanged = (current.family ?? "") !== (news.family ?? "");
481 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
482 if (familyChanged || descriptionChanged) {
483 yield* compute
484 .patchImages({
485 project: env.project,
486 image: imageName,
487 body: {
488 family: news.family,
489 description: news.description,
490 },
491 })
492 .pipe(Effect.flatMap((operation) => waitGlobalOperation(env.project, operation)));
493 current = (yield* getByName(env.project, imageName)) ?? current;
494 }
495
496 const observedLabels = tagRecord(current.labels);
497 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
498 if (upsert.length > 0 || removed.length > 0) {
499 yield* compute
500 .setLabelsImages({
501 project: env.project,
502 resource: imageName,
503 body: {
504 labels: desiredLabels,
505 labelFingerprint: current.labelFingerprint,
506 },
507 })
508 .pipe(Effect.flatMap((operation) => waitGlobalOperation(env.project, operation)));
509 // A read right after setLabels can still return the old labels.
510 current =
511 (yield* getByName(env.project, imageName).pipe(
512 Effect.repeat({
513 schedule: Schedule.spaced("2 seconds"),
514 until: (image) => {
515 const labels = diffLabels(tagRecord(image?.labels), desiredLabels);
516 return labels.upsert.length === 0 && labels.removed.length === 0;
517 },
518 times: 10,
519 }),
520 )) ?? current;
521 }
522
523 if (current === undefined) {
524 return yield* new ImageNotResolved({ imageName });
525 }
526
527 return toAttrs(current, env.project);
528 }),
529
530 delete: Effect.fn(function* ({ output }) {
531 const env = yield* GcpEnvironment.current;
532 const operation = yield* compute
533 .deleteImages({
534 project: env.project,
535 image: output.imageName,
536 })
537 .pipe(
538 Effect.catchTag("NotFound", () => Effect.undefined),
539 Effect.retry({
540 while: (error) => error._tag === "Conflict",
541 times: 8,
542 schedule: Schedule.spaced("2 seconds"),
543 }),
544 );
545 if (operation !== undefined) {
546 yield* waitGlobalOperation(env.project, operation, {
547 ignore: ["RESOURCE_NOT_FOUND"],
548 }).pipe(Effect.catchTag("NotFound", () => Effect.void));
549 }
550 yield* waitUntilGone(env.project, output.imageName);
551 }),
552 });