From 89374de57f4261e1ad3a21bd032ab4d6c0dba264 Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Sun, 14 Dec 2025 10:00:00 +0800 Subject: [PATCH] feat: centralize configuration and improve Makefile - Add config.sh with centralized container names and ports - Update healthcheck.sh to use config variables (avoid hardcoding) - Add health check targets to Makefile (health, health-*) - Add backup utility targets to Makefile (backup, backup-*) - Reorganize Makefile help output by service category --- Makefile | 71 ++++++++++++++++++++++++++++++++++++++-------- bin/healthcheck.sh | 38 +++++++++++++------------ config.sh | 43 ++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 30 deletions(-) create mode 100644 config.sh diff --git a/Makefile b/Makefile index 1eeb9d5..b2010bd 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ # Provides common operations across all services .PHONY: help all status up down logs restart clean minecraft teamspeak nextcloud +.PHONY: health health-minecraft health-teamspeak health-nextcloud +.PHONY: backup backup-minecraft backup-teamspeak backup-nextcloud backup-list backup-cleanup # Default target help: @@ -10,10 +12,14 @@ help: @echo "Usage: make [target]" @echo "" @echo "Global Commands:" - @echo " help Show this help message" - @echo " status Show status of all services" - @echo " all-up Start all services" - @echo " all-down Stop all services" + @echo " help Show this help message" + @echo " status Show status of all services" + @echo " all-up Start all services" + @echo " all-down Stop all services" + @echo " health Run health checks on all services" + @echo " backup Backup all services" + @echo " backup-list List available backups" + @echo " backup-cleanup Remove old backups" @echo "" @echo "Service-specific Commands:" @echo " Minecraft:" @@ -29,16 +35,21 @@ help: @echo " minecraft-backup Create full backup" @echo " minecraft-backup-world Backup world data only" @echo " minecraft-backup-list List available backups" + @echo " health-minecraft Check Minecraft health" @echo "" - @echo " teamspeak-up Start TeamSpeak server" - @echo " teamspeak-down Stop TeamSpeak server" - @echo " teamspeak-logs View TeamSpeak logs" - @echo " teamspeak-restart Restart TeamSpeak server" + @echo " TeamSpeak:" + @echo " teamspeak-up Start TeamSpeak server" + @echo " teamspeak-down Stop TeamSpeak server" + @echo " teamspeak-logs View TeamSpeak logs" + @echo " teamspeak-restart Restart TeamSpeak server" + @echo " health-teamspeak Check TeamSpeak health" @echo "" - @echo " nextcloud-up Start Nextcloud" - @echo " nextcloud-down Stop Nextcloud" - @echo " nextcloud-logs View Nextcloud logs" - @echo " nextcloud-restart Restart Nextcloud" + @echo " Nextcloud:" + @echo " nextcloud-up Start Nextcloud" + @echo " nextcloud-down Stop Nextcloud" + @echo " nextcloud-logs View Nextcloud logs" + @echo " nextcloud-restart Restart Nextcloud" + @echo " health-nextcloud Check Nextcloud health" @echo "" @echo "Utility Commands:" @echo " check Check prerequisites" @@ -164,3 +175,39 @@ clean: @docker container prune -f @docker volume prune -f @echo "✓ Cleanup complete" + +# ============================================================================ +# Health Check Targets +# ============================================================================ +health: + @./bin/healthcheck.sh all + +health-minecraft: + @./bin/healthcheck.sh minecraft + +health-teamspeak: + @./bin/healthcheck.sh teamspeak + +health-nextcloud: + @./bin/healthcheck.sh nextcloud + +# ============================================================================ +# Backup Targets (using bin/backup.sh) +# ============================================================================ +backup: + @./bin/backup.sh backup all + +backup-minecraft: + @./bin/backup.sh backup minecraft + +backup-teamspeak: + @./bin/backup.sh backup teamspeak + +backup-nextcloud: + @./bin/backup.sh backup nextcloud + +backup-list: + @./bin/backup.sh list + +backup-cleanup: + @./bin/backup.sh cleanup diff --git a/bin/healthcheck.sh b/bin/healthcheck.sh index c22160a..309bb1a 100755 --- a/bin/healthcheck.sh +++ b/bin/healthcheck.sh @@ -4,76 +4,78 @@ set -euo pipefail -# Source shared library +# Source shared library and config SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" source "$SCRIPT_DIR/lib/common.sh" +source "$PROJECT_ROOT/config.sh" check_minecraft() { log_info "Checking Minecraft server..." - if check_container_health "mc-fabric-1.21.1"; then + if check_container_health "$CONTAINER_MINECRAFT"; then log_info " ✓ Container is running" else log_error " ✗ Container is not running" return 1 fi - if check_port "localhost" 25565; then - log_info " ✓ Server port 25565 is accessible" + if check_port "localhost" "$PORT_MINECRAFT"; then + log_info " ✓ Server port $PORT_MINECRAFT is accessible" else - log_warn " ⚠ Server port 25565 is not accessible" + log_warn " ⚠ Server port $PORT_MINECRAFT is not accessible" fi - if check_port "localhost" 25575; then - log_info " ✓ RCON port 25575 is accessible" + if check_port "localhost" "$PORT_MINECRAFT_RCON"; then + log_info " ✓ RCON port $PORT_MINECRAFT_RCON is accessible" else - log_warn " ⚠ RCON port 25575 is not accessible" + log_warn " ⚠ RCON port $PORT_MINECRAFT_RCON is not accessible" fi } check_teamspeak() { log_info "Checking TeamSpeak server..." - if check_container_health "teamspeak-server"; then + if check_container_health "$CONTAINER_TEAMSPEAK"; then log_info " ✓ Container is running" else log_error " ✗ Container is not running" return 1 fi - if check_port "localhost" 10011; then - log_info " ✓ File transfer port 10011 is accessible" + if check_port "localhost" "$PORT_TEAMSPEAK_QUERY"; then + log_info " ✓ Query port $PORT_TEAMSPEAK_QUERY is accessible" else - log_warn " ⚠ Port 10011 is not accessible" + log_warn " ⚠ Port $PORT_TEAMSPEAK_QUERY is not accessible" fi } check_nextcloud() { log_info "Checking Nextcloud..." - if check_container_health "nextcloud"; then + if check_container_health "$CONTAINER_NEXTCLOUD"; then log_info " ✓ Nextcloud container is running" else log_error " ✗ Nextcloud container is not running" return 1 fi - if check_container_health "nextcloud-db"; then + if check_container_health "$CONTAINER_NEXTCLOUD_DB"; then log_info " ✓ Database container is running" else log_error " ✗ Database container is not running" fi - if check_container_health "nextcloud-redis"; then + if check_container_health "$CONTAINER_NEXTCLOUD_REDIS"; then log_info " ✓ Redis container is running" else log_warn " ⚠ Redis container is not running" fi - if check_port "localhost" 8080; then - log_info " ✓ Web interface port 8080 is accessible" + if check_port "localhost" "$PORT_NEXTCLOUD_WEB"; then + log_info " ✓ Web interface port $PORT_NEXTCLOUD_WEB is accessible" else - log_warn " ⚠ Port 8080 is not accessible" + log_warn " ⚠ Port $PORT_NEXTCLOUD_WEB is not accessible" fi } diff --git a/config.sh b/config.sh new file mode 100644 index 0000000..d78bdba --- /dev/null +++ b/config.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# Centralized configuration for all services +# Source this file to get consistent container names and settings + +# Prevent multiple sourcing +[[ -n "${_CONFIG_SH_LOADED:-}" ]] && return +readonly _CONFIG_SH_LOADED=1 + +# ============================================================================ +# Container Names +# ============================================================================ +# These are the canonical container names used across all scripts. +# Update here if container names change in docker-compose.yml files. + +readonly CONTAINER_MINECRAFT="${CONTAINER_MINECRAFT:-mc-fabric-1.21.1}" +readonly CONTAINER_TEAMSPEAK="${CONTAINER_TEAMSPEAK:-teamspeak-server}" +readonly CONTAINER_NEXTCLOUD="${CONTAINER_NEXTCLOUD:-nextcloud}" +readonly CONTAINER_NEXTCLOUD_DB="${CONTAINER_NEXTCLOUD_DB:-nextcloud-db}" +readonly CONTAINER_NEXTCLOUD_REDIS="${CONTAINER_NEXTCLOUD_REDIS:-nextcloud-redis}" + +# ============================================================================ +# Service Ports +# ============================================================================ +readonly PORT_MINECRAFT=25565 +readonly PORT_MINECRAFT_RCON=25575 +readonly PORT_TEAMSPEAK_VOICE=9987 +readonly PORT_TEAMSPEAK_FILETRANSFER=30033 +readonly PORT_TEAMSPEAK_QUERY=10011 +readonly PORT_NEXTCLOUD_WEB=8080 + +# ============================================================================ +# Backup Configuration +# ============================================================================ +readonly BACKUP_ROOT="${BACKUP_ROOT:-./backups}" +readonly BACKUP_RETENTION_DAYS="${BACKUP_RETENTION_DAYS:-7}" + +# ============================================================================ +# Helper function to get project root +# ============================================================================ +get_project_root() { + local script_path="${BASH_SOURCE[1]:-$0}" + cd "$(dirname "$script_path")" && pwd +}