#!/usr/bin/env bash # 双插件 VM 部署脚本。 # # 把错题本里逐条踩出来的部署纪律固化成步骤,避免每次部署重新暴露同一批历史问题: # - 回滚副本留在插件扫描目录里 → AstrBot 当成插件去导入,启动日志出 ERRO # - 属主/权限不对(0700)→ 服务读不到 main.py # - 旧 pycache 权限 → compileall 误报 # - 先部署陪伴后部署记忆 → 陪伴启动时对端不在,桥探测落进负缓存 # - 备份指针不更新 → 回滚会把之后的真机修复一并抹掉 # # 用法: # ops/deploy-plugin.sh [文件...] # 不给文件 = 整目录替换;给文件 = 只替换这些文件(相对插件根目录) # 环境变量 VM 指定目标主机,默认 astrbot-vm(需已配置 ssh 免密) # # 示例: # ops/deploy-plugin.sh companion daily_state.py planning.py # ops/deploy-plugin.sh both set -euo pipefail VM="${VM:-astrbot-vm}" TARGET="${1:-}" shift || true FILES=("$@") # 本脚本随陪伴插件仓库走;两个插件仓库是项目根目录下的同级目录 COMPANION_SRC="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" PROJECT_ROOT="$(cd "$COMPANION_SRC/.." && pwd)" MEMORY_SRC="$PROJECT_ROOT/astrbot_plugin_memory_companion-main" if [ ! -d "$MEMORY_SRC" ]; then echo "错误: 找不到记忆插件仓库 $MEMORY_SRC(两个仓库应是项目根下的同级目录)" >&2 exit 1 fi PLUGIN_DIR=/var/lib/astrbot/data/plugins BACKUP_ROOT=/var/lib/astrbot/ops-replace-backups PY=/var/lib/astrbot/.local/share/uv/tools/astrbot/bin/python # 服务以 astrbot 身份运行(systemd User=astrbot),所以属主必须是 astrbot:astrbot。 # 错题本早期写过 root:root,那是错的——按实机与 systemd 配置为准。 OWNER=astrbot:astrbot STAMP="$(date +%Y%m%d-%H%M%S)" die() { echo "错误: $*" >&2; exit 1; } case "$TARGET" in memory|companion|both) ;; *) die "用法: $0 [文件...]" ;; esac if [ "$TARGET" = both ] && [ ${#FILES[@]} -gt 0 ]; then die "both 模式不接受文件列表:两个插件的文件名会混在一起" fi echo "==> 目标主机 $VM 备份时间戳 $STAMP" # ---------- 1. 本地门禁:不让没通过测试的代码上机 ---------- echo "==> 本地测试" if [ "$TARGET" = companion ] || [ "$TARGET" = both ]; then (cd "$COMPANION_SRC" && python -m pytest tests -q) || die "陪伴侧测试未通过" fi if [ "$TARGET" = memory ] || [ "$TARGET" = both ]; then (cd "$MEMORY_SRC" && python -m pytest tests -q) || die "记忆侧测试未通过" fi # ---------- 2. 远端备份,并把回滚指针指向这次 ---------- BACKUP="$BACKUP_ROOT/deploy-$STAMP" echo "==> 远端备份 $BACKUP" ssh -o BatchMode=yes "$VM" "sudo mkdir -p '$BACKUP' for name in astrbot_plugin_memory_companion_ops astrbot_plugin_private_companion_ops; do sudo tar czf '$BACKUP'/plugin-\$name.tar.gz -C '$PLUGIN_DIR' \$name sudo cp -r '$PLUGIN_DIR'/\$name '$BACKUP'/before-\$name done sudo cp /var/lib/astrbot/data/config/astrbot_plugin_*_ops_config.json '$BACKUP'/ 2>/dev/null || true # 回滚副本一律落在备份区,绝不留在插件扫描目录里 echo '$BACKUP' | sudo tee '$BACKUP_ROOT/LATEST_M6' >/dev/null" push_one() { local src="$1" name="$2" local dest="$PLUGIN_DIR/$name" if [ ${#FILES[@]} -eq 0 ]; then echo "==> 整目录替换 $name" tar czf - -C "$src" --exclude=__pycache__ --exclude=.git --exclude=.tmp . \ | ssh -o BatchMode=yes "$VM" "cat > /tmp/$name.tar.gz" ssh -o BatchMode=yes "$VM" "sudo rm -rf '$dest' && sudo mkdir -p '$dest' \ && sudo tar xzf /tmp/$name.tar.gz -C '$dest' && rm -f /tmp/$name.tar.gz" else echo "==> 增量替换 $name: ${FILES[*]}" for f in "${FILES[@]}"; do [ -f "$src/$f" ] || die "本地不存在: $src/$f" scp -q "$src/$f" "$VM:/tmp/$(basename "$f")" ssh -o BatchMode=yes "$VM" "sudo mkdir -p \"\$(dirname '$dest/$f')\" \ && sudo cp '/tmp/$(basename "$f")' '$dest/$f' && rm -f '/tmp/$(basename "$f")'" done fi ssh -o BatchMode=yes "$VM" "sudo chown -R $OWNER '$dest' sudo find '$dest' -type d -exec chmod 755 {} + sudo find '$dest' -type f -exec chmod 644 {} + # 扫描目录里不允许留任何非插件目录(.old/.new/.bak 会被当成插件导入) leftovers=\$(sudo find '$PLUGIN_DIR' -maxdepth 1 -name '*.old' -o -maxdepth 1 -name '*.new' -o -maxdepth 1 -name '*.bak' | head) if [ -n \"\$leftovers\" ]; then echo \"错误: 插件扫描目录残留 \$leftovers\" >&2; exit 1; fi # PYTHONPYCACHEPREFIX 避开旧 pycache 的权限问题 sudo -u astrbot env PYTHONPYCACHEPREFIX=/tmp/pycache-deploy '$PY' -m compileall -q '$dest' >/dev/null \ || { echo '错误: 远端 compileall 未通过' >&2; exit 1; }" } # ---------- 3. 停服 → 记忆先于陪伴 → 启动 ---------- echo "==> 停服" ssh -o BatchMode=yes "$VM" "sudo systemctl stop astrbot" # 顺序有讲究:陪伴启动时会探测记忆插件的桥,对端不在会落进负缓存 if [ "$TARGET" = memory ] || [ "$TARGET" = both ]; then push_one "$MEMORY_SRC" astrbot_plugin_memory_companion_ops fi if [ "$TARGET" = companion ] || [ "$TARGET" = both ]; then push_one "$COMPANION_SRC" astrbot_plugin_private_companion_ops fi echo "==> 启动" ssh -o BatchMode=yes "$VM" "sudo systemctl start astrbot" sleep 60 # ---------- 4. 验证 ---------- echo "==> 验证" ssh -o BatchMode=yes "$VM" ' state=$(systemctl is-active astrbot) restarts=$(systemctl show astrbot -p NRestarts --value) echo "服务: $state NRestarts=$restarts" [ "$state" = active ] || exit 1 plugin_errors=$(sudo journalctl -u astrbot --since "3 min ago" --no-pager 2>/dev/null \ | grep -E "ERRO|Traceback" | grep -ci companion || true) echo "插件相关错误: $plugin_errors" sudo journalctl -u astrbot --since "3 min ago" --no-pager 2>/dev/null \ | grep -E "Loading plugin astrbot_plugin_(memory|private)_companion_ops" || true [ "$plugin_errors" = "0" ] || { echo "存在插件错误,考虑回滚" >&2; exit 1; }' cat < 完成。回滚: BK=\$(sudo cat $BACKUP_ROOT/LATEST_M6) sudo systemctl stop astrbot sudo rm -rf $PLUGIN_DIR/astrbot_plugin_{memory,private}_companion_ops sudo cp -r "\$BK/before-astrbot_plugin_memory_companion_ops" $PLUGIN_DIR/astrbot_plugin_memory_companion_ops sudo cp -r "\$BK/before-astrbot_plugin_private_companion_ops" $PLUGIN_DIR/astrbot_plugin_private_companion_ops sudo chown -R $OWNER $PLUGIN_DIR/astrbot_plugin_{memory,private}_companion_ops sudo systemctl start astrbot 注意:QQ 官方平台对会话启动次数有限流,短时间内反复重启会让 qq_official 适配器登录失败(session_start_limit 为 None)。批量部署时请合并重启次数。 MSG