lecoffre_node/scripts/start-with-progress.sh
2025-09-21 23:53:46 +00:00

259 lines
11 KiB
Bash
Executable File

#!/bin/bash
# Script de démarrage des services LeCoffre Node avec suivi de la progression
# Démarre les services dans l'ordre correct et affiche la progression
set -e
# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Fonction pour afficher un message
print_message() {
echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"
}
# Fonction pour attendre qu'un service soit healthy
wait_for_service() {
local service_name="$1"
local container_name="$2"
local max_wait="${3:-300}" # 5 minutes par défaut
local wait_time=0
local last_progress_time=0
local progress_interval=30 # Afficher la progression toutes les 30 secondes
print_message "Waiting for $service_name to be healthy (timeout: ${max_wait}s)..."
while [ $wait_time -lt $max_wait ]; do
local status=$(docker inspect --format='{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "no-healthcheck")
local running=$(docker inspect --format='{{.State.Running}}' "$container_name" 2>/dev/null || echo "false")
if [ "$running" = "true" ] && [ "$status" = "healthy" ]; then
print_message "${GREEN}$service_name is healthy${NC}"
return 0
elif [ "$running" = "false" ]; then
print_message "${RED}$service_name is not running${NC}"
return 1
fi
# Afficher la progression pour Tor (timeout: 15 minutes)
if [ "$service_name" = "Tor Proxy" ]; then
if [ $((wait_time - last_progress_time)) -ge $progress_interval ]; then
local bootstrap_log=$(docker logs "$container_name" --tail 20 2>/dev/null | grep "Bootstrapped" | tail -1 || echo "")
if [ -n "$bootstrap_log" ]; then
local progress=$(echo "$bootstrap_log" | grep -o '[0-9]\+%' | tail -1 || echo "0%")
if echo "$bootstrap_log" | grep -q "100%"; then
print_message "${GREEN}✓ Tor ready: Bootstrap complete (100%)${NC}"
return 0
else
print_message "${YELLOW}⏳ Tor bootstrapping: $progress (${wait_time}s elapsed)${NC}"
fi
else
print_message "${YELLOW}⏳ Tor starting: Bootstrap not yet started (${wait_time}s elapsed)${NC}"
fi
last_progress_time=$wait_time
fi
fi
# Afficher la progression pour Bitcoin (timeout: 2 heures)
if [ "$service_name" = "Bitcoin Signet" ]; then
if [ $((wait_time - last_progress_time)) -ge $progress_interval ]; then
local info=$(docker exec "$container_name" bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf getblockchaininfo 2>/dev/null || echo '{}')
local blocks=$(echo "$info" | jq -r '.blocks // 0')
local headers=$(echo "$info" | jq -r '.headers // 0')
local progress=0
if [ "$headers" -gt 0 ]; then
progress=$((blocks * 100 / headers))
fi
if [ "$blocks" -eq "$headers" ] && [ "$blocks" -gt 0 ]; then
print_message "${GREEN}✓ Bitcoin sync complete: $blocks blocks${NC}"
return 0
else
local remaining=$((headers - blocks))
local elapsed_min=$((wait_time / 60))
print_message "${YELLOW}⏳ Bitcoin IBD: $blocks/$headers blocks ($remaining remaining) - $progress% (${elapsed_min}min elapsed)${NC}"
fi
last_progress_time=$wait_time
fi
fi
# Afficher la progression pour BlindBit
if [ "$service_name" = "BlindBit Oracle" ]; then
if [ $((wait_time - last_progress_time)) -ge $progress_interval ]; then
local response=$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8000/tweaks/1 2>/dev/null || echo "000")
case "$response" in
"200")
print_message "${GREEN}✓ BlindBit ready: Oracle service responding${NC}"
return 0
;;
"000")
print_message "${YELLOW}⏳ BlindBit starting: Oracle service not yet ready (${wait_time}s elapsed)${NC}"
;;
*)
print_message "${YELLOW}⏳ BlindBit scanning: Oracle responding with code $response (${wait_time}s elapsed)${NC}"
;;
esac
last_progress_time=$wait_time
fi
fi
# Afficher la progression pour SDK Relay
if [ "$service_name" = "SDK Relay" ]; then
if [ $((wait_time - last_progress_time)) -ge $progress_interval ]; then
local logs=$(docker logs "$container_name" --tail 3 2>/dev/null | grep -E "(waiting for|blocks to download|IBD)" | tail -1 || echo "")
if [ -n "$logs" ]; then
print_message "${YELLOW}⏳ SDK Relay IBD: $logs (${wait_time}s elapsed)${NC}"
else
local response=$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8091/ 2>/dev/null || echo "000")
if [ "$response" = "200" ]; then
print_message "${GREEN}✓ SDK Relay ready: WebSocket server responding${NC}"
return 0
else
print_message "${YELLOW}⏳ SDK Relay starting: WebSocket server not yet ready (${wait_time}s elapsed)${NC}"
fi
fi
last_progress_time=$wait_time
fi
fi
# Afficher la progression pour SDK Signer
if [ "$service_name" = "SDK Signer" ]; then
if [ $((wait_time - last_progress_time)) -ge $progress_interval ]; then
local logs=$(docker logs "$container_name" --tail 5 2>/dev/null | grep -E "(Disconnected from relay|Scheduling reconnect|WebSocket error)" | tail -1 || echo "")
if [ -n "$logs" ]; then
print_message "${YELLOW}⏳ SDK Signer: Waiting for relay connection (${wait_time}s elapsed)${NC}"
else
local response=$(curl -s -o /dev/null -w '%{http_code}' http://localhost:3001/ 2>/dev/null || echo "000")
if [ "$response" = "101" ] || [ "$response" = "426" ]; then
print_message "${GREEN}✓ SDK Signer ready: WebSocket server responding${NC}"
return 0
else
print_message "${YELLOW}⏳ SDK Signer starting: WebSocket server not yet ready (${wait_time}s elapsed)${NC}"
fi
fi
last_progress_time=$wait_time
fi
fi
sleep 10
wait_time=$((wait_time + 10))
done
print_message "${RED}✗ Timeout waiting for $service_name${NC}"
return 1
}
# Fonction pour démarrer un service
start_service() {
local service_name="$1"
print_message "Starting $service_name..."
if docker compose --env-file .env.master up -d "$service_name"; then
print_message "${GREEN}$service_name started${NC}"
return 0
else
print_message "${RED}✗ Failed to start $service_name${NC}"
return 1
fi
}
# Fonction principale
main() {
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} LeCoffre Node - Startup with Progress${NC}"
echo -e "${BLUE}========================================${NC}"
echo
# Arrêter tous les services existants
print_message "Stopping existing services..."
docker compose --env-file .env.master down --remove-orphans
# Démarrer les services dans l'ordre correct
print_message "Starting services in correct order..."
echo
# Phase 1: Services de base (parallèle)
print_message "${PURPLE}Phase 1: Starting base services...${NC}"
start_service "tor"
start_service "sdk_storage"
start_service "sdk_signer"
start_service "status-api"
# Attendre les services de base
wait_for_service "Tor Proxy" "tor-proxy" 900 # 15 minutes pour Tor bootstrap
wait_for_service "SDK Storage" "sdk_storage" 120
wait_for_service "Status API" "status-api" 60
# SDK Signer peut démarrer même sans relay (il se reconnectera plus tard)
print_message "SDK Signer started (will connect to relay when available)"
# Phase 2: Services blockchain (séquentiel)
print_message "${PURPLE}Phase 2: Starting blockchain services...${NC}"
start_service "bitcoin"
wait_for_service "Bitcoin Signet" "bitcoin-signet" 7200 # 2 heures pour Bitcoin IBD
start_service "blindbit"
wait_for_service "BlindBit Oracle" "blindbit-oracle" 600 # 10 minutes pour BlindBit
start_service "sdk_relay"
wait_for_service "SDK Relay" "sdk_relay" 1800 # 30 minutes pour SDK Relay IBD
# Phase 3: Services applicatifs (séquentiel)
print_message "${PURPLE}Phase 3: Starting application services...${NC}"
start_service "lecoffre-back"
wait_for_service "LeCoffre Backend" "lecoffre-back" 120
start_service "lecoffre-front"
start_service "ihm_client"
wait_for_service "LeCoffre Frontend" "lecoffre-front" 120
wait_for_service "IHM Client" "ihm_client" 120
# Phase 4: Services de monitoring (séquentiel, indépendant)
print_message "${PURPLE}Phase 4: Starting monitoring services...${NC}"
start_service "loki"
wait_for_service "Loki" "loki" 120
start_service "promtail"
wait_for_service "Promtail" "promtail" 60
start_service "grafana"
wait_for_service "Grafana" "grafana" 120
# Phase 5: Services utilitaires
print_message "${PURPLE}Phase 5: Starting utility services...${NC}"
start_service "watchtower"
echo
print_message "${GREEN}✓ All services started successfully!${NC}"
echo
print_message "Use './scripts/monitor-progress.sh' to monitor progress"
print_message "Use './scripts/watch-progress.sh' for real-time monitoring"
echo
print_message "Testing external access..."
# Tester l'accès externe
local services=("https://dev4.4nkweb.com/status/" "https://dev4.4nkweb.com/grafana/" "https://dev4.4nkweb.com/" "https://dev4.4nkweb.com/lecoffre/")
for service in "${services[@]}"; do
if curl -s -o /dev/null -w '%{http_code}' "$service" | grep -q "200"; then
print_message "${GREEN}$service is accessible${NC}"
else
print_message "${YELLOW}$service is not yet accessible${NC}"
fi
done
echo
print_message "${GREEN}Startup complete!${NC}"
}
# Exécution
main "$@"