#!/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 print_message "Waiting for $service_name to be healthy..." 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 if [ "$service_name" = "Tor Proxy" ]; 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${NC}" fi else print_message "${YELLOW}⏳ Tor starting: Bootstrap not yet started${NC}" fi fi # Afficher la progression pour Bitcoin if [ "$service_name" = "Bitcoin Signet" ]; 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 print_message "${YELLOW}⏳ Bitcoin IBD: $blocks/$headers blocks ($(($headers - $blocks)) remaining) - $progress%${NC}" fi fi # Afficher la progression pour BlindBit if [ "$service_name" = "BlindBit Oracle" ]; 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${NC}" ;; *) print_message "${YELLOW}⏳ BlindBit scanning: Oracle responding with code $response${NC}" ;; esac fi # Afficher la progression pour SDK Relay if [ "$service_name" = "SDK Relay" ]; 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${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${NC}" fi 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" 60 wait_for_service "SDK Storage" "sdk_storage" 120 wait_for_service "SDK Signer" "sdk_signer" 120 wait_for_service "Status API" "status-api" 60 # 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" 600 # 10 minutes pour Bitcoin start_service "blindbit" wait_for_service "BlindBit Oracle" "blindbit-oracle" 300 start_service "sdk_relay" wait_for_service "SDK Relay" "sdk_relay" 600 # 10 minutes pour SDK Relay # 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 "$@"