refactor: extract git_update function, add APP_URL fallback, validate dirs upfront

This commit is contained in:
root
2026-06-24 12:55:32 +02:00
parent 267a3c0fe0
commit 2aaff12c60
+65 -71
View File
@@ -11,7 +11,12 @@ if [ -f "$SCRIPT_DIR/.env" ]; then
set +a
fi
# Prompt for site URL before unbuffer exec, so interactive input works
# Use CMS APP_URL as fallback for NITRO_SITE_URL, then prompt if still unset
if [ -z "${NITRO_SITE_URL:-}" ] && [ -n "${APP_URL:-}" ]; then
NITRO_SITE_URL="$APP_URL"
echo "--> Using APP_URL from .env: $NITRO_SITE_URL"
fi
if [ -z "${NITRO_SITE_URL:-}" ]; then
read -r -p "Enter your site URL (e.g. https://example.com): " NITRO_SITE_URL
NITRO_SITE_URL="${NITRO_SITE_URL%/}"
@@ -22,14 +27,14 @@ if [ -z "${NITRO_SITE_URL:-}" ]; then
export NITRO_SITE_URL
fi
# Prompt for branch if not set in .env
# Prompt for branch if not set in .env, default to main
if [ -z "${NITRO_BRANCH:-}" ]; then
read -r -p "Enter branch to use (main/dev) [main]: " NITRO_BRANCH
read -r -p "Enter branch (main/dev) [main]: " NITRO_BRANCH
NITRO_BRANCH="${NITRO_BRANCH:-main}"
if [ "$NITRO_BRANCH" != "main" ] && [ "$NITRO_BRANCH" != "dev" ]; then
echo "=== ❌ Invalid branch '$NITRO_BRANCH'. Choose 'main' or 'dev'. ==="
exit 1
fi
case "$NITRO_BRANCH" in
main|dev) ;;
*) echo "=== ❌ Invalid branch '$NITRO_BRANCH'. Choose 'main' or 'dev'. ==="; exit 1 ;;
esac
export NITRO_BRANCH
fi
@@ -64,20 +69,18 @@ NITRO_CLIENT="${NITRO_CLIENT_SRC:-/var/www/Nitro-V3}"
NITRO_RENDERER="${NITRO_RENDERER_SRC:-/var/www/Nitro_Render_V3}"
NITRO_BRANCH="${NITRO_BRANCH:-main}"
# Derive ws/wss protocol from the site scheme
# Derive ws/wss protocol and domain from site URL
case "$NITRO_SITE_URL" in
https://*) NITRO_WS_PROTO="wss://" ; NITRO_DOMAIN="${NITRO_SITE_URL#https://}" ;;
http://*) NITRO_WS_PROTO="ws://" ; NITRO_DOMAIN="${NITRO_SITE_URL#http://}" ;;
*) NITRO_WS_PROTO="wss://" ; NITRO_DOMAIN="$NITRO_SITE_URL" ;;
esac
# Critical URLs for catalog icons (from env, with automatic per-site defaults)
# Critical URLs (override via NITRO_* env vars, auto-derived otherwise)
NITRO_IMAGE_LIBRARY_URL="${NITRO_IMAGE_LIBRARY_URL:-$NITRO_SITE_URL/gamedata/c_images/}"
NITRO_HOF_FURNITURE_URL="${NITRO_HOF_FURNITURE_URL:-$NITRO_SITE_URL/gamedata/icons}"
NITRO_API_URL="${NITRO_API_URL:-$NITRO_SITE_URL}"
if [ -z "${NITRO_SOCKET_URL:-}" ]; then
NITRO_SOCKET_URL="${NITRO_WS_PROTO}ws.${NITRO_DOMAIN}"
fi
NITRO_SOCKET_URL="${NITRO_SOCKET_URL:-${NITRO_WS_PROTO}ws.${NITRO_DOMAIN}}"
NITRO_CAMERA_URL="${NITRO_CAMERA_URL:-$NITRO_SITE_URL/camera/photo/}"
NITRO_GAMEDATA_URL="${NITRO_GAMEDATA_URL:-$NITRO_SITE_URL/gamedata}"
NITRO_ASSET_URL="${NITRO_ASSET_URL:-$NITRO_SITE_URL/gamedata/bundled}"
@@ -88,9 +91,22 @@ MYSQL_CRED="-h $DB_HOST -P $DB_PORT -u $DB_USER --ssl-verify-server-cert=OFF"
if [ -n "$DB_PASS" ]; then
export MYSQL_PWD="$DB_PASS"
fi
# ---------------------
# Helper: forcefully remove node_modules (with sudo fallback) and restore directory owner
# Verify all required directories exist before starting
REQUIRED_DIRS=(
"$EMULATOR_DIR/Emulator"
"$NITRO_RENDERER"
"$NITRO_CLIENT"
"$NITRO_SRC_DIR"
)
for dir in "${REQUIRED_DIRS[@]}"; do
if [ ! -d "$dir" ]; then
echo "=== ❌ Required directory not found: $dir ==="
exit 1
fi
done
# Helper: forcefully remove node_modules (with sudo fallback) and restore owner
clean_node_modules() {
rm -rf node_modules 2>/dev/null || sudo rm -rf node_modules 2>/dev/null || true
if [ "$(stat -c '%U' .)" != "$(whoami)" ] && command -v sudo &> /dev/null; then
@@ -98,7 +114,25 @@ clean_node_modules() {
fi
}
# Helper: git stash → checkout → pull → return 0 if new commits, 1 otherwise
git_update() {
local repo="$1"
local branch="$2"
cd "$repo"
git stash --include-untracked || true
local old_head
old_head=$(git rev-parse HEAD)
git checkout "$branch"
git pull
if [ "$(git rev-parse HEAD)" != "$old_head" ]; then
return 0
fi
return 1
}
echo "=== Starting EPIC WEB CONTROL Update ==="
echo "--> Site: $NITRO_SITE_URL | Branch: $NITRO_BRANCH"
echo ""
HAD_UPDATES=false
NITRO_BUILT=false
@@ -107,37 +141,31 @@ NITRO_BUILT=false
# 1. Update and Build Emulator
# ----------------------------------------
echo "--> Updating Emulator..."
EMULATOR_REPO_DIR="$EMULATOR_DIR/Emulator"
[ -d "$EMULATOR_REPO_DIR" ] || { echo "=== ❌ Directory not found: $EMULATOR_REPO_DIR ==="; exit 1; }
cd "$EMULATOR_REPO_DIR"
git stash --include-untracked || true
OLD_HEAD=$(git rev-parse HEAD)
git checkout "$NITRO_BRANCH"
git pull
# --- Automatic Safe Database Backup ---
echo "--> Creating automatic database backup before update..."
mkdir -p "$BACKUP_DIR"
mariadb-dump $MYSQL_CRED --force --skip-lock-tables "$DB_NAME" > "$BACKUP_DIR/backup_$(date +%Y%m%d_%H%M%S).sql" || echo "--> Backup has missing tables (not critical) — update continues"
if git_update "$EMULATOR_DIR/Emulator" "$NITRO_BRANCH"; then
echo "--> New commits detected, building emulator..."
HAD_UPDATES=true
# --- Automatic SQL Import (exclusief backup dir) ---
echo "--> Checking for new SQL files..."
if [ -d "$SQL_DIR" ]; then
# Automatic Safe Database Backup
echo "--> Creating automatic database backup before update..."
mkdir -p "$BACKUP_DIR"
mariadb-dump $MYSQL_CRED --force --skip-lock-tables "$DB_NAME" > "$BACKUP_DIR/backup_$(date +%Y%m%d_%H%M%S).sql" || echo "--> Backup has missing tables (not critical) — update continues"
# Automatic SQL Import (exclude backup dir)
echo "--> Checking for new SQL files..."
if [ -d "$SQL_DIR" ]; then
find "$SQL_DIR" -name "*.sql" -mmin -10 -not -path "$BACKUP_DIR/*" -print0 2>/dev/null | while IFS= read -r -d '' sql_file; do
echo "--> Importing new SQL file: $(basename "$sql_file")"
mariadb $MYSQL_CRED --force "$DB_NAME" < "$sql_file" || echo "--> Error importing $(basename "$sql_file"), moving to next..."
done
else
else
echo "--> SQL directory not found, skipping SQL import."
fi
fi
if [ "$(git rev-parse HEAD)" != "$OLD_HEAD" ]; then
echo "--> New commits detected, building emulator..."
HAD_UPDATES=true
cd "$EMULATOR_DIR/Emulator"
mvn package
JAR_FILE=$(find target -maxdepth 1 -name 'Habbo-*-jar-with-dependencies.jar' -printf '%T@ %p\n' 2>/dev/null | sort -rn | sed -n '1s/^[0-9.]* //p' | xargs basename)
if [ -z "$JAR_FILE" ]; then
echo "=== ❌ No jar file found with pattern: target/Habbo-*-jar-with-dependencies.jar ==="
exit 1
@@ -145,9 +173,7 @@ if [ "$(git rev-parse HEAD)" != "$OLD_HEAD" ]; then
echo "--> Found jar file: $JAR_FILE"
echo "--> Updating emulator launch file..."
[ -d target ] || { echo "=== ❌ Build directory 'target' not found ==="; exit 1; }
cd target/
cat << EOF > emulator
#!/bin/sh
file_name_emulator=emulator.log
@@ -166,15 +192,8 @@ fi
# 2. Update Nitro_Render_V3
# ----------------------------------------
echo "--> Updating Nitro_Render_V3..."
[ -d "$NITRO_RENDERER" ] || { echo "=== ❌ Directory not found: $NITRO_RENDERER ==="; exit 1; }
cd "$NITRO_RENDERER"
git stash --include-untracked || true
OLD_HEAD=$(git rev-parse HEAD)
git checkout "$NITRO_BRANCH"
git pull
if [ "$(git rev-parse HEAD)" != "$OLD_HEAD" ]; then
if git_update "$NITRO_RENDERER" "$NITRO_BRANCH"; then
echo "--> New commits detected, running yarn install for Nitro_Render_V3..."
HAD_UPDATES=true
yarn install --frozen-lockfile || { echo "--> Retrying with clean node_modules..."; clean_node_modules && yarn install; }
@@ -187,15 +206,8 @@ fi
# 3. Update and Build Nitro-V3
# ----------------------------------------
echo "--> Updating Nitro-V3..."
[ -d "$NITRO_CLIENT" ] || { echo "=== ❌ Directory not found: $NITRO_CLIENT ==="; exit 1; }
cd "$NITRO_CLIENT"
git stash --include-untracked || true
OLD_HEAD=$(git rev-parse HEAD)
git checkout "$NITRO_BRANCH"
git pull
if [ "$(git rev-parse HEAD)" != "$OLD_HEAD" ]; then
if git_update "$NITRO_CLIENT" "$NITRO_BRANCH"; then
echo "--> New commits detected, building Nitro-V3..."
HAD_UPDATES=true
NITRO_BUILT=true
@@ -241,15 +253,10 @@ for example_file in "$NITRO_SRC_DIR"/*.example; do
*) target_name="$target_name.json" ;;
esac
# Phase 1: Merge to live config in Nitro public dir
echo "--> Merging $base -> $target_name (live)..."
node "$MERGE_SCRIPT" "$example_file" "$NITRO_SRC_DIR/$target_name"
# Phase 2: Merge to Gamedata/config for website
echo "--> Merging $base -> $target_name (gamedata)..."
node "$MERGE_SCRIPT" "$example_file" "$GAMEDATA_CONF_DIR/$target_name"
# Phase 3: Copy merged config to dist/configuration for live client
if [ -d "$NITRO_DIST_CONFIG_DIR" ]; then
cp "$NITRO_SRC_DIR/$target_name" "$NITRO_DIST_CONFIG_DIR/$target_name"
echo "--> [OK] Synced $target_name to dist/configuration/"
@@ -257,7 +264,6 @@ for example_file in "$NITRO_SRC_DIR"/*.example; do
done
# Phase 4: Restore critical icon URLs in renderer-config and ui-config
# (prevents git pull from overwriting URLs with example values)
echo "--> Ensuring critical catalog icon URLs are correct..."
FORCE_CONFIG_SCRIPT=$(cat << 'PYEOF'
import json, sys, os
@@ -283,11 +289,9 @@ for path in paths:
env_val = os.environ.get(f'NITRO_{key.upper().replace(".", "_")}')
if env_val:
data[key] = env_val
# Force /config/ path for radio and soundboard
if 'gamedata.url' in data:
data['radio.url'] = data['gamedata.url'] + '/config/radio-stations.json5?t=%timestamp%'
data['soundboard.url'] = data['gamedata.url'] + '/config/soundboard-sounds.json5?t=%timestamp%'
# Disable Google Ads
if 'show.google.ads' in data:
data['show.google.ads'] = False
with open(path, 'w') as f:
@@ -310,24 +314,17 @@ cd "$NITRO_CLIENT" && NITRO_SRC_DIR="$NITRO_SRC_DIR" NITRO_DIST_CONFIG_DIR="$NIT
# ----------------------------------------
echo "--> Starting automated cleanup..."
# 1. Remove emulator logs older than 14 days
echo "--> Removing emulator logs older than 14 days..."
find "$EMULATOR_DIR" -name "*.log" -mtime +14 -exec rm -f {} \; 2>/dev/null || true
# 2. Keep max 5 newest database backups, delete older ones
echo "--> Managing update backups (keeping max 5)..."
if [ -d "$BACKUP_DIR" ]; then
find "$BACKUP_DIR" -maxdepth 1 -name '*.sql' -printf '%T@ %p\n' 2>/dev/null | sort -rn | tail -n +6 | sed 's/^[0-9.]* //' | xargs -r rm -f || true
fi
# 3. Clean Yarn cache to save SSD space (only if yarn was used)
if [ "$HAD_UPDATES" = true ]; then
echo "--> Cleaning Yarn cache..."
yarn cache clean 2>/dev/null || true
fi
# 4. Remove old .jar files, keeping only the newest one (only if emulator was rebuilt)
if [ "$HAD_UPDATES" = true ]; then
echo "--> Cleaning up old emulator .jar files (keeping the latest)..."
find "$EMULATOR_DIR/Emulator/target" -maxdepth 1 -name 'Habbo-*-jar-with-dependencies.jar' -printf '%T@ %p\n' 2>/dev/null | sort -rn | tail -n +2 | sed 's/^[0-9.]* //' | xargs -r rm -f || true
fi
@@ -377,7 +374,6 @@ echo "--> Running extra validation..."
ERRORS=0
# Check build output exists
if [ "$NITRO_BUILT" = true ]; then
if [ -d "$NITRO_CLIENT/dist/assets" ]; then
echo "--> [OK] Nitro-V3 build assets found"
@@ -386,10 +382,9 @@ if [ "$NITRO_BUILT" = true ]; then
ERRORS=$((ERRORS + 1))
fi
else
echo "--> [SKIP] Nitro-V3 was not rebuilt (no new commits), skipping build check"
echo "--> [SKIP] Nitro-V3 was not rebuilt, skipping build check"
fi
# Check permissions on key files
for dir in "$NITRO_CLIENT" "$NITRO_RENDERER" "$EMULATOR_DIR" "$GAMEDATA_CONF_DIR"; do
if [ -d "$dir" ]; then
OWNER=$(stat -c '%U:%G' "$dir" 2>/dev/null || echo "unknown")
@@ -403,7 +398,6 @@ for dir in "$NITRO_CLIENT" "$NITRO_RENDERER" "$EMULATOR_DIR" "$GAMEDATA_CONF_DIR
fi
done
# Check if emulator service is running
if systemctl cat "$EMULATOR_SERVICE" &>/dev/null; then
SERVICE_STATUS=$(systemctl is-active "$EMULATOR_SERVICE" 2>/dev/null || echo "unknown")
if [ "$SERVICE_STATUS" = "active" ]; then