Fix HTML chart output path for nobody container user

Use MCNOAA_CHARTS_DIR env var (defaults to artifacts/charts/ for
local dev) so the container's nobody user can write HTML charts
to /tmp/charts instead of the read-only /app working directory.
This commit is contained in:
Ryan Malloy 2026-02-23 19:46:29 -07:00
parent c0826e1a36
commit 89cdeb0967
2 changed files with 11 additions and 2 deletions

View File

@ -15,6 +15,7 @@ WORKDIR /app
ENV UV_COMPILE_BYTECODE=1
ENV PATH="/app/.venv/bin:$PATH"
ENV MPLCONFIGDIR=/tmp/matplotlib
ENV MCNOAA_CHARTS_DIR=/tmp/charts
COPY --from=deps /app/.venv /app/.venv
COPY src/ src/

View File

@ -189,9 +189,17 @@ def register(mcp: FastMCP) -> None:
def _save_html(html: str, station_id: str, chart_type: str) -> Path:
"""Save HTML chart to artifacts/charts/ and return the path."""
"""Save HTML chart and return the path.
Uses $MCNOAA_CHARTS_DIR if set, otherwise falls back to
artifacts/charts/ (relative to cwd). The container sets
MCNOAA_CHARTS_DIR=/tmp/charts so the nobody user can write.
"""
import os
timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
out_dir = Path("artifacts/charts")
base = os.environ.get("MCNOAA_CHARTS_DIR", "artifacts/charts")
out_dir = Path(base)
out_dir.mkdir(parents=True, exist_ok=True)
path = out_dir / f"{station_id}_{chart_type}_{timestamp}.html"
path.write_text(html, encoding="utf-8")