343 lines
12 KiB
Bash
Executable File
343 lines
12 KiB
Bash
Executable File
#!/bin/bash
|
|
# LeCoffre Node - Script de lancement séquentiel avec progression
|
|
# Lance les services dans l'ordre logique avec suivi de l'avancement
|
|
|
|
set -e
|
|
|
|
# Couleurs pour l'affichage
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Configuration
|
|
START_TIME=$(date +%s)
|
|
MAX_WAIT=300 # 5 minutes max par service
|
|
|
|
# Fonction pour afficher un message avec timestamp
|
|
print_message() {
|
|
echo -e "${BLUE}[$(date '+%H:%M:%S')]${NC} $1"
|
|
}
|
|
|
|
# Fonction pour afficher la progression
|
|
show_progress() {
|
|
local current=$1
|
|
local total=$2
|
|
local service=$3
|
|
local percent=$((current * 100 / total))
|
|
echo -e "${CYAN}Progress: $current/$total ($percent%) - $service${NC}"
|
|
}
|
|
|
|
# Fonction pour afficher la progression détaillée
|
|
show_detailed_progress() {
|
|
local service_name=$1
|
|
|
|
echo -e "${CYAN}=== Detailed Progress ===${NC}"
|
|
|
|
# Tor Bootstrap
|
|
if docker ps --format '{{.Names}}' | grep -q "tor-proxy"; then
|
|
local bootstrap_log=$(docker logs tor-proxy --tail 10 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%")
|
|
local stage=$(echo "$bootstrap_log" | grep -o '(.*)' | sed 's/[()]//g' || echo "starting")
|
|
echo -e " ${YELLOW}Tor Bootstrap: $progress - $stage${NC}"
|
|
else
|
|
echo -e " ${YELLOW}Tor Bootstrap: Starting...${NC}"
|
|
fi
|
|
else
|
|
echo -e " ${RED}Tor: Not running${NC}"
|
|
fi
|
|
|
|
# Bitcoin Signet
|
|
if docker ps --format '{{.Names}}' | grep -q "bitcoin-signet"; then
|
|
local info=$(docker exec bitcoin-signet bitcoin-cli -signet -conf=/etc/bitcoin/bitcoin.conf getblockchaininfo 2>/dev/null || echo '{}')
|
|
local blocks=$(echo "$info" | jq -r '.blocks // 0' 2>/dev/null || echo "0")
|
|
local headers=$(echo "$info" | jq -r '.headers // 0' 2>/dev/null || echo "0")
|
|
local ibd=$(echo "$info" | jq -r '.initialblockdownload // false' 2>/dev/null || echo "true")
|
|
local verification_progress=$(echo "$info" | jq -r '.verificationprogress // 0' 2>/dev/null || echo "0")
|
|
|
|
if [ "$ibd" = "false" ] || [ "$blocks" -eq "$headers" ]; then
|
|
echo -e " ${GREEN}Bitcoin Signet: Synced ($blocks blocks)${NC}"
|
|
else
|
|
local progress=$((blocks * 100 / headers))
|
|
local ver_percent=$(echo "$verification_progress * 100" | bc -l | cut -d. -f1 2>/dev/null || echo "0")
|
|
echo -e " ${YELLOW}Bitcoin IBD: $blocks/$headers ($progress%) - Verification: $ver_percent%${NC}"
|
|
fi
|
|
else
|
|
echo -e " ${RED}Bitcoin Signet: Not running${NC}"
|
|
fi
|
|
|
|
# BlindBit Oracle
|
|
if docker ps --format '{{.Names}}' | grep -q "blindbit-oracle"; then
|
|
local api_response=$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8000/tweaks/1 2>/dev/null || echo "000")
|
|
if [ "$api_response" = "200" ]; then
|
|
echo -e " ${GREEN}BlindBit Oracle: Ready${NC}"
|
|
else
|
|
local scan_logs=$(docker logs blindbit-oracle --tail 5 2>/dev/null | grep -E "(scanning|scan|blocks|tweaks|processing)" | tail -1 || echo "")
|
|
if [ -n "$scan_logs" ]; then
|
|
echo -e " ${YELLOW}BlindBit Scan: $scan_logs${NC}"
|
|
else
|
|
echo -e " ${YELLOW}BlindBit: Starting...${NC}"
|
|
fi
|
|
fi
|
|
else
|
|
echo -e " ${RED}BlindBit Oracle: Not running${NC}"
|
|
fi
|
|
|
|
# SDK Relay
|
|
if docker ps --format '{{.Names}}' | grep -q "sdk_relay"; then
|
|
local ws_response=$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8091/ 2>/dev/null || echo "000")
|
|
if [ "$ws_response" = "200" ]; then
|
|
echo -e " ${GREEN}SDK Relay: Ready${NC}"
|
|
else
|
|
local relay_logs=$(docker logs sdk_relay --tail 5 2>/dev/null | grep -E "(IBD|blocks|headers|waiting|scanning|connecting)" | tail -1 || echo "")
|
|
if [ -n "$relay_logs" ]; then
|
|
echo -e " ${YELLOW}SDK Relay Sync: $relay_logs${NC}"
|
|
else
|
|
echo -e " ${YELLOW}SDK Relay: Starting...${NC}"
|
|
fi
|
|
fi
|
|
else
|
|
echo -e " ${RED}SDK Relay: Not running${NC}"
|
|
fi
|
|
|
|
|
|
# URLs publiques HTTPS
|
|
echo -e "${CYAN}Public URLs Status:${NC}"
|
|
local urls=(
|
|
"https://dev4.4nkweb.com/status/:Status Page"
|
|
"https://dev4.4nkweb.com/grafana/:Grafana Dashboard"
|
|
"https://dev4.4nkweb.com/:Main Site"
|
|
"https://dev4.4nkweb.com/lecoffre/:LeCoffre App"
|
|
)
|
|
|
|
for url_entry in "${urls[@]}"; do
|
|
local url="${url_entry%%:*}"
|
|
local name="${url_entry##*:}"
|
|
local response=$(curl -s -o /dev/null -w '%{http_code}' "$url" 2>/dev/null || echo "000")
|
|
if [ "$response" = "200" ]; then
|
|
echo -e " ${GREEN}$name: Accessible (HTTP $response)${NC}"
|
|
else
|
|
echo -e " ${YELLOW}$name: Not accessible (HTTP $response)${NC}"
|
|
fi
|
|
done
|
|
|
|
# URLs WebSocket publiques
|
|
echo -e "${CYAN}WebSocket URLs Status:${NC}"
|
|
local ws_urls=(
|
|
"wss://dev3.4nkweb.com/ws/:Bootstrap Relay"
|
|
"wss://dev3.4nkweb.com/ws/:Signer Service"
|
|
)
|
|
|
|
for ws_entry in "${ws_urls[@]}"; do
|
|
local ws_url="${ws_entry%%:*}"
|
|
local ws_name="${ws_entry##*:}"
|
|
# Test WebSocket avec timeout court
|
|
local ws_test=$(timeout 3 wscat -c "$ws_url" --no-color 2>/dev/null && echo "connected" || echo "failed")
|
|
if [ "$ws_test" = "connected" ]; then
|
|
echo -e " ${GREEN}$ws_name: Connected${NC}"
|
|
else
|
|
echo -e " ${YELLOW}$ws_name: Not connected${NC}"
|
|
fi
|
|
done
|
|
|
|
echo -e "${CYAN}========================${NC}"
|
|
}
|
|
|
|
# Fonction pour attendre qu'un service soit healthy
|
|
wait_for_healthy() {
|
|
local service_name=$1
|
|
local max_wait=${2:-$MAX_WAIT}
|
|
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}}' "$service_name" 2>/dev/null || echo "no-healthcheck")
|
|
local running=$(docker inspect --format='{{.State.Running}}' "$service_name" 2>/dev/null || echo "false")
|
|
|
|
if [ "$running" = "true" ] && [ "$status" = "healthy" ]; then
|
|
echo -e "${GREEN}✓ $service_name is healthy${NC}"
|
|
return 0
|
|
fi
|
|
|
|
# Afficher la progression détaillée
|
|
show_detailed_progress "$service_name"
|
|
|
|
sleep 5
|
|
wait_time=$((wait_time + 5))
|
|
done
|
|
|
|
echo -e "${RED}✗ Timeout waiting for $service_name${NC}"
|
|
return 1
|
|
}
|
|
|
|
# Fonction pour vérifier les variables d'environnement d'un service
|
|
check_service_env() {
|
|
local service_name=$1
|
|
local display_name=$2
|
|
|
|
print_message "Checking environment variables for $display_name..."
|
|
|
|
# Vérifier que le fichier .env.master existe
|
|
if [ ! -f "/home/debian/4NK_env/.env.master" ]; then
|
|
echo -e "${RED}✗ Error: .env.master file not found${NC}"
|
|
return 1
|
|
fi
|
|
|
|
# Variables critiques par service
|
|
case "$service_name" in
|
|
"bitcoin")
|
|
local critical_vars=("BITCOIN_RPC_USER" "BITCOIN_RPC_PASSWORD" "BITCOIN_RPC_PORT")
|
|
;;
|
|
"blindbit")
|
|
local critical_vars=("BLINDBIT_API_PORT" "BITCOIN_RPC_URL")
|
|
;;
|
|
"sdk_relay")
|
|
local critical_vars=("RELAY_PORT" "RELAY_HTTP_PORT" "STORAGE_URL")
|
|
;;
|
|
"sdk_storage")
|
|
local critical_vars=("STORAGE_PORT" "STORAGE_DATA_DIR")
|
|
;;
|
|
"lecoffre-front")
|
|
local critical_vars=("NEXT_PUBLIC_API_URL" "NEXT_PUBLIC_4NK_URL" "NEXT_PUBLIC_IDNOT_BASE_URL")
|
|
;;
|
|
"ihm_client")
|
|
local critical_vars=("VITE_API_URL" "VITE_4NK_URL" "VITE_RELAY_URL")
|
|
;;
|
|
"grafana")
|
|
local critical_vars=("GF_SECURITY_ADMIN_PASSWORD" "GF_DATABASE_TYPE")
|
|
;;
|
|
"loki")
|
|
local critical_vars=("LOKI_CONFIG_FILE" "LOKI_DATA_DIR")
|
|
;;
|
|
"promtail")
|
|
local critical_vars=("PROMTAIL_CONFIG_FILE" "LOKI_URL")
|
|
;;
|
|
"status-api")
|
|
local critical_vars=("STATUS_API_PORT" "STATUS_API_HOST")
|
|
;;
|
|
*)
|
|
echo -e "${YELLOW}⚠ No specific environment variables defined for $service_name${NC}"
|
|
return 0
|
|
;;
|
|
esac
|
|
|
|
# Vérifier chaque variable critique
|
|
local missing_vars=()
|
|
for var in "${critical_vars[@]}"; do
|
|
if ! grep -q "^${var}=" /home/debian/4NK_env/.env.master; then
|
|
missing_vars+=("$var")
|
|
fi
|
|
done
|
|
|
|
if [ ${#missing_vars[@]} -eq 0 ]; then
|
|
echo -e "${GREEN}✓ All critical environment variables present for $display_name${NC}"
|
|
return 0
|
|
else
|
|
echo -e "${RED}✗ Missing critical environment variables for $display_name:${NC}"
|
|
for var in "${missing_vars[@]}"; do
|
|
echo -e "${RED} - $var${NC}"
|
|
done
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Fonction pour démarrer un service
|
|
start_service() {
|
|
local service_name=$1
|
|
local display_name=$2
|
|
|
|
# Vérifier les variables d'environnement avant de démarrer
|
|
if ! check_service_env "$service_name" "$display_name"; then
|
|
echo -e "${RED}✗ Environment check failed for $display_name. Continuing anyway...${NC}"
|
|
fi
|
|
|
|
print_message "Starting $display_name..."
|
|
if ! docker compose --env-file /home/debian/4NK_env/.env.master up -d "$service_name"; then
|
|
echo -e "${YELLOW}⚠ Failed to start $display_name, continuing with next service...${NC}"
|
|
return 0
|
|
fi
|
|
|
|
# Attendre que le conteneur soit créé
|
|
sleep 2
|
|
|
|
# Vérifier si le service a un healthcheck
|
|
local has_healthcheck=$(docker inspect --format='{{.Config.Healthcheck}}' "$service_name" 2>/dev/null | grep -q "Test" && echo "true" || echo "false")
|
|
|
|
if [ "$has_healthcheck" = "true" ]; then
|
|
wait_for_healthy "$service_name"
|
|
else
|
|
# Pour les services sans healthcheck, attendre qu'ils soient running
|
|
local wait_time=0
|
|
while [ $wait_time -lt 60 ]; do
|
|
local running=$(docker inspect --format='{{.State.Running}}' "$service_name" 2>/dev/null || echo "false")
|
|
if [ "$running" = "true" ]; then
|
|
echo -e "${GREEN}✓ $display_name is running${NC}"
|
|
break
|
|
fi
|
|
sleep 2
|
|
wait_time=$((wait_time + 2))
|
|
done
|
|
fi
|
|
}
|
|
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo -e "${BLUE} LeCoffre Node - Sequential Startup${NC}"
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo
|
|
|
|
# Arrêter les services existants
|
|
print_message "Stopping existing services..."
|
|
docker compose --env-file /home/debian/4NK_env/.env.master down --remove-orphans >/dev/null 2>&1 || true
|
|
|
|
# Ordre de démarrage logique
|
|
services=(
|
|
"tor:Tor Proxy"
|
|
"bitcoin:Bitcoin Signet"
|
|
"blindbit:BlindBit Oracle"
|
|
"sdk_storage:SDK Storage"
|
|
"sdk_relay:SDK Relay"
|
|
"lecoffre-front:LeCoffre Frontend"
|
|
"ihm_client:IHM Client"
|
|
"grafana:Grafana"
|
|
"status-api:Status API"
|
|
)
|
|
|
|
total_services=${#services[@]}
|
|
current_service=0
|
|
|
|
# Démarrer les services dans l'ordre
|
|
for service in "${services[@]}"; do
|
|
current_service=$((current_service + 1))
|
|
service_name="${service%%:*}"
|
|
display_name="${service##*:}"
|
|
|
|
show_progress $current_service $total_services "$display_name"
|
|
start_service "$service_name" "$display_name"
|
|
echo
|
|
done
|
|
|
|
# Afficher le statut final
|
|
echo -e "${GREEN}🎉 All services started successfully!${NC}"
|
|
echo
|
|
echo -e "${BLUE}Final status:${NC}"
|
|
docker compose --env-file /home/debian/4NK_env/.env.master ps
|
|
|
|
# Calculer le temps total
|
|
end_time=$(date +%s)
|
|
total_time=$((end_time - START_TIME))
|
|
minutes=$((total_time / 60))
|
|
seconds=$((total_time % 60))
|
|
|
|
echo
|
|
echo -e "${GREEN}Total startup time: ${minutes}m ${seconds}s${NC}"
|
|
echo
|
|
echo -e "${BLUE}Useful commands:${NC}"
|
|
echo -e " ${YELLOW}docker compose --env-file /home/debian/4NK_env/.env.master logs -f${NC} # Voir les logs"
|
|
echo -e " ${YELLOW}docker compose --env-file /home/debian/4NK_env/.env.master down${NC} # Arrêter les services"
|
|
echo -e " ${YELLOW}docker compose --env-file /home/debian/4NK_env/.env.master ps${NC} # Voir le statut"
|
|
echo
|