4NK_vault/templates/dev/generate_promtail_config.sh
4NK Dev 5ff468bc84 feat: Système de templates automatisé v2.0.0
- Introduction du système de templates avec séparation templates/storage
- Scripts de génération automatisée pour toutes les configurations
- Résolution multi-passes des variables imbriquées
- API simplifiée qui lit uniquement storage/ (plus de traitement de variables)
- Documentation complète du nouveau système
- Support des services externes (BOOTSTRAP, LECOFFRE_BACK_MINI)
- Protection des templates sources et isolation des environnements
2025-10-05 13:53:38 +00:00

122 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Script de génération de la configuration Promtail
# Génère promtail.yml pour la collecte de logs
set -euo pipefail
# Couleurs pour les messages
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Fonctions d'affichage
print_error() { echo -e "${RED}❌ ERREUR${NC}: $1"; }
print_success() { echo -e "${GREEN}✅ SUCCÈS${NC}: $1"; }
print_info() { echo -e "${BLUE} INFO${NC}: $1"; }
print_warning() { echo -e "${YELLOW}⚠️ ATTENTION${NC}: $1"; }
# Charger les variables d'environnement
load_env_files() {
local env_files=(".env.secrets" ".env")
for env_file in "${env_files[@]}"; do
if [ -f "$env_file" ]; then
echo "📄 Chargement de $env_file..."
# shellcheck source=/dev/null
source "$env_file"
else
print_warning "Fichier $env_file non trouvé"
fi
done
}
# Fonction pour générer la configuration Promtail
generate_promtail_config() {
echo "🔧 Génération de la configuration Promtail"
# Créer le répertoire si nécessaire
mkdir -p "4NK_modules/promtail"
# Générer le fichier promtail.yml
cat > "4NK_modules/promtail/promtail.yml" << EOF
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
EOF
# Ajouter une configuration de scraping pour chaque service
for SERVICE_VAR in "${SERVICES[@]}"; do
echo " 🔄 Ajout config Promtail pour $SERVICE_VAR"
cat >> "4NK_modules/promtail/promtail.yml" << EOF
- job_name: '$SERVICE_VAR'
static_configs:
- targets:
- localhost
labels:
job: $SERVICE_VAR
__path__: \$${SERVICE_VAR}_LOGS_DIR/*.log
pipeline_stages:
- match:
selector: '{job="$SERVICE_VAR"}'
stages:
- regex:
expression: '^(?P<timestamp>\S+\s\S+)\s+(?P<level>\S+)\s+(?P<message>.*)'
- labels:
level:
- timestamp:
source: timestamp
format: '2006-01-02 15:04:05'
EOF
done
echo "✅ Configuration promtail.yml générée"
}
# Fonction principale
main() {
echo "🚀 GÉNÉRATION DE LA CONFIGURATION PROMTAIL"
echo "==========================================="
# Charger les variables d'environnement
load_env_files
# Supprimer le fichier de sortie existant (remplacer complètement)
rm -f "4NK_modules/promtail/promtail.yml" 2>/dev/null || true
# Créer le répertoire si nécessaire
mkdir -p "4NK_modules/promtail"
# Vérifier que SERVICES est défini
if [ -z "${SERVICES[*]:-}" ]; then
print_error "Variable SERVICES non définie dans .env"
exit 1
fi
echo "📋 Génération de la configuration Promtail pour ${#SERVICES[@]} services..."
# Générer la configuration Promtail
generate_promtail_config
echo ""
echo "✅ GÉNÉRATION TERMINÉE !"
echo "📁 Configuration générée: 4NK_modules/promtail/promtail.yml"
echo "🔧 Configuration Promtail pour ${#SERVICES[@]} services"
echo "📊 Services traités: ${#SERVICES[@]}"
}
# Exécuter le script principal
main "$@"