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