#!/bin/bash # Script per build e test locale dei container Docker # Utilizzo: ./build-docker.sh [linux|windows|all] set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}╔═══════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ Data-Coupler Docker Build Script ║${NC}" echo -e "${GREEN}╚═══════════════════════════════════════════╝${NC}" echo "" # Funzione per build Linux build_linux() { echo -e "${YELLOW}📦 Building Linux container...${NC}" docker build -t data-coupler:local -f Dockerfile . echo -e "${GREEN}✅ Linux container built successfully!${NC}" echo "" } # Funzione per build Windows build_windows() { echo -e "${YELLOW}📦 Building Windows container...${NC}" if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then docker build -t data-coupler:local-windows -f Dockerfile.windows . echo -e "${GREEN}✅ Windows container built successfully!${NC}" else echo -e "${RED}⚠️ Windows containers can only be built on Windows hosts${NC}" return 1 fi echo "" } # Funzione per test container test_container() { local IMAGE=$1 local CONTAINER_NAME=$2 echo -e "${YELLOW}🧪 Testing container: $IMAGE${NC}" # Stop e rimuovi container esistente docker stop $CONTAINER_NAME 2>/dev/null || true docker rm $CONTAINER_NAME 2>/dev/null || true # Avvia container echo "Starting container..." docker run -d \ --name $CONTAINER_NAME \ -p 7550:7550 \ $IMAGE # Attendi avvio (max 60 secondi) echo "Waiting for container to be ready..." for i in {1..60}; do if docker logs $CONTAINER_NAME 2>&1 | grep -q "Now listening on"; then echo -e "${GREEN}✅ Container started successfully!${NC}" break fi if [ $i -eq 60 ]; then echo -e "${RED}❌ Container failed to start within 60 seconds${NC}" docker logs $CONTAINER_NAME return 1 fi sleep 1 done # Test health endpoint echo "Testing health endpoint..." sleep 5 if curl -f http://localhost:7550/health &>/dev/null; then echo -e "${GREEN}✅ Health check passed!${NC}" else echo -e "${RED}❌ Health check failed${NC}" docker logs $CONTAINER_NAME return 1 fi # Mostra log echo "" echo -e "${YELLOW}📋 Container logs:${NC}" docker logs --tail 20 $CONTAINER_NAME echo "" echo -e "${GREEN}✅ Container test completed successfully!${NC}" echo -e "${YELLOW}💡 Container is running at: http://localhost:7550${NC}" echo -e "${YELLOW}💡 Stop with: docker stop $CONTAINER_NAME${NC}" echo "" } # Funzione per cleanup cleanup() { echo -e "${YELLOW}🧹 Cleaning up test containers...${NC}" docker stop data-coupler-test 2>/dev/null || true docker rm data-coupler-test 2>/dev/null || true docker stop data-coupler-test-windows 2>/dev/null || true docker rm data-coupler-test-windows 2>/dev/null || true echo -e "${GREEN}✅ Cleanup completed${NC}" } # Menu principale case "${1:-all}" in linux) build_linux read -p "Do you want to test the container? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then test_container "data-coupler:local" "data-coupler-test" fi ;; windows) build_windows read -p "Do you want to test the container? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then test_container "data-coupler:local-windows" "data-coupler-test-windows" fi ;; all) build_linux if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then build_windows fi read -p "Do you want to test the Linux container? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then test_container "data-coupler:local" "data-coupler-test" fi ;; clean) cleanup ;; *) echo "Usage: $0 {linux|windows|all|clean}" echo "" echo "Options:" echo " linux - Build only Linux container" echo " windows - Build only Windows container (requires Windows host)" echo " all - Build all containers (default)" echo " clean - Stop and remove test containers" exit 1 ;; esac echo "" echo -e "${GREEN}🎉 All done!${NC}"