Skip to content

Commit e0125a5

Browse files
net0wellclaude
andcommitted
feat(hosts): imagens Docker do worker e do painel (spec 009)
- Dockerfiles multi-stage do WorkerHost e do DashboardHost. A imagem final é aspnet:10.0 com o uv 0.13.4 (D-173) e usuário não-root psr (uid 10001). Os volumes são /var/lib/psr/envs (com o cache do uv), /var/lib/psr/python e /tmp/psr. PSR_PREINSTALL_PYTHON é opcional. O build compila no SDK 10.0.402 na arquitetura da máquina e publica para a de destino, então o multi-arch não depende de emulação. - O wheel do psrunner do mesmo commit vai na imagem (D-169); o entrypoint aponta PsrunnerRequirement para ele, a menos que a variável venha definida. - Dashboard: o painel é compilado numa etapa Node 24 e o MSBuild só embute o dist (-p:PsrPanelPrebuilt=true). - Core: o padrão de EnvironmentCacheRoot criava ~/.local/share só de construir as opções. Num contêiner com sistema de arquivos somente leitura isso derrubava o host. - Testes (Linux): PythonScriptRunner.Containers.Tests cobre AC-7 (não-root, raiz somente leitura, health checks, psrunner embutido, painel servido), AC-8 (dois workers com PSR_AUTO_MIGRATE aplicam as migrações uma vez) e worker ≤ 400 MB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1ddbc77 commit e0125a5

11 files changed

Lines changed: 418 additions & 3 deletions

File tree

‎.dockerignore‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
**/bin/
2+
**/obj/
3+
**/node_modules/
4+
**/.venv/
5+
**/__pycache__/
6+
**/TestResults/
7+
panel/dist/
8+
.git/
9+
.github/
10+
.vs/
11+
.vscode/
12+
.idea/
13+
docs/
14+
tests/
15+
artifacts/
16+
*.user

‎PythonScriptRunner.slnx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<Project Path="src/PythonScriptRunner.Worker/PythonScriptRunner.Worker.csproj" />
1515
</Folder>
1616
<Folder Name="/tests/">
17+
<Project Path="tests/PythonScriptRunner.Containers.Tests/PythonScriptRunner.Containers.Tests.csproj" />
1718
<Project Path="tests/PythonScriptRunner.Core.Tests/PythonScriptRunner.Core.Tests.csproj" />
1819
<Project Path="tests/PythonScriptRunner.Dashboard.Tests/PythonScriptRunner.Dashboard.Tests.csproj" />
1920
<Project Path="tests/PythonScriptRunner.EntityFrameworkCore.Tests/PythonScriptRunner.EntityFrameworkCore.Tests.csproj" />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# syntax=docker/dockerfile:1
2+
# Imagem do DashboardHost (spec 009): API, painel Vue embutido e relay do outbox; inclui o uv porque publicar uma
3+
# Etapa resolve as dependências. Runtime ASP.NET 10, usuário não-root psr.
4+
# Build a partir da raiz do repositório: docker build -f hosts/PythonScriptRunner.DashboardHost/Dockerfile .
5+
ARG UV_VERSION=0.13.4
6+
# Mesma faixa do global.json (10.0.4xx); a tag 10.0 ainda aponta para 10.0.3xx.
7+
ARG DOTNET_SDK=10.0.402
8+
9+
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv
10+
11+
# psrunner do mesmo commit (D-169): a imagem não depende do PyPI para o SDK.
12+
FROM --platform=$BUILDPLATFORM ghcr.io/astral-sh/uv:${UV_VERSION}-python3.12-trixie-slim AS psrunner
13+
WORKDIR /src
14+
COPY python/psrunner/ .
15+
RUN uv build --wheel --out-dir /wheel
16+
17+
# Painel Vue (spec 008; D-155, D-158).
18+
FROM --platform=$BUILDPLATFORM node:24-bookworm-slim AS panel
19+
WORKDIR /panel
20+
COPY panel/package.json panel/package-lock.json ./
21+
RUN npm ci --no-audit --no-fund
22+
COPY panel/ .
23+
RUN npm run build
24+
25+
# Compila na arquitetura da máquina e publica para a de destino (multi-arch sem emulação); o painel já vem pronto.
26+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:${DOTNET_SDK} AS build
27+
ARG TARGETARCH
28+
ARG VERSION=0.0.0-docker
29+
WORKDIR /src
30+
COPY global.json Directory.Build.props Directory.Packages.props .editorconfig ./
31+
COPY src/ src/
32+
COPY schemas/ schemas/
33+
COPY --from=panel /panel/dist/ panel/dist/
34+
COPY hosts/Directory.Build.props hosts/
35+
COPY hosts/PythonScriptRunner.ServiceDefaults/ hosts/PythonScriptRunner.ServiceDefaults/
36+
COPY hosts/PythonScriptRunner.DashboardHost/ hosts/PythonScriptRunner.DashboardHost/
37+
RUN arch=$([ "$TARGETARCH" = "arm64" ] && echo arm64 || echo x64) \
38+
&& dotnet publish hosts/PythonScriptRunner.DashboardHost -c Release -a "$arch" --self-contained false -o /app \
39+
-p:MinVerVersionOverride="$VERSION" -p:PsrPanelPrebuilt=true
40+
41+
FROM mcr.microsoft.com/dotnet/aspnet:10.0
42+
COPY --from=uv /uv /uvx /usr/local/bin/
43+
RUN groupadd --gid 10001 psr \
44+
&& useradd --uid 10001 --gid psr --home-dir /var/lib/psr --no-create-home --shell /usr/sbin/nologin psr \
45+
&& mkdir -p /var/lib/psr/envs /var/lib/psr/python /tmp/psr /opt/psr \
46+
&& chown -R psr:psr /var/lib/psr /tmp/psr
47+
COPY --from=psrunner /wheel/ /opt/psr/
48+
COPY --chmod=755 hosts/docker/entrypoint.sh /opt/psr/entrypoint.sh
49+
ENV HOME=/var/lib/psr \
50+
ASPNETCORE_HTTP_PORTS=8080 \
51+
UV_PYTHON_INSTALL_DIR=/var/lib/psr/python \
52+
UV_CACHE_DIR=/var/lib/psr/envs/.uv-cache \
53+
PythonScriptRunner__UvPath=/usr/local/bin/uv \
54+
PythonScriptRunner__WorkRoot=/tmp/psr \
55+
PythonScriptRunner__EnvironmentCacheRoot=/var/lib/psr/envs
56+
USER psr
57+
WORKDIR /app
58+
COPY --from=build /app .
59+
EXPOSE 8080
60+
VOLUME ["/var/lib/psr/envs", "/var/lib/psr/python", "/tmp/psr"]
61+
ENTRYPOINT ["/opt/psr/entrypoint.sh", "dotnet", "PythonScriptRunner.DashboardHost.dll"]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# syntax=docker/dockerfile:1
2+
# Imagem do WorkerHost (spec 009): runtime ASP.NET 10 + uv + psrunner do mesmo commit, usuário não-root psr.
3+
# Build a partir da raiz do repositório: docker build -f hosts/PythonScriptRunner.WorkerHost/Dockerfile .
4+
ARG UV_VERSION=0.13.4
5+
# Mesma faixa do global.json (10.0.4xx); a tag 10.0 ainda aponta para 10.0.3xx.
6+
ARG DOTNET_SDK=10.0.402
7+
8+
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv
9+
10+
# psrunner do mesmo commit (D-169): a imagem não depende do PyPI para o SDK.
11+
FROM --platform=$BUILDPLATFORM ghcr.io/astral-sh/uv:${UV_VERSION}-python3.12-trixie-slim AS psrunner
12+
WORKDIR /src
13+
COPY python/psrunner/ .
14+
RUN uv build --wheel --out-dir /wheel
15+
16+
# Compila na arquitetura da máquina e publica para a de destino (multi-arch sem emulação).
17+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:${DOTNET_SDK} AS build
18+
ARG TARGETARCH
19+
ARG VERSION=0.0.0-docker
20+
WORKDIR /src
21+
COPY global.json Directory.Build.props Directory.Packages.props .editorconfig ./
22+
COPY src/ src/
23+
COPY schemas/ schemas/
24+
COPY hosts/Directory.Build.props hosts/
25+
COPY hosts/PythonScriptRunner.ServiceDefaults/ hosts/PythonScriptRunner.ServiceDefaults/
26+
COPY hosts/PythonScriptRunner.WorkerHost/ hosts/PythonScriptRunner.WorkerHost/
27+
RUN arch=$([ "$TARGETARCH" = "arm64" ] && echo arm64 || echo x64) \
28+
&& dotnet publish hosts/PythonScriptRunner.WorkerHost -c Release -a "$arch" --self-contained false -o /app \
29+
-p:MinVerVersionOverride="$VERSION"
30+
31+
FROM mcr.microsoft.com/dotnet/aspnet:10.0
32+
# Pré-aquecimento opcional: --build-arg PSR_PREINSTALL_PYTHON=3.12 instala o intérprete na imagem.
33+
ARG PSR_PREINSTALL_PYTHON=
34+
COPY --from=uv /uv /uvx /usr/local/bin/
35+
RUN groupadd --gid 10001 psr \
36+
&& useradd --uid 10001 --gid psr --home-dir /var/lib/psr --no-create-home --shell /usr/sbin/nologin psr \
37+
&& mkdir -p /var/lib/psr/envs /var/lib/psr/python /tmp/psr /opt/psr \
38+
&& chown -R psr:psr /var/lib/psr /tmp/psr
39+
COPY --from=psrunner /wheel/ /opt/psr/
40+
COPY --chmod=755 hosts/docker/entrypoint.sh /opt/psr/entrypoint.sh
41+
ENV HOME=/var/lib/psr \
42+
ASPNETCORE_HTTP_PORTS=8080 \
43+
UV_PYTHON_INSTALL_DIR=/var/lib/psr/python \
44+
UV_CACHE_DIR=/var/lib/psr/envs/.uv-cache \
45+
PythonScriptRunner__UvPath=/usr/local/bin/uv \
46+
PythonScriptRunner__WorkRoot=/tmp/psr \
47+
PythonScriptRunner__EnvironmentCacheRoot=/var/lib/psr/envs
48+
USER psr
49+
RUN if [ -n "$PSR_PREINSTALL_PYTHON" ]; then uv python install "$PSR_PREINSTALL_PYTHON"; fi
50+
WORKDIR /app
51+
COPY --from=build /app .
52+
EXPOSE 8080
53+
VOLUME ["/var/lib/psr/envs", "/var/lib/psr/python", "/tmp/psr"]
54+
ENTRYPOINT ["/opt/psr/entrypoint.sh", "dotnet", "PythonScriptRunner.WorkerHost.dll"]

‎hosts/docker/entrypoint.sh‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
# Entrada das imagens dos hosts (spec 009): usa o psrunner embutido na imagem (D-169), a menos que
3+
# PythonScriptRunner__PsrunnerRequirement já venha definido (por exemplo, um índice privado).
4+
set -e
5+
if [ -z "${PythonScriptRunner__PsrunnerRequirement:-}" ]; then
6+
for wheel in /opt/psr/psrunner-*.whl; do
7+
PythonScriptRunner__PsrunnerRequirement="$wheel"
8+
done
9+
export PythonScriptRunner__PsrunnerRequirement
10+
fi
11+
12+
exec "$@"

‎src/PythonScriptRunner.Core/PythonScriptRunnerOptions.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ public sealed class PythonScriptRunnerOptions
77
public string WorkRoot { get; set; } = Path.Combine(Path.GetTempPath(), "psr");
88

99
/// <summary>Folder where Python environments (venvs) and lock files are cached.</summary>
10+
/// <remarks>The default only names the folder: it is created when the first environment needs it (a container with a
11+
/// read-only file system sets another root and must not fail on the default).</remarks>
1012
public string EnvironmentCacheRoot { get; set; } = Path.Combine(
11-
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create), "psr");
13+
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify), "psr");
1214

1315
/// <summary>Path or name of the <c>uv</c> executable. Default: <c>uv</c> (searched in PATH).</summary>
1416
public string UvPath { get; set; } = "uv";

‎src/PythonScriptRunner.Dashboard/PythonScriptRunner.Dashboard.csproj‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
<!--
2727
The panel (spec 008) is embedded as wwwroot (D-155): Release builds and packs run `npm ci` + `npm run build` in
2828
panel/ and embed panel/dist; other builds embed the placeholder page in wwwroot/, so Node is only needed to pack.
29-
Force either way with -p:PsrBuildPanel=true|false.
29+
Force either way with -p:PsrBuildPanel=true|false. -p:PsrPanelPrebuilt=true embeds an existing panel/dist without
30+
running npm (the image build compiles the panel in a Node stage).
3031
-->
3132
<PropertyGroup>
3233
<PsrPanelRoot>$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)', '..', '..', 'panel'))</PsrPanelRoot>
@@ -38,7 +39,7 @@
3839
<PsrPanelSources Include="$(PsrPanelRoot)src\**\*;$(PsrPanelRoot)public\**\*;$(PsrPanelRoot)index.html;$(PsrPanelRoot)package-lock.json;$(PsrPanelRoot)vite.config.ts" Exclude="$(PsrPanelRoot)src\**\*.spec.ts" />
3940
</ItemGroup>
4041

41-
<Target Name="PsrBuildPanel" Condition="'$(PsrBuildPanel)' == 'true'" Inputs="@(PsrPanelSources)" Outputs="$(PsrPanelRoot)dist\index.html">
42+
<Target Name="PsrBuildPanel" Condition="'$(PsrBuildPanel)' == 'true' and '$(PsrPanelPrebuilt)' != 'true'" Inputs="@(PsrPanelSources)" Outputs="$(PsrPanelRoot)dist\index.html">
4243
<Exec Command="npm ci --no-audit --no-fund" WorkingDirectory="$(PsrPanelRoot)" Condition="!Exists('$(PsrPanelRoot)node_modules\.package-lock.json')" />
4344
<Exec Command="npm run build" WorkingDirectory="$(PsrPanelRoot)" />
4445
</Target>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Net;
2+
using System.Reflection;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
using PythonScriptRunner.Containers.Tests.Infrastructure;
5+
using PythonScriptRunner.EntityFrameworkCore;
6+
7+
namespace PythonScriptRunner.Containers.Tests;
8+
9+
/// <summary>Spec 009: the worker and dashboard images as they run in production.</summary>
10+
public sealed class HostImageTests(HostStack stack)
11+
{
12+
private const long MaxWorkerImageBytes = 400L * 1024 * 1024;
13+
14+
private static CancellationToken Ct => TestContext.Current.CancellationToken;
15+
16+
[Fact]
17+
public async Task Spec009_AC07_BothImages_RunAsNonRootOnAReadOnlyFileSystem_AndAnswerTheHealthChecks()
18+
{
19+
var database = await stack.CreateDatabaseAsync("ac07");
20+
await using var worker = stack.Host(stack.WorkerImage, database);
21+
await using var dashboard = stack.Host(stack.DashboardImage, database, HostStack.DashboardUsers());
22+
23+
await worker.StartAsync(Ct);
24+
await dashboard.StartAsync(Ct);
25+
26+
foreach (var host in new[] { worker, dashboard })
27+
{
28+
using var http = Client(host);
29+
(await http.GetAsync("/health/live", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK);
30+
(await http.GetAsync("/health/ready", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK);
31+
(await host.ExecAsync(["id", "-u"], Ct)).Stdout.Trim().ShouldBe("10001");
32+
(await host.ExecAsync(["sh", "-c", "touch /app/escrita"], Ct)).ExitCode.ShouldNotBe(0, "the root file system is read-only");
33+
34+
// The psrunner wheel of the same commit (D-169) is what the host installs in each environment.
35+
var environment = (await host.ExecAsync(["sh", "-c", "tr '\\0' '\\n' < /proc/1/environ"], Ct)).Stdout;
36+
environment.ShouldContain("PythonScriptRunner__PsrunnerRequirement=/opt/psr/psrunner-");
37+
}
38+
39+
using var panel = Client(dashboard);
40+
(await panel.GetStringAsync("/psr/", Ct)).ShouldContain("id=\"app\"", customMessage: "the image embeds the built panel");
41+
}
42+
43+
[Fact]
44+
public async Task Spec009_AC08_TwoWorkersWithAutoMigrateStartingTogether_ApplyTheMigrationsOnce()
45+
{
46+
var database = await stack.CreateDatabaseAsync("ac08");
47+
await using var first = stack.Host(stack.WorkerImage, database);
48+
await using var second = stack.Host(stack.WorkerImage, database);
49+
50+
await Task.WhenAll(first.StartAsync(Ct), second.StartAsync(Ct));
51+
52+
var migrations = typeof(PsrDbContext).Assembly.GetTypes().Count(t => t.GetCustomAttribute<MigrationAttribute>() is not null);
53+
migrations.ShouldBeGreaterThan(0);
54+
var applied = await stack.ScalarAsync(database, $"SELECT count(*) FROM {PsrDbContext.Schema}.{PsrDbContext.MigrationsHistoryTable}");
55+
applied.ShouldBe(migrations.ToString(System.Globalization.CultureInfo.InvariantCulture));
56+
foreach (var worker in new[] { first, second })
57+
{
58+
var (stdout, stderr) = await worker.GetLogsAsync(ct: Ct);
59+
(stdout + stderr).ShouldNotContain("Hosting failed to start");
60+
}
61+
}
62+
63+
[Fact]
64+
public async Task Spec009_WorkerImage_IsAtMost400MBWithoutPythonInterpreters()
65+
{
66+
var (code, output) = await HostImages.DockerAsync(["image", "inspect", "--format", "{{.Size}}", stack.WorkerImage], Ct);
67+
68+
code.ShouldBe(0, output);
69+
long.Parse(output.Trim(), System.Globalization.CultureInfo.InvariantCulture).ShouldBeLessThanOrEqualTo(MaxWorkerImageBytes);
70+
}
71+
72+
private static HttpClient Client(IContainer host) =>
73+
new() { BaseAddress = new Uri($"http://{host.Hostname}:{host.GetMappedPublicPort(HostStack.HttpPort)}") };
74+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Diagnostics;
2+
3+
namespace PythonScriptRunner.Containers.Tests.Infrastructure;
4+
5+
/// <summary>
6+
/// The host images under test (spec 009): <c>PSR_TEST_WORKER_IMAGE</c> / <c>PSR_TEST_DASHBOARD_IMAGE</c> when the CI already
7+
/// built them, otherwise built here from the Dockerfiles with <c>docker build</c> (BuildKit).
8+
/// </summary>
9+
public static class HostImages
10+
{
11+
public const string Worker = "worker";
12+
13+
public const string Dashboard = "dashboard";
14+
15+
private static readonly string Root = FindRoot();
16+
17+
public static async Task<string> ResolveAsync(string host, CancellationToken cancellationToken)
18+
{
19+
var variable = host == Worker ? "PSR_TEST_WORKER_IMAGE" : "PSR_TEST_DASHBOARD_IMAGE";
20+
if (Environment.GetEnvironmentVariable(variable) is { Length: > 0 } image)
21+
{
22+
return image;
23+
}
24+
25+
var tag = $"psr-{host}:test";
26+
var project = host == Worker ? "PythonScriptRunner.WorkerHost" : "PythonScriptRunner.DashboardHost";
27+
var (code, output) = await DockerAsync(["build", "-f", Path.Combine("hosts", project, "Dockerfile"), "-t", tag, "."], cancellationToken);
28+
code.ShouldBe(0, $"docker build of {tag} failed:\n{output}");
29+
return tag;
30+
}
31+
32+
/// <summary>Runs the docker CLI in the repository root and returns the exit code with stdout and stderr.</summary>
33+
public static async Task<(int Code, string Output)> DockerAsync(IReadOnlyList<string> arguments, CancellationToken cancellationToken)
34+
{
35+
var start = new ProcessStartInfo("docker") { WorkingDirectory = Root, RedirectStandardOutput = true, RedirectStandardError = true };
36+
foreach (var argument in arguments)
37+
{
38+
start.ArgumentList.Add(argument);
39+
}
40+
41+
using var process = Process.Start(start)!;
42+
var stdout = process.StandardOutput.ReadToEndAsync(cancellationToken);
43+
var stderr = process.StandardError.ReadToEndAsync(cancellationToken);
44+
await process.WaitForExitAsync(cancellationToken);
45+
return (process.ExitCode, await stdout + await stderr);
46+
}
47+
48+
private static string FindRoot()
49+
{
50+
for (var directory = new DirectoryInfo(AppContext.BaseDirectory); directory is not null; directory = directory.Parent)
51+
{
52+
if (File.Exists(Path.Combine(directory.FullName, "PythonScriptRunner.slnx")))
53+
{
54+
return directory.FullName;
55+
}
56+
}
57+
58+
throw new InvalidOperationException("The repository root (PythonScriptRunner.slnx) was not found.");
59+
}
60+
}

0 commit comments

Comments
 (0)