dingdanquanliucheng/deploy/deploy-websocket.sh

161 lines
4.5 KiB
Bash
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
# 实时推送功能快速部署脚本
# 使用方法: chmod +x deploy-websocket.sh && ./deploy-websocket.sh
set -e
echo "=========================================="
echo " 实时推送功能部署脚本"
echo "=========================================="
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 检查命令是否存在
check_command() {
if ! command -v $1 &> /dev/null; then
echo -e "${RED}错误: $1 未安装${NC}"
return 1
fi
return 0
}
# 步骤1: 检查依赖
echo ""
echo "步骤 1/5: 检查依赖..."
echo "--------------------------------------------"
check_command "python3" || check_command "python"
check_command "pip3" || check_command "pip"
check_command "redis-cli"
check_command "nginx"
echo -e "${GREEN}✓ 依赖检查通过${NC}"
# 步骤2: 检查 Redis
echo ""
echo "步骤 2/5: 检查 Redis 服务..."
echo "--------------------------------------------"
if redis-cli ping | grep -q "PONG"; then
echo -e "${GREEN}✓ Redis 服务运行正常${NC}"
else
echo -e "${YELLOW}⚠ Redis 服务未运行,尝试启动...${NC}"
if command -v systemctl &> /dev/null; then
sudo systemctl start redis
sudo systemctl enable redis
elif command -v service &> /dev/null; then
sudo service redis start
else
echo -e "${RED}错误: 无法启动 Redis请手动启动${NC}"
exit 1
fi
echo -e "${GREEN}✓ Redis 服务已启动${NC}"
fi
# 步骤3: 安装 Python 依赖
echo ""
echo "步骤 3/5: 安装 Python 依赖..."
echo "--------------------------------------------"
cd backend
pip3 install -r requirements.txt -q
echo -e "${GREEN}✓ Python 依赖安装完成${NC}"
cd ..
# 步骤4: 更新 Nginx 配置
echo ""
echo "步骤 4/5: 更新 Nginx 配置..."
echo "--------------------------------------------"
# 检查是否在宝塔环境
if [ -d "/www/server/panel/vhost/nginx" ]; then
NGINX_CONF_DIR="/www/server/panel/vhost/nginx"
elif [ -d "/etc/nginx/conf.d" ]; then
NGINX_CONF_DIR="/etc/nginx/conf.d"
else
NGINX_CONF_DIR="/etc/nginx"
fi
echo "Nginx 配置目录: $NGINX_CONF_DIR"
# 复制配置文件
if [ -f "deploy/dda.gaowenbu.cn.conf" ]; then
sudo cp deploy/dda.gaowenbu.cn.conf $NGINX_CONF_DIR/
echo -e "${GREEN}✓ 已更新 dda.gaowenbu.cn 配置${NC}"
fi
if [ -f "deploy/ddy.gaowenbu.cn.conf" ]; then
sudo cp deploy/ddy.gaowenbu.cn.conf $NGINX_CONF_DIR/
echo -e "${GREEN}✓ 已更新 ddy.gaowenbu.cn 配置${NC}"
fi
# 测试 Nginx 配置
echo "测试 Nginx 配置..."
if sudo nginx -t 2>&1 | grep -q "successful"; then
echo -e "${GREEN}✓ Nginx 配置测试通过${NC}"
sudo nginx -s reload
echo -e "${GREEN}✓ Nginx 已重新加载${NC}"
else
echo -e "${RED}错误: Nginx 配置测试失败${NC}"
sudo nginx -t
exit 1
fi
# 步骤5: 重启后端服务
echo ""
echo "步骤 5/5: 重启后端服务..."
echo "--------------------------------------------"
# 检查是否有 systemd 服务
if systemctl list-unit-files | grep -q "order-flow"; then
sudo systemctl restart order-flow
echo -e "${GREEN}✓ 后端服务已重启 (systemd)${NC}"
elif [ -f "/etc/init.d/order-flow" ]; then
sudo /etc/init.d/order-flow restart
echo -e "${GREEN}✓ 后端服务已重启 (init.d)${NC}"
else
echo -e "${YELLOW}⚠ 未找到系统服务,请手动重启后端${NC}"
echo " 可以使用: cd backend && uvicorn backend.app.main:app --host 0.0.0.0 --port 8000"
fi
# 验证部署
echo ""
echo "=========================================="
echo " 部署验证"
echo "=========================================="
# 等待服务启动
sleep 3
# 检查 WebSocket 端点
echo ""
echo "检查 WebSocket 端点..."
if curl -s https://dda.gaowenbu.cn/ws/status | grep -q "active_users"; then
echo -e "${GREEN}✓ WebSocket 端点正常${NC}"
else
echo -e "${YELLOW}⚠ WebSocket 端点检查失败,请手动验证${NC}"
fi
# 完成
echo ""
echo "=========================================="
echo -e "${GREEN} 部署完成!${NC}"
echo "=========================================="
echo ""
echo "后续步骤:"
echo "1. 编译并部署前端:"
echo " cd frontend/web-admin && npm run build"
echo " cd frontend/web-sales && npm run build"
echo ""
echo "2. 在微信开发者工具中上传小程序"
echo ""
echo "3. 测试实时推送功能:"
echo " - 创建一个测试订单"
echo " - 检查是否收到实时通知"
echo ""
echo "详细说明请参考: deploy/实时推送部署指南.md"