Files
games-server-scripts/init_game.sh
2025-04-13 18:10:36 +02:00

107 lines
2.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Version 08
# Setup script for new game deployment (dirs only, port check)
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <spielname> [additional pip packages]"
exit 1
fi
GAMENAME="$1"
DIR="/var/www/$GAMENAME"
DOMAIN="$GAMENAME.orangeicebear.at"
# Find a free port between 80008999 that is not currently in use
for i in {8000..8999}; do
if ! lsof -iTCP:$i -sTCP:LISTEN -Pn >/dev/null 2>&1; then
PORT=$i
break
fi
done
if [ -z "$PORT" ]; then
echo "[ERROR] No free port found in range 80008999"
exit 2
fi
echo "Using free port $PORT"
echo
echo "Creating project folder at $DIR"
install -d -m 0750 -o games -g games "$DIR/static"
echo
cd "$DIR"
python3 -m venv venv
source venv/bin/activate
echo "[INFO] Erstelle requirements.txt"
echo -e "fastapi\nuvicorn\n${*:2}" | tr ' ' '\n' > requirements.txt
pip install -r requirements.txt
if pip list | grep -q jinja2; then
install -d -m 0750 -o games -g games "$DIR/templates"
echo "[INFO] $DIR/templates created"
else
echo "[INFO] Kein Jinja2 installiert templates/ wird nicht erstellt"
fi
echo
echo "Creating systemd service"
echo "cat > /etc/systemd/system/$GAMENAME.service <<EOF"
cat > "/etc/systemd/system/$GAMENAME.service" <<EOF
[Unit]
Description=$GAMENAME Game Server
After=network.target
[Service]
ExecStart=$DIR/venv/bin/uvicorn main:app --host 0.0.0.0 --port $PORT
WorkingDirectory=$DIR
Restart=always
User=games
Group=games
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF
echo
systemctl daemon-reexec
systemctl daemon-reload
systemctl enable --now "$GAMENAME"
systemctl status "$GAMENAME"
echo
echo "Creating nginx config"
echo "cat > /etc/nginx/sites-available/$DOMAIN <<EOF"
echo "server {"
echo " listen 80;"
echo " server_name $DOMAIN;"
echo ""
echo " access_log /var/log/nginx/games_access.log;"
echo " error_log /var/log/nginx/games.error.log;"
echo ""
echo " location / {"
echo " proxy_pass http://10.0.3.32:$PORT ;"
echo " include proxy_params;"
echo " proxy_redirect off;"
echo " }"
echo "}"
echo "EOF"
echo
echo "ln -s /etc/nginx/sites-available/$DOMAIN /etc/nginx/sites-enabled/"
echo "nginx -t && systemctl reload nginx"
echo
echo "[DEBUG] Port: $PORT"
echo "[INFO] Spiel $GAMENAME vorbereitet unter http://$DOMAIN → Port $PORT im Container"
echo "[INFO] Nginx-Logs: /var/log/nginx/games.access.log & games.error.log"
echo "[INFO] Verzeichnisstruktur angelegt. Bitte Quellcode und statische Dateien manuell hinzufügen."