The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const zone = "us-central1-a"; |
| 15 | const vmName = "alchemy-mi-src"; |
| 16 | |
| 17 | const lastSegment = (value: string | undefined): string => { |
| 18 | if (value === undefined || value.length === 0) return ""; |
| 19 | const parts = value.split("/").filter((part) => part.length > 0); |
| 20 | return parts[parts.length - 1] ?? ""; |
| 21 | }; |
| 22 | |
| 23 | const waitZoneOp = (operation: compute.Operation) => { |
| 24 | if (operation.status === "DONE") return Effect.succeed(operation); |
| 25 | const name = lastSegment(operation.name); |
| 26 | return GcpEnvironment.current.pipe( |
| 27 | Effect.flatMap(({ project }) => |
| 28 | compute.getZoneOperations({ project, zone, operation: name }).pipe( |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("2 seconds"), |
| 31 | until: (op) => op.status === "DONE", |
| 32 | times: 20, |
| 33 | }), |
| 34 | ), |
| 35 | ), |
| 36 | ); |
| 37 | }; |
| 38 | |
| 39 | const waitUntilGone = (machineImage: string) => |
| 40 | GcpEnvironment.current.pipe( |
| 41 | Effect.flatMap(({ project }) => |
| 42 | compute.getMachineImages({ project, machineImage }).pipe( |
| 43 | Effect.as("found" as const), |
| 44 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 45 | Effect.repeat({ |
| 46 | schedule: Schedule.spaced("2 seconds"), |
| 47 | until: (status) => status === "gone", |
| 48 | times: 10, |
| 49 | }), |
| 50 | ), |
| 51 | ), |
| 52 | ); |
| 53 | |
| 54 | const waitVmGone = () => |
| 55 | GcpEnvironment.current.pipe( |
| 56 | Effect.flatMap(({ project }) => |
| 57 | compute.getInstances({ project, zone, instance: vmName }).pipe( |
| 58 | Effect.as("found" as const), |
| 59 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 60 | Effect.repeat({ |
| 61 | schedule: Schedule.spaced("3 seconds"), |
| 62 | until: (status) => status === "gone", |
| 63 | times: 16, |
| 64 | }), |
| 65 | ), |
| 66 | ), |
| 67 | ); |
| 68 | |
| 69 | const getVm = () => |
| 70 | GcpEnvironment.current.pipe( |
| 71 | Effect.flatMap(({ project }) => |
| 72 | compute |
| 73 | .getInstances({ project, zone, instance: vmName }) |
| 74 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))), |
| 75 | ), |
| 76 | ); |
| 77 | |
| 78 | const waitVmRunning = () => |
| 79 | getVm().pipe( |
| 80 | Effect.repeat({ |
| 81 | schedule: Schedule.spaced("3 seconds"), |
| 82 | until: (instance) => instance?.status === "RUNNING", |
| 83 | times: 10, |
| 84 | }), |
| 85 | Effect.flatMap((instance) => |
| 86 | instance?.status === "RUNNING" |
| 87 | ? Effect.succeed(instance) |
| 88 | : Effect.fail(new Error(`source vm ${vmName} is ${instance?.status ?? "MISSING"}`)), |
| 89 | ), |
| 90 | ); |
| 91 | |
| 92 | const ensureVm = () => |
| 93 | Effect.gen(function* () { |
| 94 | const { project } = yield* GcpEnvironment.current; |
| 95 | const existing = yield* getVm(); |
| 96 | if ( |
| 97 | existing?.status === "STOPPING" || |
| 98 | existing?.status === "DELETING" || |
| 99 | existing?.status === "TERMINATED" |
| 100 | ) { |
| 101 | const operation = yield* compute |
| 102 | .deleteInstances({ project, zone, instance: vmName }) |
| 103 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 104 | if (operation !== undefined) { |
| 105 | yield* waitZoneOp(operation).pipe(Effect.ignore); |
| 106 | } |
| 107 | yield* waitVmGone(); |
| 108 | } else if (existing?.status === "RUNNING") { |
| 109 | return existing; |
| 110 | } |
| 111 | if ((yield* getVm()) === undefined) { |
| 112 | const operation = yield* compute |
| 113 | .insertInstances({ |
| 114 | project, |
| 115 | zone, |
| 116 | body: { |
| 117 | name: vmName, |
| 118 | machineType: `zones/${zone}/machineTypes/e2-micro`, |
| 119 | disks: [ |
| 120 | { |
| 121 | boot: true, |
| 122 | autoDelete: true, |
| 123 | type: "PERSISTENT", |
| 124 | initializeParams: { |
| 125 | sourceImage: "projects/debian-cloud/global/images/family/debian-12", |
| 126 | diskSizeGb: "10", |
| 127 | }, |
| 128 | }, |
| 129 | ], |
| 130 | networkInterfaces: [{ network: "global/networks/default" }], |
| 131 | }, |
| 132 | }) |
| 133 | .pipe(Effect.catchTag("Conflict", () => Effect.succeed(undefined))); |
| 134 | if (operation !== undefined) { |
| 135 | yield* waitZoneOp(operation).pipe(Effect.ignore); |
| 136 | } |
| 137 | } |
| 138 | return yield* waitVmRunning(); |
| 139 | }); |
| 140 | |
| 141 | const deleteVm = () => |
| 142 | Effect.gen(function* () { |
| 143 | const { project } = yield* GcpEnvironment.current; |
| 144 | const operation = yield* compute |
| 145 | .deleteInstances({ project, zone, instance: vmName }) |
| 146 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 147 | if (operation !== undefined) { |
| 148 | yield* waitZoneOp(operation).pipe(Effect.ignore); |
| 149 | } |
| 150 | yield* waitVmGone(); |
| 151 | }); |
| 152 | |
| 153 | test.provider.skipIf(!!process.env.FAST)( |
| 154 | "create, update, and delete a machine image", |
| 155 | (stack) => |
| 156 | Effect.gen(function* () { |
| 157 | const { project } = yield* GcpEnvironment.current; |
| 158 | yield* stack.destroy(); |
| 159 | |
| 160 | const vm = yield* ensureVm(); |
| 161 | const sourceInstance = vm.selfLink ?? `projects/${project}/zones/${zone}/instances/${vmName}`; |
| 162 | |
| 163 | const created = yield* stack.deploy( |
| 164 | Effect.gen(function* () { |
| 165 | return yield* GCP.Compute.MachineImage("Backup", { |
| 166 | sourceInstance, |
| 167 | description: "alchemy test machine image", |
| 168 | labels: { env: "test" }, |
| 169 | storageLocations: ["us-central1"], |
| 170 | }); |
| 171 | }), |
| 172 | ); |
| 173 | |
| 174 | expect(created.machineImageName).toEqual(expect.any(String)); |
| 175 | expect(created.status).toEqual("READY"); |
| 176 | expect(created.description).toEqual("alchemy test machine image"); |
| 177 | expect(created.labels).toMatchObject({ env: "test" }); |
| 178 | expect(created.project).toEqual(expect.any(String)); |
| 179 | expect(created.selfLink).toEqual(expect.any(String)); |
| 180 | expect(created.sourceInstance).toEqual(expect.any(String)); |
| 181 | |
| 182 | const fetched = yield* compute.getMachineImages({ |
| 183 | project: created.project, |
| 184 | machineImage: created.machineImageName, |
| 185 | }); |
| 186 | expect(fetched.name).toEqual(created.machineImageName); |
| 187 | expect(fetched.status).toEqual("READY"); |
| 188 | expect(fetched.description).toEqual("alchemy test machine image"); |
| 189 | expect(fetched.labels?.env).toEqual("test"); |
| 190 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 191 | expect(lastSegment(fetched.sourceInstance)).toEqual(vmName); |
| 192 | |
| 193 | const updated = yield* stack.deploy( |
| 194 | Effect.gen(function* () { |
| 195 | return yield* GCP.Compute.MachineImage("Backup", { |
| 196 | machineImageName: created.machineImageName, |
| 197 | sourceInstance, |
| 198 | description: "alchemy test machine image", |
| 199 | labels: { env: "prod", role: "golden" }, |
| 200 | storageLocations: ["us-central1"], |
| 201 | }); |
| 202 | }), |
| 203 | ); |
| 204 | |
| 205 | expect(updated.machineImageName).toEqual(created.machineImageName); |
| 206 | expect(updated.machineImageId).toEqual(created.machineImageId); |
| 207 | expect(updated.labels).toMatchObject({ |
| 208 | env: "prod", |
| 209 | role: "golden", |
| 210 | }); |
| 211 | |
| 212 | const refetched = yield* compute.getMachineImages({ |
| 213 | project: updated.project, |
| 214 | machineImage: updated.machineImageName, |
| 215 | }); |
| 216 | expect(refetched.labels?.env).toEqual("prod"); |
| 217 | expect(refetched.labels?.role).toEqual("golden"); |
| 218 | expect(refetched.name).toEqual(created.machineImageName); |
| 219 | |
| 220 | yield* stack.destroy(); |
| 221 | |
| 222 | const gone = yield* waitUntilGone(created.machineImageName); |
| 223 | expect(gone).toEqual("gone"); |
| 224 | |
| 225 | yield* deleteVm(); |
| 226 | }).pipe(logLevel), |
| 227 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 240_000 }, |
| 228 | ); |