Improve docker config

This commit is contained in:
Urtzi Alfaro
2025-07-20 02:16:51 +02:00
parent 9a67f3d175
commit 1c730c3c81
27 changed files with 2598 additions and 1161 deletions

14
scripts/docker-logs.sh Executable file
View File

@@ -0,0 +1,14 @@
# scripts/docker-logs.sh
#!/bin/bash
# View logs for specific service or all services
SERVICE=${1:-"all"}
if [ "$SERVICE" = "all" ]; then
echo "📋 Showing logs for all services..."
docker-compose logs -f --tail=100
else
echo "📋 Showing logs for $SERVICE..."
docker-compose logs -f --tail=100 $SERVICE
fi

214
scripts/docker-setup.sh Executable file
View File

@@ -0,0 +1,214 @@
# ================================================================
# FIXED SETUP SCRIPT
# scripts/docker-setup.sh
# ================================================================
#!/bin/bash
# Fixed setup script with proper error handling
set -e
ENVIRONMENT=${1:-development}
PROFILES=${2:-"development,frontend"}
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Logging functions
print_step() {
echo -e "${GREEN}[STEP]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_step "Setting up Bakery Forecasting Platform"
echo "Environment: $ENVIRONMENT"
echo "Profiles: $PROFILES"
# Check if .env file exists
if [ ! -f ".env" ]; then
print_error ".env file not found!"
echo "Please create .env file with the content from the artifact."
echo "Run: cp .env.example .env"
exit 1
fi
# Validate critical environment variables
print_step "Validating environment variables..."
# Source the .env file to check variables
set -a # automatically export all variables
source .env
set +a
# Check critical variables
critical_vars=(
"IMAGE_TAG"
"AUTH_DB_NAME"
"AUTH_DB_USER"
"AUTH_DB_PASSWORD"
"REDIS_PASSWORD"
"RABBITMQ_USER"
"RABBITMQ_PASSWORD"
"GATEWAY_PORT"
"AUTH_SERVICE_PORT"
)
missing_vars=()
for var in "${critical_vars[@]}"; do
if [ -z "${!var}" ]; then
missing_vars+=("$var")
fi
done
if [ ${#missing_vars[@]} -gt 0 ]; then
print_error "Missing required environment variables:"
printf '%s\n' "${missing_vars[@]}"
exit 1
fi
print_step "Environment variables validated successfully"
# Create necessary directories
print_step "Creating necessary directories..."
mkdir -p infrastructure/{redis,rabbitmq,postgres/init-scripts,monitoring/{prometheus/rules,grafana/{dashboards,datasources}},pgadmin}
mkdir -p backups logs models templates/{email,whatsapp}
mkdir -p shared/{config,auth,database,messaging,monitoring,utils}
# Create basic monitoring configs if they don't exist
if [ ! -f "infrastructure/monitoring/prometheus/prometheus.yml" ]; then
print_step "Creating basic Prometheus configuration..."
cat > infrastructure/monitoring/prometheus/prometheus.yml << 'EOF'
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'gateway'
static_configs:
- targets: ['gateway:8000']
- job_name: 'auth-service'
static_configs:
- targets: ['auth-service:8000']
- job_name: 'training-service'
static_configs:
- targets: ['training-service:8000']
- job_name: 'forecasting-service'
static_configs:
- targets: ['forecasting-service:8000']
- job_name: 'data-service'
static_configs:
- targets: ['data-service:8000']
- job_name: 'tenant-service'
static_configs:
- targets: ['tenant-service:8000']
- job_name: 'notification-service'
static_configs:
- targets: ['notification-service:8000']
EOF
fi
# Set proper permissions
chmod 644 infrastructure/monitoring/prometheus/prometheus.yml 2>/dev/null || true
# Stop any existing containers
print_step "Stopping existing containers..."
docker-compose down --remove-orphans 2>/dev/null || true
# Build and start services based on environment
case $ENVIRONMENT in
"development")
print_step "Starting development environment..."
IFS=',' read -ra PROFILE_ARRAY <<< "$PROFILES"
PROFILE_ARGS=""
for profile in "${PROFILE_ARRAY[@]}"; do
PROFILE_ARGS="$PROFILE_ARGS --profile $profile"
done
# Build first to catch any build errors
print_step "Building services..."
docker-compose $PROFILE_ARGS build
# Then start
print_step "Starting services..."
docker-compose $PROFILE_ARGS up -d
;;
"production")
print_step "Starting production environment..."
docker-compose -f docker-compose.yml -f docker-compose.prod.yml --profile production --profile monitoring up -d --build
;;
"testing")
print_step "Starting testing environment..."
docker-compose -f docker-compose.yml -f docker-compose.test.yml up -d --build
;;
*)
print_step "Starting with custom profiles: $PROFILES"
IFS=',' read -ra PROFILE_ARRAY <<< "$PROFILES"
PROFILE_ARGS=""
for profile in "${PROFILE_ARRAY[@]}"; do
PROFILE_ARGS="$PROFILE_ARGS --profile $profile"
done
docker-compose $PROFILE_ARGS build
docker-compose $PROFILE_ARGS up -d
;;
esac
# Wait a moment for services to start
print_step "Waiting for services to start..."
sleep 10
# Check service status
print_step "Checking service status..."
if command -v curl &> /dev/null; then
# Check if gateway is responding
if curl -f -s "http://localhost:${GATEWAY_PORT}/health" > /dev/null 2>&1; then
echo "✅ Gateway is responding"
else
echo "⚠️ Gateway is not yet responding (this is normal during first startup)"
fi
else
echo "⚠️ curl not found - skipping health check"
fi
print_step "Setup completed!"
echo ""
echo "================================================================"
echo -e "${GREEN}SERVICES AVAILABLE${NC}"
echo "================================================================"
echo "- Gateway: http://localhost:${GATEWAY_PORT}"
echo "- API Docs: http://localhost:${GATEWAY_PORT}/docs"
echo "- Dashboard: http://localhost:${DASHBOARD_PORT} (if frontend profile enabled)"
echo "- Grafana: http://localhost:${GRAFANA_PORT} (${GRAFANA_ADMIN_USER}/${GRAFANA_ADMIN_PASSWORD})"
echo "- pgAdmin: http://localhost:${PGADMIN_PORT} (${PGADMIN_EMAIL}/${PGADMIN_PASSWORD})"
echo "- RabbitMQ: http://localhost:${RABBITMQ_MANAGEMENT_PORT} (${RABBITMQ_USER}/${RABBITMQ_PASSWORD})"
echo "- Redis Commander: http://localhost:${REDIS_COMMANDER_PORT} (${REDIS_COMMANDER_USER}/${REDIS_COMMANDER_PASSWORD})"
echo ""
echo "================================================================"
echo -e "${GREEN}NEXT STEPS${NC}"
echo "================================================================"
echo "1. Check service health:"
echo " ./scripts/docker-health-check.sh"
echo ""
echo "2. View logs:"
echo " docker-compose logs -f"
echo ""
echo "3. Check specific service:"
echo " docker-compose logs -f auth-service"
echo ""
echo "If you see any errors, check the logs for more details."

90
scripts/health-check.sh Executable file
View File

@@ -0,0 +1,90 @@
# scripts/docker-health-check.sh
#!/bin/bash
# Comprehensive health check for all services
services=(
"bakery-redis:6379"
"bakery-rabbitmq:15672"
"bakery-gateway:8000"
"bakery-auth-service:8000"
"bakery-tenant-service:8000"
"bakery-training-service:8000"
"bakery-forecasting-service:8000"
"bakery-data-service:8000"
"bakery-notification-service:8000"
)
echo "🏥 Checking service health..."
for service_port in "${services[@]}"; do
service=$(echo $service_port | cut -d: -f1)
port=$(echo $service_port | cut -d: -f2)
if docker ps --format "table {{.Names}}" | grep -q "^$service$"; then
if [ "$service" = "bakery-redis" ]; then
# Redis health check
if docker exec $service redis-cli -a redis_pass123 ping > /dev/null 2>&1; then
echo "$service is healthy"
else
echo "$service is unhealthy"
fi
elif [ "$service" = "bakery-rabbitmq" ]; then
# RabbitMQ health check
if curl -s -u bakery:forecast123 http://localhost:$port/api/health/checks/alarms > /dev/null; then
echo "$service is healthy"
else
echo "$service is unhealthy"
fi
else
# HTTP service health check
container_ip=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $service)
if curl -f -s "http://$container_ip:8000/health" > /dev/null; then
echo "$service is healthy"
else
echo "$service is unhealthy"
fi
fi
else
echo "⚠️ $service is not running"
fi
done
echo ""
echo "🔍 Checking database connections..."
databases=("auth-db" "training-db" "forecasting-db" "data-db" "tenant-db" "notification-db")
for db in "${databases[@]}"; do
if docker ps --format "table {{.Names}}" | grep -q "^bakery-$db$"; then
db_name=$(echo $db | sed 's/-/_/g')
user=$(echo $db | sed 's/-db//' | sed 's/-/_/g')_user
if docker exec bakery-$db pg_isready -U $user -d $db_name > /dev/null 2>&1; then
echo "✅ bakery-$db is ready"
else
echo "❌ bakery-$db is not ready"
fi
else
echo "⚠️ bakery-$db is not running"
fi
done
echo ""
echo "📊 Service resource usage:"
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" $(docker ps --format "{{.Names}}" | grep "^bakery-")
# scripts/docker-logs.sh
#!/bin/bash
# View logs for specific service or all services
SERVICE=${1:-"all"}
if [ "$SERVICE" = "all" ]; then
echo "📋 Showing logs for all services..."
docker-compose logs -f --tail=100
else
echo "📋 Showing logs for $SERVICE..."
docker-compose logs -f --tail=100 $SERVICE
fi

106
scripts/restart-service.sh Executable file
View File

@@ -0,0 +1,106 @@
# ================================================================
# SERVICE RESTART SCRIPT
# scripts/restart-service.sh
# ================================================================
#!/bin/bash
# Restart individual service script
set -e
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
print_step() {
echo -e "${BLUE}[RESTART]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
restart_service() {
local service=$1
print_step "Restarting $service..."
# Build the service
docker-compose build $service
# Restart with zero downtime
docker-compose up -d --no-deps --force-recreate $service
# Wait a bit for the service to start
sleep 5
# Check health
local port
case $service in
"gateway") port=8000 ;;
"auth-service") port=8001 ;;
"training-service") port=8002 ;;
"forecasting-service") port=8003 ;;
"data-service") port=8004 ;;
"tenant-service") port=8005 ;;
"notification-service") port=8006 ;;
*)
print_error "Unknown service: $service"
exit 1
;;
esac
# Health check with timeout
local attempts=0
local max_attempts=12 # 60 seconds total
while [ $attempts -lt $max_attempts ]; do
if curl -f -s "http://localhost:$port/health" > /dev/null 2>&1; then
print_success "$service is healthy and ready"
return 0
fi
attempts=$((attempts + 1))
echo "Waiting for $service to be healthy... ($attempts/$max_attempts)"
sleep 5
done
print_error "$service failed to become healthy within 60 seconds"
return 1
}
# Main function
main() {
if [ $# -eq 0 ]; then
echo "Usage: $0 <service-name>"
echo ""
echo "Available services:"
echo " gateway"
echo " auth-service"
echo " training-service"
echo " forecasting-service"
echo " data-service"
echo " tenant-service"
echo " notification-service"
echo ""
echo "Example: $0 auth-service"
exit 1
fi
local service=$1
echo "================================================================"
echo "RESTARTING SERVICE: $service"
echo "================================================================"
restart_service $service
}
# Run main function
main "$@"

File diff suppressed because it is too large Load Diff

297
scripts/validate-config.sh Executable file
View File

@@ -0,0 +1,297 @@
# ================================================================
# CONFIGURATION VALIDATION SCRIPT
# scripts/validate-config.sh
# ================================================================
#!/bin/bash
# Configuration validation script
set -e
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
print_header() {
echo ""
echo "================================================================"
echo -e "${GREEN}$1${NC}"
echo "================================================================"
}
print_success() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_error() {
echo -e "${RED}[✗]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
validate_env_file() {
print_header "VALIDATING ENVIRONMENT CONFIGURATION"
if [ ! -f ".env" ]; then
print_error ".env file not found"
exit 1
fi
# Load environment variables
source .env
# Critical settings validation
critical_vars=(
"JWT_SECRET_KEY"
"AUTH_DATABASE_URL"
"TRAINING_DATABASE_URL"
"FORECASTING_DATABASE_URL"
"DATA_DATABASE_URL"
"TENANT_DATABASE_URL"
"NOTIFICATION_DATABASE_URL"
"REDIS_URL"
"RABBITMQ_URL"
)
all_good=true
for var in "${critical_vars[@]}"; do
if [ -z "${!var}" ]; then
print_error "$var is not set"
all_good=false
elif [[ "${!var}" == *"change"* ]] || [[ "${!var}" == *"default"* ]]; then
print_warning "$var appears to use default/placeholder value"
else
print_success "$var is configured"
fi
done
# Check JWT secret strength
if [ ${#JWT_SECRET_KEY} -lt 32 ]; then
print_error "JWT_SECRET_KEY must be at least 32 characters long"
all_good=false
fi
# Check environment
if [ "$ENVIRONMENT" = "production" ]; then
production_vars=("AEMET_API_KEY" "MADRID_OPENDATA_API_KEY" "SMTP_USER" "SMTP_PASSWORD")
for var in "${production_vars[@]}"; do
if [ -z "${!var}" ]; then
print_warning "$var should be configured for production"
fi
done
fi
if $all_good; then
print_success "Environment configuration is valid"
else
print_error "Environment configuration has issues"
exit 1
fi
}
validate_service_configs() {
print_header "VALIDATING SERVICE CONFIGURATIONS"
services=("auth" "training" "forecasting" "data" "tenant" "notification")
for service in "${services[@]}"; do
config_file="services/$service/app/core/config.py"
if [ -f "$config_file" ]; then
print_success "$service configuration exists"
# Check if configuration follows the standard
if grep -q "BaseServiceSettings" "$config_file"; then
print_success "$service uses BaseServiceSettings"
else
print_warning "$service doesn't inherit from BaseServiceSettings"
fi
if grep -q "DATABASE_URL" "$config_file"; then
print_success "$service has database configuration"
else
print_warning "$service missing database configuration"
fi
else
print_error "$service configuration missing"
fi
done
# Check gateway configuration
if [ -f "gateway/app/core/config.py" ]; then
print_success "Gateway configuration exists"
else
print_error "Gateway configuration missing"
fi
}
validate_shared_config() {
print_header "VALIDATING SHARED CONFIGURATION"
if [ -f "shared/config/base.py" ]; then
print_success "Base configuration exists"
if grep -q "BaseServiceSettings" "shared/config/base.py"; then
print_success "BaseServiceSettings class found"
else
print_error "BaseServiceSettings class missing"
fi
else
print_error "Base configuration missing"
fi
shared_modules=("auth" "database" "messaging" "monitoring" "utils")
for module in "${shared_modules[@]}"; do
if [ -d "shared/$module" ]; then
print_success "Shared $module module exists"
else
print_warning "Shared $module module missing"
fi
done
}
validate_docker_config() {
print_header "VALIDATING DOCKER CONFIGURATION"
if [ -f "docker-compose.yml" ]; then
print_success "Docker Compose configuration exists"
# Check if all services are defined
services=("gateway" "auth-service" "training-service" "forecasting-service" "data-service" "tenant-service" "notification-service")
for service in "${services[@]}"; do
if grep -q "$service:" docker-compose.yml; then
print_success "$service defined in docker-compose.yml"
else
print_error "$service missing from docker-compose.yml"
fi
done
# Check if all databases are defined
databases=("auth-db" "training-db" "forecasting-db" "data-db" "tenant-db" "notification-db")
for db in "${databases[@]}"; do
if grep -q "$db:" docker-compose.yml; then
print_success "$db defined in docker-compose.yml"
else
print_error "$db missing from docker-compose.yml"
fi
done
# Check infrastructure services
infra=("redis" "rabbitmq" "prometheus" "grafana")
for service in "${infra[@]}"; do
if grep -q "$service:" docker-compose.yml; then
print_success "$service defined in docker-compose.yml"
else
print_warning "$service missing from docker-compose.yml"
fi
done
else
print_error "Docker Compose configuration missing"
fi
# Check Dockerfiles
services=("gateway" "auth" "training" "forecasting" "data" "tenant" "notification")
for service in "${services[@]}"; do
if [ "$service" = "gateway" ]; then
dockerfile="gateway/Dockerfile"
else
dockerfile="services/$service/Dockerfile"
fi
if [ -f "$dockerfile" ]; then
print_success "$service Dockerfile exists"
else
print_warning "$service Dockerfile missing"
fi
done
}
validate_directory_structure() {
print_header "VALIDATING DIRECTORY STRUCTURE"
required_dirs=(
"shared/config"
"shared/auth"
"shared/database"
"shared/messaging"
"gateway/app/core"
"services/auth/app/core"
"services/training/app/core"
"services/forecasting/app/core"
"services/data/app/core"
"services/tenant/app/core"
"services/notification/app/core"
"scripts"
"logs"
"models"
"templates"
)
missing_dirs=()
for dir in "${required_dirs[@]}"; do
if [ -d "$dir" ]; then
print_success "$dir exists"
else
print_warning "$dir missing"
missing_dirs+=("$dir")
fi
done
if [ ${#missing_dirs[@]} -gt 0 ]; then
print_warning "Creating missing directories..."
for dir in "${missing_dirs[@]}"; do
mkdir -p "$dir"
print_success "Created $dir"
done
fi
}
validate_scripts() {
print_header "VALIDATING UTILITY SCRIPTS"
scripts=("setup.sh" "test.sh" "deploy.sh" "health-check.sh" "validate-config.sh")
for script in "${scripts[@]}"; do
script_path="scripts/$script"
if [ -f "$script_path" ]; then
print_success "$script exists"
if [ -x "$script_path" ]; then
print_success "$script is executable"
else
print_warning "$script is not executable - fixing..."
chmod +x "$script_path"
fi
else
print_warning "$script missing"
fi
done
}
# Main validation function
main() {
print_header "CONFIGURATION VALIDATION"
validate_directory_structure
validate_shared_config
validate_service_configs
validate_env_file
validate_docker_config
validate_scripts
print_header "VALIDATION COMPLETE"
echo "If all validations passed, you're ready to start the services!"
echo ""
echo "Next steps:"
echo "1. docker-compose up -d"
echo "2. ./scripts/health-check.sh"
}
# Run validation
main "$@"