start.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # ==============================================================================
  2. # lf_mri_platform -- Start services and GUI
  3. # Usage:
  4. # .\start.ps1 -- plug mode: Docker + GUI
  5. # .\start.ps1 -Mode real -- real mode: Docker + spectrometer + GUI
  6. # .\start.ps1 -GuiOnly -- GUI only (services already running)
  7. # .\start.ps1 -ServicesOnly -- Docker + spectrometer, no GUI
  8. # .\start.ps1 -SkipInstall -- skip venv check
  9. # .\start.ps1 -SkipSpectrometer -- skip native spectrometer even in real mode
  10. # ==============================================================================
  11. param(
  12. [ValidateSet("plug", "real")]
  13. [string]$Mode = "plug",
  14. [switch]$GuiOnly,
  15. [switch]$ServicesOnly,
  16. [switch]$SkipInstall,
  17. [switch]$SkipSpectrometer
  18. )
  19. $ErrorActionPreference = "Stop"
  20. $Root = $PSScriptRoot
  21. $GuiDir = Join-Path $Root "apps\gui"
  22. $VenvPython = Join-Path $GuiDir ".venv\Scripts\python.exe"
  23. $AppScript = Join-Path $Root "apps\gui\app.py"
  24. $EnvFile = Join-Path $Root ".env"
  25. $EnvExample = Join-Path $Root ".env.example"
  26. function Write-Step($msg) { Write-Host "`n==> $msg" -ForegroundColor Cyan }
  27. function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green }
  28. function Write-Warn($msg) { Write-Host " [!!] $msg" -ForegroundColor Yellow }
  29. function Write-Fail($msg) { Write-Host "`n[FAIL] $msg" -ForegroundColor Red; exit 1 }
  30. function Get-EnvPort($key, $default) {
  31. if (Test-Path $EnvFile) {
  32. $line = Get-Content $EnvFile | Select-String "^$key=(\d+)"
  33. if ($line) { return $line.Matches[0].Groups[1].Value }
  34. }
  35. return $default
  36. }
  37. # -- 1. Check / install GUI venv -----------------------------------------------
  38. if (-not $GuiOnly -and -not $SkipInstall) {
  39. Write-Step "Checking GUI environment"
  40. $needsInstall = $false
  41. if (-not (Test-Path $VenvPython)) {
  42. Write-Warn "Virtual environment not found -- running install"
  43. $needsInstall = $true
  44. } else {
  45. & $VenvPython -c "import PySide6" 2>$null
  46. if ($LASTEXITCODE -ne 0) {
  47. Write-Warn "GUI packages missing -- running install"
  48. $needsInstall = $true
  49. } else {
  50. Write-OK "Virtual environment ready"
  51. }
  52. }
  53. if ($needsInstall) {
  54. $installScript = Join-Path $Root "install.ps1"
  55. if (-not (Test-Path $installScript)) { Write-Fail "install.ps1 not found" }
  56. & powershell.exe -ExecutionPolicy Bypass -File $installScript
  57. if ($LASTEXITCODE -ne 0) { Write-Fail "install.ps1 failed" }
  58. }
  59. }
  60. # -- 2. Docker -----------------------------------------------------------------
  61. if (-not $GuiOnly) {
  62. Write-Step "Checking Docker"
  63. $dockerOk = $false
  64. try { & docker info *>$null; $dockerOk = $true } catch {}
  65. if (-not $dockerOk) {
  66. Write-Warn "Docker not responding -- starting Docker Desktop..."
  67. $dockerExe = "C:\Program Files\Docker\Docker\Docker Desktop.exe"
  68. if (Test-Path $dockerExe) { Start-Process $dockerExe }
  69. else { Write-Fail "Docker Desktop not found -- install from https://docker.com" }
  70. $waited = 0
  71. while ($waited -lt 60) {
  72. Start-Sleep 5; $waited += 5
  73. try { & docker info *>$null; $dockerOk = $true; break } catch {}
  74. Write-Host " ... $waited/60 s" -ForegroundColor DarkGray
  75. }
  76. if (-not $dockerOk) { Write-Fail "Docker did not start in time" }
  77. }
  78. Write-OK "Docker is running"
  79. # -- 3. .env ---------------------------------------------------------------
  80. if (-not (Test-Path $EnvFile)) {
  81. if (Test-Path $EnvExample) { Copy-Item $EnvExample $EnvFile; Write-Warn ".env created from .env.example" }
  82. else { Write-Warn ".env missing -- using Compose defaults" }
  83. }
  84. # -- 4. Start containers ---------------------------------------------------
  85. Write-Step "Starting Docker services (mode: $Mode)"
  86. $env:ORCHESTRATOR_MODE = $Mode
  87. $envArgs = if (Test-Path $EnvFile) { @("--env-file", $EnvFile) } else { @() }
  88. & docker compose @envArgs up -d
  89. if ($LASTEXITCODE -ne 0) { Write-Fail "docker compose up failed" }
  90. Write-OK "Containers started"
  91. # -- 5. Native spectrometer (real mode only) --------------------------------
  92. if ($Mode -eq "real" -and -not $SkipSpectrometer) {
  93. Write-Step "Starting native spectrometer"
  94. $SpecDir = Join-Path $Root "services\spectrometer"
  95. $SpecVenv = Join-Path $SpecDir "mvenv\Scripts\python.exe"
  96. $PicoExe = Join-Path $SpecDir "bin\pico-tcp.exe"
  97. # Kill stale pico-tcp
  98. $oldPico = Get-Process -Name "pico-tcp" -ErrorAction SilentlyContinue
  99. if ($oldPico) { $oldPico | Stop-Process -Force; Write-Warn "Killed stale pico-tcp.exe" }
  100. # Create venv on first run
  101. if (-not (Test-Path $SpecVenv)) {
  102. Write-Warn "Spectrometer venv not found -- creating..."
  103. Push-Location $SpecDir
  104. & python -m venv mvenv
  105. if ($LASTEXITCODE -ne 0) { Write-Fail "Failed to create spectrometer venv" }
  106. & $SpecVenv -m pip install -q --upgrade pip
  107. & $SpecVenv -m pip install -q -r requirements.txt
  108. if ($LASTEXITCODE -ne 0) { Write-Fail "Failed to install spectrometer packages" }
  109. Pop-Location
  110. Write-OK "Spectrometer venv created"
  111. } else {
  112. Write-OK "Spectrometer venv found"
  113. }
  114. # Migrations
  115. Push-Location $SpecDir
  116. & $SpecVenv manage.py migrate --noinput 2>&1 | Out-Null
  117. Pop-Location
  118. Write-OK "Migrations applied"
  119. # pico-tcp.exe
  120. if (Test-Path $PicoExe) { Start-Process $PicoExe -WindowStyle Hidden; Write-OK "pico-tcp.exe started" }
  121. else { Write-Warn "bin\pico-tcp.exe not found -- ADC proxy not started" }
  122. # Django runserver
  123. $running = Get-Process -Name python* -ErrorAction SilentlyContinue |
  124. Where-Object { $_.CommandLine -like "*manage.py*runserver*" }
  125. if ($running) {
  126. Write-OK "Spectrometer already running (PID $($running.Id))"
  127. } else {
  128. Start-Process $SpecVenv -ArgumentList "manage.py runserver 0.0.0.0:8000" `
  129. -WorkingDirectory $SpecDir -WindowStyle Normal
  130. Write-OK "Spectrometer started in new window"
  131. }
  132. }
  133. # -- 6. Health check -------------------------------------------------------
  134. Write-Step "Waiting for services to become healthy"
  135. $checkSpec = ($Mode -eq "real") -and (-not $SkipSpectrometer)
  136. $services = @(
  137. @{ Name = "Orchestrator"; Url = "http://localhost:$(Get-EnvPort 'ORCHESTRATOR_PORT' '1717')/health"; Required = $true },
  138. @{ Name = "Seq-Interp"; Url = "http://localhost:$(Get-EnvPort 'SEQ_INTERP_PORT' '7475')/health"; Required = $true },
  139. @{ Name = "Reconstructor"; Url = "http://localhost:$(Get-EnvPort 'RECONSTRUCTOR_PORT' '8081')/health"; Required = $true },
  140. @{ Name = "Spectroscopy"; Url = "http://localhost:$(Get-EnvPort 'SPECTROSCOPY_PORT' '8002')/health"; Required = $true },
  141. @{ Name = "Spectrometer*"; Url = "http://localhost:$(Get-EnvPort 'SPECTROMETER_PORT' '8000')/api/"; Required = $checkSpec }
  142. )
  143. $maxWait = 120; $interval = 3; $elapsed = 0
  144. while ($elapsed -lt $maxWait) {
  145. $pending = $services | Where-Object { $_.Required } | Where-Object {
  146. try { (Invoke-WebRequest $_.Url -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop).StatusCode -ge 400 }
  147. catch { $true }
  148. }
  149. if (-not $pending) { Write-OK "All required services healthy"; break }
  150. Write-Host (" ... {0}/{1} s waiting: {2}" -f $elapsed, $maxWait, ($pending.Name -join ", ")) -ForegroundColor DarkGray
  151. Start-Sleep $interval; $elapsed += $interval
  152. }
  153. if ($elapsed -ge $maxWait) { Write-Warn "Some services did not respond in time -- continuing anyway" }
  154. Write-Host ""
  155. foreach ($svc in $services) {
  156. $ok = try { (Invoke-WebRequest $svc.Url -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop).StatusCode -lt 400 } catch { $false }
  157. $icon = if ($ok) { "[OK]" } else { "[--]" }
  158. $color = if ($ok) { "Green" } elseif ($svc.Required) { "Yellow" } else { "DarkGray" }
  159. $suffix = if (-not $svc.Required -and -not $ok) { " (native — start manually if needed)" } else { "" }
  160. Write-Host (" {0,-6} {1,-16} {2}{3}" -f $icon, $svc.Name, $svc.Url, $suffix) -ForegroundColor $color
  161. }
  162. # -- 7. Write server_config.json -------------------------------------------
  163. Write-Step "Configuring GUI"
  164. $orchPort = Get-EnvPort "ORCHESTRATOR_PORT" "1717"
  165. $seqPort = Get-EnvPort "SEQ_INTERP_PORT" "7475"
  166. $spectPort = Get-EnvPort "SPECTROSCOPY_PORT" "8002"
  167. $cfgPath = Join-Path $Root "apps\gui\cfg\server_config.json"
  168. @{
  169. srv_name = "srv_interp"
  170. log_dir = "log"
  171. upload_dir = "data/input"
  172. output_dir = "data/output"
  173. server_host = "0.0.0.0"
  174. server_port = [int]$seqPort
  175. orchestrator_url = "http://localhost:$orchPort"
  176. seq_interp_url = "http://localhost:$seqPort"
  177. spectroscopy_url = "http://localhost:$spectPort"
  178. mode = $Mode
  179. } | ConvertTo-Json -Depth 3 | Set-Content $cfgPath -Encoding utf8
  180. Write-OK "server_config.json updated (mode=$Mode, ports: orch=$orchPort seq=$seqPort spectro=$spectPort)"
  181. }
  182. # -- 8. Launch GUI -------------------------------------------------------------
  183. if (-not $ServicesOnly) {
  184. Write-Step "Launching GUI"
  185. if (-not (Test-Path $VenvPython)) {
  186. Write-Warn "Venv not found -- falling back to system Python"
  187. $VenvPython = "python"
  188. }
  189. if (-not (Test-Path $AppScript)) { Write-Fail "GUI entry point not found: $AppScript" }
  190. Start-Process $VenvPython -ArgumentList "`"$AppScript`"" -WorkingDirectory $Root -WindowStyle Normal
  191. Write-OK "GUI launched"
  192. }
  193. # -- Summary -------------------------------------------------------------------
  194. Write-Host ""
  195. Write-Host "============================================================" -ForegroundColor Green
  196. Write-Host (" LF-MRI platform is running [{0} mode]" -f $Mode.ToUpper()) -ForegroundColor Green
  197. Write-Host "============================================================" -ForegroundColor Green
  198. Write-Host ""
  199. if ($Mode -eq "real") {
  200. Write-Host " Stop spectrometer: close the spectrometer terminal window"
  201. Write-Host " Stop pico-tcp: services\spectrometer\autokill.bat"
  202. }
  203. Write-Host " Stop all: .\stop.ps1"
  204. Write-Host " Update code: .\update.bat"
  205. Write-Host ""
  206. Write-Host " Press Enter to close this window..." -ForegroundColor DarkGray
  207. $null = Read-Host