Harden Mnemazine production gates

This commit is contained in:
Зарубин Филипп
2026-06-24 12:12:36 +03:00
parent 1a9b73614f
commit dd6b69b893
30 changed files with 1278 additions and 127 deletions
+41 -14
View File
@@ -10,19 +10,35 @@ REPO="${MNEMAZINE_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
[ -f "$REPO/.mnemazine/config.env" ] && . "$REPO/.mnemazine/config.env"
# Non-secret personal overrides (e.g. MNEMAZINE_INBOX), gitignored.
[ -f "$REPO/.mnemazine/config.local.sh" ] && . "$REPO/.mnemazine/config.local.sh"
VPS="${MNEMAZINE_VPS:-root@YOUR_VPS_HOST}"
VPS="${MNEMAZINE_VPS:-deploy@YOUR_VPS_HOST}"
KEY="${MNEMAZINE_VPS_KEY:-$HOME/.ssh/id_rsa}"
REMOTE_INBOX="${MNEMAZINE_REMOTE_INBOX:-/var/www/mnemazine-inbox}"
REMOTE_REPORTS="${MNEMAZINE_REMOTE_REPORTS:-/var/www/mnemazine-reports}"
REMOTE_INBOX="${MNEMAZINE_REMOTE_INBOX:-mnemazine/inbox}"
REMOTE_REPORTS="${MNEMAZINE_REMOTE_REPORTS:-mnemazine/reports}"
SSH_BIN="${MNEMAZINE_SSH_BIN:-ssh}"
SYNC_BIN="${MNEMAZINE_TELEGRAM_SYNC_BIN:-$REPO/scripts/mnemazine-telegram-sync.sh}"
# ponytail: fail fast if still on the placeholder — beats a confusing ssh error.
if [ "$VPS" = "root@YOUR_VPS_HOST" ]; then
if [ "$VPS" = "deploy@YOUR_VPS_HOST" ]; then
echo "Set MNEMAZINE_VPS (and MNEMAZINE_VPS_KEY) in $REPO/.mnemazine/config.env" >&2
exit 1
fi
SSH="ssh -i $KEY -o StrictHostKeyChecking=no -o ConnectTimeout=10"
MARKER="$REPO/.mnemazine/.last-daily"
if [ "${MNEMAZINE_REMOTE_MUTATION:-0}" != "1" ]; then
echo "Set MNEMAZINE_REMOTE_MUTATION=1 after reviewing VPS paths; poll mutates remote flags/reports." >&2
exit 1
fi
ATTEMPT_MARKER="$REPO/.mnemazine/.last-daily-attempt"
COMPLETED_MARKER="$REPO/.mnemazine/.last-daily-completed"
MAX_DAILY_ATTEMPTS="${MNEMAZINE_DAILY_MAX_ATTEMPTS:-3}"
DAILY_RETRY_SECONDS="${MNEMAZINE_DAILY_RETRY_SECONDS:-3600}"
mkdir -p "$REPO/.mnemazine"
KNOWN_HOSTS="${MNEMAZINE_SSH_KNOWN_HOSTS:-$REPO/.mnemazine/known_hosts}"
touch "$KNOWN_HOSTS"
HOST="${VPS#*@}"
if ! ssh-keygen -F "$HOST" -f "$KNOWN_HOSTS" >/dev/null 2>&1; then
echo "VPS host key is not pinned. Run: MNEMAZINE_VPS=$VPS MNEMAZINE_VPS_HOST_FINGERPRINT=SHA256:... bash $REPO/scripts/mnemazine-pin-vps-host.sh" >&2
exit 1
fi
SSH="$SSH_BIN -i $KEY -o UserKnownHostsFile=$KNOWN_HOSTS -o StrictHostKeyChecking=yes -o ConnectTimeout=10"
# Single-flight lock. macOS has no flock, so use an atomic mkdir lock dir.
# Steal a stale lock (>60 min) left by a killed run.
@@ -45,23 +61,34 @@ if [ -n "$queue" ]; then
[ -d "$REPO/reports" ] && rsync -az -e "$SSH" "$REPO/reports/" "$VPS:$REMOTE_REPORTS/" 2>/dev/null || true
fi
run=0; manual=0
run=0; manual=0; daily=0
# Consume run-now flag atomically: print + delete in one ssh.
flag="$($SSH "$VPS" "f=$REMOTE_INBOX/.run-now; if [ -f \"\$f\" ]; then cat \"\$f\"; rm -f \"\$f\"; fi" 2>/dev/null || true)"
[ -n "$flag" ] && { run=1; manual=1; }
today="$(date +%F)"
if [ "$(cat "$MARKER" 2>/dev/null || true)" != "$today" ] && [ "$(date +%H)" -ge 9 ]; then
run=1
# Mark the daily attempt BEFORE syncing: a failed run must not re-fire every
# 5 min for the rest of the day (storm). One attempt per day.
echo "$today" > "$MARKER"
today="${MNEMAZINE_POLL_TODAY:-$(date +%F)}"
hour="${MNEMAZINE_POLL_HOUR:-$(date +%H)}"
now_epoch="${MNEMAZINE_POLL_EPOCH:-$(date +%s)}"
if [ "$(cat "$COMPLETED_MARKER" 2>/dev/null || true)" != "$today" ] && [ "$hour" -ge 9 ]; then
read -r attempt_day attempt_count attempt_epoch < "$ATTEMPT_MARKER" 2>/dev/null || true
if [ "${attempt_day:-}" != "$today" ]; then
attempt_count=0
attempt_epoch=0
fi
retry_wait=$((now_epoch - ${attempt_epoch:-0}))
if [ "${attempt_count:-0}" -lt "$MAX_DAILY_ATTEMPTS" ] && { [ "${attempt_count:-0}" -eq 0 ] || [ "$retry_wait" -ge "$DAILY_RETRY_SECONDS" ]; }; then
run=1
daily=1
attempt_count=$((attempt_count + 1))
printf '%s %s %s\n' "$today" "$attempt_count" "$now_epoch" > "$ATTEMPT_MARKER"
fi
fi
[ "$run" -eq 0 ] && exit 0
if bash "$REPO/scripts/mnemazine-telegram-sync.sh"; then
if bash "$SYNC_BIN"; then
$SSH "$VPS" "echo $(date -u +%FT%TZ) > $REMOTE_INBOX/.last-run" 2>/dev/null || true
[ "$daily" = "1" ] && echo "$today" > "$COMPLETED_MARKER"
else
# A manual run-now we already consumed shouldn't vanish on failure — re-queue it.
[ "$manual" = "1" ] && $SSH "$VPS" "echo retry > $REMOTE_INBOX/.run-now" 2>/dev/null || true