#!/usr/bin/env bash
# 在训练服务器（ceshi 用户）上执行，完成 API 服务的一键部署
set -euo pipefail

REPO=/home/ceshi/mysora
VENV=/home/ceshi/venv
LOG_DIR=/data/train-output/logs

echo "=== [1/6] 安装 API 依赖 ==="
$VENV/bin/pip install \
    fastapi \
    "uvicorn[standard]" \
    gunicorn \
    celery[redis] \
    redis \
    --quiet

echo "=== [2/6] 安装 NGINX ==="
sudo apt-get install -y nginx --quiet

echo "=== [3/6] 配置 NGINX ==="
sudo cp $REPO/deploy/nginx.conf /etc/nginx/sites-available/opensora
sudo ln -sf /etc/nginx/sites-available/opensora /etc/nginx/sites-enabled/opensora
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl enable nginx
sudo systemctl restart nginx

echo "=== [4/6] 安装并启动 Redis ==="
sudo apt-get install -y redis-server --quiet
# 开启 AOF 持久化，防止重启丢失任务
sudo sed -i 's/^appendonly no/appendonly yes/' /etc/redis/redis.conf
sudo systemctl enable redis
sudo systemctl restart redis

echo "=== [5/6] 创建日志目录 ==="
mkdir -p $LOG_DIR
sudo chown -R ceshi:ceshi $LOG_DIR

echo "=== [6/6] 注册并启动 systemd 服务 ==="
sudo cp $REPO/deploy/opensora-api.service      /etc/systemd/system/
sudo cp $REPO/deploy/opensora-worker@.service  /etc/systemd/system/
sudo systemctl daemon-reload

# 启动 API 服务
sudo systemctl enable opensora-api
sudo systemctl restart opensora-api

# 启动 8 个 GPU Worker（GPU 0-7）
for i in $(seq 0 7); do
    sudo systemctl enable  opensora-worker@$i
    sudo systemctl restart opensora-worker@$i
done

echo ""
echo "=== 部署完成 ==="
echo "API 服务: http://$(hostname -I | awk '{print $1}')/v1/generate"
echo "健康检查: http://$(hostname -I | awk '{print $1}')/health"
echo ""
echo "查看 API 日志:    sudo journalctl -u opensora-api -f"
echo "查看 Worker 日志: tail -f $LOG_DIR/worker-gpu0.log"
