# ops-onehit production Dockerfile
#
# Multi-stage build:
#   Stage 1 (builder)  : 装 build 工具 + uv,build wheel
#   Stage 2 (runtime)  : 只装 runtime deps + wheel,non-root user,healthcheck
#
# 设计目标:
#   - 最终 runtime image < 200 MB(debian-slim + python + wheel)
#   - 不装 dev/test/prophet(节省 ~150 MB)
#   - 不装 torch/transformers(默认 USE_REAL_VLM=false;启用时装 torch extra)
#   - 用 tini 作 PID 1,正确处理 SIGTERM/SIGINT(zombie reaping)
#   - non-root user 跑服务,符合 CIS benchmark
#   - healthcheck 调 `/api/health`(FastAPI 内置)
#
# Build:
#   docker build -t ops-onehit:1.0.2 .
#   docker build -t ops-onehit:latest -f Dockerfile .
#
# Run:
#   docker run --rm -p 8080:8080 -v ops-onehit-data:/home/ops/data ops-onehit:1.0.2
#
# 镜像大小验证:
#   docker images ops-onehit:1.0.2 --format "{{.Size}}"

# syntax=docker/dockerfile:1.7

# ============================================================================
# Stage 1: Builder
# ============================================================================
FROM python:3.11-slim AS builder

WORKDIR /build

# System build deps(只为编译 wheel,装完就扔)
# - build-essential / gcc / g++ → 编译 C 扩展(psutil, chromadb hnswlib)
# - libssl-dev / libffi-dev    → cryptography / pyOpenSSL
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        gcc \
        g++ \
        libssl-dev \
        libffi-dev \
        && rm -rf /var/lib/apt/lists/*

# Install uv for fast wheel building
RUN pip install --no-cache-dir --upgrade pip uv

# Copy metadata first → 依赖层独立缓存
COPY pyproject.toml README.md ./

# Build wheel in /opt/venv(python -m build 产物)
RUN uv venv /opt/venv && \
    . /opt/venv/bin/activate && \
    uv pip install --no-cache "build" && \
    python -m build --wheel --outdir /wheels

# Copy source for wheel install
COPY src/ ./src/

# Install ops-onehit from source(更可靠,比 hatch build 简单)
RUN . /opt/venv/bin/activate && \
    uv pip install --no-cache /wheels/*.whl && \
    rm -rf /wheels /build

# ============================================================================
# Stage 1.5: B8 Dashboard v2 builder (Vue 3 + Vite)
# ============================================================================
# 在 builder stage 之后、runtime 之前,build Vue 3 SPA。
# 产物 /dashboard/dist 会被 runtime stage COPY 到
# /app/src/ops_onehit/api/static/dashboard/(FastAPI StaticFiles mount)
FROM node:20-alpine AS dashboard-builder

WORKDIR /dashboard

# 国内用户可设 ARG NPM_REGISTRY=https://registry.npmmirror.com
ARG NPM_REGISTRY=https://registry.npmjs.org
RUN npm config set registry ${NPM_REGISTRY}

# 依赖层独立缓存(package*.json 改了才重装)
COPY dashboard/package.json dashboard/package-lock.json* ./
RUN npm install --no-audit --no-fund

# 复制源码 + build
COPY dashboard/ ./
RUN npm run build  # 自动调 scripts/copy-to-static.js 同步到 /dashboard/dist

# ============================================================================
# Stage 2: Runtime
# ============================================================================
FROM python:3.11-slim AS runtime

# Open Container Initiative 标签(镜像元数据)
LABEL org.opencontainers.image.title="ops-onehit" \
      org.opencontainers.image.description="Local-first AI SRE Agent — monitoring, RCA, repair, and reporting" \
      org.opencontainers.image.source="https://github.com/a1580028209/ops-onehit" \
      org.opencontainers.image.url="https://github.com/a1580028209/ops-onehit" \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.vendor="a1580028209" \
      org.opencontainers.image.version="1.0.2"

# Runtime system deps
# - tini  → PID 1 正确处理信号(zombie reaping + SIGTERM 转发)
# - curl  → healthcheck 需要
RUN apt-get update && apt-get install -y --no-install-recommends \
        tini \
        curl \
        && rm -rf /var/lib/apt/lists/*

# Copy venv from builder
COPY --from=builder /opt/venv /opt/venv

# Copy B8 Dashboard v2 SPA 产物(Vue 3 + Chart.js + Cytoscape.js)
# 注意:COPY 时机在 USER ops 之前,dashboard dist 默认 root 写入
COPY --from=dashboard-builder /dashboard/dist /tmp/dashboard-dist
# 移到 ops-onehit 包静态目录(给 FastAPI StaticFiles mount)
# 这里我们 COPY 之前先创建好目标位置,并 chown 给 ops 用户
# 因为 dashboard/dist 是 readonly 没问题,但目标路径要 ops 写后续更新
USER root
RUN mkdir -p /opt/venv/lib/python3.11/site-packages/ops_onehit/api/static/dashboard && \
    cp -r /tmp/dashboard-dist/* /opt/venv/lib/python3.11/site-packages/ops_onehit/api/static/dashboard/ && \
    rm -rf /tmp/dashboard-dist && \
    chown -R ops:ops /opt/venv/lib/python3.11/site-packages/ops_onehit/api/static/dashboard
USER ops

# Non-root user(UID 1000 是大多数 Linux distros 第一个非系统 user)
RUN groupadd --system --gid 1000 ops && \
    useradd --system --uid 1000 --gid ops --home /home/ops --shell /bin/bash ops && \
    mkdir -p /home/ops/.cache /home/ops/data /home/ops/logs && \
    chown -R ops:ops /home/ops

USER ops
WORKDIR /home/ops

# 运行时环境变量
ENV PATH="/opt/venv/bin:$PATH" \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONHASHSEED=random \
    OPS_ONEHIT_DATA_DIR=/home/ops/data \
    OPS_ONEHIT_LOG_DIR=/home/ops/logs \
    OPS_ONEHIT_LOG_LEVEL=INFO

# Health check:curl /api/health(FastAPI 8080 端口)
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
    CMD curl -fsS http://localhost:8080/api/health || exit 1

EXPOSE 8080

# tini 作 PID 1 接收信号并转发
ENTRYPOINT ["/usr/bin/tini", "--"]

# 默认启动 web 面板
CMD ["ops-onehit", "serve", "--host", "0.0.0.0", "--port", "8080"]
