cyberdeck-urt/deploy.py
orca 0a99c4d0a8 cyberdeck-urt: endless Urban Terror bot arena (server image, companion, deploy)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:22:54 -05:00

69 lines
3.6 KiB
Python
Executable file

#!/usr/bin/env python3
"""Build and (re)create the Urban Terror arena's containers on the Mac mini, through Portainer.
deploy.py server the game server, `urt` (UDP 27961; the game lives in the urt-data volume)
deploy.py companion the stats collector, `urt-companion` (pushes to the site every 2 minutes)
deploy.py both
The build context is tarred up and sent to Portainer's /build, so the Mac mini needs no checkout,
SSH or registry (the same way the Q3 arena and its companion deploy). Shared helpers and the house
rules (CPU/memory caps, low CPU priority, capped logs) come from cyberdeck/macmini/lib/portainer.py.
Secrets (RCON password, site ingest token) are generated once into the Mac mini credentials file
and only travel in API request bodies; nothing is printed.
"""
import io, json, os, sys, tarfile
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "cyberdeck", "macmini", "lib"))
from portainer import call, container, secret # noqa: E402
PORT = 27961
SITE_URL = "http://192.168.8.154:3008" # urt-arena-site (cyberdeck/macmini/urtsite/deploy.py)
TZ = "EST5EDT,M3.2.0,M11.1.0" # same clock as the Q3 arena's seasons and hour chart
def build(image, files):
"""files: {path in the build context: local path}."""
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as t:
for arc, path in files.items():
t.add(path, arc)
st, out = call("POST", f"/build?t={image}&rm=1&forcerm=1&pull=1", buf.getvalue(), "application/x-tar", timeout=1800)
lines = [json.loads(l) for l in out.decode(errors="replace").splitlines() if l.strip().startswith("{")]
err = next((l["error"] for l in lines if "error" in l), None)
if st != 200 or err:
sys.exit(f"build {image} failed ({st}): {err or out[-400:]}")
print("image built:", image)
def server():
src = os.path.join(HERE, "server")
build("cyberdeck/urt:latest", {f: os.path.join(src, f) for f in
("Dockerfile", "entrypoint.sh", "cyberdeck-urt.cfg", "mapcycle.txt")})
call("POST", "/volumes/create", {"Name": "urt-data"})
container("urt", "cyberdeck/urt:latest", cpus=2, mem_mb=768, ports={f"{PORT}/udp": PORT},
binds=["urt-data:/data"], env=[f"RCON_PASSWORD={secret('urt_rcon')}", f"TZ={TZ}"],
labels={"com.centurylinklabs.watchtower.enable": "false",
"homepage.description": "Urban Terror bot arena (UDP 27961)"})
# the game server is the point of the box: give it normal CPU priority, like the Q3 server
call("POST", "/containers/urt/update", {"CpuShares": 1024})
def companion():
src = os.path.join(HERE, "companion")
build("cyberdeck/urt-companion:latest", {f: os.path.join(src, f) for f in
("Dockerfile", "companion.py", "stats.py", "live.py")})
call("POST", "/volumes/create", {"Name": "urt-companion-state"})
container("urt-companion", "cyberdeck/urt-companion:latest", cpus=0.5, mem_mb=256,
binds=["urt-companion-state:/state"],
env=[f"CYBERDECK_PORTAINER_TOKEN={open(os.path.expanduser('~/.config/herdr/plugins/config/cyberdeck.docker/token')).read().strip()}",
f"CYBERDECK_SITE_URL={SITE_URL}", f"CYBERDECK_SITE_TOKEN={secret('urt_site_token', 32)}",
f"URT_RCON={secret('urt_rcon')}", f"TZ={TZ}"],
labels={"com.centurylinklabs.watchtower.enable": "false"})
if __name__ == "__main__":
what = sys.argv[1] if len(sys.argv) > 1 else "all"
if what in ("server", "all"):
server()
if what in ("companion", "all"):
companion()