|
|
|
@@ -0,0 +1,248 @@
|
|
|
|
|
# =============================================================
|
|
|
|
|
# Docker & PostgreSQL Health Check for Quarkus Dev Environment
|
|
|
|
|
# Dành cho sinh viên học Docker/Java
|
|
|
|
|
# =============================================================
|
|
|
|
|
|
|
|
|
|
$ErrorActionPreference = "SilentlyContinue"
|
|
|
|
|
|
|
|
|
|
# --- Color helpers ---
|
|
|
|
|
function Pass($msg) { Write-Host " [PASS] $msg" -ForegroundColor Green }
|
|
|
|
|
function Fail($msg) { Write-Host " [FAIL] $msg" -ForegroundColor Red }
|
|
|
|
|
function Info($msg) { Write-Host " [INFO] $msg" -ForegroundColor Cyan }
|
|
|
|
|
function Warn($msg) { Write-Host " [WARN] $msg" -ForegroundColor Yellow }
|
|
|
|
|
function Title($msg) { Write-Host "`n===== $msg =====" -ForegroundColor Magenta }
|
|
|
|
|
function Cmd($msg) { Write-Host " >> $msg" -ForegroundColor DarkGray }
|
|
|
|
|
|
|
|
|
|
$results = @{} # track pass/fail for summary
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# 1. Docker Desktop process check
|
|
|
|
|
# =============================================================
|
|
|
|
|
Title "1. Docker Desktop Process"
|
|
|
|
|
|
|
|
|
|
Cmd "Get-Process 'Docker Desktop' ..."
|
|
|
|
|
$dockerDesktop = Get-Process -Name "Docker Desktop" -ErrorAction SilentlyContinue
|
|
|
|
|
|
|
|
|
|
if ($dockerDesktop) {
|
|
|
|
|
Pass "Docker Desktop dang chay (PID: $($dockerDesktop.Id))"
|
|
|
|
|
$results["Docker Desktop"] = $true
|
|
|
|
|
} else {
|
|
|
|
|
Fail "Docker Desktop KHONG chay"
|
|
|
|
|
Info "Hay mo Docker Desktop tu Start Menu va doi 1-2 phut"
|
|
|
|
|
$results["Docker Desktop"] = $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# 2. Docker Windows service
|
|
|
|
|
# =============================================================
|
|
|
|
|
Title "2. Docker Service Status"
|
|
|
|
|
|
|
|
|
|
Cmd "Get-Service 'com.docker.service' ..."
|
|
|
|
|
$svc = Get-Service -Name "com.docker.service" -ErrorAction SilentlyContinue
|
|
|
|
|
|
|
|
|
|
if ($svc) {
|
|
|
|
|
if ($svc.Status -eq "Running") {
|
|
|
|
|
Pass "Docker service dang chay (Status: $($svc.Status))"
|
|
|
|
|
$results["Docker Service"] = $true
|
|
|
|
|
} else {
|
|
|
|
|
Fail "Docker service ton tai nhung KHONG chay (Status: $($svc.Status))"
|
|
|
|
|
Info "Thu chay: Start-Service 'com.docker.service'"
|
|
|
|
|
$results["Docker Service"] = $false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Warn "Khong tim thay docker service - co the la Docker Desktop dang dung standalone"
|
|
|
|
|
$results["Docker Service"] = $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# 3. DOCKER_HOST environment variable
|
|
|
|
|
# =============================================================
|
|
|
|
|
Title "3. DOCKER_HOST / Docker Socket"
|
|
|
|
|
|
|
|
|
|
Cmd "`$env:DOCKER_HOST"
|
|
|
|
|
$dockerHost = $env:DOCKER_HOST
|
|
|
|
|
|
|
|
|
|
if ($dockerHost) {
|
|
|
|
|
Info "DOCKER_HOST = $dockerHost"
|
|
|
|
|
$results["Docker Host"] = $true
|
|
|
|
|
} else {
|
|
|
|
|
Info "DOCKER_HOST chua duoc dat (dung default: npipe:////./pipe/docker_engine)"
|
|
|
|
|
$results["Docker Host"] = $true # default is fine on Windows
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Kiem tra named pipe
|
|
|
|
|
Cmd "Test-Path '//./pipe/docker_engine' ..."
|
|
|
|
|
$pipe = Test-Path "//./pipe/docker_engine"
|
|
|
|
|
if ($pipe) {
|
|
|
|
|
Pass "Docker named pipe ton tai - daemon co the ket noi"
|
|
|
|
|
} else {
|
|
|
|
|
Fail "Docker named pipe KHONG ton tai - daemon chua khoi dong"
|
|
|
|
|
Info "Hay doi Docker Desktop khoi dong xong roi thu lai"
|
|
|
|
|
$results["Docker Host"] = $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# 4. Docker daemon connectivity (docker ps)
|
|
|
|
|
# =============================================================
|
|
|
|
|
Title "4. Docker Daemon Connectivity"
|
|
|
|
|
|
|
|
|
|
Cmd "docker ps ..."
|
|
|
|
|
$dockerPs = docker ps 2>&1
|
|
|
|
|
$dockerExitCode = $LASTEXITCODE
|
|
|
|
|
|
|
|
|
|
if ($dockerExitCode -eq 0) {
|
|
|
|
|
Pass "Ket noi Docker daemon thanh cong"
|
|
|
|
|
$results["Docker Daemon"] = $true
|
|
|
|
|
} else {
|
|
|
|
|
Fail "Khong ket noi duoc Docker daemon"
|
|
|
|
|
Info "Loi: $dockerPs"
|
|
|
|
|
Info "Giai phap: Khoi dong lai Docker Desktop, doi ~30 giay, thu lai"
|
|
|
|
|
$results["Docker Daemon"] = $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# 5. List all running containers
|
|
|
|
|
# =============================================================
|
|
|
|
|
Title "5. Running Containers"
|
|
|
|
|
|
|
|
|
|
if ($results["Docker Daemon"]) {
|
|
|
|
|
Cmd "docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}' ..."
|
|
|
|
|
$containers = docker ps --format "table {{.Names}}`t{{.Image}}`t{{.Status}}`t{{.Ports}}" 2>&1
|
|
|
|
|
|
|
|
|
|
if ($containers -match "NAMES") {
|
|
|
|
|
$lines = ($containers -split "`n") | Where-Object { $_ -ne "" }
|
|
|
|
|
$containerCount = $lines.Count - 1 # tru dong header
|
|
|
|
|
|
|
|
|
|
if ($containerCount -gt 0) {
|
|
|
|
|
Pass "$containerCount container dang chay:"
|
|
|
|
|
$containers | ForEach-Object { Write-Host " $_" -ForegroundColor White }
|
|
|
|
|
$results["Running Containers"] = $true
|
|
|
|
|
} else {
|
|
|
|
|
Warn "Khong co container nao dang chay"
|
|
|
|
|
$results["Running Containers"] = $false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Warn "Bo qua - Docker daemon chua ket noi duoc"
|
|
|
|
|
$results["Running Containers"] = $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# 6. PostgreSQL container check
|
|
|
|
|
# =============================================================
|
|
|
|
|
Title "6. PostgreSQL Container"
|
|
|
|
|
|
|
|
|
|
if ($results["Docker Daemon"]) {
|
|
|
|
|
# Tim container co ten chua "postgres" hoac "pg"
|
|
|
|
|
Cmd "docker ps -a --filter name=postgres ..."
|
|
|
|
|
$pgContainers = docker ps -a --format "{{.Names}}`t{{.Image}}`t{{.Status}}" 2>&1 |
|
|
|
|
|
Where-Object { $_ -match "postgres|pg" }
|
|
|
|
|
|
|
|
|
|
if ($pgContainers) {
|
|
|
|
|
$pgContainers | ForEach-Object {
|
|
|
|
|
$parts = $_ -split "`t"
|
|
|
|
|
$name = $parts[0]
|
|
|
|
|
$image = $parts[1]
|
|
|
|
|
$status = $parts[2]
|
|
|
|
|
|
|
|
|
|
if ($status -match "^Up") {
|
|
|
|
|
Pass "PostgreSQL container '$name' ($image) dang chay"
|
|
|
|
|
Pass "Status: $status"
|
|
|
|
|
$results["PostgreSQL Container"] = $true
|
|
|
|
|
$script:pgContainerName = $name
|
|
|
|
|
} else {
|
|
|
|
|
Fail "PostgreSQL container '$name' TON TAI nhung DUNG"
|
|
|
|
|
Info "Status: $status"
|
|
|
|
|
Info "Khoi dong lai: docker start $name"
|
|
|
|
|
$results["PostgreSQL Container"] = $false
|
|
|
|
|
$script:pgContainerName = $name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Fail "Khong tim thay PostgreSQL container nao"
|
|
|
|
|
Info "Tao container moi:"
|
|
|
|
|
Info " docker run -d --name postgres-dev -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:16"
|
|
|
|
|
$results["PostgreSQL Container"] = $false
|
|
|
|
|
$script:pgContainerName = $null
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Warn "Bo qua - Docker daemon chua ket noi duoc"
|
|
|
|
|
$results["PostgreSQL Container"] = $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# 7. PostgreSQL connectivity test
|
|
|
|
|
# =============================================================
|
|
|
|
|
Title "7. PostgreSQL Connectivity"
|
|
|
|
|
|
|
|
|
|
if ($results["PostgreSQL Container"] -eq $true -and $script:pgContainerName) {
|
|
|
|
|
Cmd "docker exec $($script:pgContainerName) pg_isready ..."
|
|
|
|
|
$pgReady = docker exec $script:pgContainerName pg_isready 2>&1
|
|
|
|
|
$pgExitCode = $LASTEXITCODE
|
|
|
|
|
|
|
|
|
|
if ($pgExitCode -eq 0) {
|
|
|
|
|
Pass "PostgreSQL san sang nhan ket noi: $pgReady"
|
|
|
|
|
$results["PostgreSQL Connectivity"] = $true
|
|
|
|
|
|
|
|
|
|
# Thu ket noi bang psql
|
|
|
|
|
Cmd "docker exec $($script:pgContainerName) psql -U postgres -c '\l' ..."
|
|
|
|
|
$pgList = docker exec $script:pgContainerName psql -U postgres -c "\l" 2>&1
|
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
|
|
|
Pass "Ket noi psql thanh cong, danh sach database:"
|
|
|
|
|
$pgList | ForEach-Object { Write-Host " $_" -ForegroundColor White }
|
|
|
|
|
} else {
|
|
|
|
|
Warn "pg_isready OK nhung psql that bai (co the user khac): $pgList"
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Fail "PostgreSQL CHUA san sang: $pgReady"
|
|
|
|
|
Info "Doi vai giay roi thu lai"
|
|
|
|
|
$results["PostgreSQL Connectivity"] = $false
|
|
|
|
|
}
|
|
|
|
|
} elseif ($results["PostgreSQL Container"] -eq $false) {
|
|
|
|
|
Warn "Bo qua - PostgreSQL container chua chay"
|
|
|
|
|
$results["PostgreSQL Connectivity"] = $null
|
|
|
|
|
} else {
|
|
|
|
|
Warn "Bo qua - Docker daemon chua ket noi duoc"
|
|
|
|
|
$results["PostgreSQL Connectivity"] = $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# =============================================================
|
|
|
|
|
# SUMMARY REPORT
|
|
|
|
|
# =============================================================
|
|
|
|
|
Write-Host "`n" + ("=" * 55) -ForegroundColor Magenta
|
|
|
|
|
Write-Host " SUMMARY REPORT - Docker & PostgreSQL Health Check" -ForegroundColor Magenta
|
|
|
|
|
Write-Host ("=" * 55) -ForegroundColor Magenta
|
|
|
|
|
|
|
|
|
|
$allPass = $true
|
|
|
|
|
foreach ($check in $results.Keys) {
|
|
|
|
|
$val = $results[$check]
|
|
|
|
|
if ($val -eq $true) {
|
|
|
|
|
Write-Host (" [OK] {0,-35}" -f $check) -ForegroundColor Green
|
|
|
|
|
} elseif ($val -eq $false) {
|
|
|
|
|
Write-Host (" [!!] {0,-35} << Can fix" -f $check) -ForegroundColor Red
|
|
|
|
|
$allPass = $false
|
|
|
|
|
} else {
|
|
|
|
|
Write-Host (" [--] {0,-35} (skipped)" -f $check) -ForegroundColor Yellow
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Write-Host ""
|
|
|
|
|
if ($allPass) {
|
|
|
|
|
Write-Host " Tat ca kiem tra PASS! Moi truong san sang." -ForegroundColor Green
|
|
|
|
|
Write-Host " Chay Quarkus: cd account-service && ./mvnw quarkus:dev" -ForegroundColor Cyan
|
|
|
|
|
} else {
|
|
|
|
|
Write-Host " CO MOT SO KIEM TRA THAT BAI. Xem huong dan o tren." -ForegroundColor Red
|
|
|
|
|
Write-Host ""
|
|
|
|
|
Write-Host " Giai phap nhanh:" -ForegroundColor Yellow
|
|
|
|
|
if (-not $results["Docker Desktop"]) {
|
|
|
|
|
Write-Host " 1. Mo Docker Desktop tu Start Menu" -ForegroundColor Yellow
|
|
|
|
|
}
|
|
|
|
|
if ($results["PostgreSQL Container"] -eq $false) {
|
|
|
|
|
Write-Host " 2. Chay: docker run -d --name postgres-dev -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:16" -ForegroundColor Yellow
|
|
|
|
|
}
|
|
|
|
|
if ($results["Docker Daemon"] -eq $false) {
|
|
|
|
|
Write-Host " 3. Doi Docker Desktop khoi dong xong, roi chay lai script nay" -ForegroundColor Yellow
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Write-Host ("=" * 55) -ForegroundColor Magenta
|