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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ jobs:
working-directory: site
- name: Validate Hawk app contract
run: node scripts/deploy-check.mjs --contract >/dev/null
- name: Test Python runner
run: python3 -m unittest discover -t . -s runner -p "test_*.py" -v
87 changes: 86 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Publish container

# Build and smoke test run alongside CI; only the push waits for it.
# The steps live in FRC2713/hawk_suite/.github/workflows/publish-app.yml.
# The hawk-learn steps live in FRC2713/hawk_suite/.github/workflows/publish-app.yml.
# hawk-learn-runner is built, smoke-tested under hardened sandbox flags, and
# published to ghcr.io/frc2713/hawk-learn-runner on the same sha-<commit> tag.

on:
workflow_dispatch:
Expand All @@ -15,6 +17,7 @@ permissions:
jobs:
verify:
uses: ./.github/workflows/ci.yml

build:
uses: FRC2713/hawk_suite/.github/workflows/publish-app.yml@main
permissions:
Expand All @@ -23,6 +26,7 @@ jobs:
with:
app: hawk-learn
stage: build

push:
needs: [verify, build]
uses: FRC2713/hawk_suite/.github/workflows/publish-app.yml@main
Expand All @@ -32,3 +36,84 @@ jobs:
with:
app: hawk-learn
stage: push

build-runner:
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
env:
CANDIDATE: hawk-learn-runner-candidate:${{ github.sha }}
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- name: Build runner candidate
uses: docker/build-push-action@v6
with:
context: .
file: runner/Dockerfile
load: true
platforms: linux/amd64
tags: ${{ env.CANDIDATE }}
build-args: APP_REVISION=${{ github.sha }}
cache-from: type=gha,scope=runner
cache-to: type=gha,mode=max,scope=runner
- name: Smoke test runner candidate
env:
IMAGE: ${{ env.CANDIDATE }}
run: bash scripts/smoke-runner.sh
- name: Save tested runner image
run: docker save "$CANDIDATE" -o "$RUNNER_TEMP/runner-candidate.tar"
- uses: actions/upload-artifact@v4
with:
name: hawk-learn-runner-candidate
path: ${{ runner.temp }}/runner-candidate.tar
retention-days: 1
compression-level: 1
if-no-files-found: error

push-runner:
needs: [verify, build-runner]
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
packages: write
env:
CANDIDATE: hawk-learn-runner-candidate:${{ github.sha }}
steps:
- uses: actions/download-artifact@v4
with:
name: hawk-learn-runner-candidate
path: ${{ runner.temp }}
- name: Load tested runner image
run: docker load -i "$RUNNER_TEMP/runner-candidate.tar"
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Publish tested runner image
env:
REVISION: ${{ github.sha }}
run: |
set -euo pipefail
if [[ $GITHUB_REF != refs/heads/main && $GITHUB_REF != refs/tags/v* ]]; then
echo "Refusing to publish from $GITHUB_REF; run from main or a v* tag." >&2
exit 1
fi
image="ghcr.io/frc2713/hawk-learn-runner"
docker tag "$CANDIDATE" "$image:sha-$REVISION"
docker push "$image:sha-$REVISION"
digest=$(docker buildx imagetools inspect "$image:sha-$REVISION" --format '{{.Manifest.Digest}}')
[[ $digest =~ ^sha256:[a-f0-9]{64}$ ]]
echo "Published $image@$digest from $REVISION" >> "$GITHUB_STEP_SUMMARY"
if [[ $GITHUB_REF == refs/heads/main ]]; then
aliases=(main latest)
else
aliases=("${GITHUB_REF#refs/tags/v}")
fi
for tag in "${aliases[@]}"; do
docker tag "$CANDIDATE" "$image:$tag"
docker push "$image:$tag"
done
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

__pycache__/
*.pyc
1 change: 1 addition & 0 deletions .hawk/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"optional_env": [
"PORT",
"HOST",
"RUNNER_URL",
"HAWK_SUITE_ISSUER",
"HAWK_SUITE_TRUST_PROXY",
"HAWK_SUITE_TEAM_ID"
Expand Down
36 changes: 36 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
hawk-learn:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
APP_URL: http://localhost:3000
RUNNER_URL: http://hawk-learn-runner:4000
networks:
- default
- learn_runner
depends_on:
- hawk-learn-runner

hawk-learn-runner:
build:
context: .
dockerfile: runner/Dockerfile
networks:
- learn_runner
read_only: true
tmpfs:
- /tmp:rw,noexec,nosuid,nodev,size=32m
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
pids_limit: 64
mem_limit: 256m
cpus: 1.0

networks:
learn_runner:
internal: true
23 changes: 23 additions & 0 deletions runner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# syntax=docker/dockerfile:1
FROM python:3.12-slim

ARG APP_REVISION=development
ENV APP_REVISION=${APP_REVISION}
LABEL org.opencontainers.image.revision=${APP_REVISION}

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
HOST=0.0.0.0 \
PORT=4000

WORKDIR /opt/runner
COPY runner/maze_solver.py runner/maze_driver.py runner/server.py ./
RUN chmod 0555 /opt/runner && chmod 0444 /opt/runner/*.py

USER 65534:65534
EXPOSE 4000

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD python3 -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:4000/health', timeout=2)"

CMD ["python3", "-I", "/opt/runner/server.py"]
Empty file added runner/__init__.py
Empty file.
64 changes: 64 additions & 0 deletions runner/maze_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Trusted driver executed inside the sandboxed subprocess for maze runs.

Reads the student's solution.py, runs solve(robot) or solve(maze, robot) with
exact solution.py line numbers in tracebacks, and writes the JSON trail to a
dedicated pipe fd so student stdout never collides with the trail payload.
"""

from __future__ import annotations

import json
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from maze_solver import Cell, Direction, GridMaze, Robot # noqa: E402


def main() -> None:
solution_path = sys.argv[1]
grid_path = sys.argv[2]
mode = sys.argv[3]
trail_fd = int(sys.argv[4])

with open(grid_path, "r", encoding="utf-8") as f:
grid = json.load(f)
with open(solution_path, "r", encoding="utf-8") as f:
source = f.read()

maze = GridMaze(grid)
robot = maze.robot()

namespace: dict[str, object] = {
"__name__": "__main__",
"__file__": "solution.py",
"Direction": Direction,
"Cell": Cell,
"Robot": Robot,
"GridMaze": GridMaze,
}
compiled = compile(source, "solution.py", "exec")
exec(compiled, namespace)

solve_fn = namespace.get("solve")
if not callable(solve_fn):
raise RuntimeError(
"Expected a `solve(maze, robot)` function in your code."
if mode == "astar"
else "Expected a `solve(robot)` function in your code."
)

if mode == "astar":
solve_fn(maze, robot)
else:
solve_fn(robot)

# Convert [row, col] to [x, y] = [col, row] for the frontend MazePlayground.
trail_xy = [[col, row] for row, col in robot.trail()]
payload = json.dumps(trail_xy).encode("utf-8")
with os.fdopen(trail_fd, "wb") as out:
out.write(payload)


if __name__ == "__main__":
main()
Loading
Loading