ai-model-studio / run_linux.sh
slk1st's picture
Deploy AI Model Studio
8a03d2c verified
Raw
History Blame Contribute Delete
4.73 kB
#!/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