ihm_client/scripts/integrate-4nk-node.sh

349 lines
9.5 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
TARGET_DIR="${PROJECT_ROOT}/../4NK_node"
# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Fonction d'affichage
print_info() {
echo -e "${BLUE} $1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
# Fonction d'aide
show_help() {
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " --target-dir DIR Répertoire cible 4NK_node (défaut: ../4NK_node)"
echo " --dry-run Simulation sans modification"
echo " --backup Créer une sauvegarde avant intégration"
echo " --help Afficher cette aide"
echo ""
echo "Exemples:"
echo " $0 # Intégration standard"
echo " $0 --dry-run # Simulation"
echo " $0 --target-dir /path/to/4NK_node"
}
# Variables
DRY_RUN=false
BACKUP=false
TARGET_DIR="${PROJECT_ROOT}/../4NK_node"
# Parsing des arguments
while [[ $# -gt 0 ]]; do
case $1 in
--target-dir)
TARGET_DIR="$2"
shift 2
;;
--dry-run)
DRY_RUN=true
shift
;;
--backup)
BACKUP=true
shift
;;
--help)
show_help
exit 0
;;
*)
print_error "Option inconnue: $1"
show_help
exit 1
;;
esac
done
# Vérification des prérequis
check_prerequisites() {
print_info "Vérification des prérequis..."
# Vérifier que nous sommes dans le bon répertoire
if [[ ! -f "${PROJECT_ROOT}/package.json" ]]; then
print_error "Ce script doit être exécuté depuis le répertoire ihm_client"
exit 1
fi
# Vérifier que le répertoire cible existe
if [[ ! -d "$TARGET_DIR" ]]; then
print_error "Répertoire cible 4NK_node non trouvé: $TARGET_DIR"
exit 1
fi
# Vérifier que les fichiers d'intégration existent
local required_files=(
"Dockerfile.4nk-node"
"nginx.4nk-node.conf"
"start-4nk-node.sh"
"docker-compose.4nk-node.yml"
)
for file in "${required_files[@]}"; do
if [[ ! -f "${PROJECT_ROOT}/$file" ]]; then
print_error "Fichier requis manquant: $file"
exit 1
fi
done
print_success "Prérequis vérifiés"
}
# Création de la sauvegarde
create_backup() {
if [[ "$BACKUP" == "true" ]]; then
print_info "Création de la sauvegarde..."
local backup_dir="${TARGET_DIR}/backup_$(date +%Y%m%d_%H%M%S)"
if [[ "$DRY_RUN" == "false" ]]; then
mkdir -p "$backup_dir"
cp -r "${TARGET_DIR}/docker-compose.yml" "$backup_dir/" 2>/dev/null || true
cp -r "${TARGET_DIR}/ihm_client" "$backup_dir/" 2>/dev/null || true
print_success "Sauvegarde créée: $backup_dir"
else
print_info "DRY RUN: Sauvegarde serait créée: $backup_dir"
fi
fi
}
# Création du répertoire ihm_client dans 4NK_node
create_ihm_client_directory() {
print_info "Création du répertoire ihm_client dans 4NK_node..."
local ihm_client_dir="${TARGET_DIR}/ihm_client"
if [[ "$DRY_RUN" == "false" ]]; then
mkdir -p "$ihm_client_dir"
print_success "Répertoire créé: $ihm_client_dir"
else
print_info "DRY RUN: Répertoire serait créé: $ihm_client_dir"
fi
}
# Copie des fichiers d'intégration
copy_integration_files() {
print_info "Copie des fichiers d'intégration..."
local files_to_copy=(
"Dockerfile.4nk-node:Dockerfile"
"nginx.4nk-node.conf:nginx.conf"
"start-4nk-node.sh:start.sh"
"package.json:package.json"
"tsconfig.json:tsconfig.json"
"vite.config.ts:vite.config.ts"
"index.html:index.html"
)
for file_mapping in "${files_to_copy[@]}"; do
local source_file=$(echo "$file_mapping" | cut -d: -f1)
local target_file=$(echo "$file_mapping" | cut -d: -f2)
if [[ "$DRY_RUN" == "false" ]]; then
cp "${PROJECT_ROOT}/$source_file" "${TARGET_DIR}/ihm_client/$target_file"
print_success "Copié: $source_file -> ihm_client/$target_file"
else
print_info "DRY RUN: Copié: $source_file -> ihm_client/$target_file"
fi
done
}
# Copie du code source
copy_source_code() {
print_info "Copie du code source..."
local source_dirs=("src" "public")
for dir in "${source_dirs[@]}"; do
if [[ -d "${PROJECT_ROOT}/$dir" ]]; then
if [[ "$DRY_RUN" == "false" ]]; then
cp -r "${PROJECT_ROOT}/$dir" "${TARGET_DIR}/ihm_client/"
print_success "Copié: $dir/"
else
print_info "DRY RUN: Copié: $dir/"
fi
fi
done
}
# Mise à jour du docker-compose.yml
update_docker_compose() {
print_info "Mise à jour du docker-compose.yml..."
local docker_compose_file="${TARGET_DIR}/docker-compose.yml"
local ihm_client_service="
ihm_client:
build:
context: ./ihm_client
dockerfile: Dockerfile
container_name: 4nk-ihm-client
ports:
- \"8080:80\"
environment:
- SDK_RELAY_WS_URL=ws://sdk_relay_1:8090
- SDK_RELAY_HTTP_URL=http://sdk_relay_1:8091
- BITCOIN_RPC_URL=http://bitcoin:18443
- BLINDBIT_URL=http://blindbit:8000
volumes:
- ihm_client_logs:/var/log/nginx
networks:
- btcnet
depends_on:
- sdk_relay_1
- sdk_relay_2
- sdk_relay_3
restart: unless-stopped
healthcheck:
test: [\"CMD\", \"wget\", \"--quiet\", \"--tries=1\", \"--timeout=5\", \"--spider\", \"http://localhost\"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s"
if [[ "$DRY_RUN" == "false" ]]; then
# Vérifier si le service existe déjà
if grep -q "ihm_client:" "$docker_compose_file" 2>/dev/null; then
print_warning "Service ihm_client existe déjà dans docker-compose.yml"
else
# Ajouter le service avant la section volumes
sed -i "/^volumes:/i\\$ihm_client_service" "$docker_compose_file"
# Ajouter les volumes ihm_client
sed -i '/^volumes:/a\ ihm_client_logs:\n driver: local' "$docker_compose_file"
print_success "Service ihm_client ajouté au docker-compose.yml"
fi
else
print_info "DRY RUN: Service ihm_client serait ajouté au docker-compose.yml"
fi
}
# Création du script de démarrage
create_startup_script() {
print_info "Création du script de démarrage..."
local startup_script="${TARGET_DIR}/start-ihm-client.sh"
if [[ "$DRY_RUN" == "false" ]]; then
cat > "$startup_script" << 'EOF'
#!/bin/bash
set -e
echo "🚀 Démarrage de l'interface utilisateur 4NK..."
# Vérifier que nous sommes dans le bon répertoire
if [[ ! -f "docker-compose.yml" ]]; then
echo "❌ Ce script doit être exécuté depuis le répertoire 4NK_node"
exit 1
fi
# Démarrer uniquement le service ihm_client
echo "📦 Démarrage du service ihm_client..."
docker-compose up -d ihm_client
# Attendre que le service soit prêt
echo "⏳ Attente du démarrage..."
sleep 10
# Vérifier la santé du service
if docker-compose ps ihm_client | grep -q "Up"; then
echo "✅ Interface utilisateur démarrée avec succès"
echo " 📍 URL: http://localhost:8080"
echo " 🔍 Logs: docker logs 4nk-ihm-client"
else
echo "❌ Échec du démarrage de l'interface utilisateur"
docker-compose logs ihm_client
exit 1
fi
EOF
chmod +x "$startup_script"
print_success "Script de démarrage créé: $startup_script"
else
print_info "DRY RUN: Script de démarrage serait créé: $startup_script"
fi
}
# Validation de l'intégration
validate_integration() {
print_info "Validation de l'intégration..."
local validation_files=(
"${TARGET_DIR}/ihm_client/Dockerfile"
"${TARGET_DIR}/ihm_client/nginx.conf"
"${TARGET_DIR}/ihm_client/start.sh"
"${TARGET_DIR}/docker-compose.yml"
)
for file in "${validation_files[@]}"; do
if [[ -f "$file" ]]; then
print_success "Validé: $(basename "$file")"
else
print_error "Fichier manquant: $(basename "$file")"
return 1
fi
done
print_success "Validation terminée"
}
# Fonction principale
main() {
echo "🔧 Intégration de ihm_client dans 4NK_node"
echo " Source: $PROJECT_ROOT"
echo " Cible: $TARGET_DIR"
echo " Mode: $([ "$DRY_RUN" == "true" ] && echo "DRY RUN" || echo "EXÉCUTION")"
echo ""
check_prerequisites
create_backup
create_ihm_client_directory
copy_integration_files
copy_source_code
update_docker_compose
create_startup_script
if [[ "$DRY_RUN" == "false" ]]; then
validate_integration
echo ""
print_success "Intégration terminée avec succès !"
echo ""
echo "📋 Prochaines étapes:"
echo " 1. Aller dans le répertoire 4NK_node: cd $TARGET_DIR"
echo " 2. Démarrer l'interface: ./start-ihm-client.sh"
echo " 3. Ou redémarrer tout: ./restart_4nk_node.sh"
echo " 4. Accéder à l'interface: http://localhost:8080"
else
echo ""
print_success "Simulation terminée - Aucune modification effectuée"
fi
}
# Exécution
main "$@"