pingql/deploy.sh

103 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# PingQL Deploy Script
# Usage: ./deploy.sh [web|api|monitor|db|nuke-db|all] [...]
# Example: ./deploy.sh web api
# Example: ./deploy.sh all
# Example: ./deploy.sh nuke-db (wipes all data — NOT included in "all")
set -e
SSH="ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519"
DB_HOST="root@142.132.190.209"
API_HOST="root@88.99.123.102"
WEB_HOST="root@78.47.43.36"
MONITOR_HOSTS=("root@5.161.76.127" "root@5.78.178.12" "root@5.223.51.251" "root@49.13.118.44")
deploy_db() {
echo "[db] Restarting PostgreSQL on database-eu-central..."
$SSH $DB_HOST "systemctl restart postgresql && echo 'PostgreSQL restarted'"
}
nuke_db() {
echo "⚠️ This will drop all tables in the pingql database, deleting ALL data."
echo " Tables will be recreated on next API/web restart via migrate()."
read -p "Type 'yes' to confirm: " confirm
if [ "$confirm" != "yes" ]; then
echo "Aborted."
return
fi
echo "[nuke-db] Dropping all tables on database-eu-central..."
$SSH $DB_HOST bash << 'REMOTE'
sudo -u postgres psql -d pingql -c "DROP TABLE IF EXISTS pings, api_keys, monitors, accounts CASCADE;"
echo "All tables dropped"
REMOTE
echo "[nuke-db] Done. Tables will be recreated on next API/web restart."
}
deploy_api() {
echo "[api] Deploying to api-eu-central..."
$SSH $API_HOST bash << 'REMOTE'
cd /opt/pingql
git pull
cd apps/api
/root/.bun/bin/bun install
systemctl restart pingql-api
systemctl restart caddy
echo "API deployed and restarted"
REMOTE
}
deploy_web() {
echo "[web] Deploying to web-eu-central..."
$SSH $WEB_HOST bash << 'REMOTE'
cd /opt/pingql
git pull
cd apps/web
/root/.bun/bin/bun install
/root/.bun/bin/bun run css
systemctl restart pingql-web
systemctl restart caddy
echo "Web deployed and restarted"
REMOTE
}
deploy_monitor() {
echo "[monitor] Deploying to all 4 monitors in parallel..."
for host in "${MONITOR_HOSTS[@]}"; do
(
echo "[monitor] Starting deploy on $host..."
$SSH $host bash << 'REMOTE'
cd /opt/pingql
git pull
cd apps/monitor
/root/.cargo/bin/cargo build --release
systemctl restart pingql-monitor
echo "Monitor deployed and restarted on $(hostname)"
REMOTE
) &
done
wait
echo "[monitor] All monitors deployed"
}
# Parse args
if [ $# -eq 0 ]; then
echo "Usage: $0 [web|api|monitor|db|all] [...]"
exit 1
fi
for arg in "$@"; do
case "$arg" in
db) deploy_db ;;
api) deploy_api ;;
web) deploy_web ;;
monitor) deploy_monitor ;;
nuke-db) nuke_db ;;
all) deploy_db; deploy_api; deploy_web; deploy_monitor ;;
*) echo "Unknown target: $arg (valid: web, api, monitor, db, nuke-db, all)"; exit 1 ;;
esac
done
echo "Deploy complete."