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:
@@ -15,10 +15,13 @@
|
||||
# TWO JOBS
|
||||
# test go vet + go test, and a vite build of site/web. The same checks
|
||||
# as `make check` in site/.
|
||||
# publish main only, after test: builds the image, pushes it to this
|
||||
# Gitea's container registry as :<sha> and :latest, then pulls it
|
||||
# into the web_server stack (its `usaco` service) and waits for it
|
||||
# to be healthy — see "Deploying" in site/README.md.
|
||||
# publish main only, after test: builds the two images, the site
|
||||
# (Dockerfile) and its code runner (Dockerfile.runner), pushes
|
||||
# each to this Gitea's container registry as :<sha> and :latest,
|
||||
# then pulls them into the web_server stack (services
|
||||
# `usaco-runner`, then `usaco`) and waits for each to be healthy —
|
||||
# see "Deploying" in site/README.md. The runner goes first, so the
|
||||
# new site never starts against an old runner.
|
||||
#
|
||||
# SECRETS (repo settings -> Actions -> Secrets)
|
||||
# REGISTRY_USER a Gitea user that can write packages for adamf
|
||||
@@ -34,6 +37,7 @@ on:
|
||||
|
||||
env:
|
||||
IMAGE: adam-french.co.uk/adamf/usaco
|
||||
RUNNER_IMAGE: adam-french.co.uk/adamf/usaco-runner
|
||||
|
||||
jobs:
|
||||
test:
|
||||
@@ -61,7 +65,12 @@ jobs:
|
||||
# Read-only source, and named volumes for the module and build
|
||||
# caches, for the reasons given in web_server's deploy.yaml. git
|
||||
# is installed because internal/repo and internal/index test
|
||||
# against real repositories.
|
||||
# against real repositories. internal/runner's tests compile and
|
||||
# run C++ and Python with the image's g++ and python3 (it runs as
|
||||
# root, which the rlimits do not mind, and its sh is dash, whose
|
||||
# ulimit they are written for); clang-tidy and ruff are not in
|
||||
# it, so those tests skip, and the publish job's smoke test
|
||||
# covers them in the runner image.
|
||||
- name: Go - vet and test
|
||||
shell: bash
|
||||
run: |
|
||||
@@ -106,7 +115,7 @@ jobs:
|
||||
# the legacy builder, and is kept for any older daemon. This switches on BuildKit.
|
||||
DOCKER_BUILDKIT: "1"
|
||||
steps:
|
||||
- name: Build and push the image
|
||||
- name: Build the images
|
||||
shell: bash
|
||||
run: |
|
||||
set -eu
|
||||
@@ -116,14 +125,71 @@ jobs:
|
||||
# Built natively on the host, so the image is amd64 — the only
|
||||
# platform it runs on.
|
||||
docker build -t "$IMAGE:$SHA" -t "$IMAGE:latest" "$SRC/site"
|
||||
docker build -f "$SRC/site/Dockerfile.runner" -t "$RUNNER_IMAGE:$SHA" -t "$RUNNER_IMAGE:latest" "$SRC/site"
|
||||
|
||||
# The runner image is mostly other people's toolchains (g++,
|
||||
# python3, clang-tidy, ruff on musl), which `go test` never sees.
|
||||
# So before anything is pushed, start it with the production
|
||||
# hardening and run one C++ program and one lint through it,
|
||||
# from inside the container: the host has no curl, and busybox
|
||||
# wget is enough.
|
||||
- name: Smoke-test the runner image
|
||||
shell: bash
|
||||
run: |
|
||||
set -eu
|
||||
name="usaco-runner-ci-$SHA"
|
||||
docker run -d --name "$name" --read-only --tmpfs /tmp:size=256m,exec \
|
||||
--memory 1g --pids-limit 128 --cap-drop ALL --security-opt no-new-privileges \
|
||||
"$RUNNER_IMAGE:$SHA" >/dev/null
|
||||
trap 'docker rm -f "$name" >/dev/null' EXIT
|
||||
for i in $(seq 1 10); do
|
||||
docker exec "$name" wget -qO- http://localhost:8081/health && break
|
||||
sleep 1
|
||||
done
|
||||
post() { docker exec "$name" wget -qO- --header 'Content-Type: application/json' --post-data "$2" "http://localhost:8081/$1"; }
|
||||
run=$(post run '{"lang":"cpp","source":"#include <bits/stdc++.h>\nint main(){int a,b;std::cin>>a>>b;std::cout<<a+b;}","tests":[{"name":"1","input":"1 2","output":"3"}]}')
|
||||
echo "$run"
|
||||
echo "$run" | grep -q '"verdict":"AC"'
|
||||
lint=$(post lint '{"lang":"cpp","source":"#include <bits/stdc++.h>\nint main(){int a=1,b=2,c=3;return a==b==c;}"}')
|
||||
echo "$lint"
|
||||
echo "$lint" | grep -q '"source":"clang-tidy"'
|
||||
lint=$(post lint '{"lang":"python","source":"import os\n"}')
|
||||
echo "$lint"
|
||||
echo "$lint" | grep -q '"rule":"F401"'
|
||||
|
||||
- name: Push the images
|
||||
shell: bash
|
||||
run: |
|
||||
set -eu
|
||||
docker push "$IMAGE:$SHA"
|
||||
docker push "$IMAGE:latest"
|
||||
docker push "$RUNNER_IMAGE:$SHA"
|
||||
docker push "$RUNNER_IMAGE:latest"
|
||||
|
||||
- name: Remove the scratch directory
|
||||
if: always()
|
||||
shell: bash
|
||||
run: rm -rf "$SRC"
|
||||
|
||||
# The runner first, the same way as the site below, so the new
|
||||
# site never talks to an old runner. Pinning
|
||||
# USACO_RUNNER_IMAGE_TAG in the site's .env makes this a no-op.
|
||||
- name: Roll the runner onto the new image
|
||||
working-directory: /home/adamf/deploy/web_server
|
||||
shell: bash
|
||||
run: |
|
||||
set -eu
|
||||
flock .compose.lock sh -c 'docker compose pull usaco-runner && docker compose up -d --no-deps usaco-runner'
|
||||
for i in $(seq 1 30); do
|
||||
status=$(docker inspect -f '{{.State.Health.Status}}' usaco-runner)
|
||||
echo "usaco-runner: $status"
|
||||
[ "$status" = healthy ] && exit 0
|
||||
[ "$status" = unhealthy ] && break
|
||||
sleep 5
|
||||
done
|
||||
docker logs --tail 50 usaco-runner
|
||||
exit 1
|
||||
|
||||
# Put the new image live, exactly as job-tracker's workflow does
|
||||
# for `jobs`: pull + up of the one service, under .compose.lock, which
|
||||
# web_server's deploy.yaml and maintenance.yaml hold, then wait for the
|
||||
|
||||
Reference in New Issue
Block a user