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 { Docker } from "@alchemy.run/docker/Docker"; |
| 2 | import { isInlineDockerfile, type InlineDockerfile } from "@alchemy.run/docker/Dockerfile"; |
| 3 | import { |
| 4 | validateDockerfileBuild, |
| 5 | type DockerfileBuild, |
| 6 | type DockerfileImageSource, |
| 7 | } from "@alchemy.run/docker/ImageSource"; |
| 8 | /** |
| 9 | * INTERNAL — Artifact Registry image-source machinery for GKE workloads. |
| 10 | * Not exported from the GCP barrel or `ArtifactRegistry/index.ts`. |
| 11 | * |
| 12 | * Mirrors `AWS/ECR/ImageSource.ts`: bundle / docker-build / mirror into a |
| 13 | * per-workload Docker repository so `Kubernetes.*` workloads on GKE pull |
| 14 | * from Artifact Registry. |
| 15 | */ |
| 16 | import * as artifactregistry from "@distilled.cloud/gcp/artifactregistry_v1"; |
| 17 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 18 | import * as Bundle from "alchemy/Bundle/Bundle"; |
| 19 | import { |
| 20 | normalizeInstallTargets, |
| 21 | resolvePackageInstallIdentity, |
| 22 | type PackageInstall, |
| 23 | } from "alchemy/Bundle/InstalledPackages"; |
| 24 | import { findCwdForBundle, getStableContextDir, resolveMainPath } from "alchemy/Bundle/TempRoot"; |
| 25 | import { hashDirectory } from "alchemy/Command/Memo"; |
| 26 | import { tagRecord } from "alchemy/Tags"; |
| 27 | import { |
| 28 | contextRootOf, |
| 29 | copyExtraFiles, |
| 30 | hashExtraFiles, |
| 31 | posixRelUnder, |
| 32 | resolveExtraSource, |
| 33 | type ExtraFile, |
| 34 | } from "alchemy/Util/extraFiles"; |
| 35 | import { sha256, sha256Object } from "alchemy/Util/sha256"; |
| 36 | import * as Effect from "effect/Effect"; |
| 37 | import * as FileSystem from "effect/FileSystem"; |
| 38 | import * as Path from "effect/Path"; |
| 39 | import * as Schedule from "effect/Schedule"; |
| 40 | import type * as rolldown from "rolldown"; |
| 41 | import { Credentials } from "../Credentials.ts"; |
| 42 | import { GcpEnvironment } from "../Environment.ts"; |
| 43 | import { serviceAccountHostBundle } from "../GCPBindings.ts"; |
| 44 | import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 45 | import { waitForDeleteOperation, waitForOperation } from "./internal.ts"; |
| 46 | |
| 47 | export interface BundledImageSource { |
| 48 | main: string; |
| 49 | image?: string; |
| 50 | dockerfile?: string | InlineDockerfile; |
| 51 | context?: string; |
| 52 | handler?: string; |
| 53 | build?: Bundle.BundleConfig; |
| 54 | /** |
| 55 | * Host files baked into the image next to `main`. With an external |
| 56 | * (`isExternal`) `main`, this selects the unbundled Node program mode: |
| 57 | * `main` is copied as-is and run by `node` (website serve entries). |
| 58 | */ |
| 59 | extraFiles?: ReadonlyArray<ExtraFile>; |
| 60 | /** Packages `npm install`ed into the Node program image. */ |
| 61 | install?: PackageInstall; |
| 62 | } |
| 63 | |
| 64 | export type { DockerfileImageSource }; |
| 65 | |
| 66 | export interface RegistryImageSource { |
| 67 | image: string; |
| 68 | } |
| 69 | |
| 70 | export interface ImageSourceLike { |
| 71 | main?: string; |
| 72 | handler?: string; |
| 73 | /** Bundler config for `main`; Docker build args and secrets for `context`. */ |
| 74 | build?: Bundle.BundleConfig & DockerfileBuild; |
| 75 | context?: string; |
| 76 | dockerfile?: string | InlineDockerfile; |
| 77 | image?: string; |
| 78 | extraFiles?: ReadonlyArray<ExtraFile>; |
| 79 | install?: PackageInstall; |
| 80 | } |
| 81 | |
| 82 | /** Base image for unbundled Node programs (website serve entries). */ |
| 83 | const NODE_PROGRAM_BASE_IMAGE = "node:26-slim"; |
| 84 | |
| 85 | const isNodeProgram = (source: ImageSourceLike, isExternal: boolean | undefined) => |
| 86 | isExternal === true && source.main !== undefined && source.extraFiles !== undefined; |
| 87 | |
| 88 | const nodeProgramManifest = (dependencies: Record<string, string>) => |
| 89 | `${JSON.stringify({ private: true, type: "module", dependencies }, null, 2)}\n`; |
| 90 | |
| 91 | const nodeProgramDockerfile = (options: { |
| 92 | image: string | undefined; |
| 93 | entry: string; |
| 94 | install: boolean; |
| 95 | port: number | undefined; |
| 96 | }) => { |
| 97 | const lines = [ |
| 98 | `FROM ${options.image ?? NODE_PROGRAM_BASE_IMAGE}`, |
| 99 | `WORKDIR /app`, |
| 100 | `ENV NODE_ENV=production`, |
| 101 | ]; |
| 102 | if (options.install) { |
| 103 | lines.push( |
| 104 | `COPY package.json /app/package.json`, |
| 105 | // Drop npm's download cache so it does not ship (and push) with the image. |
| 106 | `RUN npm install --omit=dev --no-fund --no-audit && npm cache clean --force`, |
| 107 | ); |
| 108 | } |
| 109 | lines.push(`COPY . /app`, `ENV HOST=0.0.0.0`); |
| 110 | if (options.port !== undefined) { |
| 111 | lines.push(`ENV PORT=${String(options.port)}`, `EXPOSE ${String(options.port)}`); |
| 112 | } |
| 113 | lines.push(`ENTRYPOINT ["node", ${JSON.stringify(`/app/${options.entry}`)}]`); |
| 114 | return `${lines.join("\n")}\n`; |
| 115 | }; |
| 116 | |
| 117 | export type ImageSourceKind = "main" | "context" | "image"; |
| 118 | |
| 119 | export const imageSourceKind = (source: ImageSourceLike): ImageSourceKind | undefined => |
| 120 | source.main !== undefined |
| 121 | ? "main" |
| 122 | : source.image !== undefined |
| 123 | ? "image" |
| 124 | : source.context !== undefined || source.dockerfile !== undefined |
| 125 | ? "context" |
| 126 | : undefined; |
| 127 | |
| 128 | export const validateImageSource = (id: string, source: ImageSourceLike): Effect.Effect<void> => { |
| 129 | if (source.image !== undefined && source.dockerfile !== undefined) { |
| 130 | return Effect.die( |
| 131 | new Error( |
| 132 | `'${id}': 'image' and 'dockerfile' are both set — declare exactly one environment source`, |
| 133 | ), |
| 134 | ); |
| 135 | } |
| 136 | if (source.image !== undefined && source.context !== undefined) { |
| 137 | return Effect.die( |
| 138 | new Error( |
| 139 | `'${id}': 'image' and 'context' are both set — declare exactly one environment source`, |
| 140 | ), |
| 141 | ); |
| 142 | } |
| 143 | if ( |
| 144 | source.dockerfile !== undefined && |
| 145 | isInlineDockerfile(source.dockerfile) && |
| 146 | source.context !== undefined |
| 147 | ) { |
| 148 | return Effect.die( |
| 149 | new Error( |
| 150 | `'${id}': inline 'dockerfile' content builds with no context — use a path dockerfile with 'context', or drop 'context'`, |
| 151 | ), |
| 152 | ); |
| 153 | } |
| 154 | return validateDockerfileBuild(id, source); |
| 155 | }; |
| 156 | |
| 157 | export interface ResolvedImage { |
| 158 | imageUri: string; |
| 159 | repositoryName: string; |
| 160 | repositoryUri: string; |
| 161 | codeHash: string; |
| 162 | } |
| 163 | |
| 164 | export interface ResolveImageOptions { |
| 165 | id: string; |
| 166 | source: ImageSourceLike; |
| 167 | repositoryName: string; |
| 168 | repositoryUri?: string; |
| 169 | tags?: Record<string, string>; |
| 170 | platform?: string; |
| 171 | port?: number; |
| 172 | isExternal?: boolean; |
| 173 | bootstrap: (importPath: string) => string; |
| 174 | session: { note: (message: string) => Effect.Effect<void> }; |
| 175 | } |
| 176 | |
| 177 | const resolveContextPaths = Effect.fn(function* (source: { |
| 178 | context: string | undefined; |
| 179 | dockerfile?: string; |
| 180 | }) { |
| 181 | const fs = yield* FileSystem.FileSystem; |
| 182 | const path = yield* Path.Path; |
| 183 | const context = path.resolve(source.context ?? "."); |
| 184 | const dockerfile = source.dockerfile |
| 185 | ? path.resolve(source.dockerfile) |
| 186 | : path.join(context, "Dockerfile"); |
| 187 | if (!(yield* fs.exists(context))) { |
| 188 | return yield* Effect.die(new Error(`Docker build context does not exist: ${context}`)); |
| 189 | } |
| 190 | if (!(yield* fs.exists(dockerfile))) { |
| 191 | return yield* Effect.die(new Error(`Dockerfile does not exist: ${dockerfile}`)); |
| 192 | } |
| 193 | return { context, dockerfile }; |
| 194 | }); |
| 195 | |
| 196 | export const computeStaticSourceHash = Effect.fn(function* ( |
| 197 | source: ImageSourceLike, |
| 198 | platform?: string, |
| 199 | ) { |
| 200 | const kind = imageSourceKind(source); |
| 201 | if (kind === "image") { |
| 202 | return (yield* sha256Object({ |
| 203 | image: source.image!, |
| 204 | platform: platform ?? "linux/amd64", |
| 205 | })).slice(0, 16); |
| 206 | } |
| 207 | if (kind === "context") { |
| 208 | if (source.dockerfile !== undefined && isInlineDockerfile(source.dockerfile)) { |
| 209 | if (typeof source.dockerfile.content !== "string") return undefined; |
| 210 | return (yield* sha256Object({ |
| 211 | dockerfile: source.dockerfile.content, |
| 212 | platform: platform ?? "linux/amd64", |
| 213 | // Absent when unset, so existing hashes hold. Secrets are excluded. |
| 214 | buildArgs: source.build?.args, |
| 215 | })).slice(0, 16); |
| 216 | } |
| 217 | const fs = yield* FileSystem.FileSystem; |
| 218 | const { context, dockerfile } = yield* resolveContextPaths({ |
| 219 | context: source.context!, |
| 220 | dockerfile: source.dockerfile, |
| 221 | }); |
| 222 | const contextHash = yield* hashDirectory({ cwd: context }); |
| 223 | const dockerfileContent = yield* fs.readFileString(dockerfile); |
| 224 | return (yield* sha256Object({ |
| 225 | contextHash, |
| 226 | dockerfile: dockerfileContent, |
| 227 | platform: platform ?? "linux/amd64", |
| 228 | buildArgs: source.build?.args, |
| 229 | })).slice(0, 16); |
| 230 | } |
| 231 | return undefined; |
| 232 | }); |
| 233 | |
| 234 | const IMAGE_NAME = "app"; |
| 235 | |
| 236 | const resourceName = (project: string, location: string, repositoryId: string) => |
| 237 | `projects/${project}/locations/${location}/repositories/${repositoryId}`; |
| 238 | |
| 239 | const dockerRepositoryUri = (project: string, location: string, repositoryId: string) => |
| 240 | `${location}-docker.pkg.dev/${project}/${repositoryId}/${IMAGE_NAME}`; |
| 241 | |
| 242 | const tagResourceName = (project: string, location: string, repositoryId: string, tag: string) => |
| 243 | `${resourceName(project, location, repositoryId)}/packages/${IMAGE_NAME}/tags/${tag}`; |
| 244 | |
| 245 | export interface ArtifactRegistryResolveOptions extends ResolveImageOptions { |
| 246 | /** Artifact Registry location (`us-central1`, …). */ |
| 247 | location: string; |
| 248 | } |
| 249 | |
| 250 | export const getArtifactRegistryCredentials = Effect.fn(function* (location: string) { |
| 251 | const creds = yield* yield* Credentials; |
| 252 | return { |
| 253 | username: "oauth2accesstoken", |
| 254 | password: creds.accessToken, |
| 255 | server: `${location}-docker.pkg.dev`, |
| 256 | }; |
| 257 | }); |
| 258 | |
| 259 | export const buildAndPushArtifactRegistryImage = Effect.fn(function* ( |
| 260 | docker: Docker["Service"], |
| 261 | options: { |
| 262 | imageUri: string; |
| 263 | context: string; |
| 264 | dockerfile?: string; |
| 265 | platform?: string; |
| 266 | location: string; |
| 267 | build?: DockerfileBuild; |
| 268 | }, |
| 269 | ) { |
| 270 | const credentials = yield* getArtifactRegistryCredentials(options.location); |
| 271 | yield* docker.image.build({ |
| 272 | tag: options.imageUri, |
| 273 | context: options.context, |
| 274 | file: options.dockerfile, |
| 275 | platform: options.platform, |
| 276 | "build-arg": options.build?.args, |
| 277 | secrets: options.build?.secrets, |
| 278 | }); |
| 279 | yield* docker.image.push(options.imageUri, credentials, options.platform).pipe( |
| 280 | Effect.retry({ |
| 281 | while: (): boolean => true, |
| 282 | schedule: Schedule.exponential("2 seconds"), |
| 283 | times: 3, |
| 284 | }), |
| 285 | ); |
| 286 | return options.imageUri; |
| 287 | }); |
| 288 | |
| 289 | /** |
| 290 | * Init-time constructor for the Artifact Registry image-source resolver. |
| 291 | */ |
| 292 | /** Delete the Artifact Registry repository `name`; missing is success. */ |
| 293 | export const destroyImageRepository = Effect.fn(function* (name: string) { |
| 294 | const operation = yield* artifactregistry |
| 295 | .deleteProjectsLocationsRepositories({ name }) |
| 296 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 297 | if (operation !== undefined) { |
| 298 | yield* waitForDeleteOperation(operation); |
| 299 | } |
| 300 | }); |
| 301 | |
| 302 | /** |
| 303 | * Delete the image repository an Effect-native host built into on |
| 304 | * reconcile (`{hostId}-src`, normalized by the host's `rfc1035`). |
| 305 | * |
| 306 | * Not gated on `output.codeHash`: an update that failed after creating the |
| 307 | * repository leaves state without a hash. The repository is deleted only |
| 308 | * when it carries this logical id's ownership labels, so a same-named |
| 309 | * foreign repository is never touched. |
| 310 | */ |
| 311 | export const destroyHostImageRepository = Effect.fn(function* ( |
| 312 | id: string, |
| 313 | output: { |
| 314 | project: string; |
| 315 | location: string; |
| 316 | }, |
| 317 | repositoryId: string, |
| 318 | ) { |
| 319 | const name = resourceName(output.project, output.location, repositoryId); |
| 320 | const repository = yield* artifactregistry |
| 321 | .getProjectsLocationsRepositories({ name }) |
| 322 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 323 | if (repository === undefined) return; |
| 324 | if (!(yield* hasAlchemyLabels(id, tagRecord(repository.labels)))) return; |
| 325 | yield* destroyImageRepository(name); |
| 326 | }); |
| 327 | |
| 328 | export const makeImageSource = Effect.gen(function* () { |
| 329 | const docker = yield* Docker; |
| 330 | const { dotAlchemy } = yield* AlchemyContext; |
| 331 | const virtualEntryPlugin = yield* Bundle.virtualEntryPlugin; |
| 332 | |
| 333 | const mainBundleOptions = ( |
| 334 | source: BundledImageSource, |
| 335 | entry: string, |
| 336 | cwd: string, |
| 337 | plugins?: rolldown.RolldownPluginOption, |
| 338 | ): { |
| 339 | inputOptions: rolldown.InputOptions; |
| 340 | outputOptions: rolldown.OutputOptions; |
| 341 | } => ({ |
| 342 | // Images here run on Cloud Run or GKE, as a service account. |
| 343 | inputOptions: serviceAccountHostBundle({ |
| 344 | ...source.build?.input, |
| 345 | input: entry, |
| 346 | cwd, |
| 347 | platform: "node", |
| 348 | external: [ |
| 349 | "bun", |
| 350 | "bun:*", |
| 351 | ...((source.build?.input?.external as string[] | undefined) ?? []), |
| 352 | ], |
| 353 | resolve: { |
| 354 | conditionNames: ["bun", "import", "module", "default"], |
| 355 | ...source.build?.input?.resolve, |
| 356 | }, |
| 357 | plugins: [source.build?.input?.plugins, plugins], |
| 358 | }), |
| 359 | outputOptions: { |
| 360 | ...source.build?.output, |
| 361 | format: "esm", |
| 362 | sourcemap: source.build?.output?.sourcemap ?? false, |
| 363 | minify: source.build?.output?.minify ?? false, |
| 364 | entryFileNames: "index.mjs", |
| 365 | }, |
| 366 | }); |
| 367 | |
| 368 | const bundleProgram = Effect.fn(function* (options: { |
| 369 | source: BundledImageSource; |
| 370 | isExternal?: boolean; |
| 371 | bootstrap: (importPath: string) => string; |
| 372 | }) { |
| 373 | const { source } = options; |
| 374 | const realMain = yield* resolveMainPath(source.main); |
| 375 | const cwd = yield* findCwdForBundle(realMain); |
| 376 | |
| 377 | const buildBundle = Effect.fn(function* ( |
| 378 | entry: string, |
| 379 | plugins?: rolldown.RolldownPluginOption, |
| 380 | ) { |
| 381 | const opts = mainBundleOptions(source, entry, cwd, plugins); |
| 382 | return yield* Bundle.build(opts.inputOptions, opts.outputOptions, source.build); |
| 383 | }); |
| 384 | |
| 385 | const bundleOutput = options.isExternal |
| 386 | ? yield* buildBundle(realMain) |
| 387 | : yield* buildBundle(realMain, virtualEntryPlugin(options.bootstrap)); |
| 388 | |
| 389 | const files = bundleOutput.files.map((file) => ({ |
| 390 | path: file.path, |
| 391 | content: |
| 392 | typeof file.content === "string" ? new TextEncoder().encode(file.content) : file.content, |
| 393 | })); |
| 394 | |
| 395 | return { files, hash: bundleOutput.hash }; |
| 396 | }); |
| 397 | |
| 398 | const generateDockerfile = (source: BundledImageSource, port?: number, envFrom?: string) => { |
| 399 | const preamble = |
| 400 | envFrom !== undefined |
| 401 | ? `FROM ${envFrom}` |
| 402 | : source.dockerfile !== undefined && isInlineDockerfile(source.dockerfile) |
| 403 | ? (source.dockerfile.content as string).trimEnd() |
| 404 | : `FROM ${source.image ?? "oven/bun:1"}`; |
| 405 | const lines = [preamble, `WORKDIR /app`, `COPY index.mjs /app/index.mjs`, `COPY *.js /app/`]; |
| 406 | if (port !== undefined) { |
| 407 | lines.push(`ENV PORT=${String(port)}`, `EXPOSE ${String(port)}`); |
| 408 | } |
| 409 | lines.push(`ENTRYPOINT ["bun", "/app/index.mjs"]`); |
| 410 | return `${lines.join("\n")}\n`; |
| 411 | }; |
| 412 | |
| 413 | const computeMainCodeHash = Effect.fn(function* (options: { |
| 414 | source: BundledImageSource; |
| 415 | isExternal?: boolean; |
| 416 | bootstrap: (importPath: string) => string; |
| 417 | port?: number; |
| 418 | platform: string; |
| 419 | }) { |
| 420 | const bundled = yield* bundleProgram({ |
| 421 | source: options.source, |
| 422 | isExternal: options.isExternal, |
| 423 | bootstrap: options.bootstrap, |
| 424 | }); |
| 425 | const df = options.source.dockerfile; |
| 426 | const isPathEnv = df !== undefined && !isInlineDockerfile(df); |
| 427 | let envIdentity: Record<string, string> = {}; |
| 428 | if (isPathEnv) { |
| 429 | const fs = yield* FileSystem.FileSystem; |
| 430 | const { context, dockerfile } = yield* resolveContextPaths({ |
| 431 | context: options.source.context!, |
| 432 | dockerfile: df, |
| 433 | }); |
| 434 | envIdentity = { |
| 435 | envContextHash: yield* hashDirectory({ cwd: context }), |
| 436 | envDockerfile: yield* fs.readFileString(dockerfile), |
| 437 | }; |
| 438 | } |
| 439 | const dockerfile = generateDockerfile( |
| 440 | options.source, |
| 441 | options.port, |
| 442 | isPathEnv ? "<env>" : undefined, |
| 443 | ); |
| 444 | const codeHash = (yield* sha256Object({ |
| 445 | bundleHash: bundled.hash, |
| 446 | dockerfile, |
| 447 | platform: options.platform, |
| 448 | ...envIdentity, |
| 449 | })).slice(0, 16); |
| 450 | return { bundled, dockerfile, codeHash }; |
| 451 | }); |
| 452 | |
| 453 | /** |
| 454 | * Hash an unbundled Node program: the entry bytes, every extra file |
| 455 | * tree, the generated Dockerfile, and the resolved install identity. |
| 456 | */ |
| 457 | const computeNodeProgram = Effect.fn(function* (options: { |
| 458 | source: BundledImageSource; |
| 459 | port?: number; |
| 460 | platform: string; |
| 461 | }) { |
| 462 | const fs = yield* FileSystem.FileSystem; |
| 463 | const path = yield* Path.Path; |
| 464 | const { source } = options; |
| 465 | const realMain = yield* resolveMainPath(source.main); |
| 466 | const extras = source.extraFiles ?? []; |
| 467 | const root = contextRootOf(realMain, extras, path, (file) => resolveExtraSource(file, path)); |
| 468 | const entry = posixRelUnder(root, realMain, path) || path.basename(realMain); |
| 469 | const requested = yield* normalizeInstallTargets(source.install); |
| 470 | const identity = |
| 471 | Object.keys(requested).length > 0 |
| 472 | ? yield* resolvePackageInstallIdentity({ |
| 473 | cwd: yield* findCwdForBundle(realMain), |
| 474 | requested, |
| 475 | }) |
| 476 | : undefined; |
| 477 | const packageJson = |
| 478 | identity !== undefined && Object.keys(identity.resolved).length > 0 |
| 479 | ? nodeProgramManifest(identity.resolved) |
| 480 | : undefined; |
| 481 | const dockerfile = nodeProgramDockerfile({ |
| 482 | image: source.image, |
| 483 | entry, |
| 484 | install: packageJson !== undefined, |
| 485 | port: options.port, |
| 486 | }); |
| 487 | const codeHash = (yield* sha256Object({ |
| 488 | main: yield* sha256(yield* fs.readFile(realMain)), |
| 489 | entry, |
| 490 | dockerfile, |
| 491 | packageJson, |
| 492 | lockfile: identity?.lockfile, |
| 493 | extraFiles: yield* hashExtraFiles(extras), |
| 494 | platform: options.platform, |
| 495 | })).slice(0, 16); |
| 496 | return { realMain, entry, dockerfile, packageJson, codeHash }; |
| 497 | }); |
| 498 | |
| 499 | const ensureRepository = Effect.fn(function* (options: { |
| 500 | id: string; |
| 501 | repositoryId: string; |
| 502 | location: string; |
| 503 | tags?: Record<string, string>; |
| 504 | }) { |
| 505 | const env = yield* GcpEnvironment.current; |
| 506 | const name = resourceName(env.project, options.location, options.repositoryId); |
| 507 | const desiredLabels = { |
| 508 | ...toLabels(options.tags), |
| 509 | ...(yield* createInternalLabels(options.id)), |
| 510 | }; |
| 511 | const existing = yield* artifactregistry |
| 512 | .getProjectsLocationsRepositories({ name }) |
| 513 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 514 | if (existing?.name) { |
| 515 | return { |
| 516 | name: existing.name, |
| 517 | created: false, |
| 518 | repositoryUri: |
| 519 | existing.registryUri !== undefined && existing.registryUri.length > 0 |
| 520 | ? `${existing.registryUri.replace(/\/+$/, "")}/${IMAGE_NAME}` |
| 521 | : dockerRepositoryUri(env.project, options.location, options.repositoryId), |
| 522 | }; |
| 523 | } |
| 524 | const created = yield* artifactregistry |
| 525 | .createProjectsLocationsRepositories({ |
| 526 | parent: `projects/${env.project}/locations/${options.location}`, |
| 527 | repositoryId: options.repositoryId, |
| 528 | body: { |
| 529 | format: "DOCKER", |
| 530 | mode: "STANDARD_REPOSITORY", |
| 531 | labels: desiredLabels, |
| 532 | }, |
| 533 | }) |
| 534 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 535 | if (created !== undefined) { |
| 536 | yield* waitForOperation(created); |
| 537 | } |
| 538 | const observed = yield* artifactregistry.getProjectsLocationsRepositories({ name }).pipe( |
| 539 | Effect.retry({ |
| 540 | while: (error) => error._tag === "NotFound", |
| 541 | times: 8, |
| 542 | schedule: Schedule.spaced("1 second"), |
| 543 | }), |
| 544 | ); |
| 545 | return { |
| 546 | name: observed.name ?? name, |
| 547 | // Created by THIS call (a lost create race reports Conflict). |
| 548 | created: created !== undefined, |
| 549 | repositoryUri: |
| 550 | observed.registryUri !== undefined && observed.registryUri.length > 0 |
| 551 | ? `${observed.registryUri.replace(/\/+$/, "")}/${IMAGE_NAME}` |
| 552 | : dockerRepositoryUri(env.project, options.location, options.repositoryId), |
| 553 | }; |
| 554 | }); |
| 555 | |
| 556 | const describeImage = Effect.fn(function* ( |
| 557 | project: string, |
| 558 | location: string, |
| 559 | repositoryId: string, |
| 560 | imageTag: string, |
| 561 | ) { |
| 562 | return yield* artifactregistry |
| 563 | .getProjectsLocationsRepositoriesPackagesTags({ |
| 564 | name: tagResourceName(project, location, repositoryId, imageTag), |
| 565 | }) |
| 566 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 567 | }); |
| 568 | |
| 569 | const resolve = Effect.fn(function* (options: ArtifactRegistryResolveOptions) { |
| 570 | const { id, source, repositoryName, location } = options; |
| 571 | const kind = imageSourceKind(source); |
| 572 | if (kind === undefined) { |
| 573 | return yield* Effect.die( |
| 574 | new Error( |
| 575 | `'${id}' must declare exactly one image source: 'main' (bundled Effect program), 'context' (Dockerfile build), or 'image' (registry reference)`, |
| 576 | ), |
| 577 | ); |
| 578 | } |
| 579 | yield* validateImageSource(id, source); |
| 580 | |
| 581 | const ensured = yield* ensureRepository({ |
| 582 | id, |
| 583 | repositoryId: repositoryName, |
| 584 | location, |
| 585 | tags: options.tags, |
| 586 | }); |
| 587 | // A failed first build/push never reaches state, so destroy could not |
| 588 | // find a repository created here. Remove it before surfacing the error. |
| 589 | return yield* buildImage(options, ensured.repositoryUri).pipe( |
| 590 | Effect.onError(() => |
| 591 | ensured.created ? destroyImageRepository(ensured.name).pipe(Effect.ignore) : Effect.void, |
| 592 | ), |
| 593 | ); |
| 594 | }); |
| 595 | |
| 596 | const buildImage = Effect.fn(function* ( |
| 597 | options: ArtifactRegistryResolveOptions, |
| 598 | ensuredRepositoryUri: string, |
| 599 | ) { |
| 600 | const { id, source, repositoryName, session, location } = options; |
| 601 | const platform = options.platform ?? "linux/amd64"; |
| 602 | const kind = imageSourceKind(source); |
| 603 | const env = yield* GcpEnvironment.current; |
| 604 | const repositoryUri = options.repositoryUri ?? ensuredRepositoryUri; |
| 605 | |
| 606 | if (isNodeProgram(source, options.isExternal)) { |
| 607 | const fs = yield* FileSystem.FileSystem; |
| 608 | const path = yield* Path.Path; |
| 609 | const program = yield* computeNodeProgram({ |
| 610 | source: source as BundledImageSource, |
| 611 | port: options.port, |
| 612 | platform, |
| 613 | }); |
| 614 | const { codeHash } = program; |
| 615 | const imageUri = `${repositoryUri}:${codeHash}`; |
| 616 | if (yield* describeImage(env.project, location, repositoryName, codeHash)) { |
| 617 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 618 | } |
| 619 | // Hash-keyed so a stale tree from an earlier build never leaks in. |
| 620 | const contextDir = yield* getStableContextDir( |
| 621 | program.realMain, |
| 622 | dotAlchemy, |
| 623 | `${id}-node-${codeHash}`, |
| 624 | ); |
| 625 | yield* docker.materialize({ |
| 626 | context: contextDir, |
| 627 | dockerfile: program.dockerfile, |
| 628 | files: |
| 629 | program.packageJson !== undefined |
| 630 | ? [{ path: "package.json", content: program.packageJson }] |
| 631 | : [], |
| 632 | }); |
| 633 | yield* copyExtraFiles(contextDir, source.extraFiles); |
| 634 | const entryPath = path.join(contextDir, program.entry); |
| 635 | if (!(yield* fs.exists(entryPath).pipe(Effect.orElseSucceed(() => false)))) { |
| 636 | yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true }); |
| 637 | yield* fs.copy(program.realMain, entryPath, { overwrite: true }); |
| 638 | } |
| 639 | yield* session.note(`Building container image ${imageUri}...`); |
| 640 | yield* buildAndPushArtifactRegistryImage(docker, { |
| 641 | imageUri, |
| 642 | context: contextDir, |
| 643 | platform, |
| 644 | location, |
| 645 | }).pipe(Effect.ensuring(fs.remove(contextDir, { recursive: true }).pipe(Effect.ignore))); |
| 646 | yield* session.note(`Pushed ${imageUri}`); |
| 647 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 648 | } |
| 649 | |
| 650 | if (kind === "main") { |
| 651 | const df = source.dockerfile; |
| 652 | if (df !== undefined && isInlineDockerfile(df) && typeof df.content !== "string") { |
| 653 | return yield* Effect.die( |
| 654 | new Error( |
| 655 | `'${id}': inline dockerfile content did not resolve to a string — Outputs in Dockerfile.inline must be resolvable at deploy time`, |
| 656 | ), |
| 657 | ); |
| 658 | } |
| 659 | yield* session.note(`Bundling ${id} program...`); |
| 660 | const { bundled, dockerfile, codeHash } = yield* computeMainCodeHash({ |
| 661 | source: source as BundledImageSource, |
| 662 | isExternal: options.isExternal, |
| 663 | bootstrap: options.bootstrap, |
| 664 | port: options.port, |
| 665 | platform, |
| 666 | }); |
| 667 | const imageUri = `${repositoryUri}:${codeHash}`; |
| 668 | if (yield* describeImage(env.project, location, repositoryName, codeHash)) { |
| 669 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 670 | } |
| 671 | |
| 672 | let envFrom: string | undefined; |
| 673 | if (df !== undefined && !isInlineDockerfile(df)) { |
| 674 | const envPaths = yield* resolveContextPaths({ |
| 675 | context: source.context!, |
| 676 | dockerfile: df, |
| 677 | }); |
| 678 | envFrom = `alchemy-env-${id.toLowerCase()}:${codeHash}`; |
| 679 | yield* session.note(`Building environment image for ${id}...`); |
| 680 | yield* docker.image.build({ |
| 681 | context: envPaths.context, |
| 682 | file: envPaths.dockerfile, |
| 683 | tag: envFrom, |
| 684 | platform, |
| 685 | }); |
| 686 | } |
| 687 | const finalDockerfile = |
| 688 | envFrom === undefined |
| 689 | ? dockerfile |
| 690 | : generateDockerfile(source as BundledImageSource, options.port, envFrom); |
| 691 | |
| 692 | const realMain = yield* resolveMainPath((source as BundledImageSource).main); |
| 693 | const contextDir = yield* getStableContextDir(realMain, dotAlchemy, `${id}-image`); |
| 694 | yield* docker.materialize({ |
| 695 | context: contextDir, |
| 696 | dockerfile: finalDockerfile, |
| 697 | files: bundled.files.map((file, index) => ({ |
| 698 | path: index === 0 ? "index.mjs" : file.path, |
| 699 | content: file.content, |
| 700 | })), |
| 701 | }); |
| 702 | yield* session.note(`Building container image ${imageUri}...`); |
| 703 | yield* buildAndPushArtifactRegistryImage(docker, { |
| 704 | imageUri, |
| 705 | context: contextDir, |
| 706 | platform, |
| 707 | location, |
| 708 | }); |
| 709 | yield* session.note(`Pushed ${imageUri}`); |
| 710 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 711 | } |
| 712 | |
| 713 | if (kind === "image") { |
| 714 | const ref = (source as RegistryImageSource).image; |
| 715 | const codeHash = (yield* computeStaticSourceHash(source, platform))!; |
| 716 | const imageUri = `${repositoryUri}:${codeHash}`; |
| 717 | if (yield* describeImage(env.project, location, repositoryName, codeHash)) { |
| 718 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 719 | } |
| 720 | yield* session.note(`Pulling container image ${ref}...`); |
| 721 | yield* docker.image.pull(ref, platform).pipe(Effect.timeout("4 minutes")); |
| 722 | yield* docker.image.tag(ref, imageUri); |
| 723 | yield* session.note(`Pushing mirrored image ${imageUri}...`); |
| 724 | const credentials = yield* getArtifactRegistryCredentials(location); |
| 725 | yield* docker.image.push(imageUri, credentials, platform).pipe( |
| 726 | Effect.retry({ |
| 727 | while: (): boolean => true, |
| 728 | schedule: Schedule.exponential("2 seconds"), |
| 729 | times: 3, |
| 730 | }), |
| 731 | ); |
| 732 | yield* session.note(`Pushed ${imageUri}`); |
| 733 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 734 | } |
| 735 | |
| 736 | const externalDf = (source as DockerfileImageSource).dockerfile; |
| 737 | if (externalDf !== undefined && isInlineDockerfile(externalDf)) { |
| 738 | if (typeof externalDf.content !== "string") { |
| 739 | return yield* Effect.die( |
| 740 | new Error( |
| 741 | `'${id}': inline dockerfile content did not resolve to a string — Outputs in Dockerfile.inline must be resolvable at deploy time`, |
| 742 | ), |
| 743 | ); |
| 744 | } |
| 745 | const codeHash = (yield* computeStaticSourceHash(source, platform))!; |
| 746 | const imageUri = `${repositoryUri}:${codeHash}`; |
| 747 | if (yield* describeImage(env.project, location, repositoryName, codeHash)) { |
| 748 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 749 | } |
| 750 | const contextDir = yield* getStableContextDir(dotAlchemy, dotAlchemy, `${id}-image`); |
| 751 | yield* docker.materialize({ |
| 752 | context: contextDir, |
| 753 | dockerfile: externalDf.content, |
| 754 | files: [], |
| 755 | }); |
| 756 | yield* session.note(`Building container image ${imageUri}...`); |
| 757 | yield* buildAndPushArtifactRegistryImage(docker, { |
| 758 | imageUri, |
| 759 | context: contextDir, |
| 760 | platform, |
| 761 | location, |
| 762 | build: source.build, |
| 763 | }); |
| 764 | yield* session.note(`Pushed ${imageUri}`); |
| 765 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 766 | } |
| 767 | |
| 768 | const { context, dockerfile } = yield* resolveContextPaths({ |
| 769 | context: (source as DockerfileImageSource).context!, |
| 770 | dockerfile: externalDf, |
| 771 | }); |
| 772 | const codeHash = (yield* computeStaticSourceHash(source, platform))!; |
| 773 | const imageUri = `${repositoryUri}:${codeHash}`; |
| 774 | if (yield* describeImage(env.project, location, repositoryName, codeHash)) { |
| 775 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 776 | } |
| 777 | yield* session.note(`Building container image ${imageUri}...`); |
| 778 | yield* buildAndPushArtifactRegistryImage(docker, { |
| 779 | imageUri, |
| 780 | context, |
| 781 | dockerfile, |
| 782 | platform, |
| 783 | location, |
| 784 | build: source.build, |
| 785 | }); |
| 786 | yield* session.note(`Pushed ${imageUri}`); |
| 787 | return { imageUri, repositoryName, repositoryUri, codeHash }; |
| 788 | }); |
| 789 | |
| 790 | const hash = Effect.fn(function* (options: { |
| 791 | source: ImageSourceLike; |
| 792 | platform?: string; |
| 793 | port?: number; |
| 794 | isExternal?: boolean; |
| 795 | bootstrap: (importPath: string) => string; |
| 796 | }) { |
| 797 | const platform = options.platform ?? "linux/amd64"; |
| 798 | if (isNodeProgram(options.source, options.isExternal)) { |
| 799 | const { codeHash } = yield* computeNodeProgram({ |
| 800 | source: options.source as BundledImageSource, |
| 801 | port: options.port, |
| 802 | platform, |
| 803 | }); |
| 804 | return codeHash; |
| 805 | } |
| 806 | if (imageSourceKind(options.source) === "main") { |
| 807 | const df = options.source.dockerfile; |
| 808 | if (df !== undefined && isInlineDockerfile(df) && typeof df.content !== "string") { |
| 809 | return undefined; |
| 810 | } |
| 811 | const { codeHash } = yield* computeMainCodeHash({ |
| 812 | source: options.source as BundledImageSource, |
| 813 | isExternal: options.isExternal, |
| 814 | bootstrap: options.bootstrap, |
| 815 | port: options.port, |
| 816 | platform, |
| 817 | }); |
| 818 | return codeHash; |
| 819 | } |
| 820 | return yield* computeStaticSourceHash(options.source, platform); |
| 821 | }); |
| 822 | |
| 823 | return { |
| 824 | resolve, |
| 825 | hash, |
| 826 | destroyRepository: destroyImageRepository, |
| 827 | resourceName, |
| 828 | }; |
| 829 | }); |
| 830 | |
| 831 | export type ImageSource = Effect.Success<typeof makeImageSource>; |