nex_animation/scripts/docker-entrypoint.sh

53 lines
3.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# =============================================================================
# 容器入口:先把「部署数据包」灌进库,再把 PID 1 交给 uvicorn / celery。
# 人不敲它,ENTRYPOINT 敲它(Dockerfile 最后一行)。
#
# 为什么需要这一步:镜像里只有代码。模型配置、加密主密钥、管理员账号在
# ./scripts/seed/*.json —— 它被 gitignore 忽略、被 .dockerignore 排除,
# 由 compose 只读挂到 /app/scripts/seed。少了灌数据这一环,新库起来就是个空壳:
# 模型注册表是空的(只有 mock),AUTH_ENABLED=true 下连页面都登不进去。
#
# 为什么默认 --only-missing:数据集是开发机某一刻的快照,而服务器上人工调过的
# 默认位是本机事实。每次重启都按包里的值重对齐默认位,等于「重启一次配置回滚一次」。
# --only-missing 只补库里没有的行、不动已有行 —— 这条语义正是 seed.py 里刚修过的坑。
#
# 为什么灌失败不拦启动:这时 API 和页面本身是好的,缺的只是几条模型配置;
# 直接 exit 1 会让整站在「数据集比代码旧」这种小事上白屏。告警打到日志,
# `docker compose logs` 看得见,运维自己决定要不要 AUTO_SEED=force 硬灌。
#
# AUTO_SEED:only-missing 补空缺(默认)| force 包里说的算(覆盖默认位)| off 跳过
# =============================================================================
set -uo pipefail
# 根目录从脚本自己的位置反推,不写死 /app:镜像里的 WORKDIR 是 /app,
# 但把仓库换个目录跑、或在 CI 里离线复现入口契约时路径就不是它了——
# 写死会让「数据集存在与否」的判断静默变成 False,表现是空库悄悄起来了。
APP_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SEED_DIR="$APP_ROOT/scripts/seed"
SEED_PY="$APP_ROOT/scripts/seed.py"
is_api=0
[ "${1:-}" = "uvicorn" ] && is_api=1
# worker/beat 的第一个参数是 celery,同一个库被多个容器同时灌会互相踩,只在 API 进程做一次。
mode="${AUTO_SEED:-only-missing}"
if [ "$is_api" = 1 ] && [ "$mode" != "off" ]; then
if [ -s "$SEED_DIR/model_specs.json" ]; then
seed_args=(--only-missing)
[ "$mode" = force ] && seed_args=()
echo "[entrypoint] 导入部署数据包:seed.py install ${seed_args[*]-}(AUTO_SEED=$mode)"
if ! python "$SEED_PY" install ${seed_args[@]+"${seed_args[@]}"}; then
echo "[entrypoint][warn] 数据包没灌进去(每条 error 上面都写了怎么修):服务照常启动," >&2
echo "[entrypoint][warn] 模型配置可能不完整。补灌:docker compose exec app \\" >&2
echo "[entrypoint][warn] python scripts/seed.py install --only-missing" >&2
fi
else
echo "[entrypoint] 没挂 $SEED_DIR 数据集,跳过导入:空库只有 mock 模型可用"
fi
fi
# exec 而不是直接跑:uvicorn 要当 PID 1,docker stop 的 SIGTERM 才传得进去,
# 30 秒后才会被 SIGKILL 的「超时强杀」也就不会发生(媒体写一半被杀会留脏文件)。
exec "$@"