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 { unownedUnlessPrevious } from "alchemy/AdoptPolicy"; |
| 3 | import { deepEqual, 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 { matchesDesired } from "../Proto.ts"; |
| 21 | import type { Providers } from "../Providers.ts"; |
| 22 | import { type WaitComputeOptions, waitZoneOperation } from "./operations.ts"; |
| 23 | |
| 24 | export type InstanceAttachedDisk = { |
| 25 | /** |
| 26 | * Disk URL or partial URL (`zones/{zone}/disks/{disk}`). |
| 27 | */ |
| 28 | source: string; |
| 29 | /** |
| 30 | * Device name exposed to the guest as `/dev/disk/by-id/google-{deviceName}`. |
| 31 | * @default the disk name |
| 32 | */ |
| 33 | deviceName?: string; |
| 34 | /** |
| 35 | * Attach mode. |
| 36 | * @default "READ_WRITE" |
| 37 | */ |
| 38 | mode?: "READ_WRITE" | "READ_ONLY"; |
| 39 | }; |
| 40 | |
| 41 | export type InstanceProps = { |
| 42 | /** |
| 43 | * Instance name. If omitted, a unique RFC1035 name is generated from the |
| 44 | * stack, stage, and logical id. 1-63 characters, lowercase letter first. |
| 45 | */ |
| 46 | instanceName?: string; |
| 47 | /** |
| 48 | * Zone to create the instance in (e.g. `us-central1-a`). Immutable — |
| 49 | * changing it replaces the instance. |
| 50 | * @default "us-central1-a" |
| 51 | */ |
| 52 | zone?: string; |
| 53 | /** |
| 54 | * Machine type short name (`e2-micro`) or partial/full URL. Immutable — |
| 55 | * changing it replaces the instance. |
| 56 | * @default "e2-micro" |
| 57 | */ |
| 58 | machineType?: string; |
| 59 | /** |
| 60 | * Optional description. |
| 61 | */ |
| 62 | description?: string; |
| 63 | /** |
| 64 | * User labels. Alchemy ownership labels are merged in automatically. |
| 65 | */ |
| 66 | labels?: Record<string, string>; |
| 67 | /** |
| 68 | * Network tags used by VPC firewall rules. |
| 69 | */ |
| 70 | tags?: string[]; |
| 71 | /** |
| 72 | * Instance metadata key/value pairs (replaces the instance metadata set). |
| 73 | */ |
| 74 | metadata?: Record<string, string>; |
| 75 | /** |
| 76 | * Source image for the boot disk. Immutable — changing it replaces the |
| 77 | * instance. |
| 78 | * @default "projects/debian-cloud/global/images/family/debian-12" |
| 79 | */ |
| 80 | sourceImage?: string; |
| 81 | /** |
| 82 | * Boot disk size in GB. |
| 83 | * @default 10 |
| 84 | */ |
| 85 | diskSizeGb?: number; |
| 86 | /** |
| 87 | * Boot disk type short name (`pd-balanced`) or URL. Immutable — |
| 88 | * changing it replaces the instance. |
| 89 | * @default "pd-standard" |
| 90 | */ |
| 91 | bootDiskType?: string; |
| 92 | /** |
| 93 | * VPC network URL or partial URL. |
| 94 | * @default "global/networks/default" |
| 95 | */ |
| 96 | network?: string; |
| 97 | /** |
| 98 | * Optional subnetwork URL or partial URL. |
| 99 | */ |
| 100 | subnetwork?: string; |
| 101 | /** |
| 102 | * Attach an ephemeral public IPv4 address. |
| 103 | * @default false |
| 104 | */ |
| 105 | associatePublicIp?: boolean; |
| 106 | /** |
| 107 | * Reserved static external IPv4 address to assign to the primary network |
| 108 | * interface (e.g. `GCP.Compute.Address(...).address`). Implies a public |
| 109 | * access config; takes precedence over `associatePublicIp`. |
| 110 | */ |
| 111 | natIP?: string; |
| 112 | /** |
| 113 | * Additional (non-boot) persistent disks to attach, e.g. |
| 114 | * `GCP.Compute.Disk(...).selfLink`. The disks must be in the instance's |
| 115 | * zone. Disks attached out-of-band that are not listed here are detached. |
| 116 | */ |
| 117 | attachedDisks?: InstanceAttachedDisk[]; |
| 118 | /** |
| 119 | * Create a preemptible VM. Immutable — changing it replaces the instance. |
| 120 | * @default false |
| 121 | */ |
| 122 | preemptible?: boolean; |
| 123 | /** |
| 124 | * VM provisioning model. Use `SPOT` for Spot VMs. Immutable — changing it |
| 125 | * replaces the instance. |
| 126 | * @default "STANDARD" |
| 127 | */ |
| 128 | provisioningModel?: compute.SchedulingProvisioningModelEnum; |
| 129 | /** |
| 130 | * Automatically restart the VM if Compute Engine terminates it. |
| 131 | * Ignored (forced off) for preemptible and Spot VMs. |
| 132 | * @default true |
| 133 | */ |
| 134 | automaticRestart?: boolean; |
| 135 | /** |
| 136 | * Host maintenance behavior. Preemptible and Spot VMs require |
| 137 | * `TERMINATE`, which is the default for them. |
| 138 | */ |
| 139 | onHostMaintenance?: compute.SchedulingOnHostMaintenanceEnum; |
| 140 | /** |
| 141 | * Action taken when a Spot VM is preempted. |
| 142 | * @default "STOP" for Spot VMs |
| 143 | */ |
| 144 | instanceTerminationAction?: compute.SchedulingInstanceTerminationActionEnum; |
| 145 | /** |
| 146 | * Service account email the VM runs as; `"default"` selects the |
| 147 | * project's Compute Engine default service account. Changing it replaces |
| 148 | * the instance (Compute Engine only allows it on a stopped VM). |
| 149 | */ |
| 150 | serviceAccount?: string; |
| 151 | /** |
| 152 | * OAuth scopes granted to the attached service account. Changing them |
| 153 | * replaces the instance (Compute Engine only allows it on a stopped VM). |
| 154 | */ |
| 155 | oauthScopes?: string[]; |
| 156 | /** |
| 157 | * Shielded VM Secure Boot, vTPM, and integrity-monitoring settings. |
| 158 | * Changing them replaces the instance (Compute Engine only allows it on a |
| 159 | * stopped VM). |
| 160 | */ |
| 161 | shieldedInstanceConfig?: compute.ShieldedInstanceConfig; |
| 162 | /** |
| 163 | * Allow packets with non-matching source/destination IPs (IP forwarding). |
| 164 | * Changing it replaces the instance. |
| 165 | * @default false |
| 166 | */ |
| 167 | canIpForward?: boolean; |
| 168 | /** |
| 169 | * Protect the instance from accidental deletion. |
| 170 | * @default false |
| 171 | */ |
| 172 | deletionProtection?: boolean; |
| 173 | }; |
| 174 | |
| 175 | export type Instance = Resource< |
| 176 | "GCP.Compute.Instance", |
| 177 | InstanceProps, |
| 178 | { |
| 179 | /** Instance name (RFC1035). */ |
| 180 | instanceName: string; |
| 181 | /** Server-assigned numeric instance id. */ |
| 182 | instanceId: string; |
| 183 | /** Project id. */ |
| 184 | project: string; |
| 185 | /** Zone short name (e.g. `us-central1-a`). */ |
| 186 | zone: string; |
| 187 | /** Machine type short name (e.g. `e2-micro`). */ |
| 188 | machineType: string; |
| 189 | /** Current instance status (`RUNNING`, `TERMINATED`, …). */ |
| 190 | status: string | undefined; |
| 191 | /** User labels (Alchemy ownership labels stripped). */ |
| 192 | labels: Record<string, string>; |
| 193 | /** Network tags. */ |
| 194 | tags: string[]; |
| 195 | /** Instance metadata key/value pairs. */ |
| 196 | metadata: Record<string, string>; |
| 197 | /** Whether deletion protection is enabled. */ |
| 198 | deletionProtection: boolean; |
| 199 | /** Whether IP forwarding is enabled. */ |
| 200 | canIpForward: boolean; |
| 201 | /** Effective scheduling policy. */ |
| 202 | scheduling: compute.Scheduling | undefined; |
| 203 | /** Attached service account email. */ |
| 204 | serviceAccount: string | undefined; |
| 205 | /** OAuth scopes granted to the attached service account. */ |
| 206 | oauthScopes: string[]; |
| 207 | /** Effective Shielded VM settings. */ |
| 208 | shieldedInstanceConfig: compute.ShieldedInstanceConfig | undefined; |
| 209 | /** Primary internal IPv4. */ |
| 210 | networkIP: string | undefined; |
| 211 | /** Ephemeral or reserved public IPv4, if any. */ |
| 212 | natIP: string | undefined; |
| 213 | /** Additional (non-boot) disks attached to the instance. */ |
| 214 | attachedDisks: { |
| 215 | source: string; |
| 216 | deviceName: string | undefined; |
| 217 | mode: string | undefined; |
| 218 | }[]; |
| 219 | /** Compute Engine self-link. */ |
| 220 | selfLink: string | undefined; |
| 221 | /** RFC3339 creation timestamp. */ |
| 222 | creationTimestamp: string | undefined; |
| 223 | /** CPU platform reported by Compute Engine. */ |
| 224 | cpuPlatform: string | undefined; |
| 225 | }, |
| 226 | never, |
| 227 | Providers |
| 228 | >; |
| 229 | |
| 230 | /** |
| 231 | * A Google Compute Engine VM instance. |
| 232 | * |
| 233 | * ### Creating an Instance |
| 234 | * **Example:** Generated name |
| 235 | * ```typescript |
| 236 | * const vm = yield* GCP.Compute.Instance("web", { |
| 237 | * zone: "us-central1-a", |
| 238 | * machineType: "e2-micro", |
| 239 | * }); |
| 240 | * ``` |
| 241 | * |
| 242 | * **Example:** Explicit name, labels, and metadata |
| 243 | * ```typescript |
| 244 | * const vm = yield* GCP.Compute.Instance("web", { |
| 245 | * instanceName: "web-1", |
| 246 | * zone: "us-central1-a", |
| 247 | * machineType: "e2-micro", |
| 248 | * sourceImage: "projects/debian-cloud/global/images/family/debian-12", |
| 249 | * labels: { env: "prod" }, |
| 250 | * tags: ["http-server"], |
| 251 | * metadata: { "enable-oslogin": "TRUE" }, |
| 252 | * }); |
| 253 | * ``` |
| 254 | * |
| 255 | * ### Spot VMs and Identity |
| 256 | * **Example:** Spot VM with a dedicated service account and Shielded VM |
| 257 | * ```typescript |
| 258 | * const runner = yield* GCP.IAM.ServiceAccount("runner", {}); |
| 259 | * const vm = yield* GCP.Compute.Instance("runner", { |
| 260 | * machineType: "n2-standard-4", |
| 261 | * bootDiskType: "pd-balanced", |
| 262 | * provisioningModel: "SPOT", |
| 263 | * instanceTerminationAction: "STOP", |
| 264 | * serviceAccount: runner.email, |
| 265 | * oauthScopes: ["https://www.googleapis.com/auth/cloud-platform"], |
| 266 | * shieldedInstanceConfig: { |
| 267 | * enableIntegrityMonitoring: true, |
| 268 | * enableSecureBoot: true, |
| 269 | * enableVtpm: true, |
| 270 | * }, |
| 271 | * }); |
| 272 | * ``` |
| 273 | * |
| 274 | * ### Static IPs and Extra Disks |
| 275 | * **Example:** Web server with a static IP and a data disk |
| 276 | * ```typescript |
| 277 | * const ip = yield* GCP.Compute.Address("web-ip", { region: "us-central1" }); |
| 278 | * const data = yield* GCP.Compute.Disk("web-data", { |
| 279 | * zone: "us-central1-a", |
| 280 | * sizeGb: 10, |
| 281 | * }); |
| 282 | * const vm = yield* GCP.Compute.Instance("web", { |
| 283 | * zone: "us-central1-a", |
| 284 | * natIP: ip.address, |
| 285 | * attachedDisks: [{ source: data.selfLink, deviceName: "data" }], |
| 286 | * tags: ["http-server"], |
| 287 | * metadata: { |
| 288 | * "startup-script": "#!/bin/bash\ncd /tmp && python3 -m http.server 80", |
| 289 | * }, |
| 290 | * }); |
| 291 | * ``` |
| 292 | * |
| 293 | * ### Starting and Stopping |
| 294 | * **Example:** Start a bound instance |
| 295 | * ```typescript |
| 296 | * const start = yield* GCP.Compute.StartInstance(vm); |
| 297 | * yield* start(); |
| 298 | * ``` |
| 299 | * |
| 300 | * @resource |
| 301 | * @category Compute |
| 302 | */ |
| 303 | export const Instance = Resource<Instance>("GCP.Compute.Instance"); |
| 304 | |
| 305 | export class InstanceNotResolved extends Data.TaggedError("GCP.Compute.InstanceNotResolved")<{ |
| 306 | instanceName: string; |
| 307 | zone: string; |
| 308 | }> {} |
| 309 | |
| 310 | export class InstanceStillExists extends Data.TaggedError("GCP.Compute.InstanceStillExists")<{ |
| 311 | instanceName: string; |
| 312 | zone: string; |
| 313 | status: string; |
| 314 | }> {} |
| 315 | |
| 316 | export class InstanceNotSettled extends Data.TaggedError("GCP.Compute.InstanceNotSettled")<{ |
| 317 | instanceName: string; |
| 318 | zone: string; |
| 319 | status: string; |
| 320 | }> {} |
| 321 | |
| 322 | const DEFAULT_ZONE = "us-central1-a"; |
| 323 | const DEFAULT_MACHINE_TYPE = "e2-micro"; |
| 324 | const DEFAULT_SOURCE_IMAGE = "projects/debian-cloud/global/images/family/debian-12"; |
| 325 | const DEFAULT_NETWORK = "global/networks/default"; |
| 326 | const DEFAULT_DISK_SIZE_GB = 10; |
| 327 | const DEFAULT_BOOT_DISK_TYPE = "pd-standard"; |
| 328 | |
| 329 | const lastSegment = (value: string | undefined): string => { |
| 330 | if (value === undefined || value.length === 0) return ""; |
| 331 | const parts = value.split("/"); |
| 332 | return parts[parts.length - 1] ?? value; |
| 333 | }; |
| 334 | |
| 335 | const rfc1035 = (name: string): string => { |
| 336 | let next = name |
| 337 | .toLowerCase() |
| 338 | .replace(/[^a-z0-9-]/g, "-") |
| 339 | .replace(/-+/g, "-") |
| 340 | .replace(/^-+|-+$/g, ""); |
| 341 | if (!/^[a-z]/.test(next)) next = `n${next}`; |
| 342 | next = next.slice(0, 63).replace(/-+$/g, ""); |
| 343 | return next.length > 0 ? next : "instance"; |
| 344 | }; |
| 345 | |
| 346 | const machineTypeUrl = (zone: string, machineType: string): string => |
| 347 | machineType.includes("/") ? machineType : `zones/${zone}/machineTypes/${machineType}`; |
| 348 | |
| 349 | const diskTypeUrl = (zone: string, diskType: string): string => |
| 350 | diskType.includes("/") ? diskType : `zones/${zone}/diskTypes/${diskType}`; |
| 351 | |
| 352 | const userLabels = ( |
| 353 | labels: Record<string, string | undefined> | null | undefined, |
| 354 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 355 | |
| 356 | const metadataRecord = (metadata: compute.Metadata | undefined): Record<string, string> => |
| 357 | Object.fromEntries( |
| 358 | (metadata?.items ?? []) |
| 359 | .filter((item) => item.key !== undefined) |
| 360 | .map((item) => [item.key!, item.value ?? ""]), |
| 361 | ); |
| 362 | |
| 363 | const sameRecord = (left: Record<string, string>, right: Record<string, string>): boolean => { |
| 364 | const keys = new Set([...Object.keys(left), ...Object.keys(right)]); |
| 365 | for (const key of keys) { |
| 366 | if (left[key] !== right[key]) return false; |
| 367 | } |
| 368 | return true; |
| 369 | }; |
| 370 | |
| 371 | const sameTags = (left: string[], right: string[]): boolean => { |
| 372 | if (left.length !== right.length) return false; |
| 373 | const a = [...left].sort(); |
| 374 | const b = [...right].sort(); |
| 375 | return a.every((value, index) => value === b[index]); |
| 376 | }; |
| 377 | |
| 378 | /** |
| 379 | * `serviceAccount: "default"` is shorthand for the project's Compute Engine |
| 380 | * default service account, which the API reports by its full email. |
| 381 | */ |
| 382 | const sameServiceAccount = (observed: string | undefined, desired: string): boolean => |
| 383 | observed === desired || |
| 384 | (desired === "default" && |
| 385 | observed !== undefined && |
| 386 | observed.endsWith("-compute@developer.gserviceaccount.com")); |
| 387 | |
| 388 | const isSpot = (news: InstanceProps): boolean => |
| 389 | (news.provisioningModel ?? "STANDARD") === "SPOT" || news.preemptible === true; |
| 390 | |
| 391 | /** |
| 392 | * The scheduling fields Compute Engine lets us change on a live VM, limited |
| 393 | * to what the user set (or what Spot forces). `preemptible` and |
| 394 | * `provisioningModel` are create-time only and replace the instance instead. |
| 395 | */ |
| 396 | const desiredScheduling = (news: InstanceProps): compute.Scheduling => { |
| 397 | const spot = isSpot(news); |
| 398 | const onHostMaintenance = news.onHostMaintenance ?? (spot ? "TERMINATE" : undefined); |
| 399 | const instanceTerminationAction = |
| 400 | news.instanceTerminationAction ?? (news.provisioningModel === "SPOT" ? "STOP" : undefined); |
| 401 | return { |
| 402 | automaticRestart: spot ? false : news.automaticRestart !== false, |
| 403 | ...(onHostMaintenance !== undefined ? { onHostMaintenance } : {}), |
| 404 | ...(instanceTerminationAction !== undefined ? { instanceTerminationAction } : {}), |
| 405 | }; |
| 406 | }; |
| 407 | |
| 408 | const toName = (id: string, name: string | undefined, existing?: string) => |
| 409 | Effect.gen(function* () { |
| 410 | return ( |
| 411 | name ?? |
| 412 | existing ?? |
| 413 | rfc1035( |
| 414 | yield* createPhysicalName({ |
| 415 | id, |
| 416 | maxLength: 63, |
| 417 | lowercase: true, |
| 418 | }), |
| 419 | ) |
| 420 | ); |
| 421 | }); |
| 422 | |
| 423 | const extraDisks = (instance: compute.Instance) => |
| 424 | (instance.disks ?? []).filter((disk) => disk.boot !== true); |
| 425 | |
| 426 | /** Disks are zonal, so the disk name identifies a disk within the zone. */ |
| 427 | const diskKey = (source: string | undefined) => lastSegment(source); |
| 428 | |
| 429 | const desiredAttachedDisk = (disk: InstanceAttachedDisk): compute.AttachedDisk => ({ |
| 430 | source: disk.source, |
| 431 | deviceName: disk.deviceName, |
| 432 | mode: disk.mode ?? "READ_WRITE", |
| 433 | type: "PERSISTENT", |
| 434 | autoDelete: false, |
| 435 | }); |
| 436 | |
| 437 | type DesiredAccess = { kind: "none" } | { kind: "ephemeral" } | { kind: "static"; natIP: string }; |
| 438 | |
| 439 | const desiredAccess = (news: InstanceProps): DesiredAccess => |
| 440 | news.natIP !== undefined |
| 441 | ? { kind: "static", natIP: news.natIP } |
| 442 | : news.associatePublicIp === true |
| 443 | ? { kind: "ephemeral" } |
| 444 | : { kind: "none" }; |
| 445 | |
| 446 | const accessConfigsFor = (access: DesiredAccess): compute.AccessConfig[] => |
| 447 | access.kind === "none" |
| 448 | ? [] |
| 449 | : [ |
| 450 | { |
| 451 | type: "ONE_TO_ONE_NAT", |
| 452 | name: "External NAT", |
| 453 | ...(access.kind === "static" ? { natIP: access.natIP } : {}), |
| 454 | }, |
| 455 | ]; |
| 456 | |
| 457 | const toAttrs = (instance: compute.Instance, project: string) => { |
| 458 | const nic = instance.networkInterfaces?.[0]; |
| 459 | return { |
| 460 | instanceName: instance.name ?? "", |
| 461 | instanceId: instance.id ?? "", |
| 462 | project, |
| 463 | zone: lastSegment(instance.zone), |
| 464 | machineType: lastSegment(instance.machineType), |
| 465 | status: instance.status, |
| 466 | labels: userLabels(instance.labels), |
| 467 | tags: [...(instance.tags?.items ?? [])], |
| 468 | metadata: metadataRecord(instance.metadata), |
| 469 | deletionProtection: instance.deletionProtection === true, |
| 470 | canIpForward: instance.canIpForward === true, |
| 471 | scheduling: instance.scheduling, |
| 472 | serviceAccount: instance.serviceAccounts?.[0]?.email, |
| 473 | oauthScopes: [...(instance.serviceAccounts?.[0]?.scopes ?? [])], |
| 474 | shieldedInstanceConfig: instance.shieldedInstanceConfig, |
| 475 | networkIP: nic?.networkIP, |
| 476 | natIP: nic?.accessConfigs?.[0]?.natIP, |
| 477 | attachedDisks: extraDisks(instance).map((disk) => ({ |
| 478 | source: disk.source ?? "", |
| 479 | deviceName: disk.deviceName, |
| 480 | mode: disk.mode, |
| 481 | })), |
| 482 | selfLink: instance.selfLink, |
| 483 | creationTimestamp: instance.creationTimestamp, |
| 484 | cpuPlatform: instance.cpuPlatform, |
| 485 | }; |
| 486 | }; |
| 487 | |
| 488 | const getByName = (project: string, zone: string, instance: string) => |
| 489 | compute |
| 490 | .getInstances({ project, zone, instance }) |
| 491 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 492 | |
| 493 | const waitUntilGone = (project: string, zone: string, instanceName: string) => |
| 494 | getByName(project, zone, instanceName).pipe( |
| 495 | Effect.flatMap((instance) => |
| 496 | instance === undefined |
| 497 | ? Effect.void |
| 498 | : Effect.fail( |
| 499 | new InstanceStillExists({ |
| 500 | instanceName, |
| 501 | zone, |
| 502 | status: instance.status ?? "UNKNOWN", |
| 503 | }), |
| 504 | ), |
| 505 | ), |
| 506 | Effect.retry({ |
| 507 | while: (error) => error._tag === "GCP.Compute.InstanceStillExists", |
| 508 | times: 18, |
| 509 | schedule: Schedule.spaced("3 seconds"), |
| 510 | }), |
| 511 | ); |
| 512 | |
| 513 | /** Transitional statuses Compute passes through before an instance settles. */ |
| 514 | const TRANSITIONAL_STATUSES = new Set([ |
| 515 | "PENDING", |
| 516 | "PROVISIONING", |
| 517 | "STAGING", |
| 518 | "REPAIRING", |
| 519 | "STOPPING", |
| 520 | "PENDING_STOP", |
| 521 | "SUSPENDING", |
| 522 | ]); |
| 523 | |
| 524 | /** Poll until the instance reports a settled status (e.g. `RUNNING`). */ |
| 525 | const waitUntilSettled = (project: string, zone: string, instanceName: string) => |
| 526 | getByName(project, zone, instanceName).pipe( |
| 527 | Effect.flatMap( |
| 528 | (instance): Effect.Effect<compute.Instance, InstanceNotResolved | InstanceNotSettled> => |
| 529 | instance === undefined |
| 530 | ? Effect.fail(new InstanceNotResolved({ instanceName, zone })) |
| 531 | : TRANSITIONAL_STATUSES.has(instance.status ?? "") |
| 532 | ? Effect.fail( |
| 533 | new InstanceNotSettled({ |
| 534 | instanceName, |
| 535 | zone, |
| 536 | status: instance.status ?? "UNKNOWN", |
| 537 | }), |
| 538 | ) |
| 539 | : Effect.succeed(instance), |
| 540 | ), |
| 541 | Effect.retry({ |
| 542 | while: (error) => |
| 543 | error._tag === "GCP.Compute.InstanceNotSettled" || |
| 544 | error._tag === "GCP.Compute.InstanceNotResolved", |
| 545 | times: 60, |
| 546 | schedule: Schedule.spaced("5 seconds"), |
| 547 | }), |
| 548 | ); |
| 549 | |
| 550 | const applyZoneOp = <E, R>( |
| 551 | project: string, |
| 552 | zone: string, |
| 553 | start: Effect.Effect<compute.Operation, E, R>, |
| 554 | options?: WaitComputeOptions, |
| 555 | ) => |
| 556 | Effect.gen(function* () { |
| 557 | const op = yield* start; |
| 558 | return yield* waitZoneOperation(project, zone, op, options); |
| 559 | }); |
| 560 | |
| 561 | const insertBody = ( |
| 562 | news: InstanceProps, |
| 563 | instanceName: string, |
| 564 | zone: string, |
| 565 | desiredLabels: Record<string, string>, |
| 566 | ): compute.Instance => { |
| 567 | const provisioningModel = news.provisioningModel ?? "STANDARD"; |
| 568 | const metadata = news.metadata |
| 569 | ? { |
| 570 | items: Object.entries(news.metadata).map(([key, value]) => ({ |
| 571 | key, |
| 572 | value, |
| 573 | })), |
| 574 | } |
| 575 | : undefined; |
| 576 | return { |
| 577 | name: instanceName, |
| 578 | machineType: machineTypeUrl(zone, news.machineType ?? DEFAULT_MACHINE_TYPE), |
| 579 | description: news.description, |
| 580 | labels: desiredLabels, |
| 581 | tags: news.tags ? { items: news.tags } : undefined, |
| 582 | metadata, |
| 583 | canIpForward: news.canIpForward === true, |
| 584 | deletionProtection: news.deletionProtection === true, |
| 585 | scheduling: { |
| 586 | ...desiredScheduling(news), |
| 587 | provisioningModel, |
| 588 | // Spot VMs are reported (and must be created) as preemptible. |
| 589 | preemptible: isSpot(news), |
| 590 | }, |
| 591 | serviceAccounts: |
| 592 | news.serviceAccount !== undefined || news.oauthScopes !== undefined |
| 593 | ? [ |
| 594 | { |
| 595 | // Scopes are only meaningful against an identity; Compute |
| 596 | // Engine rejects a serviceAccounts entry without an email. |
| 597 | email: news.serviceAccount ?? "default", |
| 598 | scopes: news.oauthScopes, |
| 599 | }, |
| 600 | ] |
| 601 | : undefined, |
| 602 | shieldedInstanceConfig: news.shieldedInstanceConfig, |
| 603 | disks: [ |
| 604 | { |
| 605 | boot: true, |
| 606 | autoDelete: true, |
| 607 | type: "PERSISTENT", |
| 608 | initializeParams: { |
| 609 | sourceImage: news.sourceImage ?? DEFAULT_SOURCE_IMAGE, |
| 610 | diskSizeGb: String(news.diskSizeGb ?? DEFAULT_DISK_SIZE_GB), |
| 611 | diskType: diskTypeUrl(zone, news.bootDiskType ?? DEFAULT_BOOT_DISK_TYPE), |
| 612 | }, |
| 613 | }, |
| 614 | ...(news.attachedDisks ?? []).map(desiredAttachedDisk), |
| 615 | ], |
| 616 | networkInterfaces: [ |
| 617 | { |
| 618 | network: news.network ?? DEFAULT_NETWORK, |
| 619 | subnetwork: news.subnetwork, |
| 620 | accessConfigs: accessConfigsFor(desiredAccess(news)), |
| 621 | }, |
| 622 | ], |
| 623 | }; |
| 624 | }; |
| 625 | |
| 626 | export const InstanceProvider = () => |
| 627 | Provider.succeed(Instance, { |
| 628 | stables: ["instanceName", "instanceId", "project", "zone", "selfLink", "creationTimestamp"], |
| 629 | |
| 630 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 631 | if (!isResolved(news)) return undefined; |
| 632 | const previousZone = olds?.zone ?? output?.zone ?? DEFAULT_ZONE; |
| 633 | const nextZone = news.zone ?? DEFAULT_ZONE; |
| 634 | if (lastSegment(previousZone) !== lastSegment(nextZone)) { |
| 635 | return { action: "replace" as const }; |
| 636 | } |
| 637 | const previousName = olds?.instanceName ?? output?.instanceName; |
| 638 | const nextName = news.instanceName ?? previousName; |
| 639 | if (previousName !== undefined && nextName !== undefined && previousName !== nextName) { |
| 640 | return { action: "replace" as const, deleteFirst: true }; |
| 641 | } |
| 642 | const previousType = lastSegment(olds?.machineType ?? DEFAULT_MACHINE_TYPE); |
| 643 | const nextType = lastSegment(news.machineType ?? DEFAULT_MACHINE_TYPE); |
| 644 | if (olds !== undefined && previousType !== nextType) { |
| 645 | return { action: "replace" as const, deleteFirst: true }; |
| 646 | } |
| 647 | // setServiceAccount and updateShieldedInstanceConfig are only permitted |
| 648 | // on a TERMINATED VM, and we never stop a user's VM, so these replace. |
| 649 | // When the previous props never declared them, diff against the |
| 650 | // observed VM instead so spelling out what already runs is a no-op. |
| 651 | const serviceAccountChanged = |
| 652 | olds?.serviceAccount !== undefined |
| 653 | ? olds.serviceAccount !== (news.serviceAccount ?? "") |
| 654 | : news.serviceAccount !== undefined && |
| 655 | !sameServiceAccount(output?.serviceAccount, news.serviceAccount); |
| 656 | const oauthScopesChanged = |
| 657 | olds?.oauthScopes !== undefined |
| 658 | ? !sameTags(olds.oauthScopes, news.oauthScopes ?? []) |
| 659 | : news.oauthScopes !== undefined && |
| 660 | !sameTags(output?.oauthScopes ?? [], news.oauthScopes); |
| 661 | const shieldedChanged = |
| 662 | olds?.shieldedInstanceConfig !== undefined |
| 663 | ? !deepEqual(olds.shieldedInstanceConfig, news.shieldedInstanceConfig ?? {}, { |
| 664 | stripNullish: true, |
| 665 | }) |
| 666 | : news.shieldedInstanceConfig !== undefined && |
| 667 | !matchesDesired(output?.shieldedInstanceConfig, news.shieldedInstanceConfig); |
| 668 | // Boot disk, NIC network, and IP forwarding are fixed at create (or |
| 669 | // need a stopped VM), so changing them replaces the instance. |
| 670 | const creationSettingChanged = |
| 671 | olds !== undefined && |
| 672 | ((olds.sourceImage ?? DEFAULT_SOURCE_IMAGE) !== |
| 673 | (news.sourceImage ?? DEFAULT_SOURCE_IMAGE) || |
| 674 | (olds.diskSizeGb ?? DEFAULT_DISK_SIZE_GB) !== (news.diskSizeGb ?? DEFAULT_DISK_SIZE_GB) || |
| 675 | lastSegment(olds.bootDiskType ?? DEFAULT_BOOT_DISK_TYPE) !== |
| 676 | lastSegment(news.bootDiskType ?? DEFAULT_BOOT_DISK_TYPE) || |
| 677 | lastSegment(olds.network ?? DEFAULT_NETWORK) !== |
| 678 | lastSegment(news.network ?? DEFAULT_NETWORK) || |
| 679 | lastSegment(olds.subnetwork) !== lastSegment(news.subnetwork) || |
| 680 | (olds.canIpForward === true) !== (news.canIpForward === true) || |
| 681 | (olds.preemptible === true) !== (news.preemptible === true) || |
| 682 | (olds.provisioningModel ?? "STANDARD") !== (news.provisioningModel ?? "STANDARD")); |
| 683 | if ( |
| 684 | creationSettingChanged || |
| 685 | serviceAccountChanged || |
| 686 | oauthScopesChanged || |
| 687 | shieldedChanged |
| 688 | ) { |
| 689 | return { action: "replace" as const, deleteFirst: true }; |
| 690 | } |
| 691 | return undefined; |
| 692 | }), |
| 693 | |
| 694 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 695 | const env = yield* GcpEnvironment.current; |
| 696 | const instanceName = yield* toName(id, olds?.instanceName, output?.instanceName); |
| 697 | const zone = lastSegment(olds?.zone ?? output?.zone ?? DEFAULT_ZONE); |
| 698 | const existing = yield* getByName(env.project, zone, instanceName); |
| 699 | if (existing === undefined) return undefined; |
| 700 | const attrs = toAttrs(existing, env.project); |
| 701 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) |
| 702 | ? attrs |
| 703 | : unownedUnlessPrevious(attrs, previous, "instanceName"); |
| 704 | }), |
| 705 | |
| 706 | list: () => |
| 707 | Effect.gen(function* () { |
| 708 | const env = yield* GcpEnvironment.current; |
| 709 | const pages = yield* compute.aggregatedListInstances |
| 710 | .pages({ |
| 711 | project: env.project, |
| 712 | filter: "labels.alchemy-id:*", |
| 713 | returnPartialSuccess: true, |
| 714 | maxResults: 500, |
| 715 | }) |
| 716 | .pipe(Stream.runCollect); |
| 717 | return Array.from(pages).flatMap((page) => |
| 718 | Object.values(page.items ?? {}).flatMap((scoped) => |
| 719 | (scoped?.instances ?? []).map((instance) => toAttrs(instance, env.project)), |
| 720 | ), |
| 721 | ); |
| 722 | }), |
| 723 | |
| 724 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 725 | const env = yield* GcpEnvironment.current; |
| 726 | const zone = lastSegment(news.zone ?? output?.zone ?? DEFAULT_ZONE); |
| 727 | const instanceName = yield* toName(id, news.instanceName, output?.instanceName); |
| 728 | const desiredLabels = { |
| 729 | ...toLabels(news.labels), |
| 730 | ...(yield* createInternalLabels(id)), |
| 731 | }; |
| 732 | |
| 733 | let current = yield* getByName(env.project, zone, instanceName); |
| 734 | |
| 735 | if (current === undefined) { |
| 736 | yield* applyZoneOp( |
| 737 | env.project, |
| 738 | zone, |
| 739 | compute.insertInstances({ |
| 740 | project: env.project, |
| 741 | zone, |
| 742 | body: insertBody(news, instanceName, zone, desiredLabels), |
| 743 | }), |
| 744 | { ignore: ["RESOURCE_ALREADY_EXISTS"] }, |
| 745 | ).pipe(Effect.catchTag("Conflict", () => Effect.void)); |
| 746 | current = yield* getByName(env.project, zone, instanceName).pipe( |
| 747 | Effect.flatMap((existing) => |
| 748 | existing !== undefined |
| 749 | ? Effect.succeed(existing) |
| 750 | : Effect.fail(new InstanceNotResolved({ instanceName, zone })), |
| 751 | ), |
| 752 | Effect.retry({ |
| 753 | while: (error) => error._tag === "GCP.Compute.InstanceNotResolved", |
| 754 | schedule: Schedule.spaced("2 seconds"), |
| 755 | times: 8, |
| 756 | }), |
| 757 | ); |
| 758 | } |
| 759 | |
| 760 | if (current === undefined) { |
| 761 | return yield* new InstanceNotResolved({ instanceName, zone }); |
| 762 | } |
| 763 | |
| 764 | const observedLabels = tagRecord(current.labels); |
| 765 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 766 | if (upsert.length > 0 || removed.length > 0) { |
| 767 | yield* applyZoneOp( |
| 768 | env.project, |
| 769 | zone, |
| 770 | compute.setLabelsInstances({ |
| 771 | project: env.project, |
| 772 | zone, |
| 773 | instance: instanceName, |
| 774 | body: { |
| 775 | labels: desiredLabels, |
| 776 | labelFingerprint: current.labelFingerprint, |
| 777 | }, |
| 778 | }), |
| 779 | ); |
| 780 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 781 | } |
| 782 | |
| 783 | if (news.tags !== undefined) { |
| 784 | const observedTags = [...(current.tags?.items ?? [])]; |
| 785 | if (!sameTags(observedTags, news.tags)) { |
| 786 | yield* applyZoneOp( |
| 787 | env.project, |
| 788 | zone, |
| 789 | compute.setTagsInstances({ |
| 790 | project: env.project, |
| 791 | zone, |
| 792 | instance: instanceName, |
| 793 | body: { |
| 794 | items: news.tags, |
| 795 | fingerprint: current.tags?.fingerprint, |
| 796 | }, |
| 797 | }), |
| 798 | ); |
| 799 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | if (news.metadata !== undefined) { |
| 804 | const observedMetadata = metadataRecord(current.metadata); |
| 805 | if (!sameRecord(observedMetadata, news.metadata)) { |
| 806 | yield* applyZoneOp( |
| 807 | env.project, |
| 808 | zone, |
| 809 | compute.setMetadataInstances({ |
| 810 | project: env.project, |
| 811 | zone, |
| 812 | instance: instanceName, |
| 813 | body: { |
| 814 | fingerprint: current.metadata?.fingerprint, |
| 815 | items: Object.entries(news.metadata).map(([key, value]) => ({ |
| 816 | key, |
| 817 | value, |
| 818 | })), |
| 819 | }, |
| 820 | }), |
| 821 | ); |
| 822 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | const nic = current.networkInterfaces?.[0]; |
| 827 | const nicName = nic?.name ?? "nic0"; |
| 828 | const observedAccess = nic?.accessConfigs?.[0]; |
| 829 | const access = desiredAccess(news); |
| 830 | const accessMatches = |
| 831 | access.kind === "none" |
| 832 | ? observedAccess === undefined |
| 833 | : access.kind === "ephemeral" |
| 834 | ? observedAccess !== undefined |
| 835 | : observedAccess?.natIP === access.natIP; |
| 836 | if (!accessMatches) { |
| 837 | if (observedAccess !== undefined) { |
| 838 | yield* applyZoneOp( |
| 839 | env.project, |
| 840 | zone, |
| 841 | compute.deleteAccessConfigInstances({ |
| 842 | project: env.project, |
| 843 | zone, |
| 844 | instance: instanceName, |
| 845 | networkInterface: nicName, |
| 846 | accessConfig: observedAccess.name ?? "External NAT", |
| 847 | }), |
| 848 | ); |
| 849 | } |
| 850 | for (const config of accessConfigsFor(access)) { |
| 851 | yield* applyZoneOp( |
| 852 | env.project, |
| 853 | zone, |
| 854 | compute.addAccessConfigInstances({ |
| 855 | project: env.project, |
| 856 | zone, |
| 857 | instance: instanceName, |
| 858 | networkInterface: nicName, |
| 859 | body: config, |
| 860 | }), |
| 861 | ); |
| 862 | } |
| 863 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 864 | } |
| 865 | |
| 866 | const desiredDisks = news.attachedDisks ?? []; |
| 867 | const observedDisks = extraDisks(current); |
| 868 | const desiredKeys = new Set(desiredDisks.map((disk) => diskKey(disk.source))); |
| 869 | const observedKeys = new Set(observedDisks.map((disk) => diskKey(disk.source))); |
| 870 | for (const disk of observedDisks) { |
| 871 | if (desiredKeys.has(diskKey(disk.source))) continue; |
| 872 | yield* applyZoneOp( |
| 873 | env.project, |
| 874 | zone, |
| 875 | compute.detachDiskInstances({ |
| 876 | project: env.project, |
| 877 | zone, |
| 878 | instance: instanceName, |
| 879 | deviceName: disk.deviceName ?? diskKey(disk.source), |
| 880 | }), |
| 881 | ); |
| 882 | } |
| 883 | for (const disk of desiredDisks) { |
| 884 | if (observedKeys.has(diskKey(disk.source))) continue; |
| 885 | yield* applyZoneOp( |
| 886 | env.project, |
| 887 | zone, |
| 888 | compute.attachDiskInstances({ |
| 889 | project: env.project, |
| 890 | zone, |
| 891 | instance: instanceName, |
| 892 | body: desiredAttachedDisk(disk), |
| 893 | }), |
| 894 | ); |
| 895 | } |
| 896 | if ( |
| 897 | observedDisks.some((disk) => !desiredKeys.has(diskKey(disk.source))) || |
| 898 | desiredDisks.some((disk) => !observedKeys.has(diskKey(disk.source))) |
| 899 | ) { |
| 900 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 901 | } |
| 902 | |
| 903 | const desiredProtection = news.deletionProtection === true; |
| 904 | if ((current.deletionProtection === true) !== desiredProtection) { |
| 905 | yield* applyZoneOp( |
| 906 | env.project, |
| 907 | zone, |
| 908 | compute.setDeletionProtectionInstances({ |
| 909 | project: env.project, |
| 910 | zone, |
| 911 | resource: instanceName, |
| 912 | deletionProtection: desiredProtection, |
| 913 | }), |
| 914 | ); |
| 915 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 916 | } |
| 917 | |
| 918 | const scheduling = desiredScheduling(news); |
| 919 | if (!matchesDesired(current.scheduling, scheduling)) { |
| 920 | yield* applyZoneOp( |
| 921 | env.project, |
| 922 | zone, |
| 923 | compute.setSchedulingInstances({ |
| 924 | project: env.project, |
| 925 | zone, |
| 926 | instance: instanceName, |
| 927 | // Merge onto observed scheduling so the create-time fields |
| 928 | // (provisioningModel, preemptible) are preserved verbatim. |
| 929 | body: { ...current.scheduling, ...scheduling }, |
| 930 | }), |
| 931 | ); |
| 932 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 933 | } |
| 934 | |
| 935 | // `description` is the one full-body field we can update without ever |
| 936 | // disturbing a running VM: allow REFRESH but not RESTART (NO_EFFECT is |
| 937 | // a dry run and never applies). `canIpForward` needs a RESTART, so it |
| 938 | // replaces the instance in `diff` instead. |
| 939 | if ((current.description ?? "") !== (news.description ?? "")) { |
| 940 | yield* applyZoneOp( |
| 941 | env.project, |
| 942 | zone, |
| 943 | compute.updateInstances({ |
| 944 | project: env.project, |
| 945 | zone, |
| 946 | instance: instanceName, |
| 947 | mostDisruptiveAllowedAction: "REFRESH", |
| 948 | body: { ...current, description: news.description ?? "" }, |
| 949 | }), |
| 950 | ); |
| 951 | current = (yield* getByName(env.project, zone, instanceName)) ?? current; |
| 952 | } |
| 953 | |
| 954 | current = yield* waitUntilSettled(env.project, zone, instanceName); |
| 955 | return toAttrs(current, env.project); |
| 956 | }), |
| 957 | |
| 958 | delete: Effect.fn(function* ({ output, force }) { |
| 959 | const env = yield* GcpEnvironment.current; |
| 960 | const zone = lastSegment(output.zone); |
| 961 | const instance = output.instanceName; |
| 962 | if (output.deletionProtection === true || force === true) { |
| 963 | yield* applyZoneOp( |
| 964 | env.project, |
| 965 | zone, |
| 966 | compute.setDeletionProtectionInstances({ |
| 967 | project: env.project, |
| 968 | zone, |
| 969 | resource: instance, |
| 970 | deletionProtection: false, |
| 971 | }), |
| 972 | ).pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 973 | } |
| 974 | yield* applyZoneOp( |
| 975 | env.project, |
| 976 | zone, |
| 977 | compute.deleteInstances({ |
| 978 | project: env.project, |
| 979 | zone, |
| 980 | instance, |
| 981 | }), |
| 982 | { ignore: ["RESOURCE_NOT_FOUND"] }, |
| 983 | ).pipe( |
| 984 | Effect.retry({ |
| 985 | while: (error) => |
| 986 | error._tag === "Conflict" || |
| 987 | (error._tag === "GCP.OperationFailed" && error.reason === "RESOURCE_NOT_READY"), |
| 988 | times: 8, |
| 989 | schedule: Schedule.spaced("3 seconds"), |
| 990 | }), |
| 991 | Effect.catchTag("NotFound", () => Effect.void), |
| 992 | ); |
| 993 | yield* waitUntilGone(env.project, zone, instance); |
| 994 | }), |
| 995 | }); |