Files
awoooi/scripts/repair-bot/repair-bot-110.sh
OG T 4b24ecd67f fix(sprint3): 首席架構師 Review C1/C2/C3/M3/m1 修正
C1: _ssh_execute 直接接收 key_path 參數,不反查 LAYER_SSH_CONFIG
C2: PlaybookService.create() proxy,Router 不再穿透呼叫 _repository
C3: CD Step 1b sed 替換 IMAGE_TAG_PLACEHOLDER,消除失敗中斷風險
M3: repair-bot 110/188 regex 統一 [a-z0-9][a-z0-9-]{0,30},禁止底線
m1: defaultMode 0400 加八進位說明注釋
m2: _ssh_execute 用 deadline 計算剩餘 timeout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-05 13:07:59 +08:00

68 lines
1.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# scripts/repair-bot/repair-bot-110.sh
# 修復機器人白名單腳本 — 110 主機 (DevOps 金庫)
# 2026-04-05 Claude Code: Sprint 3 Host Auto-Repair
#
# 安全設計:
# - SSH authorized_keys 的 command= 指向此腳本
# - 只允許執行 COMPOSE_DIRS 中定義的修復指令
# - 格式: repair:<component>
# - SSH key 洩漏也只能執行白名單內的 docker compose up -d
#
# 部署位置: /home/wooo/bin/repair-bot-110.sh (on 192.168.0.110)
# 使用者: wooo
LOG="${HOME}/.repair-bot.log"
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG"; }
# 白名單: component → compose dir
declare -A COMPOSE_DIRS=(
["sentry"]="/opt/sentry"
["harbor"]="/home/wooo/harbor/harbor"
["gitea"]="/home/wooo/gitea"
["gitea-runner"]="/home/wooo/act-runner"
["langfuse"]="/home/wooo/langfuse"
["alertmanager"]="/home/wooo/monitoring"
["signoz"]="/home/wooo/signoz/deploy/docker"
)
CMD="${SSH_ORIGINAL_COMMAND:-}"
log "repair-bot-110 invoked: CMD=$CMD"
if [[ "$CMD" =~ ^repair:([a-z0-9][a-z0-9-]{0,30})$ ]]; then # M3: 統一 Python 端 regex禁止底線
COMPONENT="${BASH_REMATCH[1]}"
DIR="${COMPOSE_DIRS[$COMPONENT]}"
if [ -z "$DIR" ]; then
log "DENIED: unknown component '$COMPONENT'"
echo "REPAIR_DENIED:unknown_component:$COMPONENT"
exit 1
fi
if [ ! -d "$DIR" ]; then
log "DENIED: directory not found '$DIR'"
echo "REPAIR_DENIED:dir_not_found:$DIR"
exit 1
fi
log "EXECUTING: cd $DIR && docker compose up -d"
cd "$DIR" && docker compose up -d 2>&1 | tail -5
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
log "REPAIR_OK: $COMPONENT"
echo "REPAIR_OK:$COMPONENT"
else
log "REPAIR_FAIL: $COMPONENT (exit $EXIT_CODE)"
echo "REPAIR_FAIL:$COMPONENT:exit_$EXIT_CODE"
exit 1
fi
elif [ "$CMD" = "health" ]; then
# 健康檢查 — 允許連線測試
echo "REPAIR_BOT_HEALTHY:110"
else
log "DENIED: invalid command '$CMD'"
echo "REPAIR_DENIED:invalid_command"
exit 1
fi