@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, 06:00 UTC

main
ts 485 lines 16.9 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 const DEFAULT_SNAPSHOT_TYPE = "STANDARD";
24
25 export type RegionSnapshotProps = {
26 /**
27 * Snapshot name (RFC1035, 1-63 characters). If omitted, a unique name is
28 * generated from the stack, stage, and logical id. Changing the name
29 * replaces the snapshot.
30 */
31 snapshotName?: string;
32 /**
33 * Region the snapshot lives in (e.g. `us-central1`). Immutable —
34 * changing it replaces the snapshot. `US-CENTRAL1` is accepted and
35 * normalized to `us-central1`.
36 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
37 */
38 region?: string;
39 /**
40 * Source regional disk URL or relative path used to create the snapshot
41 * (`projects/{project}/regions/{region}/disks/{disk}`,
42 * `regions/{region}/disks/{disk}`, or a full self-link). Immutable —
43 * changing it replaces the snapshot.
44 */
45 sourceDisk: string;
46 /**
47 * Source instant snapshot URL. Immutable — changing it replaces the
48 * snapshot.
49 */
50 sourceInstantSnapshot?: string;
51 /**
52 * Optional description. Immutable — changing it replaces the snapshot.
53 */
54 description?: string;
55 /**
56 * Snapshot type (`STANDARD` or `ARCHIVE`). Immutable — changing it
57 * replaces the snapshot.
58 * @default "STANDARD"
59 */
60 snapshotType?: compute.SnapshotSnapshotTypeEnum | (string & {});
61 /**
62 * Cloud Storage locations to store the snapshot (regional or
63 * multi-regional codes such as `us-central1` or `us`). Immutable —
64 * changing it replaces the snapshot.
65 */
66 storageLocations?: string[];
67 /**
68 * Snapshot chain name for advanced chargeback tracking. Immutable —
69 * changing it replaces the snapshot.
70 */
71 chainName?: string;
72 /**
73 * Attempt an application-consistent snapshot by flushing the guest OS.
74 * Input-only; not persisted on the resource.
75 */
76 guestFlush?: boolean;
77 /**
78 * User labels. Alchemy ownership labels are merged in automatically.
79 */
80 labels?: Record<string, string>;
81 };
82
83 export type RegionSnapshot = Resource<
84 "GCP.Compute.RegionSnapshot",
85 RegionSnapshotProps,
86 {
87 /** Snapshot name. */
88 snapshotName: string;
89 /** Server-assigned numeric id. */
90 snapshotId: string | undefined;
91 /** Project id. */
92 project: string;
93 /** Region short name (`us-central1`). */
94 region: string;
95 /** Source disk URL. */
96 sourceDisk: string | undefined;
97 /** Server-assigned source disk id. */
98 sourceDiskId: string | undefined;
99 /** Source instant snapshot URL, if any. */
100 sourceInstantSnapshot: string | undefined;
101 /** Snapshot type (`STANDARD` or `ARCHIVE`). */
102 snapshotType: string | undefined;
103 /** Cloud Storage locations. */
104 storageLocations: string[];
105 /** Snapshot chain name, if set. */
106 chainName: string | undefined;
107 /** Optional description. */
108 description: string | undefined;
109 /** Server-reported status (`READY`, `CREATING`, …). */
110 status: string | undefined;
111 /** Size of the source disk in GB. */
112 diskSizeGb: string | undefined;
113 /** Storage used by the snapshot, in bytes. */
114 storageBytes: string | undefined;
115 /** User labels (Alchemy ownership labels stripped). */
116 labels: Record<string, string>;
117 /** Compute Engine self-link. */
118 selfLink: string | undefined;
119 /** RFC3339 creation timestamp. */
120 creationTimestamp: string | undefined;
121 },
122 never,
123 Providers
124 >;
125
126 /**
127 * A regional Compute Engine persistent-disk snapshot.
128 *
129 * Regional snapshots are created from a regional disk (or instant
130 * snapshot) and stored in the same region. Name, source, type, storage
131 * locations, chain name, description, and region are immutable —
132 * changing them replaces the snapshot. Labels are updated in place via
133 * `regionSnapshots.setLabels`.
134 *
135 * ### Creating a Regional Snapshot
136 * **Example:** Snapshot of a regional disk
137 * ```typescript
138 * const disk = yield* GCP.Compute.RegionDisk("data", {
139 * region: "us-central1",
140 * sizeGb: 200,
141 * });
142 * const snapshot = yield* GCP.Compute.RegionSnapshot("nightly", {
143 * region: "us-central1",
144 * sourceDisk: disk.selfLink,
145 * });
146 * ```
147 *
148 * @resource
149 * @category Compute
150 */
151 export const RegionSnapshot = Resource<RegionSnapshot>("GCP.Compute.RegionSnapshot");
152
153 export class RegionSnapshotNotResolved extends Data.TaggedError(
154 "GCP.Compute.RegionSnapshotNotResolved",
155 )<{
156 snapshotName: string;
157 region: string;
158 }> {}
159
160 export class RegionSnapshotNotReady extends Data.TaggedError("GCP.Compute.RegionSnapshotNotReady")<{
161 snapshotName: string;
162 status: string;
163 }> {}
164
165 export class RegionSnapshotFailed extends Data.TaggedError("GCP.Compute.RegionSnapshotFailed")<{
166 snapshotName: string;
167 status: string;
168 }> {}
169
170 export class RegionSnapshotStillExists extends Data.TaggedError(
171 "GCP.Compute.RegionSnapshotStillExists",
172 )<{
173 snapshotName: string;
174 status: string;
175 }> {}
176
177 const lastSegment = (value: string | undefined): string | undefined => {
178 if (value === undefined || value.length === 0) return undefined;
179 const parts = value.split("/");
180 return parts[parts.length - 1] || value;
181 };
182
183 const normalizeRegion = (region: string | undefined, defaultRegion: string) =>
184 (lastSegment(region ?? defaultRegion) ?? defaultRegion).toLowerCase();
185
186 const canonicalizeSource = (source: string | undefined): string => {
187 if (source === undefined || source.length === 0) return "";
188 const cleaned = source.split("?")[0] ?? source;
189 const zonal = cleaned.match(/(zones\/[^/]+\/disks\/[^/]+)$/);
190 if (zonal?.[1] !== undefined) return zonal[1];
191 const regional = cleaned.match(/(regions\/[^/]+\/disks\/[^/]+)$/);
192 if (regional?.[1] !== undefined) return regional[1];
193 const instant = cleaned.match(/((?:zones|regions)\/[^/]+\/instantSnapshots\/[^/]+)$/);
194 if (instant?.[1] !== undefined) return instant[1];
195 return cleaned.replace(/^https?:\/\/[^/]+\//, "");
196 };
197
198 const sameLocations = (
199 left: readonly string[] | undefined,
200 right: readonly string[] | undefined,
201 ): boolean => {
202 const a = [...(left ?? [])].map((value) => value.toLowerCase()).sort();
203 const b = [...(right ?? [])].map((value) => value.toLowerCase()).sort();
204 return a.length === b.length && a.every((value, index) => value === b[index]);
205 };
206
207 const userLabels = (
208 labels: Record<string, string | undefined> | null | undefined,
209 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
210
211 const toName = (id: string, name: string | undefined, existing?: string) =>
212 Effect.gen(function* () {
213 if (name !== undefined) return name;
214 if (existing !== undefined) return existing;
215 const generated = yield* createPhysicalName({
216 id,
217 maxLength: 63,
218 lowercase: true,
219 });
220 return /^[a-z]/.test(generated) ? generated : `s${generated}`.slice(0, 63);
221 });
222
223 const toAttrs = (snapshot: compute.Snapshot, project: string) => ({
224 snapshotName: snapshot.name ?? snapshot.id ?? "",
225 snapshotId: snapshot.id,
226 project,
227 region: (lastSegment(snapshot.region) ?? "").toLowerCase(),
228 sourceDisk: snapshot.sourceDisk,
229 sourceDiskId: snapshot.sourceDiskId,
230 sourceInstantSnapshot: snapshot.sourceInstantSnapshot,
231 snapshotType: snapshot.snapshotType,
232 storageLocations: snapshot.storageLocations ?? [],
233 chainName: snapshot.chainName,
234 description: snapshot.description,
235 status: snapshot.status,
236 diskSizeGb: snapshot.diskSizeGb,
237 storageBytes: snapshot.storageBytes,
238 labels: userLabels(snapshot.labels),
239 selfLink: snapshot.selfLink,
240 creationTimestamp: snapshot.creationTimestamp,
241 });
242
243 const getByName = (project: string, region: string, snapshot: string) =>
244 compute
245 .getRegionSnapshots({ project, region, snapshot })
246 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
247
248 const waitSnapshotReady = (project: string, region: string, snapshotName: string) =>
249 getByName(project, region, snapshotName).pipe(
250 Effect.flatMap((snapshot) =>
251 snapshot?.status === "FAILED"
252 ? Effect.fail(new RegionSnapshotFailed({ snapshotName, status: "FAILED" }))
253 : Effect.succeed(snapshot),
254 ),
255 Effect.filterOrFail(
256 (snapshot): snapshot is compute.Snapshot =>
257 snapshot !== undefined && snapshot.status === "READY",
258 (snapshot) =>
259 new RegionSnapshotNotReady({
260 snapshotName,
261 status: snapshot?.status ?? "MISSING",
262 }),
263 ),
264 // Snapshot uploads routinely take a few minutes to reach READY.
265 Effect.retry({
266 while: (error) => error._tag === "GCP.Compute.RegionSnapshotNotReady",
267 times: 72,
268 schedule: Schedule.spaced("5 seconds"),
269 }),
270 );
271
272 const waitSnapshotGone = (project: string, region: string, snapshotName: string) =>
273 getByName(project, region, snapshotName).pipe(
274 Effect.flatMap((snapshot) =>
275 snapshot === undefined
276 ? Effect.void
277 : Effect.fail(
278 new RegionSnapshotStillExists({
279 snapshotName,
280 status: snapshot.status ?? "UNKNOWN",
281 }),
282 ),
283 ),
284 Effect.retry({
285 while: (error) => error instanceof RegionSnapshotStillExists,
286 times: 10,
287 schedule: Schedule.spaced("3 seconds"),
288 }),
289 );
290
291 export const RegionSnapshotProvider = () =>
292 Provider.succeed(RegionSnapshot, {
293 stables: [
294 "snapshotName",
295 "snapshotId",
296 "project",
297 "region",
298 "sourceDisk",
299 "sourceDiskId",
300 "sourceInstantSnapshot",
301 "snapshotType",
302 "storageLocations",
303 "chainName",
304 "selfLink",
305 "creationTimestamp",
306 ],
307
308 diff: Effect.fn(function* ({ news, olds, output }) {
309 if (!isResolved(news)) return undefined;
310 const env = yield* GcpEnvironment.current;
311
312 const previousName = olds?.snapshotName ?? output?.snapshotName;
313 const nextName = news.snapshotName ?? previousName;
314 const previousRegion = normalizeRegion(olds?.region ?? output?.region, env.region);
315 const nextRegion = normalizeRegion(news.region ?? output?.region, env.region);
316 const previousSource = canonicalizeSource(olds?.sourceDisk ?? output?.sourceDisk);
317 const nextSource = canonicalizeSource(news.sourceDisk);
318 const previousInstant = canonicalizeSource(
319 olds?.sourceInstantSnapshot ?? output?.sourceInstantSnapshot,
320 );
321 const nextInstant = canonicalizeSource(news.sourceInstantSnapshot);
322 const previousType = olds?.snapshotType ?? output?.snapshotType ?? DEFAULT_SNAPSHOT_TYPE;
323 const nextType = news.snapshotType ?? DEFAULT_SNAPSHOT_TYPE;
324 const previousDescription = olds?.description ?? output?.description ?? "";
325 const nextDescription = news.description ?? "";
326 const previousChain = olds?.chainName ?? output?.chainName ?? "";
327 const nextChain = news.chainName ?? "";
328 const locationsSpecified = news.storageLocations !== undefined;
329 const locationsChanged =
330 locationsSpecified &&
331 !sameLocations(news.storageLocations, olds?.storageLocations ?? output?.storageLocations);
332
333 const replace =
334 previousRegion !== nextRegion ||
335 (previousName !== undefined && nextName !== undefined && previousName !== nextName) ||
336 (nextSource.length > 0 && previousSource.length > 0 && previousSource !== nextSource) ||
337 previousInstant !== nextInstant ||
338 previousType !== nextType ||
339 previousDescription !== nextDescription ||
340 previousChain !== nextChain ||
341 locationsChanged;
342
343 if (!replace) return undefined;
344 return {
345 action: "replace" as const,
346 deleteFirst:
347 previousRegion === nextRegion &&
348 previousName !== undefined &&
349 nextName !== undefined &&
350 previousName === nextName,
351 };
352 }),
353
354 read: Effect.fn(function* ({ id, olds, output }) {
355 const env = yield* GcpEnvironment.current;
356 const snapshotName = yield* toName(id, olds?.snapshotName, output?.snapshotName);
357 const region = normalizeRegion(olds?.region ?? output?.region, env.region);
358 const existing = yield* getByName(env.project, region, snapshotName);
359 if (existing === undefined) return undefined;
360 const attrs = toAttrs(existing, env.project);
361 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
362 }),
363
364 list: () =>
365 Effect.gen(function* () {
366 const env = yield* GcpEnvironment.current;
367 return yield* compute.listRegionSnapshots
368 .items({
369 project: env.project,
370 region: env.region,
371 filter: "labels.alchemy-id:*",
372 maxResults: 500,
373 returnPartialSuccess: true,
374 })
375 .pipe(
376 Stream.filter((snapshot) =>
377 Object.keys(snapshot.labels ?? {}).some((key) => key.startsWith("alchemy-")),
378 ),
379 Stream.map((snapshot) => toAttrs(snapshot, env.project)),
380 Stream.runCollect,
381 Effect.map((chunk) => Array.from(chunk)),
382 );
383 }),
384
385 reconcile: Effect.fn(function* ({ id, news, output }) {
386 const env = yield* GcpEnvironment.current;
387 const snapshotName = yield* toName(id, news.snapshotName, output?.snapshotName);
388 const region = normalizeRegion(news.region ?? output?.region, env.region);
389 const desiredLabels = {
390 ...toLabels(news.labels),
391 ...(yield* createInternalLabels(id)),
392 };
393
394 let current = yield* getByName(env.project, region, snapshotName);
395 if (current?.status === "DELETING") {
396 yield* waitSnapshotGone(env.project, region, snapshotName);
397 current = undefined;
398 }
399
400 if (current === undefined) {
401 const inserted = yield* compute
402 .insertRegionSnapshots({
403 project: env.project,
404 region,
405 body: {
406 name: snapshotName,
407 sourceDisk: news.sourceDisk,
408 sourceInstantSnapshot: news.sourceInstantSnapshot,
409 description: news.description,
410 labels: desiredLabels,
411 snapshotType: news.snapshotType,
412 storageLocations: news.storageLocations,
413 chainName: news.chainName,
414 guestFlush: news.guestFlush,
415 },
416 })
417 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
418 if (inserted !== undefined) {
419 yield* waitRegionOperation(env.project, region, inserted, {
420 ignore: ["RESOURCE_ALREADY_EXISTS"],
421 });
422 }
423 current = yield* waitSnapshotReady(env.project, region, snapshotName);
424 }
425
426 if (current === undefined) {
427 return yield* new RegionSnapshotNotResolved({ snapshotName, region });
428 }
429
430 if (current.status !== "READY") {
431 current = yield* waitSnapshotReady(env.project, region, snapshotName);
432 }
433
434 if (current === undefined) {
435 return yield* new RegionSnapshotNotResolved({ snapshotName, region });
436 }
437
438 const observedLabels = tagRecord(current.labels);
439 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
440 if (upsert.length > 0 || removed.length > 0) {
441 const labeled = yield* compute.setLabelsRegionSnapshots({
442 project: env.project,
443 region,
444 resource: snapshotName,
445 body: {
446 labels: desiredLabels,
447 labelFingerprint: current.labelFingerprint,
448 },
449 });
450 yield* waitRegionOperation(env.project, region, labeled);
451 current = yield* getByName(env.project, region, snapshotName);
452 }
453
454 if (current === undefined) {
455 return yield* new RegionSnapshotNotResolved({ snapshotName, region });
456 }
457
458 return toAttrs(current, env.project);
459 }),
460
461 delete: Effect.fn(function* ({ output }) {
462 const env = yield* GcpEnvironment.current;
463 const region = normalizeRegion(output.region, env.region);
464 const deleted = yield* compute
465 .deleteRegionSnapshots({
466 project: output.project,
467 region,
468 snapshot: output.snapshotName,
469 })
470 .pipe(
471 Effect.catchTag("NotFound", () => Effect.undefined),
472 Effect.retry({
473 while: (error) => error._tag === "Conflict",
474 times: 8,
475 schedule: Schedule.spaced("2 seconds"),
476 }),
477 );
478 if (deleted !== undefined) {
479 yield* waitRegionOperation(output.project, region, deleted, {
480 ignore: ["RESOURCE_NOT_FOUND"],
481 });
482 }
483 yield* waitSnapshotGone(output.project, region, output.snapshotName);
484 }),
485 });