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

main
ts 515 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 { waitZoneOperation } from "./operations.ts";
22
23 const DEFAULT_ZONE = "us-central1-a";
24 const DEFAULT_TYPE = "hyperdisk-balanced";
25 const MAX_NAME_LENGTH = 63;
26
27 export type StoragePoolCapacityProvisioningType =
28 | compute.StoragePoolCapacityProvisioningTypeEnum
29 | (string & {});
30 export type StoragePoolPerformanceProvisioningType =
31 | compute.StoragePoolPerformanceProvisioningTypeEnum
32 | (string & {});
33
34 export type StoragePoolProps = {
35 /**
36 * Storage pool name (RFC1035, 1-63 characters). If omitted, a unique
37 * name is generated from the stack, stage, and logical id. Changing the
38 * name replaces the pool.
39 */
40 storagePoolName?: string;
41 /**
42 * Zone the pool lives in (e.g. `us-central1-a`). Immutable — changing it
43 * replaces the pool.
44 * @default "us-central1-a"
45 */
46 zone?: string;
47 /**
48 * Storage pool type (`hyperdisk-balanced`, `hyperdisk-throughput`,
49 * `hyperdisk-extreme`, …). Immutable — changing it replaces the pool.
50 * @default "hyperdisk-balanced"
51 */
52 storagePoolType?: string;
53 /**
54 * Optional description.
55 */
56 description?: string;
57 /**
58 * User labels. Alchemy ownership labels are merged in automatically.
59 */
60 labels?: Record<string, string>;
61 /**
62 * Provisioned pool capacity in GiB. Mutable in place. Hyperdisk Balanced
63 * pools typically require at least 10240 GiB.
64 */
65 poolProvisionedCapacityGb?: number | string;
66 /**
67 * Provisioned IOPS (hyperdisk-balanced). Mutable in place.
68 */
69 poolProvisionedIops?: number | string;
70 /**
71 * Provisioned throughput in MiB/s (hyperdisk-balanced /
72 * hyperdisk-throughput). Mutable in place.
73 */
74 poolProvisionedThroughput?: number | string;
75 /**
76 * Capacity provisioning type. Immutable — changing it replaces the pool.
77 */
78 capacityProvisioningType?: StoragePoolCapacityProvisioningType;
79 /**
80 * Performance provisioning type. Immutable — changing it replaces the
81 * pool.
82 */
83 performanceProvisioningType?: StoragePoolPerformanceProvisioningType;
84 };
85
86 export type StoragePool = Resource<
87 "GCP.Compute.StoragePool",
88 StoragePoolProps,
89 {
90 /** Storage pool name. */
91 storagePoolName: string;
92 /** Server-assigned numeric id. */
93 storagePoolId: string | undefined;
94 /** Project id. */
95 project: string;
96 /** Zone short name (`us-central1-a`). */
97 zone: string;
98 /** Storage pool type short name. */
99 storagePoolType: string | undefined;
100 /** Optional description. */
101 description: string | undefined;
102 /** User labels (Alchemy ownership labels stripped). */
103 labels: Record<string, string>;
104 /** Provisioned capacity in GiB. */
105 poolProvisionedCapacityGb: string | undefined;
106 /** Provisioned IOPS. */
107 poolProvisionedIops: string | undefined;
108 /** Provisioned throughput in MiB/s. */
109 poolProvisionedThroughput: string | undefined;
110 /** Capacity provisioning type. */
111 capacityProvisioningType: string | undefined;
112 /** Performance provisioning type. */
113 performanceProvisioningType: string | undefined;
114 /** Server-reported state (`READY`, `CREATING`, …). */
115 state: string | undefined;
116 /** Compute Engine self-link. */
117 selfLink: string | undefined;
118 /** RFC3339 creation timestamp. */
119 creationTimestamp: string | undefined;
120 },
121 never,
122 Providers
123 >;
124
125 /**
126 * A zonal Compute Engine Hyperdisk storage pool.
127 *
128 * Storage pools pre-purchase Hyperdisk capacity, IOPS, and throughput.
129 * Changing `zone`, `storagePoolType`, `capacityProvisioningType`, or
130 * `performanceProvisioningType` replaces the pool. Capacity, IOPS,
131 * throughput, labels, and description update in place.
132 *
133 * ### Creating a Storage Pool
134 * **Example:** Hyperdisk Balanced pool
135 * ```typescript
136 * const pool = yield* GCP.Compute.StoragePool("disks", {
137 * storagePoolType: "hyperdisk-balanced",
138 * poolProvisionedCapacityGb: 10240,
139 * poolProvisionedIops: 10000,
140 * poolProvisionedThroughput: 1024,
141 * labels: { env: "prod" },
142 * });
143 * ```
144 *
145 * **Example:** Named pool in a specific zone
146 * ```typescript
147 * const pool = yield* GCP.Compute.StoragePool("disks", {
148 * storagePoolName: "app-hyperdisk",
149 * zone: "us-central1-a",
150 * description: "shared hyperdisk capacity",
151 * poolProvisionedCapacityGb: 10240,
152 * });
153 * ```
154 *
155 * @resource
156 * @category Compute
157 */
158 export const StoragePool = Resource<StoragePool>("GCP.Compute.StoragePool");
159
160 export class StoragePoolNotResolved extends Data.TaggedError("GCP.Compute.StoragePoolNotResolved")<{
161 storagePoolName: string;
162 zone: string;
163 }> {}
164
165 export class StoragePoolNotReady extends Data.TaggedError("GCP.Compute.StoragePoolNotReady")<{
166 storagePoolName: string;
167 status: string;
168 }> {}
169
170 export class StoragePoolFailed extends Data.TaggedError("GCP.Compute.StoragePoolFailed")<{
171 storagePoolName: string;
172 status: string;
173 }> {}
174
175 export class StoragePoolStillExists extends Data.TaggedError("GCP.Compute.StoragePoolStillExists")<{
176 storagePoolName: string;
177 status: string;
178 }> {}
179
180 const lastSegment = (value: string | undefined): string => {
181 if (value === undefined || value.length === 0) return "";
182 const trimmed = value.replace(/\/+$/, "");
183 const parts = trimmed.split("/");
184 return parts[parts.length - 1] || trimmed;
185 };
186
187 const normalizeZone = (zone: string | undefined) => lastSegment(zone ?? DEFAULT_ZONE).toLowerCase();
188
189 const asString = (value: number | string | undefined): string | undefined => {
190 if (value === undefined) return undefined;
191 return String(value);
192 };
193
194 const userLabels = (
195 labels: Record<string, string | undefined> | null | undefined,
196 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
197
198 const toName = (id: string, name: string | undefined, existing?: string) =>
199 Effect.gen(function* () {
200 if (name !== undefined) return name;
201 if (existing !== undefined) return existing;
202 const generated = yield* createPhysicalName({
203 id,
204 maxLength: MAX_NAME_LENGTH,
205 lowercase: true,
206 });
207 return /^[a-z]/.test(generated) ? generated : `s${generated}`.slice(0, MAX_NAME_LENGTH);
208 });
209
210 const poolTypeUrl = (project: string, zone: string, type: string) =>
211 type.includes("/") ? type : `projects/${project}/zones/${zone}/storagePoolTypes/${type}`;
212
213 const toAttrs = (pool: compute.StoragePool, project: string): StoragePool["Attributes"] => ({
214 storagePoolName: pool.name ?? pool.id ?? "",
215 storagePoolId: pool.id,
216 project,
217 zone: normalizeZone(pool.zone),
218 storagePoolType: lastSegment(pool.storagePoolType) || undefined,
219 description: pool.description,
220 labels: userLabels(pool.labels),
221 poolProvisionedCapacityGb: pool.poolProvisionedCapacityGb,
222 poolProvisionedIops: pool.poolProvisionedIops,
223 poolProvisionedThroughput: pool.poolProvisionedThroughput,
224 capacityProvisioningType: pool.capacityProvisioningType,
225 performanceProvisioningType: pool.performanceProvisioningType,
226 state: pool.state,
227 selfLink: pool.selfLink,
228 creationTimestamp: pool.creationTimestamp,
229 });
230
231 const getByName = (project: string, zone: string, storagePool: string) =>
232 compute
233 .getStoragePools({ project, zone, storagePool })
234 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
235
236 const waitPoolReady = (project: string, zone: string, storagePoolName: string) =>
237 getByName(project, zone, storagePoolName).pipe(
238 Effect.flatMap((pool) =>
239 pool?.state === "FAILED"
240 ? Effect.fail(
241 new StoragePoolFailed({
242 storagePoolName,
243 status: "FAILED",
244 }),
245 )
246 : Effect.succeed(pool),
247 ),
248 Effect.filterOrFail(
249 (pool): pool is compute.StoragePool => pool !== undefined && pool.state === "READY",
250 (pool) =>
251 new StoragePoolNotReady({
252 storagePoolName,
253 status: pool?.state ?? "MISSING",
254 }),
255 ),
256 Effect.retry({
257 while: (error) => error._tag === "GCP.Compute.StoragePoolNotReady",
258 times: 10,
259 schedule: Schedule.spaced("2 seconds"),
260 }),
261 );
262
263 const waitPoolGone = (project: string, zone: string, storagePoolName: string) =>
264 getByName(project, zone, storagePoolName).pipe(
265 Effect.flatMap((pool) =>
266 pool === undefined
267 ? Effect.void
268 : Effect.fail(
269 new StoragePoolStillExists({
270 storagePoolName,
271 status: pool.state ?? "UNKNOWN",
272 }),
273 ),
274 ),
275 Effect.retry({
276 while: (error) => error instanceof StoragePoolStillExists,
277 times: 10,
278 schedule: Schedule.spaced("2 seconds"),
279 }),
280 );
281
282 export const StoragePoolProvider = () =>
283 Provider.succeed(StoragePool, {
284 stables: [
285 "storagePoolName",
286 "storagePoolId",
287 "project",
288 "zone",
289 "storagePoolType",
290 "selfLink",
291 "creationTimestamp",
292 ],
293
294 diff: Effect.fn(function* ({ news, olds, output }) {
295 if (!isResolved(news)) return undefined;
296
297 const previousName = olds?.storagePoolName ?? output?.storagePoolName;
298 const nextName = news.storagePoolName ?? previousName;
299 const nameChanged =
300 previousName !== undefined && nextName !== undefined && previousName !== nextName;
301
302 const previousZone = normalizeZone(olds?.zone ?? output?.zone);
303 const nextZone = normalizeZone(news.zone ?? output?.zone);
304 const previousType =
305 lastSegment(olds?.storagePoolType) || lastSegment(output?.storagePoolType) || DEFAULT_TYPE;
306 const nextType = lastSegment(news.storagePoolType) || DEFAULT_TYPE;
307 const previousCapacityType =
308 olds?.capacityProvisioningType ?? output?.capacityProvisioningType;
309 const nextCapacityType = news.capacityProvisioningType;
310 const previousPerfType =
311 olds?.performanceProvisioningType ?? output?.performanceProvisioningType;
312 const nextPerfType = news.performanceProvisioningType;
313
314 const typeChanged = previousType !== nextType;
315 const capacityTypeChanged =
316 previousCapacityType !== undefined &&
317 nextCapacityType !== undefined &&
318 previousCapacityType !== nextCapacityType;
319 const perfTypeChanged =
320 previousPerfType !== undefined &&
321 nextPerfType !== undefined &&
322 previousPerfType !== nextPerfType;
323
324 if (
325 nameChanged ||
326 previousZone !== nextZone ||
327 typeChanged ||
328 capacityTypeChanged ||
329 perfTypeChanged
330 ) {
331 return {
332 action: "replace" as const,
333 deleteFirst:
334 previousName !== undefined && nextName === previousName && previousZone === nextZone,
335 };
336 }
337 return undefined;
338 }),
339
340 read: Effect.fn(function* ({ id, olds, output }) {
341 const env = yield* GcpEnvironment.current;
342 const storagePoolName = yield* toName(id, olds?.storagePoolName, output?.storagePoolName);
343 const zone = normalizeZone(olds?.zone ?? output?.zone);
344 const existing = yield* getByName(env.project, zone, storagePoolName);
345 if (existing === undefined) return undefined;
346 const attrs = toAttrs(existing, env.project);
347 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
348 }),
349
350 list: () =>
351 Effect.gen(function* () {
352 const env = yield* GcpEnvironment.current;
353 const pages = yield* compute.aggregatedListStoragePools
354 .pages({
355 project: env.project,
356 filter: "labels.alchemy-id:*",
357 returnPartialSuccess: true,
358 maxResults: 500,
359 })
360 .pipe(Stream.runCollect);
361 return Array.from(pages).flatMap((page) =>
362 Object.values(page.items ?? {}).flatMap((scoped) =>
363 (scoped?.storagePools ?? [])
364 .filter((pool) =>
365 Object.keys(pool.labels ?? {}).some((key) => key.startsWith("alchemy-")),
366 )
367 .map((pool) => toAttrs(pool, env.project)),
368 ),
369 );
370 }),
371
372 reconcile: Effect.fn(function* ({ id, news, output }) {
373 const env = yield* GcpEnvironment.current;
374 const storagePoolName = yield* toName(id, news.storagePoolName, output?.storagePoolName);
375 const zone = normalizeZone(news.zone ?? output?.zone);
376 const storagePoolType = lastSegment(news.storagePoolType) || DEFAULT_TYPE;
377 const desiredLabels = {
378 ...toLabels(news.labels),
379 ...(yield* createInternalLabels(id)),
380 };
381 const capacity = asString(news.poolProvisionedCapacityGb);
382 const iops = asString(news.poolProvisionedIops);
383 const throughput = asString(news.poolProvisionedThroughput);
384
385 let current = yield* getByName(env.project, zone, storagePoolName);
386 if (current?.state === "DELETING") {
387 yield* waitPoolGone(env.project, zone, storagePoolName);
388 current = undefined;
389 }
390
391 if (current === undefined) {
392 const inserted = yield* compute
393 .insertStoragePools({
394 project: env.project,
395 zone,
396 body: {
397 name: storagePoolName,
398 description: news.description,
399 labels: desiredLabels,
400 storagePoolType: poolTypeUrl(env.project, zone, storagePoolType),
401 poolProvisionedCapacityGb: capacity,
402 poolProvisionedIops: iops,
403 poolProvisionedThroughput: throughput,
404 capacityProvisioningType: news.capacityProvisioningType,
405 performanceProvisioningType: news.performanceProvisioningType,
406 },
407 })
408 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
409 if (inserted !== undefined) {
410 yield* waitZoneOperation(env.project, zone, inserted, {
411 ignore: ["RESOURCE_ALREADY_EXISTS"],
412 });
413 }
414 current = yield* waitPoolReady(env.project, zone, storagePoolName);
415 }
416
417 if (current === undefined) {
418 return yield* new StoragePoolNotResolved({
419 storagePoolName,
420 zone,
421 });
422 }
423
424 if (current.state === "CREATING") {
425 current = yield* waitPoolReady(env.project, zone, storagePoolName);
426 }
427
428 if (current === undefined) {
429 return yield* new StoragePoolNotResolved({
430 storagePoolName,
431 zone,
432 });
433 }
434
435 const observedLabels = tagRecord(current.labels);
436 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
437 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
438 const capacityChanged =
439 capacity !== undefined && capacity !== (current.poolProvisionedCapacityGb ?? "");
440 const iopsChanged = iops !== undefined && iops !== (current.poolProvisionedIops ?? "");
441 const throughputChanged =
442 throughput !== undefined && throughput !== (current.poolProvisionedThroughput ?? "");
443 const labelsChanged = upsert.length > 0 || removed.length > 0;
444
445 if (
446 descriptionChanged ||
447 capacityChanged ||
448 iopsChanged ||
449 throughputChanged ||
450 labelsChanged
451 ) {
452 const patched = yield* compute.updateStoragePools({
453 project: env.project,
454 zone,
455 storagePool: storagePoolName,
456 updateMask: [
457 descriptionChanged ? "description" : undefined,
458 capacityChanged ? "poolProvisionedCapacityGb" : undefined,
459 iopsChanged ? "poolProvisionedIops" : undefined,
460 throughputChanged ? "poolProvisionedThroughput" : undefined,
461 labelsChanged ? "labels" : undefined,
462 ]
463 .filter((field): field is string => field !== undefined)
464 .join(","),
465 body: {
466 description: news.description,
467 poolProvisionedCapacityGb: capacity,
468 poolProvisionedIops: iops,
469 poolProvisionedThroughput: throughput,
470 labels: desiredLabels,
471 labelFingerprint: current.labelFingerprint,
472 },
473 });
474 yield* waitZoneOperation(env.project, zone, patched);
475 current =
476 (yield* getByName(env.project, zone, storagePoolName)) ??
477 (yield* waitPoolReady(env.project, zone, storagePoolName));
478 }
479
480 if (current === undefined) {
481 return yield* new StoragePoolNotResolved({
482 storagePoolName,
483 zone,
484 });
485 }
486
487 return toAttrs(current, env.project);
488 }),
489
490 delete: Effect.fn(function* ({ output }) {
491 const env = yield* GcpEnvironment.current;
492 const project = output.project || env.project;
493 const zone = normalizeZone(output.zone);
494 const deleted = yield* compute
495 .deleteStoragePools({
496 project,
497 zone,
498 storagePool: output.storagePoolName,
499 })
500 .pipe(
501 Effect.catchTag("NotFound", () => Effect.undefined),
502 Effect.retry({
503 while: (error) => error._tag === "Conflict",
504 times: 8,
505 schedule: Schedule.spaced("2 seconds"),
506 }),
507 );
508 if (deleted !== undefined) {
509 yield* waitZoneOperation(project, zone, deleted, {
510 ignore: ["RESOURCE_NOT_FOUND"],
511 });
512 }
513 yield* waitPoolGone(project, zone, output.storagePoolName);
514 }),
515 });