Files
petwash/verify-config.ps1
GukSang.Jin 9c66b6cd82
2026-03-03 16:55:02 +08:00

120 lines
3.9 KiB
PowerShell

# 配置验证脚本
# PetWash API Docker 部署前检查
Write-Host "======================================" -ForegroundColor Cyan
Write-Host "PetWash API 配置验证" -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
Write-Host ""
$allPassed = $true
# 1. 检查 Docker
Write-Host "1. 检查 Docker..." -ForegroundColor Yellow
try {
$dockerVersion = docker --version 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] Docker 已安装: $dockerVersion" -ForegroundColor Green
} else {
Write-Host " [FAIL] Docker 未安装" -ForegroundColor Red
$allPassed = $false
}
} catch {
Write-Host " [FAIL] Docker 未安装" -ForegroundColor Red
$allPassed = $false
}
# 2. 检查 Docker Compose
Write-Host "2. 检查 Docker Compose..." -ForegroundColor Yellow
try {
$composeVersion = docker-compose --version 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] Docker Compose 已安装: $composeVersion" -ForegroundColor Green
} else {
Write-Host " [FAIL] Docker Compose 未安装" -ForegroundColor Red
$allPassed = $false
}
} catch {
Write-Host " [FAIL] Docker Compose 未安装" -ForegroundColor Red
$allPassed = $false
}
# 3. 检查配置文件
Write-Host "3. 检查配置文件..." -ForegroundColor Yellow
$configFiles = @(
"PetWash.Api/Dockerfile",
"docker-compose.yml",
"PetWash.Api/appsettings.json",
"PetWash.Api/appsettings.Production.json",
"init-database.sql"
)
foreach ($file in $configFiles) {
if (Test-Path $file) {
Write-Host " [OK] $file 存在" -ForegroundColor Green
} else {
Write-Host " [FAIL] $file 不存在" -ForegroundColor Red
$allPassed = $false
}
}
# 4. 检查 MySQL 包
Write-Host "4. 检查 NuGet 包..." -ForegroundColor Yellow
$csprojContent = Get-Content "PetWash.Api/PetWash.Api.csproj" -Raw
if ($csprojContent -match "Pomelo.EntityFrameworkCore.MySql") {
Write-Host " [OK] MySQL 包已添加" -ForegroundColor Green
} else {
Write-Host " [FAIL] MySQL 包未添加" -ForegroundColor Red
$allPassed = $false
}
# 5. 检查 MySQL 连接
Write-Host "5. 检查 MySQL 连接..." -ForegroundColor Yellow
$mysqlHost = "101.132.182.216"
$mysqlPort = 3306
try {
$tcpClient = New-Object System.Net.Sockets.TcpClient
$tcpClient.Connect($mysqlHost, $mysqlPort)
$tcpClient.Close()
Write-Host " [OK] MySQL 服务器可访问" -ForegroundColor Green
} catch {
Write-Host " [FAIL] MySQL 服务器不可访问" -ForegroundColor Red
Write-Host " 请检查网络连接和防火墙设置" -ForegroundColor Yellow
$allPassed = $false
}
# 6. 检查项目编译
Write-Host "6. 检查项目编译..." -ForegroundColor Yellow
try {
Push-Location "PetWash.Api"
$buildResult = dotnet build --no-restore 2>&1
Pop-Location
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] 项目编译成功" -ForegroundColor Green
} else {
Write-Host " [FAIL] 项目编译失败" -ForegroundColor Red
$allPassed = $false
}
} catch {
Write-Host " [FAIL] 项目编译失败" -ForegroundColor Red
$allPassed = $false
Pop-Location
}
# 总结
Write-Host ""
Write-Host "======================================" -ForegroundColor Cyan
if ($allPassed) {
Write-Host "[SUCCESS] 所有检查通过!可以开始部署" -ForegroundColor Green
Write-Host ""
Write-Host "下一步:" -ForegroundColor Yellow
Write-Host "1. 初始化数据库" -ForegroundColor White
Write-Host " mysql -h 101.132.182.216 -P 3306 -u root -p" -ForegroundColor Gray
Write-Host " 然后运行: SOURCE init-database.sql" -ForegroundColor Gray
Write-Host "2. 运行部署脚本: .\deploy.ps1" -ForegroundColor White
} else {
Write-Host "[FAILED] 部分检查未通过,请修复后再部署" -ForegroundColor Red
}
Write-Host "======================================" -ForegroundColor Cyan