# Incremental update of an already-built coderai image.
#
# Re-layers ONLY the application code, launcher scripts and service configs on
# top of an existing base image (the heavy bundle: python, venvs, native libs,
# lip-sync, ds4, parler). Those base layers are inherited unchanged — there is no
# 20 GB bundle recopy — so this builds in seconds even with an empty build cache.
#
# Driven by packaging/linux/update_oci_image.sh, which keeps an immutable
# `coderai:base` tag so repeated updates always start from the same bundle and
# never stack app layers on top of each other.
ARG BASE_IMAGE=coderai:base
FROM ${BASE_IMAGE}

# Refresh the app tree plus the scripts/configs that live outside it. The big
# /opt/coderai/{python,*-venv,local-libs,Wav2Lip,SadTalker,ds4,py310} trees are
# left as inherited layers. (COPY overwrites/adds; a file deleted from the repo
# is pruned by the cleanup RUN below for the known-stale paths.)
COPY . /opt/coderai/app
COPY packaging/linux/launcher/coderai-oci /usr/local/bin/coderai
COPY packaging/linux/launcher/with-env /usr/local/bin/with-env
COPY packaging/linux/launcher/coderai-entrypoint /usr/local/bin/coderai-entrypoint
COPY packaging/linux/launcher/coderai-upgrade /usr/local/bin/coderai-upgrade
COPY packaging/linux/launcher/wav2lip /usr/local/bin/wav2lip
COPY packaging/linux/launcher/sadtalker /usr/local/bin/sadtalker
COPY packaging/linux/nginx.conf /etc/nginx/nginx.conf
COPY packaging/linux/supervisord.conf /etc/supervisor/supervisord.conf
COPY packaging/linux/README-RUN.txt /opt/coderai/README-RUN.txt

RUN set -eux; \
    chmod +x /usr/local/bin/coderai /usr/local/bin/with-env /usr/local/bin/coderai-entrypoint \
             /usr/local/bin/coderai-upgrade \
             /usr/local/bin/wav2lip /usr/local/bin/sadtalker /opt/coderai/app/coderai; \
    mkdir -p /config /models /cache /opt/coderai/app/models; \
    rm -rf \
      /opt/coderai/app/.git \
      /opt/coderai/app/venv* \
      /opt/coderai/app/.venv \
      /opt/coderai/app/township_output \
      /opt/coderai/app/offload \
      /opt/coderai/app/dist \
      /opt/coderai/app/.packaging-cache; \
    find /opt/coderai/app -type d -name __pycache__ -prune -exec rm -rf '{}' +; \
    /opt/coderai/python/bin/python3 -c "import importlib.util, sys; m=[n for n in ('fastapi','uvicorn','torch') if importlib.util.find_spec(n) is None]; sys.exit('base image missing: '+', '.join(m) if m else 0)"

# Context-free GPU memory query dependency (provides `pynvml`). Pure-python and
# tiny, so install it in the overlay too — the base may predate it. The code
# falls back to nvidia-smi when absent, hence `|| true` keeps offline builds OK.
RUN /opt/coderai/python/bin/python3 -m pip install --no-input --no-cache-dir "nvidia-ml-py>=12.0.0" || true

# openssh-client so `coderai-docker --upgrade --ssh-key …` can fetch over SSH
# (git is present but the CUDA runtime base ships no ssh). Tiny; keep the overlay
# still fast. `|| true` keeps offline overlay builds working (HTTPS upgrade still
# works without it).
RUN apt-get update && apt-get install -y --no-install-recommends openssh-client \
    && rm -rf /var/lib/apt/lists/* || true

# ENTRYPOINT / EXPOSE / VOLUME / ENV / WORKDIR are inherited from the base image.
