mottery/docker_service_manager.sh

211 lines
6.1 KiB
Bash

#!/bin/bash
# Docker环境服务管理脚本
SERVICE_NAME="lottery-system-docker"
case "$1" in
start)
echo "🚀 启动彩票数据分析系统 (Docker环境)..."
# 启动后端服务
cd backend
source venv/bin/activate
nohup uvicorn app.main:app --host 0.0.0.0 --port 8000 > backend.log 2>&1 &
echo $! > backend.pid
cd ..
# 启动定时任务
nohup python3 scheduler.py > scheduler.log 2>&1 &
echo $! > scheduler.pid
# 重启nginx容器
echo "🔄 重启nginx容器..."
docker restart dnmp-nginx
echo "✅ 服务启动完成"
;;
stop)
echo "🛑 停止彩票数据分析系统..."
# 停止后端
if [ -f "backend/backend.pid" ]; then
kill $(cat backend/backend.pid) 2>/dev/null
rm backend/backend.pid
fi
# 停止定时任务
if [ -f "scheduler.pid" ]; then
kill $(cat scheduler.pid) 2>/dev/null
rm scheduler.pid
fi
# 强制停止所有相关进程
pkill -f uvicorn
pkill -f scheduler.py
echo "✅ 服务停止完成"
;;
restart)
echo "🔄 重启彩票数据分析系统..."
$0 stop
sleep 2
$0 start
;;
status)
echo "📊 服务状态检查 (Docker环境)..."
# 检查后端
if curl -f http://localhost:8000/health > /dev/null 2>&1; then
echo "✅ 后端服务: 运行中 (localhost:8000)"
else
echo "❌ 后端服务: 未运行"
fi
# 检查nginx容器
if docker ps | grep -q dnmp-nginx; then
echo "✅ nginx容器: 运行中"
else
echo "❌ nginx容器: 未运行"
fi
# 检查前端访问
if curl -f https://cp.mzh.one/ > /dev/null 2>&1; then
echo "✅ 前端页面: 可访问"
else
echo "❌ 前端页面: 不可访问"
fi
# 检查API访问
if curl -f https://cp.mzh.one/api/v1/lottery/ssq/latest > /dev/null 2>&1; then
echo "✅ API接口: 可访问"
else
echo "❌ API接口: 不可访问"
fi
# 检查定时任务
if pgrep -f scheduler.py > /dev/null; then
echo "✅ 定时任务: 运行中"
else
echo "❌ 定时任务: 未运行"
fi
# 显示进程信息
echo ""
echo "📋 进程信息:"
ps aux | grep -E "(uvicorn|scheduler)" | grep -v grep
# 显示容器信息
echo ""
echo "📋 容器信息:"
docker ps | grep dnmp
;;
logs)
echo "📋 查看日志..."
case "$2" in
backend)
tail -f backend/backend.log
;;
scheduler)
tail -f scheduler.log
;;
nginx)
docker exec dnmp-nginx tail -f /var/log/nginx/error.log
;;
nginx-access)
docker exec dnmp-nginx tail -f /var/log/nginx/access.log
;;
*)
echo "用法: $0 logs {backend|scheduler|nginx|nginx-access}"
;;
esac
;;
deploy)
echo "🚀 部署系统 (Docker环境)..."
# 构建前端
cd frontend
npm run build
cd ..
# 检查前端构建文件是否存在
if [ ! -d "frontend/dist" ]; then
echo "❌ 前端构建失败"
exit 1
fi
# 复制前端文件到nginx容器
echo "📁 部署前端文件到nginx容器..."
docker cp frontend/dist/. dnmp-nginx:/opt/mottery/frontend/dist/
# 重启服务
$0 restart
echo "✅ 部署完成"
;;
fix-nginx)
echo "🔧 修复nginx配置..."
# 运行修复脚本
if [ -f "fix_docker_nginx.sh" ]; then
chmod +x fix_docker_nginx.sh
./fix_docker_nginx.sh
else
echo "❌ 找不到修复脚本 fix_docker_nginx.sh"
exit 1
fi
;;
test-backend)
echo "🧪 测试后端连接..."
# 测试不同的后端地址
echo "测试 localhost:8000..."
if curl -f http://localhost:8000/health > /dev/null 2>&1; then
echo "✅ localhost:8000 连接正常"
else
echo "❌ localhost:8000 连接失败"
fi
echo "测试 host.docker.internal:8000..."
if curl -f http://host.docker.internal:8000/health > /dev/null 2>&1; then
echo "✅ host.docker.internal:8000 连接正常"
else
echo "❌ host.docker.internal:8000 连接失败"
fi
echo "测试 172.17.0.1:8000..."
if curl -f http://172.17.0.1:8000/health > /dev/null 2>&1; then
echo "✅ 172.17.0.1:8000 连接正常"
else
echo "❌ 172.17.0.1:8000 连接失败"
fi
echo "测试 172.18.0.1:8000..."
if curl -f http://172.18.0.1:8000/health > /dev/null 2>&1; then
echo "✅ 172.18.0.1:8000 连接正常"
else
echo "❌ 172.18.0.1:8000 连接失败"
fi
;;
*)
echo "用法: $0 {start|stop|restart|status|logs|deploy|fix-nginx|test-backend}"
echo ""
echo "命令说明:"
echo " start - 启动所有服务"
echo " stop - 停止所有服务"
echo " restart - 重启所有服务"
echo " status - 查看服务状态"
echo " logs - 查看日志 (用法: $0 logs {backend|scheduler|nginx|nginx-access})"
echo " deploy - 部署系统"
echo " fix-nginx - 修复nginx配置"
echo " test-backend - 测试后端连接"
exit 1
;;
esac