Spaces:
Running
Running
File size: 4,726 Bytes
8a03d2c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #!/usr/bin/env bash
# Vcore AI Proxy Linux 一键部署运行脚本
# 用法:chmod +x run_linux.sh && ./run_linux.sh
set -Eeuo pipefail
info() { printf '\033[36m[INFO]\033[0m %s\n' "$*"; }
ok() { printf '\033[32m[OK]\033[0m %s\n' "$*"; }
warn() { printf '\033[33m[WARN]\033[0m %s\n' "$*"; }
fail() { printf '\033[31m[FAIL]\033[0m %s\n' "$*"; }
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_DIR"
export PYTHONPATH="$PROJECT_DIR${PYTHONPATH:+:$PYTHONPATH}"
PORT="${PORT_API:-2156}"
PYTHON_BIN="${PYTHON_BIN:-}"
VENV_DIR="$PROJECT_DIR/.venv"
LOG_DIR="$PROJECT_DIR/logs"
CONFIG_DIR="$PROJECT_DIR/config"
MARKER="$VENV_DIR/.deps.ok"
mkdir -p "$LOG_DIR" "$CONFIG_DIR"
info "Vcore AI Proxy Linux 一键部署启动"
info "项目目录: $PROJECT_DIR"
need_cmd() {
command -v "$1" >/dev/null 2>&1
}
install_packages() {
if need_cmd apt-get; then
info "正在安装系统依赖(apt)..."
if [ "$(id -u)" -eq 0 ]; then
apt-get update
apt-get install -y python3 python3-venv python3-pip curl ca-certificates
elif need_cmd sudo; then
sudo apt-get update
sudo apt-get install -y python3 python3-venv python3-pip curl ca-certificates
else
fail "缺少 sudo,无法自动安装依赖。请用 root 运行脚本。"
exit 1
fi
elif need_cmd dnf; then
info "正在安装系统依赖(dnf)..."
if [ "$(id -u)" -eq 0 ]; then
dnf install -y python3 python3-pip curl ca-certificates
elif need_cmd sudo; then
sudo dnf install -y python3 python3-pip curl ca-certificates
else
fail "缺少 sudo,无法自动安装依赖。请用 root 运行脚本。"
exit 1
fi
elif need_cmd yum; then
info "正在安装系统依赖(yum)..."
if [ "$(id -u)" -eq 0 ]; then
yum install -y python3 python3-pip curl ca-certificates
elif need_cmd sudo; then
sudo yum install -y python3 python3-pip curl ca-certificates
else
fail "缺少 sudo,无法自动安装依赖。请用 root 运行脚本。"
exit 1
fi
elif need_cmd apk; then
info "正在安装系统依赖(apk)..."
if [ "$(id -u)" -eq 0 ]; then
apk add --no-cache python3 py3-pip curl ca-certificates
elif need_cmd sudo; then
sudo apk add --no-cache python3 py3-pip curl ca-certificates
else
fail "缺少 sudo,无法自动安装依赖。请用 root 运行脚本。"
exit 1
fi
else
warn "未识别包管理器,将尝试使用现有 Python。"
fi
}
if [ -z "$PYTHON_BIN" ]; then
if need_cmd python3; then
PYTHON_BIN="$(command -v python3)"
else
install_packages
PYTHON_BIN="$(command -v python3 || true)"
fi
fi
if [ -z "$PYTHON_BIN" ]; then
fail "未找到 python3,且无法自动安装。"
exit 1
fi
if ! "$PYTHON_BIN" - <<'PY' >/dev/null 2>&1
import sys
raise SystemExit(0 if sys.version_info >= (3, 10) else 1)
PY
then
warn "当前 Python 版本低于 3.10,尝试安装系统 Python。"
install_packages
fi
if [ ! -d "$VENV_DIR" ]; then
info "正在创建 Python 虚拟环境..."
if ! "$PYTHON_BIN" -m venv "$VENV_DIR"; then
install_packages
"$PYTHON_BIN" -m venv "$VENV_DIR"
fi
fi
PY="$VENV_DIR/bin/python"
PIP="$VENV_DIR/bin/pip"
REQ_HASH=""
if command -v sha256sum >/dev/null 2>&1; then
REQ_HASH="$(sha256sum requirements.txt | awk '{print $1}')"
else
REQ_HASH="$(python3 - <<'PY'
import hashlib
print(hashlib.sha256(open('requirements.txt','rb').read()).hexdigest())
PY
)"
fi
OLD_HASH=""
[ -f "$MARKER" ] && OLD_HASH="$(cat "$MARKER" 2>/dev/null || true)"
if [ "$REQ_HASH" != "$OLD_HASH" ]; then
info "正在安装/更新 Python 依赖..."
"$PY" -m pip install --upgrade pip
"$PIP" install -r requirements.txt
printf '%s' "$REQ_HASH" > "$MARKER"
else
ok "Python 依赖已就绪"
fi
if need_cmd ufw; then
if [ "$(id -u)" -eq 0 ]; then ufw allow "$PORT"/tcp >/dev/null 2>&1 || true
elif need_cmd sudo; then sudo ufw allow "$PORT"/tcp >/dev/null 2>&1 || true
fi
fi
LOCAL_URL="http://127.0.0.1:$PORT/admin"
LAN_IP="$(hostname -I 2>/dev/null | awk '{print $1}')"
ok "部署完成"
info "本机管理页面: $LOCAL_URL"
[ -n "$LAN_IP" ] && info "局域网管理页面: http://$LAN_IP:$PORT/admin"
info "首次启动会在控制台/日志中生成管理员密码。"
info "按 Ctrl+C 可停止服务。"
if command -v xdg-open >/dev/null 2>&1; then
(
for _ in $(seq 1 30); do
if command -v curl >/dev/null 2>&1 && curl -fsS "http://127.0.0.1:$PORT/health" >/dev/null 2>&1 && curl -fsS "$LOCAL_URL" >/dev/null 2>&1; then
xdg-open "$LOCAL_URL?t=$(date +%s)" >/dev/null 2>&1 || true
break
fi
sleep 1
done
) &
fi
exec "$PY" -m main
|