#!/bin/bash # Script pour mettre à jour les healthchecks avec des tests de progression set -e COMPOSE_FILE="/home/debian/4NK_env/lecoffre_node/docker-compose.yml" BACKUP_FILE="/home/debian/4NK_env/lecoffre_node/docker-compose.yml.backup" echo "Mise à jour des healthchecks avec tests de progression..." # Créer une sauvegarde cp "$COMPOSE_FILE" "$BACKUP_FILE" # Fonction pour remplacer un healthcheck replace_healthcheck() { local service_name="$1" local old_test="$2" local new_test="$3" echo "Mise à jour du healthcheck pour $service_name..." # Utiliser awk pour remplacer le test awk -v service="$service_name" -v old_test="$old_test" -v new_test="$new_test" ' BEGIN { in_service = 0; in_healthcheck = 0; replaced = 0 } /^ [a-zA-Z_]+:/ { if (in_healthcheck) in_healthcheck = 0 if ($0 ~ "^ " service ":") in_service = 1 else in_service = 0 } /^ healthcheck:/ { if (in_service) in_healthcheck = 1 } /^ test:/ { if (in_healthcheck && !replaced) { print " test: " new_test replaced = 1 next } } { print } ' "$COMPOSE_FILE" > "$COMPOSE_FILE.tmp" && mv "$COMPOSE_FILE.tmp" "$COMPOSE_FILE" } # Mettre à jour Tor replace_healthcheck "tor" \ '["CMD", "sh", "-c", "if test -f /var/log/tor/tor.log && test -s /var/log/tor/tor.log; then echo '\''Tor ready: SOCKS proxy listening on port 9050'\''; exit 0; else echo '\''Tor starting: SOCKS proxy not yet ready'\''; exit 1; fi"]' \ '["CMD", "sh", "-c", "if test -f /var/log/tor/tor.log && test -s /var/log/tor/tor.log; then bootstrap_log=\$(tail -20 /var/log/tor/tor.log | grep '\''Bootstrapped'\'' | tail -1); if echo \"\$bootstrap_log\" | grep -q '\''100%'\''; then echo '\''Tor ready: Bootstrap complete (100%)'\''; exit 0; else progress=\$(echo \"\$bootstrap_log\" | grep -o '\''[0-9]\\\\+%'\'' | tail -1 || echo '\''0%'\''); echo \"Tor bootstrapping: \$progress\"; exit 1; fi; else echo '\''Tor starting: Bootstrap not yet started'\''; exit 1; fi"]' # Mettre à jour Bitcoin replace_healthcheck "bitcoin" \ '["CMD", "sh", "-c", "if bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf getblockchaininfo > /dev/null 2>&1; then echo '\''Bitcoin ready: RPC responding'\''; exit 0; else echo '\''Bitcoin starting: RPC not ready'\''; exit 1; fi"]' \ '["CMD", "sh", "-c", "info=\$(bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf getblockchaininfo 2>/dev/null || echo '\''{}'\''); blocks=\$(echo \"\$info\" | jq -r '\''.blocks // 0'\''); headers=\$(echo \"\$info\" | jq -r '\''.headers // 0'\''); ibd=\$(echo \"\$info\" | jq -r '\''.initialblockdownload // false'\''); if [ \"\$ibd\" = \"false\" ] || [ \"\$blocks\" -eq \"\$headers\" ]; then echo \"Bitcoin ready: Synced (\$blocks blocks)\"; exit 0; else remaining=\$((headers - blocks)); progress=\$((blocks * 100 / headers)); echo \"Bitcoin IBD: \$blocks/\$headers (\$remaining remaining) - \$progress%\"; exit 1; fi"]' # Mettre à jour BlindBit replace_healthcheck "blindbit" \ '["CMD", "sh", "-c", "if wget -q --spider http://localhost:8000/tweaks/1; then echo '\''BlindBit ready: Oracle service responding'\''; exit 0; else echo '\''BlindBit starting: Oracle service not yet ready'\''; exit 1; fi"]' \ '["CMD", "sh", "-c", "scan_logs=\$(tail -10 /var/log/blindbit/blindbit.log 2>/dev/null | grep -E \"(scanning|scan|blocks|tweaks)\" | tail -1 || echo \"\"); if [ -n \"\$scan_logs\" ]; then echo \"BlindBit scanning: \$scan_logs\"; exit 1; else if wget -q --spider http://localhost:8000/tweaks/1; then echo '\''BlindBit ready: Oracle service responding'\''; exit 0; else echo '\''BlindBit starting: Oracle service not yet ready'\''; exit 1; fi; fi"]' # Mettre à jour SDK Relay replace_healthcheck "sdk_relay" \ '["CMD", "sh", "-c", "if curl -f http://localhost:8091/ >/dev/null 2>&1; then echo '\''SDK Relay ready: WebSocket server responding'\''; exit 0; else echo '\''SDK Relay IBD: Waiting for Bitcoin sync to complete'\''; exit 1; fi"]' \ '["CMD", "sh", "-c", "relay_logs=\$(tail -10 /var/log/sdk_relay/sdk_relay.log 2>/dev/null | grep -E \"(IBD|blocks|headers|waiting|scanning)\" | tail -1 || echo \"\"); if [ -n \"\$relay_logs\" ]; then echo \"SDK Relay sync: \$relay_logs\"; exit 1; else if curl -f http://localhost:8091/ >/dev/null 2>&1; then echo '\''SDK Relay ready: WebSocket server responding'\''; exit 0; else echo '\''SDK Relay starting: WebSocket server not yet ready'\''; exit 1; fi; fi"]' echo "Healthchecks mis à jour avec succès!" echo "Sauvegarde créée: $BACKUP_FILE"