Detect Python/Node/PM2 on deploy (install only if missing); uninstall removes /opt/eth_hedge_sim.

Keep .env and DB backup under /root/backups before deleting the install directory.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-26 23:08:51 +08:00
parent 2b348854fe
commit 4d331da962
6 changed files with 185 additions and 30 deletions
+118 -12
View File
@@ -84,22 +84,128 @@ resolve_repo_root() {
return 1
}
ensure_system_deps() {
step "检查系统依赖"
# ---------- 运行时检测(有则跳过,无则安装) ----------
python_runtime_ok() {
command -v python3 >/dev/null 2>&1 || return 1
# 能创建 venv 即可;pip 在项目 .venv 内安装
python3 -c "import venv" >/dev/null 2>&1 || return 1
return 0
}
node_runtime_ok() {
command -v node >/dev/null 2>&1 || return 1
command -v npm >/dev/null 2>&1 || return 1
return 0
}
pm2_runtime_ok() {
command -v pm2 >/dev/null 2>&1
}
print_runtime_probe() {
echo "环境检测:"
if python_runtime_ok; then
echo " [OK] Python3 + venv → $(python3 --version 2>&1)"
else
echo " [缺] Python3 / venv → 将自动安装"
fi
if node_runtime_ok; then
echo " [OK] Node.js + npm → $(node --version 2>&1) / npm $(npm --version 2>&1)"
else
echo " [缺] Node.js / npm → 将自动安装 Node ${NODE_MAJOR}"
fi
if pm2_runtime_ok; then
echo " [OK] PM2 → $(pm2 -v 2>&1)"
else
echo " [缺] PM2 → 将自动安装"
fi
}
install_python_runtime() {
step "安装 Python3 环境 (python3 / venv / pip)"
export DEBIAN_FRONTEND=noninteractive
if command -v apt-get >/dev/null 2>&1; then
apt-get update -qq
apt-get install -y git ca-certificates curl python3 python3-venv python3-pip
if ! command -v apt-get >/dev/null 2>&1; then
die "非 apt 系统,请手动安装 python3 python3-venv python3-pip"
fi
if ! command -v node >/dev/null 2>&1; then
step "安装 Node.js ${NODE_MAJOR}"
curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash -
apt-get install -y nodejs
apt-get update -qq
apt-get install -y python3 python3-venv python3-pip
if ! python_runtime_ok; then
die "Python 环境安装后仍不可用,请检查 apt 源"
fi
if ! command -v pm2 >/dev/null 2>&1; then
step "安装 PM2"
npm install -g pm2
log "Python 已就绪: $(python3 --version 2>&1)"
}
install_node_runtime() {
step "安装 Node.js ${NODE_MAJOR}"
export DEBIAN_FRONTEND=noninteractive
if ! command -v apt-get >/dev/null 2>&1; then
die "非 apt 系统,请手动安装 Node.js ${NODE_MAJOR}+"
fi
apt-get update -qq
apt-get install -y ca-certificates curl
curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash -
apt-get install -y nodejs
if ! node_runtime_ok; then
die "Node.js 安装后仍不可用"
fi
log "Node 已就绪: $(node --version 2>&1)"
}
install_pm2_runtime() {
step "安装 PM2"
if ! command -v npm >/dev/null 2>&1; then
die "安装 PM2 需要先有 npm"
fi
npm install -g pm2
if ! pm2_runtime_ok; then
die "PM2 安装后仍不可用"
fi
log "PM2 已就绪: $(pm2 -v 2>&1)"
}
ensure_base_packages() {
local need=()
command -v git >/dev/null 2>&1 || need+=(git)
command -v curl >/dev/null 2>&1 || need+=(curl)
if [[ ${#need[@]} -eq 0 ]]; then
log "基础包 git/curl 已存在,跳过"
return 0
fi
if ! command -v apt-get >/dev/null 2>&1; then
die "缺少依赖: ${need[*]},且无 apt-get"
fi
step "安装基础包: ${need[*]}"
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y ca-certificates "${need[@]}"
}
ensure_system_deps() {
step "环境检测 (Python / Node / PM2)"
print_runtime_probe
ensure_base_packages
if python_runtime_ok; then
log "Python 环境已存在,跳过安装"
else
install_python_runtime
fi
if node_runtime_ok; then
log "Node.js 环境已存在,跳过安装"
else
install_node_runtime
fi
if pm2_runtime_ok; then
log "PM2 已存在,跳过安装"
else
install_pm2_runtime
fi
step "环境确认"
print_runtime_probe
}
pm2_stop_project() {