Files

109 lines
4.5 KiB
Bash
Raw Permalink Normal View History

2026-11-18 15:18:38 +01:00
#!/usr/bin/env bash
# One-time (and re-runnable) server setup for Robot Clash. Run ON the server as root:
# bash setup.sh [domain] default: roboclash.michael-media.de
# Installs Node 24 + Caddy (auto-TLS), creates the service user, the release layout, the hardened systemd
# service, the firewall and a daily DB backup. Migrates the old /opt/robotclash/{game,server} layout.
# Afterwards deploy from your machine: bash server/deploy/deploy.sh --reset-db
2026-11-18 15:18:38 +01:00
set -euo pipefail
DOMAIN="${1:-roboclash.michael-media.de}"
APP=/opt/robotclash
SVC=robotclash
2026-11-18 15:18:38 +01:00
echo ">> Robot Clash setup for $DOMAIN"
# --- Node 24 LTS (Node 20 is end-of-life since 2026-04-30) ----------------------------------------
if ! node -v 2>/dev/null | grep -q '^v24\.'; then
curl -fsSL https://deb.nodesource.com/setup_24.x | bash -
2026-11-18 15:18:38 +01:00
apt-get install -y nodejs
fi
apt-get install -y build-essential python3 ufw sqlite3 curl
2026-11-18 15:18:38 +01:00
# --- Caddy (reverse proxy + automatic HTTPS) ------------------------------------------------------
if ! command -v caddy >/dev/null 2>&1; then
apt-get install -y debian-keyring debian-archive-keyring apt-transport-https gpg
2026-11-18 15:18:38 +01:00
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list >/dev/null
apt-get update
apt-get install -y caddy
fi
# --- service user + layout: releases/<time>-<commit>, current -> active release, shared/ (.env + DB) --
id "$SVC" >/dev/null 2>&1 || useradd --system --home-dir "$APP" --shell /usr/sbin/nologin "$SVC"
mkdir -p "$APP/releases" "$APP/shared" "$APP/backups"
2026-11-18 15:18:38 +01:00
# old layout (code + .env + DB directly in /opt/robotclash/server): keep the secret, park the rest
if [ -f "$APP/server/.env" ] && [ ! -f "$APP/shared/.env" ]; then cp "$APP/server/.env" "$APP/shared/.env"; fi
if [ -d "$APP/server" ] || [ -d "$APP/game" ]; then
systemctl stop "$SVC" 2>/dev/null || true
LEG="$APP/legacy-$(date +%F-%H%M)"; mkdir -p "$LEG"
[ -d "$APP/server" ] && mv "$APP/server" "$LEG/"
[ -d "$APP/game" ] && mv "$APP/game" "$LEG/"
echo ">> old layout moved to $LEG (delete it once the new setup runs)"
fi
if [ ! -f "$APP/shared/.env" ]; then
printf 'PORT=8080\nJWT_SECRET=%s\n' "$(head -c 32 /dev/urandom | base64 | tr -d '\n')" > "$APP/shared/.env"
echo ">> wrote $APP/shared/.env (random JWT_SECRET)"
2026-11-18 15:18:38 +01:00
fi
# keys the new server no longer uses; DB lives in shared/
sed -i '/^DB_FILE=/d;/^ADMIN_UID=/d;/^ANTICHEAT=/d' "$APP/shared/.env"
echo "DB_FILE=$APP/shared/data.sqlite" >> "$APP/shared/.env"
grep -q '^PORT=' "$APP/shared/.env" || echo 'PORT=8080' >> "$APP/shared/.env"
chown -R "$SVC:$SVC" "$APP/shared" "$APP/backups"
chmod 700 "$APP/shared"; chmod 600 "$APP/shared/.env"
2026-11-18 15:18:38 +01:00
# --- systemd service (no root, read-only system, writes only to shared/) --------------------------
cat > "/etc/systemd/system/$SVC.service" <<EOF
2026-11-18 15:18:38 +01:00
[Unit]
Description=Robot Clash server
After=network.target
[Service]
User=$SVC
Group=$SVC
WorkingDirectory=$APP/current/server
EnvironmentFile=$APP/shared/.env
EnvironmentFile=-$APP/current/server/.release
Environment=NODE_ENV=development
2026-11-18 15:18:38 +01:00
ExecStart=/usr/bin/node app.js
Restart=always
RestartSec=2
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
ReadWritePaths=$APP/shared
2026-11-18 15:18:38 +01:00
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable "$SVC" >/dev/null
2026-11-18 15:18:38 +01:00
# --- Caddy site: TLS + proxy; WebSocket /ws is proxied as-is (no compression on the socket) --------
2026-11-18 15:18:38 +01:00
cat > /etc/caddy/Caddyfile <<EOF
$DOMAIN {
@compress not path /ws
encode @compress gzip
2026-11-18 15:18:38 +01:00
reverse_proxy localhost:8080
}
EOF
systemctl reload caddy 2>/dev/null || systemctl restart caddy
# --- firewall -------------------------------------------------------------------------------------
ufw allow 22 >/dev/null; ufw allow 80 >/dev/null; ufw allow 443 >/dev/null
ufw --force enable >/dev/null
# --- daily SQLite backup (keep 14) ----------------------------------------------------------------
cat > /etc/cron.daily/robotclash-backup <<EOF
#!/usr/bin/env bash
D=$APP/backups
[ -f $APP/shared/data.sqlite ] && sqlite3 $APP/shared/data.sqlite ".backup '\$D/daily-\$(date +%F).sqlite'"
ls -1t "\$D"/daily-*.sqlite 2>/dev/null | tail -n +15 | xargs -r rm
ls -1t "\$D"/pre-*.sqlite 2>/dev/null | tail -n +11 | xargs -r rm
2026-11-18 15:18:38 +01:00
EOF
chmod +x /etc/cron.daily/robotclash-backup
echo ">> Setup done. node $(node -v), caddy $(caddy version | cut -d' ' -f1)"
echo ">> Next (from your machine): bash server/deploy/deploy.sh --reset-db"