Improve docker config
This commit is contained in:
297
scripts/validate-config.sh
Executable file
297
scripts/validate-config.sh
Executable 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 "$@"
|
||||
Reference in New Issue
Block a user