@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, 06:00 UTC

main
ts 108 lines 3.74 KB
Raw
1 import type { RegistryCredentials } from "@alchemy.run/docker/Docker";
2 import {
3 makeContainerImageSource,
4 type ImageRegistryTarget,
5 } from "@alchemy.run/docker/ImageSource";
6 import { createPhysicalName } from "alchemy/PhysicalName";
7 /**
8 * Image publishing for connections that declare a
9 * {@link Connection.registry}: `main` programs and `context` Dockerfiles are
10 * built on the deploying machine and pushed to `<server>/<name>:<hash>`,
11 * where the cluster's nodes pull them. Used when the cluster's platform
12 * adapter has no managed registry of its own (EKS pushes to ECR instead).
13 */
14 import * as Effect from "effect/Effect";
15 import type { RegistryState, WorkloadImageSource } from "../ClusterAdapter.ts";
16 import type { ContainerRegistry } from "../Connection.ts";
17
18 /** Docker credentials for a registry, or `undefined` to use `docker login`. */
19 const credentialsOf = (registry: ContainerRegistry): RegistryCredentials | undefined =>
20 registry.username !== undefined && registry.password !== undefined
21 ? {
22 // Docker keys credentials by host; `server` may carry a namespace.
23 server: registry.server.split("/")[0]!,
24 username: registry.username,
25 password: registry.password,
26 }
27 : undefined;
28
29 const trimServer = (server: string) => server.replace(/\/+$/, "");
30
31 export const makeConnectionRegistry = Effect.gen(function* () {
32 const images = yield* makeContainerImageSource;
33
34 const resolve = Effect.fn(function* (options: {
35 id: string;
36 registry: ContainerRegistry;
37 source: WorkloadImageSource;
38 platform: string;
39 port?: number | undefined;
40 isExternal?: boolean | undefined;
41 bootstrap: (importPath: string) => string;
42 state: Record<string, unknown> | undefined;
43 session: { note: (message: string) => Effect.Effect<void> };
44 }) {
45 const server = trimServer(options.registry.server);
46 // Keep the repository stable across updates; a changed `server` starts
47 // a new one.
48 const repository =
49 options.state?.kind === "registry" &&
50 options.state.server === server &&
51 typeof options.state.repository === "string"
52 ? options.state.repository
53 : `${server}/${yield* createPhysicalName({
54 id: options.id,
55 maxLength: 96,
56 lowercase: true,
57 })}`;
58 const target: ImageRegistryTarget = {
59 repositoryUri: Effect.succeed(repository),
60 // Generic registries have no cheap existence probe; resolve only runs
61 // when the workload changes, and the local build cache makes a
62 // rebuild of unchanged content fast.
63 hasTag: () => Effect.succeed(false),
64 credentials: Effect.succeed(credentialsOf(options.registry)),
65 };
66 const resolved = yield* images.resolve(
67 {
68 id: options.id,
69 source: options.source,
70 platform: options.platform,
71 port: options.port,
72 isExternal: options.isExternal,
73 bootstrap: options.bootstrap,
74 session: options.session,
75 },
76 target,
77 );
78 return {
79 imageUri: resolved.imageUri,
80 codeHash: resolved.codeHash,
81 state: {
82 kind: "registry" as const,
83 server,
84 repository,
85 } satisfies RegistryState,
86 };
87 });
88
89 const hash = (options: {
90 source: WorkloadImageSource;
91 platform: string;
92 port?: number | undefined;
93 isExternal?: boolean | undefined;
94 bootstrap: (importPath: string) => string;
95 }) =>
96 images.hash({
97 source: options.source,
98 platform: options.platform,
99 port: options.port,
100 isExternal: options.isExternal,
101 bootstrap: options.bootstrap,
102 });
103
104 return { resolve, hash };
105 });
106
107 /** The publisher returned by {@link makeConnectionRegistry}. */
108 export interface ConnectionRegistry extends Effect.Success<typeof makeConnectionRegistry> {}