#!/bin/bash # ============================================================================== # VALIDATE DATABASES - Control Plane # ============================================================================== # Proposito: Validar configuracion de bases de datos contra registry # Mantenido por: DevOps-Agent + Database-Agent # ============================================================================== set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CONTROL_PLANE="$(cd "$SCRIPT_DIR/../../.." && pwd)" WORKSPACE="$(cd "$CONTROL_PLANE/.." && pwd)" DATABASES_REGISTRY="$CONTROL_PLANE/registries/databases.registry.yml" # Colores RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' echo "==============================================" echo " VALIDACION DE BASES DE DATOS" echo "==============================================" echo "" # Verificar que existe databases.registry.yml if [ ! -f "$DATABASES_REGISTRY" ]; then echo -e "${RED}[ERROR]${NC} No existe databases.registry.yml" exit 1 fi echo -e "${GREEN}[OK]${NC} databases.registry.yml encontrado" echo "" # Verificar YAML valido if command -v python3 &> /dev/null; then if python3 -c "import yaml; yaml.safe_load(open('$DATABASES_REGISTRY'))" 2>/dev/null; then echo -e "${GREEN}[OK]${NC} YAML valido" else echo -e "${RED}[ERROR]${NC} databases.registry.yml no es YAML valido" exit 1 fi else echo -e "${YELLOW}[WARN]${NC} python3 no disponible, saltando validacion YAML" fi echo "" echo "--- Bases de Datos Registradas ---" # Extraer bases de datos del registry grep -E "^ [a-z_]+:" "$DATABASES_REGISTRY" | grep -v "roles:" | grep -v "schemas:" | grep -v "extensions:" | head -20 | while read -r line; do DB=$(echo "$line" | sed 's/://g' | xargs) if [ -n "$DB" ] && [[ ! "$DB" =~ ^(owner|runtime|migrator|readonly|name|description|charset|collation|database|host|local|development|production|type|version|port)$ ]]; then echo -e " ${BLUE}*${NC} $DB" fi done echo "" echo "--- Validando Service Descriptors ---" ERRORS=0 WARNINGS=0 find "$WORKSPACE/projects" -name "service.descriptor.yml" 2>/dev/null | while read -r descriptor; do REL_PATH=$(echo "$descriptor" | sed "s|$WORKSPACE/||") # Verificar que tiene db_ref definido if grep -q "db_ref:" "$descriptor" 2>/dev/null; then DB_REF=$(grep "db_ref:" "$descriptor" | head -1 | awk '{print $2}' | tr -d '"') # Extraer nombre de BD del ref (formato: databases.{name}) DB_NAME=$(echo "$DB_REF" | sed 's/databases\.//') # Verificar que la BD esta en el registry if grep -q "^ $DB_NAME:" "$DATABASES_REGISTRY" 2>/dev/null; then echo -e " ${GREEN}[OK]${NC} $REL_PATH -> $DB_NAME" else echo -e " ${RED}[ERROR]${NC} $REL_PATH -> $DB_NAME (no en registry)" ((ERRORS++)) || true fi fi done echo "" echo "--- Validando Convencion de Roles ---" # Verificar que cada BD tiene los 3 roles requeridos echo "" echo "Verificando roles por BD..." for db in gamilit erp_core erp_construccion erp_mecanicas trading betting; do if grep -q "^ $db:" "$DATABASES_REGISTRY" 2>/dev/null; then ROLES_OK=true for role in owner runtime; do if ! grep -A 10 "^ $db:" "$DATABASES_REGISTRY" | grep -q "$role:" 2>/dev/null; then echo -e " ${YELLOW}[WARN]${NC} $db: falta rol '$role'" ROLES_OK=false fi done if [ "$ROLES_OK" = true ]; then echo -e " ${GREEN}[OK]${NC} $db: roles completos" fi fi done echo "" echo "==============================================" if [ $ERRORS -gt 0 ]; then echo -e "${RED}VALIDACION FALLIDA${NC} - $ERRORS errores encontrados" exit 1 else echo -e "${GREEN}VALIDACION EXITOSA${NC}" exit 0 fi