start.ps1 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # ==============================================================================
  2. # lf_mri_platform — Start services and GUI
  3. # Usage:
  4. # .\start.ps1 — start Docker services + GUI
  5. # .\start.ps1 -GuiOnly — start GUI only (no Docker)
  6. # .\start.ps1 -Mode real — start in real hardware mode
  7. # .\start.ps1 -ServicesOnly — start Docker services only
  8. # ==============================================================================
  9. param(
  10. [switch]$GuiOnly,
  11. [switch]$ServicesOnly,
  12. [ValidateSet("plug","real")]
  13. [string]$Mode = "plug",
  14. [string]$GuiDir = "..\lf_mri\MRI-testing\lf_mri_gui",
  15. [string]$RepoRoot = "..\lf_mri\MRI-testing"
  16. )
  17. $ErrorActionPreference = "Stop"
  18. function Write-Step($msg) { Write-Host "`n==> $msg" -ForegroundColor Cyan }
  19. function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green }
  20. function Write-Warn($msg) { Write-Host " [!!] $msg" -ForegroundColor Yellow }
  21. $venvPython = Join-Path $GuiDir ".venv\Scripts\python.exe"
  22. $appScript = Join-Path $GuiDir "app.py"
  23. # ── Start Docker services ─────────────────────────────────────────────────────
  24. if (-not $GuiOnly) {
  25. Write-Step "Starting Docker services (mode: $Mode)"
  26. # Check Docker is running
  27. try { & docker info *>$null }
  28. catch {
  29. Write-Warn "Docker is not running. Starting Docker Desktop..."
  30. Start-Process "C:\Program Files\Docker\Docker\Docker Desktop.exe" -ErrorAction SilentlyContinue
  31. Write-Host " Waiting 20 seconds for Docker to start..."
  32. Start-Sleep 20
  33. }
  34. # Load .env
  35. $envFile = Join-Path $PSScriptRoot ".env"
  36. if (-not (Test-Path $envFile)) {
  37. Copy-Item (Join-Path $PSScriptRoot ".env.example") $envFile
  38. Write-Warn ".env not found — created from .env.example"
  39. }
  40. # Set mode and launch
  41. $env:ORCHESTRATOR_MODE = $Mode
  42. & docker compose --env-file $envFile up --build -d
  43. Write-OK "Services started in '$Mode' mode"
  44. Write-Host ""
  45. Write-Host " Waiting for services to become healthy..." -ForegroundColor DarkGray
  46. # Wait for orchestrator health (up to 60s)
  47. $maxWait = 60
  48. $elapsed = 0
  49. $orchPort = (Get-Content $envFile | Select-String "ORCHESTRATOR_PORT=(\d+)").Matches[0].Groups[1].Value
  50. if (-not $orchPort) { $orchPort = "1717" }
  51. while ($elapsed -lt $maxWait) {
  52. try {
  53. $r = Invoke-WebRequest "http://localhost:$orchPort/health" -UseBasicParsing -TimeoutSec 2 -ErrorAction SilentlyContinue
  54. if ($r.StatusCode -eq 200) {
  55. Write-OK "Orchestrator is up (http://localhost:$orchPort)"
  56. break
  57. }
  58. } catch {}
  59. Start-Sleep 3
  60. $elapsed += 3
  61. Write-Host " ... $elapsed/$maxWait s" -ForegroundColor DarkGray
  62. }
  63. if ($elapsed -ge $maxWait) {
  64. Write-Warn "Orchestrator not responding after ${maxWait}s — GUI will use local fallback"
  65. }
  66. }
  67. # ── Start GUI ─────────────────────────────────────────────────────────────────
  68. if (-not $ServicesOnly) {
  69. Write-Step "Starting LF-MRI GUI"
  70. if (-not (Test-Path $venvPython)) {
  71. Write-Warn "Venv not found at $venvPython"
  72. Write-Warn "Run .\install.ps1 first, or using system python..."
  73. $venvPython = "python"
  74. }
  75. Write-OK "Launching GUI..."
  76. $absRepoRoot = Resolve-Path $RepoRoot
  77. Start-Process $venvPython -ArgumentList "`"$appScript`"" `
  78. -WorkingDirectory $absRepoRoot `
  79. -WindowStyle Normal
  80. }
  81. # ── Status summary ────────────────────────────────────────────────────────────
  82. Write-Host ""
  83. Write-Host " Service endpoints:" -ForegroundColor White
  84. Write-Host " Orchestrator http://localhost:1717/docs"
  85. Write-Host " Seq-Interp http://localhost:7475/docs"
  86. Write-Host " Spectrometer http://localhost:8000/admin/"
  87. Write-Host " Reconstructor http://localhost:8081/docs"
  88. Write-Host " Spectroscopy http://localhost:8002/docs"
  89. Write-Host ""
  90. Write-Host " Stop services: .\stop.ps1"