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
| 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 { adopt } from "alchemy/AdoptPolicy"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const waitUntilGone = (project: string, zone: string, instance: string) => |
| 16 | compute.getInstances({ project, zone, instance }).pipe( |
| 17 | Effect.as("found" as const), |
| 18 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("3 seconds"), |
| 21 | until: (status) => status === "gone", |
| 22 | times: 10, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | const CLOUD_PLATFORM = "https://www.googleapis.com/auth/cloud-platform"; |
| 27 | |
| 28 | test.provider.skipIf(!!process.env.FAST)( |
| 29 | "adopting a VM and declaring its observed identity does not replace it", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | yield* stack.destroy(); |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | const zone = "us-central1-a"; |
| 35 | const instanceName = `adopt-${stack.stage}` |
| 36 | .toLowerCase() |
| 37 | .replace(/[^a-z0-9-]/g, "-") |
| 38 | .slice(0, 60); |
| 39 | |
| 40 | // A VM created outside Alchemy, with the default service account and |
| 41 | // a Shielded VM config Alchemy never declared. |
| 42 | const existing = yield* compute |
| 43 | .getInstances({ project, zone, instance: instanceName }) |
| 44 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 45 | if (existing === undefined) { |
| 46 | yield* compute.insertInstances({ |
| 47 | project, |
| 48 | zone, |
| 49 | body: { |
| 50 | name: instanceName, |
| 51 | machineType: `zones/${zone}/machineTypes/e2-micro`, |
| 52 | disks: [ |
| 53 | { |
| 54 | boot: true, |
| 55 | autoDelete: true, |
| 56 | initializeParams: { |
| 57 | sourceImage: "projects/debian-cloud/global/images/family/debian-12", |
| 58 | }, |
| 59 | }, |
| 60 | ], |
| 61 | networkInterfaces: [{ network: "global/networks/default" }], |
| 62 | serviceAccounts: [{ email: "default", scopes: [CLOUD_PLATFORM] }], |
| 63 | shieldedInstanceConfig: { |
| 64 | enableSecureBoot: false, |
| 65 | enableVtpm: true, |
| 66 | enableIntegrityMonitoring: true, |
| 67 | }, |
| 68 | }, |
| 69 | }); |
| 70 | } |
| 71 | const foreign = yield* compute.getInstances({ project, zone, instance: instanceName }).pipe( |
| 72 | Effect.retry({ |
| 73 | while: (error) => error._tag === "NotFound", |
| 74 | schedule: Schedule.spaced("3 seconds"), |
| 75 | times: 20, |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | const program = (identity: Partial<GCP.Compute.InstanceProps>) => |
| 80 | GCP.Compute.Instance("Adopted", { |
| 81 | instanceName, |
| 82 | zone, |
| 83 | machineType: "e2-micro", |
| 84 | associatePublicIp: false, |
| 85 | ...identity, |
| 86 | }).pipe(adopt(true)); |
| 87 | |
| 88 | // Spelling out what already runs: "default" resolves to the observed |
| 89 | // account and a partial Shielded VM config matches the observed one. |
| 90 | const declared = { |
| 91 | serviceAccount: "default", |
| 92 | oauthScopes: [CLOUD_PLATFORM], |
| 93 | shieldedInstanceConfig: { enableVtpm: true }, |
| 94 | }; |
| 95 | expect((yield* stack.plan(program(declared))).resources.Adopted.action).not.toBe("replace"); |
| 96 | const adopted = yield* stack.deploy(program(declared)); |
| 97 | expect(adopted.instanceId).toEqual(foreign.id); |
| 98 | |
| 99 | // Identity settings can only change on a stopped VM, so they replace. |
| 100 | expect( |
| 101 | (yield* stack.plan( |
| 102 | program({ ...declared, shieldedInstanceConfig: { enableSecureBoot: true } }), |
| 103 | )).resources.Adopted, |
| 104 | ).toMatchObject({ action: "replace", deleteFirst: true }); |
| 105 | const runner = `runner@${project}.iam.gserviceaccount.com`; |
| 106 | expect( |
| 107 | (yield* stack.plan(program({ ...declared, serviceAccount: runner }))).resources.Adopted, |
| 108 | ).toMatchObject({ action: "replace", deleteFirst: true }); |
| 109 | |
| 110 | yield* stack.destroy(); |
| 111 | expect(yield* waitUntilGone(project, zone, instanceName)).toEqual("gone"); |
| 112 | }).pipe(logLevel), |
| 113 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 300_000 }, |
| 114 | ); |
| 115 | |
| 116 | test.provider.skipIf(!!process.env.FAST)( |
| 117 | "create, update, and delete an instance", |
| 118 | (stack) => |
| 119 | Effect.gen(function* () { |
| 120 | yield* stack.destroy(); |
| 121 | |
| 122 | const created = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | return yield* GCP.Compute.Instance("Vm", { |
| 125 | zone: "us-central1-a", |
| 126 | machineType: "e2-micro", |
| 127 | labels: { env: "test" }, |
| 128 | tags: ["alchemy-test"], |
| 129 | metadata: { role: "test" }, |
| 130 | associatePublicIp: false, |
| 131 | bootDiskType: "pd-balanced", |
| 132 | provisioningModel: "STANDARD", |
| 133 | onHostMaintenance: "MIGRATE", |
| 134 | serviceAccount: "default", |
| 135 | oauthScopes: ["https://www.googleapis.com/auth/cloud-platform"], |
| 136 | shieldedInstanceConfig: { |
| 137 | enableIntegrityMonitoring: true, |
| 138 | enableSecureBoot: true, |
| 139 | enableVtpm: true, |
| 140 | }, |
| 141 | }); |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | expect(created.instanceName).toEqual(expect.any(String)); |
| 146 | expect(created.zone).toEqual("us-central1-a"); |
| 147 | expect(created.machineType).toEqual("e2-micro"); |
| 148 | expect(created.labels).toMatchObject({ env: "test" }); |
| 149 | expect(created.tags).toEqual(["alchemy-test"]); |
| 150 | expect(created.metadata).toMatchObject({ role: "test" }); |
| 151 | expect(created.scheduling?.provisioningModel).toEqual("STANDARD"); |
| 152 | expect(created.scheduling?.onHostMaintenance).toEqual("MIGRATE"); |
| 153 | expect(created.serviceAccount).toMatch(/-compute@developer\.gserviceaccount\.com$/); |
| 154 | expect(created.oauthScopes).toContain("https://www.googleapis.com/auth/cloud-platform"); |
| 155 | expect(created.shieldedInstanceConfig).toMatchObject({ |
| 156 | enableIntegrityMonitoring: true, |
| 157 | enableSecureBoot: true, |
| 158 | enableVtpm: true, |
| 159 | }); |
| 160 | |
| 161 | const fetched = yield* compute.getInstances({ |
| 162 | project: created.project, |
| 163 | zone: created.zone, |
| 164 | instance: created.instanceName, |
| 165 | }); |
| 166 | expect(fetched.name).toEqual(created.instanceName); |
| 167 | expect(fetched.labels?.env).toEqual("test"); |
| 168 | expect(fetched.tags?.items).toEqual(["alchemy-test"]); |
| 169 | expect(fetched.scheduling?.provisioningModel).toEqual("STANDARD"); |
| 170 | expect(fetched.serviceAccounts?.[0]?.email).toEqual(created.serviceAccount); |
| 171 | expect(fetched.shieldedInstanceConfig).toMatchObject({ |
| 172 | enableIntegrityMonitoring: true, |
| 173 | enableSecureBoot: true, |
| 174 | enableVtpm: true, |
| 175 | }); |
| 176 | |
| 177 | const bootDisk = yield* compute.getDisks({ |
| 178 | project: created.project, |
| 179 | zone: created.zone, |
| 180 | disk: fetched.disks?.[0]?.source?.split("/").pop() ?? "", |
| 181 | }); |
| 182 | expect(bootDisk.type).toMatch(/\/diskTypes\/pd-balanced$/); |
| 183 | |
| 184 | const updated = yield* stack.deploy( |
| 185 | Effect.gen(function* () { |
| 186 | return yield* GCP.Compute.Instance("Vm", { |
| 187 | instanceName: created.instanceName, |
| 188 | zone: "us-central1-a", |
| 189 | machineType: "e2-micro", |
| 190 | labels: { env: "prod", role: "web" }, |
| 191 | tags: ["alchemy-prod"], |
| 192 | metadata: { role: "prod" }, |
| 193 | description: "alchemy instance update", |
| 194 | associatePublicIp: false, |
| 195 | bootDiskType: "pd-balanced", |
| 196 | provisioningModel: "STANDARD", |
| 197 | onHostMaintenance: "MIGRATE", |
| 198 | automaticRestart: false, |
| 199 | serviceAccount: "default", |
| 200 | oauthScopes: ["https://www.googleapis.com/auth/cloud-platform"], |
| 201 | shieldedInstanceConfig: { |
| 202 | enableIntegrityMonitoring: true, |
| 203 | enableSecureBoot: true, |
| 204 | enableVtpm: true, |
| 205 | }, |
| 206 | }); |
| 207 | }), |
| 208 | ); |
| 209 | |
| 210 | expect(updated.instanceName).toEqual(created.instanceName); |
| 211 | expect(updated.instanceId).toEqual(created.instanceId); |
| 212 | expect(updated.labels).toMatchObject({ env: "prod", role: "web" }); |
| 213 | expect(updated.tags).toEqual(["alchemy-prod"]); |
| 214 | expect(updated.metadata).toMatchObject({ role: "prod" }); |
| 215 | expect(updated.scheduling?.automaticRestart).toEqual(false); |
| 216 | expect(updated.scheduling?.onHostMaintenance).toEqual("MIGRATE"); |
| 217 | |
| 218 | const refetched = yield* compute.getInstances({ |
| 219 | project: created.project, |
| 220 | zone: created.zone, |
| 221 | instance: created.instanceName, |
| 222 | }); |
| 223 | expect(refetched.id).toEqual(created.instanceId); |
| 224 | expect(refetched.scheduling?.automaticRestart).toEqual(false); |
| 225 | expect(refetched.description).toEqual("alchemy instance update"); |
| 226 | |
| 227 | yield* stack.destroy(); |
| 228 | |
| 229 | const gone = yield* waitUntilGone(created.project, created.zone, created.instanceName); |
| 230 | expect(gone).toEqual("gone"); |
| 231 | }).pipe(logLevel), |
| 232 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 240_000 }, |
| 233 | ); |