SkateSpots/scripts/deploy-community-bot.sh

53 lines
2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Build + deploy the SkateSpots Community Cordn bot to the relay host.
#
# scripts/deploy-community-bot.sh
# scripts/deploy-community-bot.sh --no-build
#
# The host defaults to the ssh alias `skatespots-cvm` (same VPS as the relay).
# Override with SKATESPOTS_COMMUNITY_HOST / SKATESPOTS_COMMUNITY_DIR.
set -euo pipefail
cd "$(dirname "$0")/.."
HOST="${SKATESPOTS_COMMUNITY_HOST:-skatespots-cvm}"
REMOTE_DIR="${SKATESPOTS_COMMUNITY_DIR:-/opt/skatespots-community}"
REMOTE_STATE="${SKATESPOTS_COMMUNITY_STATE:-/var/lib/skatespots-community/state.json}"
ARTIFACT="build/community-bot.cjs"
if [[ "${1:-}" != "--no-build" ]]; then
scripts/build-community-bot.sh
fi
[[ -f "$ARTIFACT" ]] || { echo "missing $ARTIFACT (run without --no-build first)" >&2; exit 1; }
echo "→ uploading to $HOST:$REMOTE_DIR"
scp -q "$ARTIFACT" "$HOST:/tmp/community-bot.cjs.new"
echo "→ installing + restarting skatespots-community"
ssh "$HOST" "REMOTE_DIR='$REMOTE_DIR' bash -s" <<'REMOTE'
set -euo pipefail
cd "$REMOTE_DIR"
sudo -n cp community-bot.cjs "community-bot.cjs.bak-$(date +%Y%m%d-%H%M%S)"
sudo -n mv /tmp/community-bot.cjs.new community-bot.cjs
sudo -n chmod 644 community-bot.cjs
# Keep the five newest backups.
ls -1t community-bot.cjs.bak-* 2>/dev/null | tail -n +6 | xargs -r sudo -n rm -f
sudo -n systemctl restart skatespots-community
sleep 3
if ! systemctl is-active --quiet skatespots-community; then
echo "✗ skatespots-community is not active" >&2
journalctl -u skatespots-community -n 30 --no-pager >&2 || true
exit 1
fi
echo "✓ skatespots-community active"
journalctl -u skatespots-community -n 8 --no-pager | tail -8
REMOTE
echo "→ refreshing the 0600 state backup in ~/.config/skatespots"
mkdir -p "$HOME/.config/skatespots"
chmod 700 "$HOME/.config/skatespots"
if scp -q "$HOST:$REMOTE_STATE" "$HOME/.config/skatespots/community-state.json"; then
chmod 600 "$HOME/.config/skatespots/community-state.json"
echo "✓ state backup updated"
else
echo "⚠ could not copy the bot state (service is still running)" >&2
fi