#!/bin/bash # ============================================================================= # Script de Redémarrage Complet 4NK Node # ============================================================================= # Date: $(date) # Motif: Redémarrage propre pour intégrer dev3.4nkweb.com # ============================================================================= set -e # Arrêter en cas d'erreur # ============================================================================= # CONFIGURATION # ============================================================================= # 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 # Configuration du projet PROJECT_NAME="4NK Node" PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" WORKSPACE_DIR="$(dirname "$PROJECT_DIR")" # Réseau Docker NETWORK_NAME="4nk_node_btcnet" # Images Docker TOR_IMAGE="dperson/torproxy:latest" BITCOIN_IMAGE="4nk_node_bitcoin" BLINDBIT_IMAGE="4nk_node_blindbit" RELAY_IMAGE="4nk_node_sdk_relay_1" # Volumes BITCOIN_VOLUME="bitcoin_data" BLINDBIT_VOLUME="blindbit_data" RELAY_1_VOLUME="sdk_relay_1_data" RELAY_2_VOLUME="sdk_relay_2_data" RELAY_3_VOLUME="sdk_relay_3_data" # Ports TOR_PORTS=("9050:9050" "9051:9051") BITCOIN_PORTS=("38333:38333" "18443:18443" "29000:29000") BLINDBIT_PORTS=("8000:8000") RELAY_1_PORTS=("8090:8090" "8091:8091") RELAY_2_PORTS=("8092:8090" "8093:8091") RELAY_3_PORTS=("8094:8090" "8095:8091") # Chemins de configuration BITCOIN_CONF="$PROJECT_DIR/bitcoin/bitcoin.conf" BLINDBIT_CONF="$PROJECT_DIR/blindbit/blindbit.toml" RELAY_1_CONF="$PROJECT_DIR/sdk_relay/.conf.docker.relay1" RELAY_2_CONF="$PROJECT_DIR/sdk_relay/.conf.docker.relay2" RELAY_3_CONF="$PROJECT_DIR/sdk_relay/.conf.docker.relay3" EXTERNAL_NODES_CONF="$PROJECT_DIR/sdk_relay/external_nodes.conf" # Variables d'environnement communes COMMON_ENV=( "RUST_LOG=debug,bitcoincore_rpc=trace" "HOME=/home/bitcoin" "BITCOIN_COOKIE_PATH=/home/bitcoin/.bitcoin/signet/.cookie" "ENABLE_SYNC_TEST=1" ) # ============================================================================= # FONCTIONS UTILITAIRES # ============================================================================= print_header() { echo -e "${BLUE}=============================================================================${NC}" echo -e "${BLUE}$1${NC}" echo -e "${BLUE}=============================================================================${NC}" } print_step() { echo -e "${CYAN}🔄 $1${NC}" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } print_info() { echo -e "${PURPLE}ℹ️ $1${NC}" } wait_for_container() { local container_name=$1 local max_attempts=${2:-30} local attempt=1 print_info "Attente du démarrage de $container_name..." while [ $attempt -le $max_attempts ]; do if docker ps --format "table {{.Names}}" | grep -q "^$container_name$"; then if docker ps --format "table {{.Status}}" --filter "name=$container_name" | grep -q "Up"; then print_success "$container_name est démarré" return 0 fi fi echo -n "." sleep 2 ((attempt++)) done print_error "Timeout: $container_name n'a pas démarré dans les temps" return 1 } check_file_exists() { local file_path=$1 local description=$2 if [ ! -f "$file_path" ]; then print_error "Fichier manquant: $description ($file_path)" return 1 fi print_success "Fichier trouvé: $description" return 0 } build_port_mapping() { local ports=("$@") local mapping="" for port in "${ports[@]}"; do if [ -n "$mapping" ]; then mapping="$mapping -p $port" else mapping="-p $port" fi done echo "$mapping" } build_env_vars() { local env_vars="" for env_var in "${COMMON_ENV[@]}"; do if [ -n "$env_vars" ]; then env_vars="$env_vars -e $env_var" else env_vars="-e $env_var" fi done echo "$env_vars" } # ============================================================================= # FONCTIONS PRINCIPALES # ============================================================================= stop_all_services() { print_header "ARRÊT DE TOUS LES SERVICES" print_step "Arrêt de tous les conteneurs" docker stop $(docker ps -q) 2>/dev/null || true print_step "Arrêt de docker-compose" docker-compose down -v 2>/dev/null || true print_step "Vérification qu'aucun conteneur ne tourne" if docker ps --format "table {{.Names}}" | grep -q .; then print_warning "Des conteneurs sont encore en cours d'exécution" docker ps else print_success "Aucun conteneur en cours d'exécution" fi } cleanup_containers() { print_header "NETTOYAGE COMPLET" print_step "Suppression de tous les conteneurs" local removed_containers=$(docker rm -f $(docker ps -aq) 2>/dev/null || true) if [ -n "$removed_containers" ]; then print_info "Conteneurs supprimés: $removed_containers" else print_info "Aucun conteneur à supprimer" fi print_step "Nettoyage des réseaux" local removed_networks=$(docker network prune -f 2>/dev/null || true) if [ -n "$removed_networks" ]; then print_info "Réseaux supprimés: $removed_networks" else print_info "Aucun réseau à supprimer" fi } create_network() { print_header "CRÉATION DU RÉSEAU" print_step "Création du réseau Docker: $NETWORK_NAME" local network_id=$(docker network create "$NETWORK_NAME" 2>/dev/null || true) if [ -n "$network_id" ]; then print_success "Réseau créé: $network_id" else print_info "Réseau déjà existant ou erreur" fi } start_tor() { print_header "DÉMARRAGE DE TOR PROXY" print_step "Démarrage de Tor Proxy" local tor_ports=$(build_port_mapping "${TOR_PORTS[@]}") local tor_container_id=$(docker run -d \ --name tor-proxy \ --network "$NETWORK_NAME" \ --network-alias tor \ $tor_ports \ "$TOR_IMAGE") print_success "Tor Proxy démarré: $tor_container_id" wait_for_container "tor-proxy" 10 } start_bitcoin() { print_header "DÉMARRAGE DE BITCOIN CORE" # Vérification du fichier de configuration check_file_exists "$BITCOIN_CONF" "Configuration Bitcoin" print_step "Démarrage de Bitcoin Core" local bitcoin_ports=$(build_port_mapping "${BITCOIN_PORTS[@]}") local bitcoin_container_id=$(docker run -d \ --name bitcoin-signet \ --network "$NETWORK_NAME" \ --network-alias bitcoin \ $bitcoin_ports \ -v "$BITCOIN_VOLUME:/home/bitcoin/.bitcoin" \ -v "$BITCOIN_CONF:/home/bitcoin/.bitcoin/bitcoin.conf" \ "$BITCOIN_IMAGE") print_success "Bitcoin Core démarré: $bitcoin_container_id" wait_for_container "bitcoin-signet" 15 } start_blindbit() { print_header "DÉMARRAGE DE BLINDBIT ORACLE" # Vérification du fichier de configuration check_file_exists "$BLINDBIT_CONF" "Configuration Blindbit" print_step "Démarrage de Blindbit Oracle" local blindbit_ports=$(build_port_mapping "${BLINDBIT_PORTS[@]}") local blindbit_container_id=$(docker run -d \ --name blindbit-oracle \ --network "$NETWORK_NAME" \ --network-alias blindbit \ $blindbit_ports \ -v "$BLINDBIT_VOLUME:/data" \ -v "$BLINDBIT_CONF:/data/blindbit.toml" \ -v "$BITCOIN_VOLUME:/home/bitcoin/.bitcoin" \ "$BLINDBIT_IMAGE") print_success "Blindbit Oracle démarré: $blindbit_container_id" wait_for_container "blindbit-oracle" 15 } build_relay_image() { print_header "CONSTRUCTION DE L'IMAGE SDK_RELAY" print_step "Construction de l'image sdk_relay" print_info "Cette étape peut prendre plusieurs minutes..." if docker build -f sdk_relay/Dockerfile -t "$RELAY_IMAGE" ..; then print_success "Image sdk_relay construite avec succès" else print_error "Échec de la construction de l'image sdk_relay" exit 1 fi } start_relay() { local relay_number=$1 local relay_name="sdk_relay_$relay_number" local relay_conf_var="RELAY_${relay_number}_CONF" local relay_conf="${!relay_conf_var}" local relay_volume_var="RELAY_${relay_number}_VOLUME" local relay_volume="${!relay_volume_var}" local relay_ports_var="RELAY_${relay_number}_PORTS[@]" local relay_ports=("${!relay_ports_var}") print_header "DÉMARRAGE DE RELAY $relay_number" # Vérification du fichier de configuration check_file_exists "$relay_conf" "Configuration Relay $relay_number" # Vérification du fichier de configuration externe check_file_exists "$EXTERNAL_NODES_CONF" "Configuration des nœuds externes" print_step "Démarrage de $relay_name" local ports_mapping=$(build_port_mapping "${relay_ports[@]}") local env_vars=$(build_env_vars) local relay_container_id=$(docker run -d \ --name "$relay_name" \ --network "$NETWORK_NAME" \ --network-alias "$relay_name" \ $ports_mapping \ -v "$BITCOIN_VOLUME:/home/bitcoin/.bitcoin" \ -v "$BITCOIN_CONF:/home/bitcoin/.bitcoin/bitcoin.conf" \ -v "$relay_volume:/home/bitcoin/.4nk" \ -v "$relay_conf:/home/bitcoin/.conf.docker" \ -v "$PROJECT_DIR/sdk_relay/external_nodes.conf:/home/bitcoin/.4nk/external_nodes.conf" \ $env_vars \ "$RELAY_IMAGE" \ /bin/sh -c "cp /home/bitcoin/.conf.docker /home/bitcoin/.conf && cp /home/bitcoin/.bitcoin/signet/.cookie /home/bitcoin/.4nk/bitcoin.cookie && chmod 600 /home/bitcoin/.4nk/bitcoin.cookie && /usr/local/bin/sdk_relay --config .conf") print_success "$relay_name démarré: $relay_container_id" wait_for_container "$relay_name" 20 } start_all_relays() { print_header "DÉMARRAGE DE TOUS LES RELAYS" start_relay 1 start_relay 2 start_relay 3 } verify_final_status() { print_header "VÉRIFICATION FINALE" print_step "État de tous les services" docker ps print_step "Résumé des services actifs" echo -e "${GREEN}Services en cours d'exécution:${NC}" docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" print_step "Vérification des ports" local expected_services=("tor-proxy" "bitcoin-signet" "blindbit-oracle" "sdk_relay_1" "sdk_relay_2" "sdk_relay_3") local running_services=0 for service in "${expected_services[@]}"; do if docker ps --format "table {{.Names}}" | grep -q "^$service$"; then print_success "$service: ✅ En cours d'exécution" ((running_services++)) else print_error "$service: ❌ Non démarré" fi done print_info "Services actifs: $running_services/${#expected_services[@]}" if [ $running_services -eq ${#expected_services[@]} ]; then print_success "Tous les services sont opérationnels !" else print_warning "Certains services ne sont pas démarrés" exit 1 fi } show_usage() { echo -e "${BLUE}Usage: $0 [OPTIONS]${NC}" echo "" echo -e "${CYAN}Options:${NC}" echo -e " ${GREEN}-h, --help${NC} Afficher cette aide" echo -e " ${GREEN}-s, --stop${NC} Arrêter tous les services" echo -e " ${GREEN}-c, --clean${NC} Nettoyer les conteneurs" echo -e " ${GREEN}-n, --network${NC} Créer le réseau" echo -e " ${GREEN}-t, --tor${NC} Démarrer Tor" echo -e " ${GREEN}-b, --bitcoin${NC} Démarrer Bitcoin" echo -e " ${GREEN}-l, --blindbit${NC} Démarrer Blindbit" echo -e " ${GREEN}-r, --relays${NC} Démarrer les relais" echo -e " ${GREEN}-v, --verify${NC} Vérifier le statut" echo "" echo -e "${CYAN}Exemples:${NC}" echo -e " ${GREEN}$0${NC} Redémarrage complet" echo -e " ${GREEN}$0 -s${NC} Arrêter tous les services" echo -e " ${GREEN}$0 -r${NC} Démarrer uniquement les relais" echo "" } # ============================================================================= # FONCTION PRINCIPALE # ============================================================================= main() { print_header "SCRIPT DE REDÉMARRAGE COMPLET $PROJECT_NAME" print_info "Répertoire de travail: $PROJECT_DIR" print_info "Date: $(date)" # Traitement des arguments if [ $# -eq 0 ]; then # Redémarrage complet par défaut stop_all_services cleanup_containers create_network start_tor start_bitcoin start_blindbit build_relay_image start_all_relays verify_final_status else # Traitement des options while [[ $# -gt 0 ]]; do case $1 in -h|--help) show_usage exit 0 ;; -s|--stop) stop_all_services ;; -c|--clean) cleanup_containers ;; -n|--network) create_network ;; -t|--tor) start_tor ;; -b|--bitcoin) start_bitcoin ;; -l|--blindbit) start_blindbit ;; -r|--relays) build_relay_image start_all_relays ;; -v|--verify) verify_final_status ;; *) print_error "Option inconnue: $1" show_usage exit 1 ;; esac shift done fi print_header "REDÉMARRAGE TERMINÉ" print_success "L'infrastructure $PROJECT_NAME est maintenant opérationnelle !" print_info "Services actifs: $(docker ps --format "table {{.Names}}" | wc -l)" print_info "Ports exposés: $(docker ps --format "table {{.Ports}}" | grep -o '[0-9]*->[0-9]*' | wc -l)" } # ============================================================================= # EXÉCUTION # ============================================================================= # Vérification de Docker if ! command -v docker &> /dev/null; then print_error "Docker n'est pas installé ou n'est pas dans le PATH" exit 1 fi # Vérification que Docker daemon est en cours d'exécution if ! docker info &> /dev/null; then print_error "Docker daemon n'est pas en cours d'exécution" exit 1 fi # Exécution du script principal main "$@"