# Unity-ROS2 Docker Management Script # PowerShell script for managing ROS2 Docker containers param( [switch]$Build, [switch]$Monitor, [switch]$Stop, [switch]$Logs, [switch]$Status, [string]$Service = "ros2-unity" ) # Set error handling $ErrorActionPreference = "Stop" # Get script directory $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $ProjectRoot = Split-Path -Parent $ScriptDir $DockerDir = Join-Path $ProjectRoot "docker" Write-Host "=== Unity-ROS2 Docker Management Script ===" -ForegroundColor Green Write-Host "Project Root: $ProjectRoot" -ForegroundColor Cyan Write-Host "Docker Config Dir: $DockerDir" -ForegroundColor Cyan # Check if Docker is running function Test-DockerRunning { try { docker version | Out-Null return $true } catch { Write-Host "Error: Docker is not running or not installed" -ForegroundColor Red Write-Host "Please start Docker Desktop and try again" -ForegroundColor Yellow return $false } } # Check container status function Get-ContainerStatus { param([string]$ContainerName) try { $status = docker ps -a --filter "name=$ContainerName" --format "{{.Status}}" if ($status) { return $status } else { return "Not Found" } } catch { return "Unknown" } } # Show system status function Show-SystemStatus { Write-Host "`n=== System Status ===" -ForegroundColor Yellow # Docker status if (Test-DockerRunning) { Write-Host "Docker: Running" -ForegroundColor Green } else { Write-Host "Docker: Not Running" -ForegroundColor Red return } # Container status $mainStatus = Get-ContainerStatus "unity-ros2-system" $monitorStatus = Get-ContainerStatus "ros2-monitor" Write-Host "Main Container (unity-ros2-system): $mainStatus" -ForegroundColor Cyan Write-Host "Monitor Container (ros2-monitor): $monitorStatus" -ForegroundColor Cyan # Network connection test if ($mainStatus -like "*Up*") { Write-Host "`nTesting network connection..." -ForegroundColor Yellow try { $containerIP = docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' unity-ros2-system Write-Host "Container IP: $containerIP" -ForegroundColor Green # Test port $testResult = Test-NetConnection -ComputerName "localhost" -Port 10000 -WarningAction SilentlyContinue if ($testResult.TcpTestSucceeded) { Write-Host "Port 10000: Accessible" -ForegroundColor Green } else { Write-Host "Port 10000: Not Accessible" -ForegroundColor Red } } catch { Write-Host "Network test failed: $($_.Exception.Message)" -ForegroundColor Yellow } } } # Build images function Build-Images { Write-Host "`nBuilding Docker images..." -ForegroundColor Yellow Set-Location $DockerDir try { docker-compose build --no-cache Write-Host "Images built successfully" -ForegroundColor Green } catch { Write-Host "Image build failed: $($_.Exception.Message)" -ForegroundColor Red exit 1 } } # Start services function Start-Services { param([bool]$IncludeMonitor = $false) Write-Host "`nStarting ROS2 services..." -ForegroundColor Yellow Set-Location $DockerDir try { if ($IncludeMonitor) { docker-compose --profile monitoring up -d Write-Host "Main and monitoring services started" -ForegroundColor Green } else { docker-compose up -d ros2-unity Write-Host "Main service started" -ForegroundColor Green } # Wait for service initialization Write-Host "Waiting for service initialization..." -ForegroundColor Yellow Start-Sleep -Seconds 10 # Show connection info Write-Host "`nConnection Information:" -ForegroundColor Cyan Write-Host " Unity Connection Address: localhost" -ForegroundColor White Write-Host " TCP Port: 10000" -ForegroundColor White Write-Host " Backup Ports: 10001, 10002" -ForegroundColor White } catch { Write-Host "Service startup failed: $($_.Exception.Message)" -ForegroundColor Red exit 1 } } # Stop services function Stop-Services { Write-Host "`nStopping ROS2 services..." -ForegroundColor Yellow Set-Location $DockerDir try { docker-compose down Write-Host "Services stopped" -ForegroundColor Green } catch { Write-Host "Service stop failed: $($_.Exception.Message)" -ForegroundColor Red } } # Show logs function Show-Logs { param([string]$ServiceName = "ros2-unity") Write-Host "`nShowing service logs ($ServiceName)..." -ForegroundColor Yellow Set-Location $DockerDir try { docker-compose logs -f $ServiceName } catch { Write-Host "Cannot get logs: $($_.Exception.Message)" -ForegroundColor Red } } # Main logic if (-not (Test-DockerRunning)) { exit 1 } # Handle parameters switch ($true) { $Status { Show-SystemStatus break } $Build { Build-Images break } $Stop { Stop-Services break } $Logs { Show-Logs -ServiceName $Service break } $Monitor { Start-Services -IncludeMonitor $true Show-SystemStatus break } default { Start-Services -IncludeMonitor $false Show-SystemStatus Write-Host "`nNext Steps:" -ForegroundColor Green Write-Host " 1. Configure Unity connection parameters (localhost:10000)" -ForegroundColor White Write-Host " 2. Run Unity project for testing" -ForegroundColor White Write-Host " 3. Use -Logs parameter to view detailed logs" -ForegroundColor White Write-Host " 4. Use -Monitor parameter to start monitoring service" -ForegroundColor White Write-Host " 5. Use -Stop parameter to stop all services" -ForegroundColor White } } Write-Host "`n=== Script Execution Complete ===" -ForegroundColor Green