Game server manager: web UI for Valheim and Project Zomboid servers
FastAPI + vanilla JS app run as a Docker container behind Caddy (HTTPS by IP). install.sh installs or updates it on a server in one command, with preflight checks and Docker setup; deploy.sh pushes a local checkout to a server over SSH and runs install.sh there. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
cb9a6d1970
20 changed files with 3801 additions and 0 deletions
173
install.sh
Executable file
173
install.sh
Executable file
|
|
@ -0,0 +1,173 @@
|
|||
#!/usr/bin/env bash
|
||||
# Install or update the game server manager on this machine.
|
||||
#
|
||||
# curl -fsSL https://git.neilbantoc.dev/pub/game-servers/raw/branch/main/install.sh | sudo bash
|
||||
#
|
||||
# Settings (environment variables, all optional):
|
||||
# SITE address to serve the web UI on: a domain pointing here, or this machine's public IP.
|
||||
# Default: the existing install's address, otherwise this machine's public IPv4.
|
||||
# ADMIN_PASSWORD web UI password, used on first install only. Default: a random one, printed at the end.
|
||||
# GSM_REPO repository to install from (default below); GSM_REF the branch or tag (default main).
|
||||
# GSM_SRC install from this local checkout instead of downloading (deploy.sh uses this).
|
||||
# SKIP_DOCKER_INSTALL=1 fail instead of installing Docker when it's missing.
|
||||
set -euo pipefail
|
||||
|
||||
GSM_REPO=${GSM_REPO:-https://git.neilbantoc.dev/pub/game-servers}
|
||||
GSM_REF=${GSM_REF:-main}
|
||||
DIR=/opt/gsm
|
||||
DATA=/srv/gameservers
|
||||
|
||||
say() { printf '\033[1m==> %s\033[0m\n' "$*"; }
|
||||
warn() { printf '\033[33mwarning:\033[0m %s\n' "$*" >&2; }
|
||||
die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; }
|
||||
have() { command -v "$1" >/dev/null 2>&1; }
|
||||
random() { local s; s=$(head -c 4096 /dev/urandom | LC_ALL=C tr -dc "$1"); printf %s "${s:0:$2}"; }
|
||||
|
||||
# ---------- preflight ----------
|
||||
|
||||
say "Checking this machine"
|
||||
[ "$(uname -s)" = Linux ] || die "this installer only supports Linux"
|
||||
[ "$(id -u)" -eq 0 ] || die "run as root, e.g. curl -fsSL <url> | sudo bash"
|
||||
|
||||
case "$(uname -m)" in
|
||||
x86_64|amd64) ;;
|
||||
*) warn "this is a $(uname -m) machine. The manager will run, but the game server images are built for x86_64, so games may not start." ;;
|
||||
esac
|
||||
|
||||
mem_mb=$(awk '/^MemTotal:/ {print int($2 / 1024)}' /proc/meminfo)
|
||||
[ "$mem_mb" -ge 3500 ] || warn "only ${mem_mb} MB of RAM. One Valheim server needs about 1.5 GB and Project Zomboid needs 3 GB or more."
|
||||
|
||||
mkdir -p "$DATA"
|
||||
disk_gb=$(df -Pk "$DATA" | awk 'NR == 2 {print int($4 / 1048576)}')
|
||||
[ "$disk_gb" -ge 10 ] || warn "only ${disk_gb} GB free in $DATA. Each game server needs a few GB, plus room for backups."
|
||||
|
||||
pkg_install() {
|
||||
if have apt-get; then DEBIAN_FRONTEND=noninteractive apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null
|
||||
elif have dnf; then dnf install -y -q "$@"
|
||||
elif have yum; then yum install -y -q "$@"
|
||||
else die "please install: $*"
|
||||
fi
|
||||
}
|
||||
missing=()
|
||||
have curl || missing+=(curl)
|
||||
have tar || missing+=(tar)
|
||||
have ss || missing+=(iproute2)
|
||||
if [ ${#missing[@]} -gt 0 ]; then
|
||||
say "Installing ${missing[*]}"
|
||||
pkg_install "${missing[@]}"
|
||||
fi
|
||||
|
||||
# Docker
|
||||
if ! have docker; then
|
||||
[ "${SKIP_DOCKER_INSTALL:-}" = 1 ] && die "Docker is not installed (see https://docs.docker.com/engine/install/)"
|
||||
say "Installing Docker (get.docker.com)"
|
||||
curl -fsSL https://get.docker.com | sh >/dev/null
|
||||
fi
|
||||
if ! docker info >/dev/null 2>&1; then
|
||||
have systemctl && systemctl enable --now docker >/dev/null 2>&1 || true
|
||||
docker info >/dev/null 2>&1 || die "Docker is installed but not running (try: systemctl start docker)"
|
||||
fi
|
||||
|
||||
# Containers named gsm and caddy are ours if they run the gsm image / mount our Caddyfile.
|
||||
exists() { docker inspect "$1" >/dev/null 2>&1; }
|
||||
ours_gsm() { [ "$(docker inspect -f '{{.Config.Image}}' gsm 2>/dev/null)" = gsm ]; }
|
||||
ours_caddy() { docker inspect -f '{{range .Mounts}}{{.Source}} {{end}}' caddy 2>/dev/null | grep -q "$DIR/Caddyfile"; }
|
||||
exists gsm && ! ours_gsm && die "a container named 'gsm' already exists and isn't this app. Rename or remove it first."
|
||||
exists caddy && ! ours_caddy && die "a container named 'caddy' already exists and isn't this app's. Rename or remove it first."
|
||||
|
||||
port_used() { ss -tlnH "sport = :$1" | grep -q .; }
|
||||
if port_used 443 && ! ours_caddy; then
|
||||
die "port 443 is in use by another program ($(ss -tlnpH 'sport = :443' | grep -o 'users:(("[^"]*' | cut -d'"' -f2 | head -1)). The manager needs it for HTTPS."
|
||||
fi
|
||||
if port_used 8080 && ! ours_gsm; then
|
||||
die "port 8080 is in use by another program. The manager listens on 127.0.0.1:8080."
|
||||
fi
|
||||
# Without port 80 Caddy can still get certificates (TLS-ALPN challenge on 443), it just can't redirect http:// to https://.
|
||||
redirects=1
|
||||
if { port_used 80 && ! ours_caddy; } || grep -q disable_redirects "$DIR/Caddyfile" 2>/dev/null; then
|
||||
redirects=0
|
||||
warn "port 80 is used by another program, so http:// addresses won't redirect to https://"
|
||||
fi
|
||||
|
||||
# Address to serve on
|
||||
is_private() { [[ $1 =~ ^(10|127)\. || $1 =~ ^192\.168\. || $1 =~ ^172\.(1[6-9]|2[0-9]|3[01])\. || $1 =~ ^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\. || $1 =~ ^169\.254\. ]]; }
|
||||
if [ -z "${SITE:-}" ] && [ -f "$DIR/Caddyfile" ]; then
|
||||
SITE=$(grep -m1 -E '^[^[:space:]{}#][^[:space:]]* \{$' "$DIR/Caddyfile" | cut -d' ' -f1 || true)
|
||||
fi
|
||||
if [ -z "${SITE:-}" ]; then
|
||||
SITE=$(ip -4 route get 1.1.1.1 2>/dev/null | grep -o 'src [0-9.]*' | cut -d' ' -f2 || true)
|
||||
if [ -z "$SITE" ] || is_private "$SITE"; then
|
||||
for url in https://api.ipify.org https://ipv4.icanhazip.com https://checkip.amazonaws.com; do
|
||||
SITE=$(curl -4 -fsS --max-time 10 "$url" 2>/dev/null | tr -d '[:space:]' || true)
|
||||
[[ $SITE =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] && break
|
||||
done
|
||||
fi
|
||||
[[ $SITE =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "couldn't find this machine's public IPv4 address. Set it yourself: SITE=<ip or domain>"
|
||||
fi
|
||||
[[ $SITE =~ ^[A-Za-z0-9.-]+$ ]] || die "SITE must be a domain name or IPv4 address, got '$SITE'"
|
||||
|
||||
if have ufw && ufw status 2>/dev/null | grep -q '^Status: active'; then
|
||||
say "Opening the HTTPS port in ufw"
|
||||
ufw allow 443/tcp >/dev/null
|
||||
[ $redirects = 1 ] && ufw allow 80/tcp >/dev/null
|
||||
fi
|
||||
|
||||
# ---------- install ----------
|
||||
|
||||
mkdir -p "$DIR"
|
||||
if [ -n "${GSM_SRC:-}" ]; then
|
||||
src=$GSM_SRC
|
||||
else
|
||||
say "Downloading $GSM_REPO ($GSM_REF)"
|
||||
src=$(mktemp -d)
|
||||
trap 'rm -rf "$src"' EXIT
|
||||
curl -fsSL "$GSM_REPO/archive/$GSM_REF.tar.gz" | tar -xz -C "$src" --strip-components=1
|
||||
fi
|
||||
[ -f "$src/app/Dockerfile" ] && [ -f "$src/caddy/Caddyfile" ] || die "$src doesn't look like a game-servers checkout"
|
||||
rm -rf "$DIR/app.new"
|
||||
cp -r "$src/app" "$DIR/app.new"
|
||||
rm -rf "$DIR/app"
|
||||
mv "$DIR/app.new" "$DIR/app"
|
||||
|
||||
first_install=0
|
||||
if [ ! -f "$DIR/.env" ]; then
|
||||
first_install=1
|
||||
password=${ADMIN_PASSWORD:-$(random a-zA-Z0-9 16)}
|
||||
umask 077
|
||||
printf 'ADMIN_PASSWORD=%s\nSECRET_KEY=%s\n' "$password" "$(random a-f0-9 64)" > "$DIR/.env"
|
||||
umask 022
|
||||
fi
|
||||
|
||||
say "Building and starting the manager"
|
||||
docker build -q -t gsm "$DIR/app" >/dev/null
|
||||
docker rm -f gsm >/dev/null 2>&1 || true
|
||||
docker run -d --name gsm --restart unless-stopped --network host \
|
||||
--env-file "$DIR/.env" \
|
||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||
-v "$DATA:$DATA" \
|
||||
gsm >/dev/null
|
||||
|
||||
say "Starting Caddy (HTTPS) for $SITE"
|
||||
{ [ $redirects = 0 ] && printf '{\n\tauto_https disable_redirects\n}\n\n'
|
||||
sed "s/__SITE__/$SITE/" "$src/caddy/Caddyfile"; } > "$DIR/Caddyfile.new"
|
||||
cat "$DIR/Caddyfile.new" > "$DIR/Caddyfile" # in place: Caddy bind-mounts this file
|
||||
rm "$DIR/Caddyfile.new"
|
||||
if exists caddy; then
|
||||
docker start caddy >/dev/null
|
||||
out=$(docker exec caddy caddy reload --config /etc/caddy/Caddyfile 2>&1) || { echo "$out" >&2; die "Caddy rejected the new config"; }
|
||||
else
|
||||
docker run -d --name caddy --restart unless-stopped --network host \
|
||||
-v "$DIR/Caddyfile:/etc/caddy/Caddyfile:ro" \
|
||||
-v caddy_data:/data -v caddy_config:/config \
|
||||
caddy:2 >/dev/null
|
||||
fi
|
||||
|
||||
echo
|
||||
say "Game server manager is running at https://$SITE"
|
||||
if [ $first_install = 1 ]; then
|
||||
if [ -n "${ADMIN_PASSWORD:-}" ]; then echo "Log in with the ADMIN_PASSWORD you set."
|
||||
else echo "Password: $password (saved in $DIR/.env; edit it there and re-run this script to change it)"
|
||||
fi
|
||||
echo "The HTTPS certificate can take a minute to arrive on first start."
|
||||
echo "Players connect on UDP ports 2456+ (Valheim) and 16261+ (Project Zomboid): open them in your provider's firewall if it has one."
|
||||
fi
|
||||
Loading…
Add table
Add a link
Reference in a new issue