@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 242 lines 9.15 KB
Raw
1 import {
2 DockerEnvironment,
3 type Docker,
4 type RegistryCredentials,
5 } from "@alchemy.run/docker/Docker";
6 import * as builders from "@distilled.cloud/fly-io/builders";
7 import * as Clock from "effect/Clock";
8 import * as Data from "effect/Data";
9 import * as Duration from "effect/Duration";
10 import * as Effect from "effect/Effect";
11 import { Base64 } from "effect/encoding";
12 import * as FileSystem from "effect/FileSystem";
13 import * as HttpClient from "effect/http/HttpClient";
14 import * as HttpClientRequest from "effect/http/HttpClientRequest";
15 import * as HttpClientResponse from "effect/http/HttpClientResponse";
16 import * as Path from "effect/Path";
17 import * as Redacted from "effect/Redacted";
18 import * as Schedule from "effect/Schedule";
19 import * as Schema from "effect/Schema";
20
21 /**
22 * Build images on Fly's remote builders, the way `fly deploy` does: Fly's
23 * GraphQL API starts a Depot build for the App, Depot hands back a BuildKit
24 * machine next to Fly's registry, and `docker buildx` drives it through the
25 * `remote` driver. Only the build context leaves this machine; the base
26 * image is pulled and the result pushed from the builder.
27 */
28
29 const DEPOT_API = "https://api.depot.dev/depot.cli.v1.BuildService";
30 /**
31 * Depot builders push `registry.fly.io` images through Fly's internal registry
32 * mirror, which takes the same login (`authConfigs` in flyctl's imgsrc).
33 */
34 const FLY_REGISTRY_MIRROR = "_api.internal:5000";
35 const HEALTH_INTERVAL = Duration.seconds(5);
36 /** Builders usually go active within ~15s; give up well before the deploy would. */
37 const MAX_PENDING_POLLS = 240;
38 /** How often, and how long, to wait for the builder's push to reach `registry.fly.io`. */
39 const UPSTREAM_POLL = Duration.seconds(2);
40 const MAX_UPSTREAM_POLLS = 30;
41 /** Pushes of one image before giving up on it reaching the registry. */
42 const MAX_PUSHES = 3;
43
44 export class RemoteBuilderError extends Data.TaggedError("Fly.RemoteBuilderError")<{
45 operation: string;
46 message: string;
47 }> {}
48
49 const BuildKitConnection = Schema.Union([
50 Schema.Struct({
51 active: Schema.Struct({
52 endpoint: Schema.String,
53 serverName: Schema.String,
54 cert: Schema.Struct({ cert: Schema.String, key: Schema.String }),
55 caCert: Schema.Struct({ cert: Schema.String }),
56 }),
57 }),
58 Schema.Struct({ pending: Schema.Struct({ waitMs: Schema.optional(Schema.Number) }) }),
59 ]);
60
61 const depot = Effect.fn(function* <A>(
62 method: string,
63 token: Redacted.Redacted<string>,
64 body: unknown,
65 response: Schema.Codec<A, unknown>,
66 ) {
67 const client = yield* HttpClient.HttpClient;
68 const fail = (message: string) => new RemoteBuilderError({ operation: method, message });
69 const res = yield* client
70 .execute(
71 HttpClientRequest.post(`${DEPOT_API}/${method}`).pipe(
72 HttpClientRequest.bearerToken(Redacted.value(token)),
73 HttpClientRequest.bodyJsonUnsafe(body),
74 ),
75 )
76 .pipe(Effect.mapError((cause) => fail(String(cause))));
77 if (res.status < 200 || res.status >= 300) {
78 const text = yield* res.text.pipe(Effect.orElseSucceed(() => ""));
79 return yield* fail(`HTTP ${res.status}: ${text}`);
80 }
81 return yield* HttpClientResponse.schemaBodyJson(response)(res).pipe(
82 Effect.mapError(() => fail("unexpected response body")),
83 );
84 });
85
86 const basicAuth = ({ username, password }: RegistryCredentials) =>
87 Base64.encode(
88 `${username}:${Redacted.isRedacted(password) ? Redacted.value(password) : password}`,
89 );
90
91 const platformOf = (platform: string) =>
92 /arm|aarch/.test(platform) ? "BUILDER_PLATFORM_ARM64" : "BUILDER_PLATFORM_AMD64";
93
94 /** Poll Depot until the build's BuildKit machine is active. */
95 const acquireConnection = Effect.fn(function* (
96 buildId: string,
97 token: Redacted.Redacted<string>,
98 platform: string,
99 ) {
100 for (let poll = 0; poll < MAX_PENDING_POLLS; poll++) {
101 const connection = yield* depot(
102 "GetBuildKitConnection",
103 token,
104 { buildId, platform: platformOf(platform) },
105 BuildKitConnection,
106 );
107 if ("active" in connection) return connection.active;
108 yield* Effect.sleep(Duration.millis(connection.pending.waitMs ?? 500));
109 }
110 return yield* new RemoteBuilderError({
111 operation: "GetBuildKitConnection",
112 message: `builder for build ${buildId} did not become active`,
113 });
114 });
115
116 /**
117 * Build `imageRef` from `contextDir` on a Fly remote builder and push it to
118 * `registry` from there.
119 */
120 export const buildOnRemoteBuilder = Effect.fn(function* (input: {
121 docker: Docker["Service"];
122 appName: string;
123 contextDir: string;
124 imageRef: string;
125 platform: string;
126 registry: RegistryCredentials;
127 note?: (message: string) => Effect.Effect<void>;
128 }) {
129 const note = input.note ?? ((_message: string) => Effect.void);
130 const { buildId, buildToken } = yield* builders.ensureDepotRemoteBuilder({
131 input: { appName: input.appName, builderScope: "app" },
132 });
133 const token = Redacted.isRedacted(buildToken) ? buildToken : Redacted.make(buildToken);
134 const platform = platformOf(input.platform);
135
136 const finish = (result: object) =>
137 depot("FinishBuild", token, { buildId, ...result }, Schema.Unknown).pipe(Effect.ignore);
138
139 return yield* Effect.gen(function* () {
140 // Depot reclaims a builder that stops reporting health.
141 yield* depot("ReportBuildHealth", token, { buildId, platform }, Schema.Unknown).pipe(
142 Effect.ignore,
143 Effect.repeat(Schedule.spaced(HEALTH_INTERVAL)),
144 Effect.forkScoped,
145 );
146 const connection = yield* acquireConnection(buildId, token, input.platform);
147
148 const fs = yield* FileSystem.FileSystem;
149 const path = yield* Path.Path;
150 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-fly-builder-" });
151 const file = (name: string, content: string) =>
152 fs
153 .writeFileString(path.join(dir, name), content, { mode: 0o600 })
154 .pipe(Effect.as(path.join(dir, name)));
155 const cacert = yield* file("ca.pem", connection.caCert.cert);
156 const cert = yield* file("cert.pem", connection.cert.cert);
157 const key = yield* file("key.pem", connection.cert.key);
158
159 const builder = `alchemy-fly-${buildId}`.replace(/[^a-zA-Z0-9_-]/g, "-");
160 yield* Effect.acquireRelease(
161 input.docker.run([
162 "buildx",
163 "create",
164 "--name",
165 builder,
166 "--driver",
167 "remote",
168 "--driver-opt",
169 `cacert=${cacert},cert=${cert},key=${key},servername=${connection.serverName}`,
170 connection.endpoint,
171 ]),
172 () => input.docker.run(["buildx", "rm", builder]).pipe(Effect.ignore),
173 );
174
175 // The builder pushes through its host's registry mirror, which forwards
176 // to registry.fly.io. Machines pull from there, so confirm the tag landed
177 // (usually within seconds) while the builder is still held. The mirror
178 // occasionally drops a push without an error, so a tag that hasn't
179 // landed is pushed again: the builder has every layer cached, so a
180 // repeat build only re-sends the image.
181 const build = input.docker.image
182 .build(
183 {
184 context: input.contextDir,
185 tag: input.imageRef,
186 platform: input.platform,
187 // Buildx attaches provenance by default, which pushes an image index.
188 // The builder's registry mirror never forwards an index to
189 // registry.fly.io, so the image would only exist on the builder.
190 // flyctl's BuildKit client adds no attestations either.
191 args: ["--provenance=false", "--sbom=false"],
192 },
193 undefined,
194 input.registry,
195 )
196 .pipe(
197 Effect.provideService(DockerEnvironment, {
198 BUILDX_BUILDER: builder,
199 DOCKER_AUTH_CONFIG: JSON.stringify({
200 auths: { [FLY_REGISTRY_MIRROR]: { auth: basicAuth(input.registry) } },
201 }),
202 }),
203 );
204 const landed = input.docker
205 .run(["buildx", "imagetools", "inspect", "--raw", input.imageRef])
206 .pipe(
207 Effect.provideService(DockerEnvironment, {
208 DOCKER_AUTH_CONFIG: JSON.stringify({
209 auths: { [input.registry.server]: { auth: basicAuth(input.registry) } },
210 }),
211 }),
212 Effect.retry({ schedule: Schedule.spaced(UPSTREAM_POLL), times: MAX_UPSTREAM_POLLS }),
213 Effect.mapError(
214 () =>
215 new RemoteBuilderError({
216 operation: "upload",
217 message: `${input.imageRef} did not reach ${input.registry.server} after ${MAX_PUSHES} pushes`,
218 }),
219 ),
220 );
221 const started = yield* Clock.currentTimeMillis;
222 yield* build.pipe(
223 Effect.andThen(landed),
224 Effect.tapError((error) =>
225 error._tag === "Fly.RemoteBuilderError" && error.operation === "upload"
226 ? note(`${input.imageRef} hasn't reached ${input.registry.server} yet`)
227 : Effect.void,
228 ),
229 Effect.retry({
230 while: (error) => error._tag === "Fly.RemoteBuilderError" && error.operation === "upload",
231 times: MAX_PUSHES - 1,
232 }),
233 );
234 const waited = (yield* Clock.currentTimeMillis) - started;
235 yield* note(`${input.imageRef} available on ${input.registry.server} after ${waited}ms`);
236 }).pipe(
237 Effect.scoped,
238 Effect.tapError((error) => finish({ error: { error: String(error) } })),
239 Effect.tap(() => finish({ success: {} })),
240 Effect.onInterrupt(() => finish({ canceled: {} })),
241 );
242 });