Run and lint solutions on the site
A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.
Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.
C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.
CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
# =============================================================================
|
||||
# The USACO site's code runner (cmd/usacorunner, internal/runner): compiles
|
||||
# and runs a solution against its tests, and lints it, for the solve page.
|
||||
# It runs whatever is submitted, so it is its own image and its own
|
||||
# container, apart from the site, which holds every secret. Two stages:
|
||||
#
|
||||
# build compiles the Go service, as in Dockerfile (go.mod/go.sum first,
|
||||
# so the dependency layer is cached across edits).
|
||||
# runtime alpine with the toolchains the service shells out to:
|
||||
# g++ + musl-dev builds C++ with the CLI's flags
|
||||
# python3 runs Python, and checks its syntax
|
||||
# clang-extra-tools clang-tidy, for lint (optional)
|
||||
# ruff Python lint (optional)
|
||||
# tini PID 1; see below
|
||||
# A missing linter only means less lint; see internal/runner.
|
||||
#
|
||||
# The build context is site/, like Dockerfile: no problems are in the image.
|
||||
# The site sends the source, its tests and any headers (debugging.h,
|
||||
# leetcode.h, driver.h) with each request.
|
||||
#
|
||||
# It runs as uid 1000 and writes only to $TMPDIR (/tmp), one directory per
|
||||
# request, which it removes afterwards. In production the container is
|
||||
# hardened around that (web_server's docker-compose.yml, service
|
||||
# usaco-runner):
|
||||
# read_only root, with a tmpfs at /tmp that must NOT be noexec, because the
|
||||
# compiled program runs from there; no network but the internal one the
|
||||
# site shares; no capabilities; no-new-privileges; mem_limit, cpus and a
|
||||
# pids_limit, which is what stops a fork bomb (the service does not set
|
||||
# RLIMIT_NPROC; limits.go says why).
|
||||
# Each program also gets its own rlimits (CPU, 512 MB address space, 64 MB
|
||||
# files) and a wall-clock limit, set by the service per test.
|
||||
#
|
||||
# tini is PID 1 because a program's orphans are re-parented to PID 1, and
|
||||
# the Go service never reaps processes it did not start: without an init, a
|
||||
# fork bomb's children stay zombies after the kill and hold the pids_limit
|
||||
# for good, and every later run fails to fork.
|
||||
# =============================================================================
|
||||
FROM golang:1.26 AS build
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN --mount=type=cache,target=/go/pkg/mod go mod download
|
||||
COPY cmd ./cmd
|
||||
COPY internal ./internal
|
||||
RUN --mount=type=cache,target=/go/pkg/mod \
|
||||
--mount=type=cache,target=/root/.cache/go-build \
|
||||
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/usacorunner ./cmd/usacorunner
|
||||
|
||||
FROM alpine:3.22
|
||||
RUN apk add --no-cache g++ musl-dev python3 clang-extra-tools ruff tini \
|
||||
&& adduser -D -H -u 1000 app
|
||||
COPY --from=build /out/usacorunner /app/usacorunner
|
||||
USER app
|
||||
ENV TMPDIR=/tmp
|
||||
EXPOSE 8081
|
||||
# busybox's wget, which is enough for plain HTTP on localhost.
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
||||
CMD wget -qO- http://localhost:8081/health || exit 1
|
||||
ENTRYPOINT ["/sbin/tini", "--"]
|
||||
CMD ["/app/usacorunner"]
|
||||
Reference in New Issue
Block a user