#!/usr/bin/env bash # ============================================================================= # iPass 控制面自更新(在 .11 上执行) # # 干的事:从 Gitea 下载新版 ipass-agent.py → 备份旧版 → 语法校验 → # 停旧控制面 → 按原参数重启 → /health 自检 # # 用法(在 .11 上、用跑控制面的那个账号): # bash <(curl -fsSL http://192.168.1.18:13300/ipass/ipass/raw/branch/main/deploy/update-agent-face.sh) # 或分两步: # curl -fsSL http://192.168.1.18:13300/ipass/ipass/raw/branch/main/deploy/update-agent-face.sh -o /tmp/uaf.sh # bash /tmp/uaf.sh # # 安全性: # - 只动控制面进程(python3 ipass-agent.py),网关容器不受任何影响 # - 旧脚本备份为 <路径>.bak-<时间戳>,随时可拷回原位 + 重跑本脚本回退 # - token 存 ~/ipass/.ipass-agent-token,重启沿用 → 远端 agent.sh 无需改任何配置 # ============================================================================= set -euo pipefail RAW_BASE="http://192.168.1.18:13300/ipass/ipass/raw" # 主/备两种 Gitea raw 路径形态(不同版本分支写法不同,逐个试) RAW_PATHS=( "$RAW_BASE/branch/main/deploy/ipass-agent.py" "$RAW_BASE/refs/heads/main/deploy/ipass-agent.py" ) c_ok() { printf '\033[32m✓\033[0m %s\n' "$*"; } c_err() { printf '\033[31m✗\033[0m %s\n' "$*" >&2; } c_warn(){ printf '\033[33m!\033[0m %s\n' "$*"; } step() { printf '\n\033[36m==> %s\033[0m\n' "$*"; } # ---------------------------------------------------------------- 1. 定位现有控制面 step "1/6 定位控制面脚本" DEST="" OLD_ARGS="" # 优先:从正在跑的进程反推(最准,还能原样保留启动参数) for pid in $(pgrep -f "ipass-agent.py" 2>/dev/null || true); do cmd="$(tr '\0' ' ' <"/proc/$pid/cmdline" 2>/dev/null || true)" case "$cmd" in *--stop*) continue ;; # 排除恰好同名的一次性调用 esac # cmdline 形如 "python3 /path/ipass-agent.py --bg --name ipass ..." set -- $cmd shift # 去掉解释器 [ "${1:-}" = "python" ] || [ "${1:-}" = "python3" ] && shift cand="${1:-}" [ -f "$cand" ] && DEST="$cand" # 保留除 --bg / --stop 外的启动参数,重启时原样带上 shift for a in "$@"; do case "$a" in --bg|--stop) ;; *) OLD_ARGS="$OLD_ARGS $a" ;; esac done break done # 兜底:常见位置 if [ -z "$DEST" ]; then for cand in "$HOME/ipass/ipass-agent.py" "$HOME/ipass-agent.py"; do [ -f "$cand" ] && DEST="$cand" && break done fi if [ -z "$DEST" ]; then c_warn "没找到正在跑的控制面,按约定装到 ~/ipass/ipass-agent.py(首次部署)" mkdir -p "$HOME/ipass" DEST="$HOME/ipass/ipass-agent.py" fi c_ok "目标: $DEST${OLD_ARGS:+(原启动参数:$OLD_ARGS)}" # ---------------------------------------------------------------- 2. 备份 step "2/6 备份旧版" TS="$(date +%Y%m%d-%H%M%S)" if [ -f "$DEST" ]; then cp "$DEST" "$DEST.bak-$TS" c_ok "已备份 → $DEST.bak-$TS" else c_warn "原文件不存在,跳过备份" fi # ---------------------------------------------------------------- 3. 下载新版 step "3/6 下载新版 ipass-agent.py" TMP="$(mktemp /tmp/ipass-agent.XXXXXX.py)" GOT="" for url in "${RAW_PATHS[@]}"; do if curl -fsSL --connect-timeout 5 -m 30 "$url" -o "$TMP" 2>/dev/null; then GOT="$url"; break fi done if [ -z "$GOT" ] || ! grep -q "ipass-agent" "$TMP" || ! grep -q "def " "$TMP"; then c_err "下载失败或内容不对(Gitea: 192.168.1.18:13300 不可达?)" c_err "手动方式:scp 本仓库 deploy/ipass-agent.py 到 $DEST 后重跑本脚本" rm -f "$TMP"; exit 1 fi c_ok "已下载 ← ${GOT#*$RAW_BASE/} ($(wc -c <"$TMP") 字节)" # ---------------------------------------------------------------- 4. 语法校验 step "4/6 语法校验" if ! python3 -m py_compile "$TMP" 2>/tmp/ipass-agent-pycompile.err; then c_err "新版脚本 py_compile 不通过,已中止(旧版未动):" sed 's/^/ /' /tmp/ipass-agent-pycompile.err >&2 rm -f "$TMP"; exit 1 fi mv "$TMP" "$DEST" c_ok "已替换 $DEST" # ---------------------------------------------------------------- 5. 重启 step "5/6 重启控制面" python3 "$DEST" --stop >/dev/null 2>&1 || true sleep 1 # shellcheck disable=SC2086 python3 "$DEST" --bg$OLD_ARGS sleep 2 # ---------------------------------------------------------------- 6. 自检 step "6/6 自检" OK="" for _ in $(seq 1 10); do if curl -sf -m 3 http://127.0.0.1:18099/health -o /tmp/ipass-agent-health.json 2>/dev/null; then OK=1; break fi sleep 1 done if [ -n "$OK" ]; then c_ok "控制面已就绪: $(cat /tmp/ipass-agent-health.json)" printf '\n下一步(远端/本机任意一处):\n' printf ' bash deploy/agent.sh update ipass-1.0.7 # 新容器将自动挂审计卷\n' else c_err "10 秒未就绪,查日志: tail -50 ~/ipass/ipass-agent.log" exit 1 fi