Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 3 additions & 32 deletions packages/alchemy/src/AWS/ECS/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as Provider from "../../Provider.ts";
import { Resource, type ResourceBinding } from "../../Resource.ts";
import type { RuntimeContext } from "../../RuntimeContext.ts";
import {
createHostRuntimeContext,
createContainerRuntimeContext,
type HostRuntimeContext,
type ServerHost,
} from "../../Server/Process.ts";
Expand Down Expand Up @@ -309,37 +309,8 @@ export interface TaskRuntimeContext extends HostRuntimeContext {
readonly Type: "AWS.ECS.Task";
}

/**
* Host runtime context for ECS container platforms: extends the shared
* process host context so an impl shape's `run` effect is registered as a
* one-shot runner (the container exits when it completes) and the HTTP
* server only boots when the impl actually declares a `fetch` handler.
*/
export const createContainerRuntimeContext =
(type: string) =>
(id: string): HostRuntimeContext => {
const base = createHostRuntimeContext(type)(id);
// Capture the host serve BEFORE Object.assign overwrites `base.serve`
// with the wrapper below — calling `base.serve` inside the wrapper would
// resolve to the wrapper itself (property lookup happens at call time)
// and recurse without bound the moment an impl declares `fetch`.
const serveBase = base.serve;
const serve: HostRuntimeContext["serve"] = (handler, options) =>
Effect.gen(function* () {
const shape = options?.shape;
const run = shape?.run;
if (Effect.isEffect(run)) {
yield* base.run(run as Effect.Effect<void, never, any>);
}
// Boot the HTTP server only for an impl that declared `fetch` — a
// pure one-shot `{ run }` program must exit when `run` completes
// rather than parking behind the 404 fallback server forever.
if (shape === undefined || shape.fetch !== undefined) {
yield* serveBase(handler, options);
}
}) as Effect.Effect<void, never, never>;
return Object.assign(base, { serve });
};
// Shared with `Docker.Service`; lives beside the host runtime context.
export { createContainerRuntimeContext } from "../../Server/Process.ts";

/**
* A Fargate task definition with a container image from one of three
Expand Down
70 changes: 55 additions & 15 deletions packages/alchemy/src/Docker/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import * as Provider from "../Provider.ts";
import { Resource } from "../Resource.ts";
import { createInternalTags, hasAlchemyTags } from "../Tags.ts";
import { toSeconds } from "../Util/Duration.ts";
import { Docker, dockerPhysicalName } from "./Docker.ts";
import { Docker, dockerContextName, dockerPhysicalName } from "./Docker.ts";
import type { Providers } from "./Providers.ts";

export interface ContainerProps {
/** Image reference or Docker image resource. */
image: Container.Image;
/** Docker context name or context resource. */
context?: Docker.ContextRef;
/**
* Container name.
*
Expand Down Expand Up @@ -187,6 +189,18 @@ export interface Container extends Resource<
* stopTimeout: "30 seconds",
* start: true,
* });
*
* @example Use a Docker.Context resource
* ```typescript
* const remote = yield* Docker.Context("remote", {
* name: "remote-build",
* docker: "host=ssh://docker@example.com",
* });
*
* const api = yield* Docker.Container("api", {
* image: "nginx:alpine",
* context: remote,
* });
* ```
*/
export const Container = Resource<Container>("Docker.Container");
Expand All @@ -199,9 +213,12 @@ export const Container = Resource<Container>("Docker.Container");
*/
export const inspectContainer = (
name: string,
context?: Docker.ContextRef,
): Effect.Effect<Container["Attributes"], PlatformError, Docker> =>
Docker.pipe(
Effect.flatMap((docker) => docker.container.inspect(name)),
Effect.flatMap((docker) =>
docker.container.inspect(name, dockerContextName(context)),
),
Effect.map((container) =>
toContainerAttributes(container, container.Config.Image),
),
Expand All @@ -217,6 +234,7 @@ export const ContainerProvider = () =>
live: Docker.Container,
news: ContainerProps,
) {
const context = dockerContextName(news.context);
const connect = new Map<string, Container.NetworkMapping>();
const disconnect = new Set<string>();
const noop = new Set<string>();
Expand All @@ -241,7 +259,7 @@ export const ContainerProvider = () =>
yield* Effect.forEach(
disconnect,
(network) =>
docker.network.disconnect({ network, container: live.Id }),
docker.network.disconnect({ network, container: live.Id, context }),
{ concurrency: "unbounded" },
);
yield* Effect.forEach(
Expand All @@ -251,6 +269,7 @@ export const ContainerProvider = () =>
network: network.name,
container: live.Id,
alias: network.aliases,
context,
}),
{ concurrency: "unbounded" },
);
Expand All @@ -259,9 +278,10 @@ export const ContainerProvider = () =>
return Container.Provider.of({
list: () => Effect.succeed([]),
read: Effect.fn(function* ({ id, instanceId, olds, output }) {
const context = dockerContextName(olds.context);
const name = yield* dockerPhysicalName(id, olds, instanceId);
const info = yield* docker.container
.inspect(name)
.inspect(name, context)
.pipe(
Effect.catchReason(
"PlatformError",
Expand Down Expand Up @@ -294,6 +314,11 @@ export const ContainerProvider = () =>
// round-trip (it deserializes as `undefined`) — without comparable
// prior create args, let the engine apply its default update logic.
if (olds.image === undefined) return undefined;
if (
dockerContextName(olds.context) !== dockerContextName(news.context)
) {
return { action: "replace" as const, deleteFirst: true };
}
const oldArgs = yield* makeCreateArgs(id, olds, instanceId);
const newArgs = yield* makeCreateArgs(id, news, instanceId);
if (!Equal.equals(oldArgs, newArgs)) {
Expand All @@ -307,9 +332,10 @@ export const ContainerProvider = () =>
}
}),
reconcile: Effect.fn(function* ({ id, instanceId, news }) {
const context = dockerContextName(news.context);
const args = yield* makeCreateArgs(id, news, instanceId);
const live = yield* docker.container
.inspect(args.name)
.inspect(args.name, context)
.pipe(
Effect.catchReason(
"PlatformError",
Expand All @@ -321,12 +347,12 @@ export const ContainerProvider = () =>
if (live) {
yield* reconcileNetworks(live, news);
if (news.start && live.State.Status !== "running") {
yield* docker.container.start(live.Id);
yield* docker.container.start(live.Id, context);
} else if (!news.start && live.State.Status === "running") {
yield* docker.container.stop(live.Id);
yield* docker.container.stop(live.Id, context);
}
return yield* docker.container
.inspect(live.Id)
.inspect(live.Id, context)
.pipe(
Effect.map((info) => toContainerAttributes(info, args.image)),
);
Expand All @@ -335,6 +361,7 @@ export const ContainerProvider = () =>
const internalTags = yield* createInternalTags(id);
const { stdout: containerId } = yield* docker.container.create({
...args,
context,
label: { ...args.label, ...internalTags },
});
yield* Effect.forEach(
Expand All @@ -344,20 +371,33 @@ export const ContainerProvider = () =>
network: network.name,
container: containerId,
alias: network.aliases,
context,
}),
{ concurrency: "unbounded" },
);
if (news.start) {
yield* docker.container.start(containerId);
yield* docker.container.start(containerId, context);
}
const info = yield* docker.container.inspect(containerId);
const info = yield* docker.container.inspect(containerId, context);
return toContainerAttributes(info, args.image);
}),
delete: Effect.fn(({ output }) =>
docker.container.stop(output.name).pipe(
Effect.andThen(docker.container.remove(output.name, true)),
Effect.catchReason("PlatformError", "NotFound", () => Effect.void),
),
delete: Effect.fn(({ olds, output }) =>
docker.container
.stop(output.name, dockerContextName(olds.context))
.pipe(
Effect.andThen(
docker.container.remove(
output.name,
true,
dockerContextName(olds.context),
),
),
Effect.catchReason(
"PlatformError",
"NotFound",
() => Effect.void,
),
),
),
});
}),
Expand Down
Loading