fix: switch-env.sh修复路径/协议/数据库名+添加SSH_HOST硬编码

This commit is contained in:
2026-06-02 12:13:47 +08:00
parent 2c0bdf18ce
commit f8da9ff8f7

View File

@@ -12,10 +12,12 @@ PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
ENV_FILE="$PROJECT_ROOT/.env"
# ---------- 配置 ----------
SSH_HOST="root@1panel-server"
PROXY_DIR="/www/sites/xmclassmate.top/proxy"
# 1Panel 管理的 OpenResty 容器的实际配置文件路径
BASE_DIR="/opt/1panel/apps/openresty/openresty"
PROXY_DIR="${BASE_DIR}/www/sites/xmclassmate.top/proxy"
ROOT_CONF="${PROXY_DIR}/root.conf"
NGINX_CONTAINER="1Panel-openresty-ABu5"
SSH_HOST="root@1panel-server"
# ---------- 颜色输出 ----------
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
@@ -62,11 +64,11 @@ show_status() {
echo " 绿色 :4434 $( [ "$active" = "green" ] && echo '★ 活动中' || echo '○ 待命中' )"
echo ""
echo " 反向代理: xmclassmate.top"
echo " 数据库: milkydata_dev"
echo " 数据库: milkydata (blue/green) / milkydata_dev (dev)"
echo ""
# 检查两个端口是否可达
local blue_ok; blue_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 https://127.0.0.1:4433/health -k" 2>/dev/null || echo "000")
local blue_ok; blue_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 https://127.0.0.1:4433/health -k" 2>/dev/null || echo "000")
local green_ok; green_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 http://127.0.0.1:4434/health" 2>/dev/null || echo "000")
echo " 蓝色健康: HTTP $blue_ok"
echo " 绿色健康: HTTP $green_ok"
@@ -92,16 +94,19 @@ switch_to() {
# 先验证目标环境健康
echo "验证目标环境健康..."
local target_ok; target_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 http://127.0.0.1:${target_port}/health" 2>/dev/null || echo "000")
local proto="http"; [ "$target_port" = "4433" ] && proto="https"
local extra=""; [ "$proto" = "https" ] && extra="-k"
local target_ok; target_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 ${proto}://127.0.0.1:${target_port}/health ${extra}" 2>/dev/null || echo "000")
if [ "$target_ok" != "200" ]; then
error "目标环境 :${target_port} 健康检查失败 (HTTP $target_ok),取消切换"
exit 1
fi
info "目标环境健康 ✅"
# 修改 proxy_pass
info "修改 OpenResty proxy_pass → :${target_port}..."
ssh "$SSH_HOST" "docker exec ${NGINX_CONTAINER} sh -c \"sed -i 's|proxy_pass http://127.0.0.1:[0-9]*|proxy_pass http://127.0.0.1:${target_port}|' ${ROOT_CONF}\"" 2>&1 || {
# 修改 proxy_pass(根据端口自动选择 http/https
local target_proto="http"; [ "$target_port" = "4433" ] && target_proto="https"
info "修改 OpenResty proxy_pass → ${target_proto}://127.0.0.1:${target_port}..."
ssh "$SSH_HOST" "docker exec ${NGINX_CONTAINER} sh -c \"sed -i 's|proxy_pass https\?://127.0.0.1:[0-9]*|proxy_pass ${target_proto}://127.0.0.1:${target_port}|' ${ROOT_CONF}\"" 2>&1 || {
error "修改 proxy_pass 失败"
exit 1
}