Files
web-hosts/scripts/startup.sh
2025-12-26 13:38:04 +01:00

226 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
#
# Web Hosts Startup Script
# Starts all domain containers and reloads nginx
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOSTS_DIR="$(dirname "$SCRIPT_DIR")"
DOMAINS_DIR="$HOSTS_DIR/domains"
LOGS_DIR="$HOSTS_DIR/logs"
NGINX_RELOAD="/docker/www/scripts/nginx-reload.sh"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
mkdir -p "$LOGS_DIR"
LOG_FILE="$LOGS_DIR/startup.log"
log() {
local msg="[$(date '+%Y-%m-%d %H:%M:%S')] $1"
echo -e "${BLUE}[INFO]${NC} $1"
echo "$msg" >> "$LOG_FILE"
}
log_ok() {
local msg="[$(date '+%Y-%m-%d %H:%M:%S')] OK: $1"
echo -e "${GREEN}[OK]${NC} $1"
echo "$msg" >> "$LOG_FILE"
}
log_warn() {
local msg="[$(date '+%Y-%m-%d %H:%M:%S')] WARN: $1"
echo -e "${YELLOW}[WARN]${NC} $1"
echo "$msg" >> "$LOG_FILE"
}
log_error() {
local msg="[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1"
echo -e "${RED}[ERROR]${NC} $1"
echo "$msg" >> "$LOG_FILE"
}
# Start containers for a domain
start_domain() {
local domain_dir="$1"
local domain_name=$(basename "$domain_dir")
# Skip if no compose.yaml
if [ ! -f "$domain_dir/compose.yaml" ]; then
log_warn "$domain_name: No compose.yaml found, skipping"
return 0
fi
# Check if compose.yaml has actual services (not just placeholder)
if ! grep -q "services:" "$domain_dir/compose.yaml" 2>/dev/null; then
log_warn "$domain_name: No services defined, skipping"
return 0
fi
# Check if services section has actual content
local service_count=$(grep -E "^\s+\w+:" "$domain_dir/compose.yaml" 2>/dev/null | wc -l)
if [ "$service_count" -lt 2 ]; then
log_warn "$domain_name: No services configured yet, skipping"
return 0
fi
log "Starting $domain_name..."
cd "$domain_dir"
if podman-compose up -d 2>&1 | tail -5; then
log_ok "$domain_name started"
return 0
else
log_error "$domain_name failed to start"
return 1
fi
}
# Stop containers for a domain
stop_domain() {
local domain_dir="$1"
local domain_name=$(basename "$domain_dir")
if [ ! -f "$domain_dir/compose.yaml" ]; then
return 0
fi
log "Stopping $domain_name..."
cd "$domain_dir"
podman-compose down 2>&1 || true
log_ok "$domain_name stopped"
}
# Reload nginx
reload_nginx() {
log "Reloading nginx..."
if [ -x "$NGINX_RELOAD" ]; then
if "$NGINX_RELOAD" 2>&1; then
log_ok "Nginx reloaded"
return 0
fi
fi
# Fallback: try direct reload
if sudo podman exec gitea-nginx nginx -s reload 2>/dev/null; then
log_ok "Nginx reloaded (direct)"
return 0
fi
log_error "Failed to reload nginx"
return 1
}
# Start all domains
start_all() {
log "=========================================="
log "Starting all web host containers"
log "=========================================="
echo ""
local failed=0
for domain_dir in "$DOMAINS_DIR"/*/; do
if [ -d "$domain_dir" ]; then
start_domain "$domain_dir" || ((failed++))
echo ""
fi
done
# Wait for containers to be ready
log "Waiting for containers to initialize..."
sleep 5
# Reload nginx
reload_nginx || true
echo ""
log "=========================================="
if [ $failed -eq 0 ]; then
log_ok "All domains started successfully"
else
log_warn "$failed domain(s) failed to start"
fi
log "=========================================="
return $failed
}
# Stop all domains
stop_all() {
log "=========================================="
log "Stopping all web host containers"
log "=========================================="
echo ""
for domain_dir in "$DOMAINS_DIR"/*/; do
if [ -d "$domain_dir" ]; then
stop_domain "$domain_dir"
fi
done
echo ""
log_ok "All domains stopped"
}
# Show status
status() {
log "=========================================="
log "Web Host Container Status"
log "=========================================="
echo ""
for domain_dir in "$DOMAINS_DIR"/*/; do
if [ -d "$domain_dir" ] && [ -f "$domain_dir/compose.yaml" ]; then
local domain_name=$(basename "$domain_dir")
echo -e "${BLUE}=== $domain_name ===${NC}"
cd "$domain_dir"
podman-compose ps 2>/dev/null || echo " No containers"
echo ""
fi
done
}
# Print usage
usage() {
echo "Web Hosts Startup Script"
echo ""
echo "Usage: $0 <command>"
echo ""
echo "Commands:"
echo " start Start all domain containers and reload nginx"
echo " stop Stop all domain containers"
echo " restart Restart all domain containers"
echo " status Show status of all containers"
echo ""
}
case "${1:-start}" in
start)
start_all
;;
stop)
stop_all
;;
restart)
stop_all
sleep 2
start_all
;;
status)
status
;;
*)
usage
exit 1
;;
esac