Update 2025-04-13_18:10:36

This commit is contained in:
root
2025-04-13 18:10:36 +02:00
commit 5cae372ac6
3 changed files with 367 additions and 0 deletions

219
gitea_push.sh Executable file
View File

@ -0,0 +1,219 @@
#!/bin/zsh
# Script Version: 1.5
# Description: Pushes the current folder (e.g. /etc) to a nested Gitea repo using provided nesting arguments. Auto-creates the remote repo via Gitea API if missing.
# Set variables
# ========
# Try to extract GITEA_API_TOKEN from ~/.gitea_token if not set
if [ -z "$GITEA_API_TOKEN" ] && [ -f "$HOME/.gitea_token" ]; then
GITEA_API_TOKEN=$(<"$HOME/.gitea_token")
export GITEA_API_TOKEN
fi
GITEA_USER=$(awk '{for(i=1;i<=NF;i++) if($i=="login") print $(i+1)}' ~/.netrc | head -n1)
if [ -z "$GITEA_USER" ]; then
echo "[ERROR] No login found in ~/.netrc"
exit 1
fi
GITEA_URL="https://$(awk '{for(i=1;i<=NF;i++) if($i=="machine") print $(i+1)}' ~/.netrc | head -n1)"
if [ -z "$GITEA_URL" ]; then
echo "[ERROR] No URL found in ~/.netrc"
exit 1
fi
GITEA_API_URL="$GITEA_URL/api/v1"
PRIVATE=false
DEBUG=false
COMMIT_MESSAGE="Update $(date +"%F_%T")"
# Logging function
# ========
log() {
local level="$1"; shift
if [[ "$level" == "DEBUG" && "$DEBUG" != true ]]; then return; fi
local color_reset="$(tput sgr0)"
local color=""
case "$level" in
INFO) color="$(tput setaf 2)" ;; # green
WARNING) color="$(tput setaf 3)" ;; # yellow
ERROR) color="$(tput setaf 1)" ;; # red
DEBUG) color="$(tput setaf 4)" ;; # blue
esac
echo "${color}[$level] $*${color_reset}"
}
# Functions
# ========
create_repo() {
log INFO "Repository does not exist. Creating via API: $REMOTE_PATH"
log DEBUG "POST $GITEA_API_URL/user/repos with name=$REMOTE_PATH and private=$PRIVATE"
RESPONSE=$(curl -s -X POST \
-H "Authorization: token $GITEA_API_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"name\": \"$FOLDER_NAME\", \"private\": $PRIVATE}" \
"$GITEA_API_URL/user/repos")
if echo "$RESPONSE" | grep -q '"clone_url"'; then
log INFO "Remote repository created successfully."
HTTP_STATUS=200
else
log ERROR "Failed to create remote repository: $RESPONSE"
exit 1
fi
}
prepare_commit() {
git add .
if git diff --quiet HEAD && ! git rev-parse --verify HEAD >/dev/null 2>&1; then
log INFO "Creating initial commit"
git commit -m "$COMMIT_MESSAGE"
elif ! git diff --quiet HEAD; then
log INFO "Committing changes"
git commit -m "$COMMIT_MESSAGE"
else
log INFO "Nothing to commit"
fi
}
setup_remote() {
if git remote | grep -q '^origin$'; then
log INFO "Updating remote origin URL"
git remote set-url origin "$GIT_REMOTE"
else
log INFO "Adding remote origin"
git remote add origin "$GIT_REMOTE"
fi
}
push_changes() {
log INFO "Pushing to $GIT_REMOTE"
git push -u origin main
}
# Show help if no arguments are given
# ========
if [ $# -eq 0 ]; then
echo "GITEA_API_TOKEN=<your token>"
echo "Usage: $0 [--private] [--debug] [--message \"your commit message\"] <host_group>"
echo "Example: $0 server"
echo " $0 --private workstation"
echo " $0 --debug server"
echo " $0 --message \"minor update\" server"
echo
echo "Note: You must cd into the target folder before running this script."
echo "For example:"
echo " cd /etc && $0 server"
echo
echo "Authentication:"
echo " Git operations (clone, push, pull) use ~/.netrc with your Git password:"
echo " machine \$(echo \"$GITEA_URL\" | sed 's|https\?://||') login $GITEA_USER password \"<your Git password>\""
echo " chmod 600 ~/.netrc"
echo
echo " API operations (e.g. creating repos) use a Personal Access Token stored in ~/.gitea_token"
echo " echo \"<your_token>\" > ~/.gitea_token && chmod 600 ~/.gitea_token"
exit 0
fi
# Parse arguments
# ========
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
--private)
PRIVATE=true
shift
;;
--debug)
DEBUG=true
shift
;;
--message)
COMMIT_MESSAGE="$2"
shift 2
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
if [[ $# -ne 1 ]]; then
echo "Usage: $0 [--private] [--debug] [--message \"your commit message\"] <host_group>"
exit 1
fi
HOST_GROUP=$(echo "$1" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9-')
HOST_NAME=$(hostname -s | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9-')
FOLDER_NAME="${HOST_NAME}-${HOST_GROUP}-$(basename "$PWD")"
REPO_PATH="$PWD"
REMOTE_PATH="$FOLDER_NAME"
GIT_REMOTE="$GITEA_URL/$GITEA_USER/$FOLDER_NAME.git"
# Git authentication hint
log DEBUG "Ensure ~/.netrc has: machine <host> login $GITEA_USER password <your Git password>"
# Check or create remote repo
check_or_create_repo() {
if [ -z "$GITEA_API_TOKEN" ]; then
log WARNING "GITEA_API_TOKEN is not set. Skipping API repo creation."
return
fi
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: token $GITEA_API_TOKEN" \
"$GITEA_API_URL/repos/$GITEA_USER/$FOLDER_NAME")
if [ "$HTTP_STATUS" -ne 200 ]; then
create_repo
else
log INFO "Remote repository already exists."
fi
}
check_or_create_repo
# Main Process
# ========
# Safety check against pushing from / or $HOME
if [[ "$PWD" == "$HOME" || "$PWD" == "/" ]]; then
log ERROR "Refusing to run inside \$PWD=$PWD"
exit 1
fi
log INFO "Pushing $REPO_PATH to $GIT_REMOTE"
cd "$REPO_PATH" || { log ERROR "Directory $REPO_PATH not found"; exit 1; }
# Initialize git if needed
# Branch is fixed to 'main' for simplicity and consistency
if [ ! -d .git ]; then
log INFO "Initializing Git repo"
git init
git config init.defaultBranch main
git checkout -b main
else
log DEBUG ".git directory already present"
fi
# Ensure at least one commit exists
prepare_commit
# Set or update remote
if [ "$HTTP_STATUS" -eq 200 ]; then
setup_remote
else
log WARNING "Skipping remote setup repository does not exist."
fi
# Push to remote
if [ "$HTTP_STATUS" -eq 200 ]; then
push_changes
else
log WARNING "Skipping push repository does not exist."
fi

42
init_db.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/bash
# Script Version: 03
# Description: Erstellt PostgreSQL-User und -Datenbank für ein Spiel ($GAMENAME als Argument)
# ========
# Variablen setzen
# ========
GAMENAME="$1"
DBUSER="$GAMENAME"
DBPASS=$(openssl rand -hex 16)
# Check
if [ -z "$GAMENAME" ]; then
echo "Usage: $0 <GAMENAME>"
exit 1
fi
# ========
# PostgreSQL-User erstellen (falls nicht vorhanden)
# ========
sudo -u postgres psql <<EOF
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_user WHERE usename = '${DBUSER}') THEN
CREATE USER ${DBUSER} WITH PASSWORD '${DBPASS}';
END IF;
END
\$\$;
EOF
# ========
# PostgreSQL-Datenbank erstellen (außerhalb PL/pgSQL!)
# ========
DB_EXISTS=$(sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='${GAMENAME}'")
if [ "$DB_EXISTS" != "1" ]; then
sudo -u postgres createdb -O "$DBUSER" "$GAMENAME"
fi
# Feedback
echo "✅ PostgreSQL-Datenbank '$GAMENAME' und User '$DBUSER' bereit."
echo "🔐 Passwort: $DBPASS"

106
init_game.sh Executable file
View File

@ -0,0 +1,106 @@
#!/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."