Heal existing /opt install when manage.sh is missing on curl bootstrap.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-25 08:54:09 +08:00
parent 2a29181a90
commit 8c14759e78
+83 -17
View File
@@ -32,22 +32,93 @@ unset _script_src
set -u
repo_ready() {
[[ -f "${1}/deploy/manage.sh" && -f "${1}/deploy/pull_and_restart.sh" ]]
[[ -f "${1}/deploy/manage.sh" && -f "${1}/deploy/pull_and_restart.sh" && -d "${1}/deploy/lib" ]]
}
ensure_git_cli() {
if command -v git >/dev/null 2>&1; then
return 0
fi
if command -v apt-get >/dev/null 2>&1; then
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y git ca-certificates curl
else
echo "错误: 未找到 git" >&2
exit 1
fi
}
sync_repo_if_present() {
local root="$1"
if [[ -d "${root}/.git" ]] && command -v git >/dev/null 2>&1; then
git -C "${root}" pull -q --ff-only 2>/dev/null || true
git -C "${root}" fetch --all --prune 2>/dev/null || true
git -C "${root}" checkout "${GIT_BRANCH}" 2>/dev/null || true
git -C "${root}" pull --ff-only origin "${GIT_BRANCH}" 2>/dev/null \
|| git -C "${root}" pull --ff-only 2>/dev/null \
|| true
fi
}
# 旧目录已存在但缺 manage.sh:尽量 git pull / 修复后切到仓库内脚本
heal_existing_install() {
local root="$1"
echo "检测到已有目录但缺少管理脚本: ${root}"
echo "尝试同步最新代码…"
ensure_git_cli
if [[ -d "${root}/.git" ]]; then
sync_repo_if_present "${root}"
if repo_ready "${root}"; then
echo "同步成功,切换到仓库内 manage.sh"
exec bash "${root}/deploy/manage.sh" "$@" </dev/tty
fi
echo "git pull 后仍不完整,尝试强制对齐 origin/${GIT_BRANCH}"
git -C "${root}" fetch origin "${GIT_BRANCH}" || true
git -C "${root}" reset --hard "origin/${GIT_BRANCH}" || true
if repo_ready "${root}"; then
echo "对齐成功,切换到仓库内 manage.sh"
exec bash "${root}/deploy/manage.sh" "$@" </dev/tty
fi
fi
# 无 .git 或仍不可用:备份 .env/data 后重新 clone
echo "将备份配置并重新克隆到 ${root}"
local stamp backup_dir
stamp="$(date +%Y%m%d-%H%M%S)"
backup_dir="/root/backups/eth_hedge_sim/heal-${stamp}"
mkdir -p "${backup_dir}"
[[ -f "${root}/.env" ]] && cp -a "${root}/.env" "${backup_dir}/.env"
[[ -d "${root}/backend/data" ]] && cp -a "${root}/backend/data" "${backup_dir}/data" || true
echo "备份目录: ${backup_dir}"
local removed="${root}.old.${stamp}"
mv "${root}" "${removed}"
echo "旧目录已移至: ${removed}"
mkdir -p "$(dirname "${root}")"
git clone -b "${GIT_BRANCH}" "${GIT_URL}" "${root}"
if [[ -f "${backup_dir}/.env" ]]; then
cp -a "${backup_dir}/.env" "${root}/.env"
echo "已恢复 .env"
fi
if [[ -d "${backup_dir}/data" ]]; then
mkdir -p "${root}/backend"
cp -a "${backup_dir}/data" "${root}/backend/data"
echo "已恢复 backend/data"
fi
exec bash "${root}/deploy/manage.sh" "$@" </dev/tty
}
bootstrap_repo() {
if repo_ready "${INSTALL_ROOT}"; then
REPO_ROOT="${INSTALL_ROOT}"
DEPLOY_DIR="${REPO_ROOT}/deploy"
LIB_DIR="${DEPLOY_DIR}/lib"
sync_repo_if_present "${REPO_ROOT}"
# pull 后可能才有 lib,再校验一次
if ! repo_ready "${REPO_ROOT}"; then
heal_existing_install "${INSTALL_ROOT}" "$@"
fi
return 0
fi
if [[ -n "${REPO_ROOT}" ]] && repo_ready "${REPO_ROOT}"; then
@@ -56,27 +127,22 @@ bootstrap_repo() {
return 0
fi
# 目录在,但还是旧版(你当前的情况)
if [[ -d "${INSTALL_ROOT}" ]]; then
if [[ "$(id -u)" -ne 0 ]]; then
echo "错误: 请使用 root 执行" >&2
exit 1
fi
heal_existing_install "${INSTALL_ROOT}" "$@"
fi
echo "eth_hedge_sim 部署管理器 — 首次自举"
echo "将克隆到: ${INSTALL_ROOT}"
if [[ "$(id -u)" -ne 0 ]]; then
echo "错误: 请使用 root 执行" >&2
exit 1
fi
if ! command -v git >/dev/null 2>&1; then
if command -v apt-get >/dev/null 2>&1; then
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y git ca-certificates curl
else
echo "错误: 未找到 git" >&2
exit 1
fi
fi
if [[ -d "${INSTALL_ROOT}" ]]; then
echo "错误: ${INSTALL_ROOT} 已存在但不是有效仓库" >&2
echo "请手动处理后再运行,或设置 INSTALL_ROOT 指向其它路径" >&2
exit 1
fi
ensure_git_cli
mkdir -p "$(dirname "${INSTALL_ROOT}")"
git clone -b "${GIT_BRANCH}" "${GIT_URL}" "${INSTALL_ROOT}"
# clone 后用仓库内脚本 + 终端 stdin(管道已 EOF)