168 lines
6.8 KiB
PowerShell
168 lines
6.8 KiB
PowerShell
# start.ps1 — Sobe o ambiente de desenvolvimento completo via Docker
|
|
# Uso: .\start.ps1
|
|
|
|
$ErrorActionPreference = "SilentlyContinue"
|
|
|
|
Write-Host ""
|
|
Write-Host "==> saas_imobiliaria — Ambiente de Desenvolvimento" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
# ── 1. Docker instalado? ─────────────────────────────────────────────────────
|
|
if (-not (Get-Command "docker" -ErrorAction SilentlyContinue)) {
|
|
Write-Host "[ERRO] Docker nao encontrado no PATH." -ForegroundColor Red
|
|
Write-Host " Instale o Docker Desktop em: https://www.docker.com/products/docker-desktop" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# ── 2. Docker rodando? ───────────────────────────────────────────────────────
|
|
docker info 2>&1 | Out-Null
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "[ERRO] Docker nao esta rodando." -ForegroundColor Red
|
|
Write-Host " Abra o Docker Desktop, aguarde inicializar e rode o script novamente." -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# ── 3. Garantir DNS no daemon.json (corrige lookup failures no BuildKit) ──────
|
|
$daemonPath = "$env:USERPROFILE\.docker\daemon.json"
|
|
$needRestart = $false
|
|
|
|
if (Test-Path $daemonPath) {
|
|
$raw = Get-Content $daemonPath -Raw -ErrorAction SilentlyContinue
|
|
$daemon = try { $raw | ConvertFrom-Json } catch { $null }
|
|
if ($null -eq $daemon -or -not $daemon.PSObject.Properties["dns"] -or $daemon.dns.Count -eq 0) {
|
|
if ($null -eq $daemon) { $daemon = [PSCustomObject]@{} }
|
|
$daemon | Add-Member -Force -MemberType NoteProperty -Name "dns" -Value @("8.8.8.8", "8.8.4.4")
|
|
$daemon | ConvertTo-Json -Depth 10 | Set-Content $daemonPath -Encoding UTF8
|
|
$needRestart = $true
|
|
}
|
|
}
|
|
else {
|
|
New-Item -ItemType Directory -Path (Split-Path $daemonPath) -Force | Out-Null
|
|
'{"dns":["8.8.8.8","8.8.4.4"]}' | Set-Content $daemonPath -Encoding UTF8
|
|
$needRestart = $true
|
|
}
|
|
|
|
if ($needRestart) {
|
|
Write-Host "[CORRECAO APLICADA] DNS 8.8.8.8 adicionado ao Docker daemon ($daemonPath)." -ForegroundColor Yellow
|
|
Write-Host " Reinicie o Docker Desktop e rode .\start.ps1 novamente." -ForegroundColor White
|
|
exit 0
|
|
}
|
|
|
|
# ── 4. Criar .env se nao existir ─────────────────────────────────────────────
|
|
if (-not (Test-Path ".env")) {
|
|
Copy-Item ".env.example" ".env"
|
|
Write-Host "[INFO] .env criado com valores padrao de desenvolvimento." -ForegroundColor Gray
|
|
}
|
|
|
|
# ── 5. Funções auxiliares de porta ───────────────────────────────────────────
|
|
function Get-FreePort {
|
|
param([int]$Preferred)
|
|
$usedPorts = (Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue).LocalPort
|
|
if ($usedPorts -notcontains $Preferred) { return $Preferred }
|
|
$port = $Preferred + 1
|
|
while ($usedPorts -contains $port) { $port++ }
|
|
return $port
|
|
}
|
|
|
|
function Get-ContainerPort {
|
|
param([string]$Service, [int]$ContainerPort)
|
|
$out = docker-compose port $Service $ContainerPort 2>$null
|
|
if ($out -match ":(\d+)$") { return [int]$Matches[1] }
|
|
return $null
|
|
}
|
|
|
|
function Test-ServiceRunning {
|
|
param([string]$Service)
|
|
$id = (docker-compose ps -q $Service 2>$null) | Where-Object { $_ -ne "" } | Select-Object -First 1
|
|
if (-not $id) { return $false }
|
|
$state = docker inspect --format "{{.State.Running}}" $id 2>$null
|
|
return ($state -eq "true")
|
|
}
|
|
|
|
# ── 6. Verificar quais servicos ja estao rodando ──────────────────────────────
|
|
$runningServices = @()
|
|
foreach ($svc in @("db", "backend", "frontend")) {
|
|
if (Test-ServiceRunning $svc) { $runningServices += $svc }
|
|
}
|
|
|
|
# Para serviços já rodando: pegar a porta real do container
|
|
# Para serviços que vão subir: encontrar uma porta livre no host
|
|
if ($runningServices -contains "db") {
|
|
$portDb = Get-ContainerPort "db" 5432
|
|
if (-not $portDb) { $portDb = Get-FreePort 5432 }
|
|
} else {
|
|
$portDb = Get-FreePort 5432
|
|
}
|
|
|
|
if ($runningServices -contains "backend") {
|
|
$portBackend = Get-ContainerPort "backend" 5000
|
|
if (-not $portBackend) { $portBackend = Get-FreePort 5000 }
|
|
} else {
|
|
$portBackend = Get-FreePort 5000
|
|
}
|
|
|
|
if ($runningServices -contains "frontend") {
|
|
$portFrontend = Get-ContainerPort "frontend" 5173
|
|
if (-not $portFrontend) { $portFrontend = Get-FreePort 5173 }
|
|
} else {
|
|
$portFrontend = Get-FreePort 5173
|
|
}
|
|
|
|
if ($portDb -ne 5432) { Write-Host "[PORTA] Banco : $portDb (5432 ocupada)" -ForegroundColor Yellow }
|
|
if ($portBackend -ne 5000) { Write-Host "[PORTA] Backend : $portBackend (5000 ocupada)" -ForegroundColor Yellow }
|
|
if ($portFrontend -ne 5173) { Write-Host "[PORTA] Frontend : $portFrontend (5173 ocupada)" -ForegroundColor Yellow }
|
|
|
|
$toStart = @()
|
|
foreach ($svc in @("db", "backend", "frontend")) {
|
|
if ($runningServices -notcontains $svc) {
|
|
$toStart += $svc
|
|
}
|
|
else {
|
|
Write-Host "[OK] $svc ja esta rodando." -ForegroundColor DarkGray
|
|
}
|
|
}
|
|
|
|
# ── 7. Subir apenas os servicos necessarios ───────────────────────────────────
|
|
if ($toStart.Count -eq 0) {
|
|
Write-Host ""
|
|
Write-Host "[OK] Todos os servicos ja estao online." -ForegroundColor Green
|
|
}
|
|
else {
|
|
$env:PORT_DB = $portDb
|
|
$env:PORT_BACKEND = $portBackend
|
|
$env:PORT_FRONTEND = $portFrontend
|
|
|
|
$composeArgs = @("up", "--build", "-d") + $toStart
|
|
Write-Host ""
|
|
Write-Host "==> Subindo: $($toStart -join ', ')..." -ForegroundColor Green
|
|
|
|
try {
|
|
& docker-compose $composeArgs
|
|
if ($LASTEXITCODE -ne 0) { throw "docker-compose retornou $LASTEXITCODE" }
|
|
} catch {
|
|
Write-Host "[ERRO] Falha ao subir servicos: $_" -ForegroundColor Red
|
|
}
|
|
}
|
|
|
|
# ── 8. Aguardar frontend ficar pronto ─────────────────────────────────────────
|
|
$frontendUrl = "http://localhost:$portFrontend"
|
|
$maxWait = 60
|
|
$waited = 0
|
|
Write-Host ""
|
|
Write-Host " Aguardando frontend..." -ForegroundColor Gray
|
|
while ($waited -lt $maxWait) {
|
|
try {
|
|
$r = Invoke-WebRequest -Uri $frontendUrl -TimeoutSec 2 -UseBasicParsing -ErrorAction Stop
|
|
if ($r.StatusCode -eq 200) { break }
|
|
}
|
|
catch {}
|
|
Start-Sleep -Seconds 2
|
|
$waited += 2
|
|
}
|
|
|
|
# ── 9. Links finais ──────────────────────────────────────────────────────────
|
|
Write-Host ""
|
|
Write-Host " Frontend : $frontendUrl" -ForegroundColor Cyan
|
|
Write-Host " Backend : http://localhost:$portBackend" -ForegroundColor DarkCyan
|
|
Write-Host ""
|
|
|