build(deploy): release-based deploy with health check and rollback, node 24, service user (P3-1b, P3-2, P3-3)
This commit is contained in:
@@ -1,69 +1,90 @@
|
||||
#!/usr/bin/env bash
|
||||
# One-time server setup for Robot Clash. Run ON the Hetzner box as root:
|
||||
# bash setup.sh # uses roboclash.michael-media.de
|
||||
# bash setup.sh my.domain.tld # custom domain
|
||||
# Installs Node + Caddy (auto-TLS), creates the systemd service, firewall and a daily DB backup.
|
||||
# Then push the code (deploy/push.ps1 from Windows) and: systemctl start robotclash
|
||||
# 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
|
||||
set -euo pipefail
|
||||
DOMAIN="${1:-roboclash.michael-media.de}"
|
||||
APP=/opt/robotclash
|
||||
SVC=robotclash
|
||||
|
||||
echo ">> Robot Clash setup for $DOMAIN"
|
||||
|
||||
# --- Node LTS -------------------------------------------------------------------------------------
|
||||
if ! command -v node >/dev/null 2>&1; then
|
||||
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
|
||||
# --- 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 -
|
||||
apt-get install -y nodejs
|
||||
fi
|
||||
apt-get install -y build-essential python3 git ufw sqlite3 curl
|
||||
apt-get install -y build-essential python3 ufw sqlite3 curl
|
||||
|
||||
# --- 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
|
||||
apt-get install -y debian-keyring debian-archive-keyring apt-transport-https gpg
|
||||
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
|
||||
|
||||
mkdir -p "$APP/game" "$APP/server" "$APP/backups"
|
||||
# --- 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"
|
||||
|
||||
# --- .env (random JWT secret, kept across deploys) ------------------------------------------------
|
||||
if [ ! -f "$APP/server/.env" ]; then
|
||||
cat > "$APP/server/.env" <<EOF
|
||||
PORT=8080
|
||||
JWT_SECRET=$(head -c 32 /dev/urandom | base64 | tr -d '\n')
|
||||
ADMIN_UID=
|
||||
DB_FILE=$APP/server/data.sqlite
|
||||
ANTICHEAT=0
|
||||
EOF
|
||||
echo ">> wrote $APP/server/.env (random JWT_SECRET)"
|
||||
# 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
|
||||
|
||||
# --- systemd service ------------------------------------------------------------------------------
|
||||
cat > /etc/systemd/system/robotclash.service <<EOF
|
||||
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)"
|
||||
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"
|
||||
|
||||
# --- systemd service (no root, read-only system, writes only to shared/) --------------------------
|
||||
cat > "/etc/systemd/system/$SVC.service" <<EOF
|
||||
[Unit]
|
||||
Description=Robot Clash server
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
WorkingDirectory=$APP/server
|
||||
EnvironmentFile=$APP/server/.env
|
||||
User=$SVC
|
||||
Group=$SVC
|
||||
WorkingDirectory=$APP/current/server
|
||||
EnvironmentFile=$APP/shared/.env
|
||||
EnvironmentFile=-$APP/current/server/.release
|
||||
Environment=NODE_ENV=development
|
||||
ExecStart=/usr/bin/node app.js
|
||||
Restart=always
|
||||
RestartSec=2
|
||||
User=root
|
||||
NoNewPrivileges=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
PrivateTmp=true
|
||||
ReadWritePaths=$APP/shared
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
systemctl daemon-reload
|
||||
systemctl enable robotclash
|
||||
systemctl enable "$SVC" >/dev/null
|
||||
|
||||
# --- Caddy site -----------------------------------------------------------------------------------
|
||||
# --- Caddy site: TLS + proxy; WebSocket /ws is proxied as-is (no compression on the socket) --------
|
||||
cat > /etc/caddy/Caddyfile <<EOF
|
||||
$DOMAIN {
|
||||
encode gzip
|
||||
@compress not path /ws
|
||||
encode @compress gzip
|
||||
reverse_proxy localhost:8080
|
||||
}
|
||||
EOF
|
||||
@@ -77,11 +98,11 @@ ufw --force enable >/dev/null
|
||||
cat > /etc/cron.daily/robotclash-backup <<EOF
|
||||
#!/usr/bin/env bash
|
||||
D=$APP/backups
|
||||
sqlite3 $APP/server/data.sqlite ".backup '\$D/data-\$(date +%F).sqlite'" 2>/dev/null || cp $APP/server/data.sqlite "\$D/data-\$(date +%F).sqlite" 2>/dev/null || true
|
||||
ls -1t "\$D"/data-*.sqlite 2>/dev/null | tail -n +15 | xargs -r rm
|
||||
[ -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
|
||||
EOF
|
||||
chmod +x /etc/cron.daily/robotclash-backup
|
||||
|
||||
echo ">> Setup done."
|
||||
echo ">> DNS: point $DOMAIN A-record to this server's IP."
|
||||
echo ">> Next: push the code (deploy/push.ps1 from Windows), then: systemctl start robotclash"
|
||||
echo ">> Setup done. node $(node -v), caddy $(caddy version | cut -d' ' -f1)"
|
||||
echo ">> Next (from your machine): bash server/deploy/deploy.sh --reset-db"
|
||||
|
||||
Reference in New Issue
Block a user