ba-auto-daily/pull_logs.sh

55 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Pulls ~/ba_logs/ (cron run logs -- daily.log, q4h.log, etc., see
# ba_cron_run.sh and CLAUDE.md's "Scheduled runs (cron)" section) from
# nik-gpu into this repo's scratchpad/ba_logs/ for local reading (e.g. in
# VS Code). Local dev tooling only, not part of the ba_dailies.sh
# game-automation launcher -- same category as setup.sh/clean_scratchpad.sh.
#
# After a successful pull, also deletes the just-synced *.log files on the
# remote side (per explicit user request), so ~/ba_logs/ doesn't grow
# forever. This makes the remote side no longer read-only, unlike before --
# gated behind ba_cron_run.sh's own shared flock (~/ba_logs/ba_dailies.lock)
# to stay safe: that script holds this exact lock for its ENTIRE run,
# wrapping the whole `>> "$LOG_FILE"` append span, which can last many
# minutes (e.g. the "daily" preset's arena fights/event sweeps). If this
# script's own non-blocking flock attempt on the same lock file fails, a
# cron run is currently in progress -- deletion is skipped for this
# invocation (the local copy already pulled above is kept regardless)
# rather than risking `rm` unlinking a log file out from under an open
# write fd, which would silently lose that run's remaining log output to
# an unlinked inode with no error and no way to recover it.
#
# Usage:
# ./pull_logs.sh [host]
#
# host defaults to nik-gpu (the ssh alias used throughout this project).
HOST="${1:-nik-gpu}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEST="$SCRIPT_DIR/scratchpad/ba_logs"
mkdir -p "$DEST"
rsync -av "$HOST:ba_logs/" "$DEST/"
echo "Synced $HOST:~/ba_logs/ -> $DEST"
DELETE_RESULT="$(ssh "$HOST" bash -s <<'REMOTE'
LOCK_FILE="$HOME/ba_logs/ba_dailies.lock"
exec 9>"$LOCK_FILE"
if flock -n 9; then
rm -f "$HOME"/ba_logs/*.log
echo "DELETED"
else
echo "SKIPPED"
fi
REMOTE
)"
if [ "$DELETE_RESULT" = "DELETED" ]; then
echo "Deleted *.log on $HOST:~/ba_logs/ (already synced above)"
else
echo "Skipped remote log deletion -- a cron run is currently in progress on $HOST (local copy above is still up to date; rerun later to clear the remote logs)"
fi