nex_buddy/scripts/docker-deploy.sh

70 lines
2.0 KiB
Bash
Executable File
Raw 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
# Docker 部署:scripts/docker-deploy.sh [--full] [--init] [--no-build]
# --full 同时启动 PostgreSQL / Redis(docker-compose.full.yml)
# --init 首次部署:导入码表与通用月度运势
# --no-build 跳过镜像构建,直接用已有镜像启动
set -euo pipefail
cd "$(dirname "$0")/.."
FULL=0
INIT=0
BUILD=1
usage() {
cat <<'EOF'
Docker 部署:scripts/docker-deploy.sh [选项]
--full 同时启动 PostgreSQL / Redis
--init 首次部署:导入码表与通用月度运势
--no-build 跳过镜像构建
EOF
}
for arg in "$@"; do
case "$arg" in
--full) FULL=1 ;;
--init) INIT=1 ;;
--no-build) BUILD=0 ;;
-h|--help) usage; exit 0 ;;
*) echo "未知参数:$arg(用 --help 查看用法)" >&2; exit 2 ;;
esac
done
if ! command -v docker >/dev/null 2>&1; then
echo "!! 找不到 docker" >&2
exit 1
fi
if ! docker compose version >/dev/null 2>&1; then
echo "!! 需要 Docker Compose v2(docker compose)" >&2
exit 1
fi
if [ ! -f .env ]; then
echo "!! 缺少 .env:先执行 cp .env.example .env 并填写数据库/Redis/和风凭据" >&2
exit 1
fi
FILES=(-f docker-compose.yml)
if [ -f docker-compose.override.yml ]; then
FILES+=(-f docker-compose.override.yml)
echo ">> 使用 docker-compose.override.yml"
fi
if [ "$FULL" = 1 ]; then
FILES+=(-f docker-compose.full.yml)
fi
if [ "$BUILD" = 1 ]; then
echo ">> 构建镜像(pip/npm/apt 使用国内镜像源,可用 build arg 覆盖)"
docker compose "${FILES[@]}" build
fi
echo ">> 启动容器"
docker compose "${FILES[@]}" up -d --wait
if [ "$INIT" = 1 ]; then
echo ">> 初始化码表与运势"
docker compose "${FILES[@]}" exec -T api python server/scripts/seed_catalog.py
docker compose "${FILES[@]}" exec -T api python server/scripts/seed_fortunes.py
fi
docker compose "${FILES[@]}" ps
echo ">> 完成;首次建管理员:docker compose exec api python server/scripts/create_admin.py --account admin@nexbuddy.com --password '强口令'"