ball-tracking-cv/deploy.sh
2025-08-10 10:24:15 +08:00

149 lines
4.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 部署脚本
# 使用方法: ./deploy.sh
set -e # 遇到错误立即退出
PROJECT_NAME="ball-tracking-server"
USER="root"
PYTHON_VERSION="python3"
echo "🚀 开始部署 $PROJECT_NAME..."
# 1. 检查当前目录和权限
echo "📍 当前目录: $(pwd)"
echo "👤 当前用户: $(whoami)"
# 2. 更新代码 (如果使用 git)
if [ -d ".git" ]; then
echo "📥 更新代码..."
git pull origin main # 或者你的主分支名
fi
# 3. 检查 Python 版本
echo "🐍 检查 Python 版本..."
$PYTHON_VERSION --version
which $PYTHON_VERSION
# 4. 创建虚拟环境 (如果不存在)
if [ ! -d "venv" ]; then
echo "🐍 创建 Python 虚拟环境..."
$PYTHON_VERSION -m venv venv
if [ ! -f "venv/bin/activate" ]; then
echo "❌ 错误: 虚拟环境创建失败,检查 Python 安装"
echo "尝试使用以下命令手动创建:"
echo "$PYTHON_VERSION -m venv venv"
exit 1
fi
echo "✅ 虚拟环境创建成功"
else
echo "✅ 虚拟环境已存在"
fi
# 5. 检查虚拟环境激活脚本
if [ ! -f "venv/bin/activate" ]; then
echo "❌ 错误: 找不到虚拟环境激活脚本"
echo "虚拟环境目录内容:"
ls -la venv/ || echo "venv 目录不存在或无法访问"
echo "尝试删除并重新创建虚拟环境..."
rm -rf venv
$PYTHON_VERSION -m venv venv
fi
# 6. 激活虚拟环境并安装依赖
echo "📦 安装 Python 依赖..."
source venv/bin/activate
echo "🐍 虚拟环境已激活: $(which python)"
pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
pip install gunicorn supervisor -i https://mirrors.aliyun.com/pypi/simple/ # 生产环境依赖
# 7. 构建前端
echo "🏗️ 构建前端..."
cd app/web
if command -v bun &> /dev/null; then
echo "使用 Bun 构建..."
bun install
bun run build
elif command -v npm &> /dev/null; then
echo "使用 npm 构建..."
npm install
npm run build
else
echo "❌ 错误: 未找到 bun 或 npm"
exit 1
fi
# 8. 返回项目根目录
cd "../.."
# 9. 创建必要的目录
echo "📁 创建必要目录..."
mkdir -p logs
mkdir -p data/sessions
# 10. 设置权限
echo "🔐 设置文件权限..."
chown -R $USER:$USER .
chmod +x deploy.sh
chmod +x start.sh
chmod +x stop.sh
# 11. 检查并安装 Supervisor
echo "🔍 检查 Supervisor 安装状态..."
if ! command -v supervisorctl &> /dev/null; then
echo "📦 安装 Supervisor..."
if command -v apt-get &> /dev/null; then
# Debian/Ubuntu
sudo apt-get update
sudo apt-get install -y supervisor
elif command -v yum &> /dev/null; then
# CentOS/RHEL
sudo yum install -y supervisor
elif command -v dnf &> /dev/null; then
# Fedora
sudo dnf install -y supervisor
else
echo "❌ 错误: 无法自动安装 Supervisor请手动安装"
echo "Ubuntu/Debian: sudo apt-get install supervisor"
echo "CentOS/RHEL: sudo yum install supervisor"
echo "Fedora: sudo dnf install supervisor"
exit 1
fi
fi
# 12. 检查 Supervisor 配置目录
if [ ! -d "/etc/supervisor/conf.d" ]; then
echo "📁 创建 Supervisor 配置目录..."
sudo mkdir -p /etc/supervisor/conf.d
fi
# 13. 启动 Supervisor 服务
echo "🚀 启动 Supervisor 服务..."
if command -v systemctl &> /dev/null; then
sudo systemctl enable supervisor
sudo systemctl start supervisor
elif command -v service &> /dev/null; then
sudo service supervisor start
fi
# 14. 复制配置文件到系统目录 (需要 sudo)
echo "⚙️ 配置系统服务..."
if [ -f "/etc/supervisor/conf.d/$PROJECT_NAME.conf" ]; then
sudo cp supervisor.conf /etc/supervisor/conf.d/$PROJECT_NAME.conf
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart $PROJECT_NAME
else
sudo cp supervisor.conf /etc/supervisor/conf.d/$PROJECT_NAME.conf
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start $PROJECT_NAME
fi
echo "✅ 部署完成!"
echo "📝 查看日志: sudo supervisorctl tail -f $PROJECT_NAME"
echo "🔄 重启服务: sudo supervisorctl restart $PROJECT_NAME"
echo "⏹️ 停止服务: sudo supervisorctl stop $PROJECT_NAME"