furniture-3d-system-Wu/scripts/validate-config.ps1
WuYongJie 9d58c6f707 feat: 添加生产环境 Docker 配置和配置验证脚本
- 创建 docker-compose.prod.yml 生产环境配置
  - 添加资源限制(CPU、内存)
  - 添加健康检查配置
  - 优化日志配置
  - 添加网络隔离
- 创建 validate-config.ps1 和 validate-config.sh 配置验证脚本
  - 验证环境变量配置
  - 验证端口可用性
  - 验证依赖服务连接
  - 验证开发环境和 Docker
2025-12-22 18:00:55 +08:00

218 lines
6.0 KiB
PowerShell
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ============================================
# 配置验证脚本 (PowerShell)
# ============================================
# 功能:
# - 验证环境变量配置
# - 验证端口可用性
# - 验证依赖服务连接
# 使用:
# .\scripts\validate-config.ps1
# ============================================
param(
[switch]$Verbose
)
$ErrorActionPreference = "Continue"
$script:HasErrors = $false
$script:HasWarnings = $false
function Write-Check {
param([string]$Name, [string]$Status, [string]$Message)
$icon = switch ($Status) {
"OK" { "✅" }
"WARN" { "⚠️" }
"ERROR" { "❌" }
default { "ℹ️" }
}
Write-Host " $icon $Name : $Message"
if ($Status -eq "ERROR") { $script:HasErrors = $true }
if ($Status -eq "WARN") { $script:HasWarnings = $true }
}
Write-Host ""
Write-Host "========================================"
Write-Host " 配置验证脚本"
Write-Host "========================================"
Write-Host ""
# 1. 检查环境变量文件
Write-Host "📋 检查环境变量..."
if (Test-Path ".env") {
Write-Check "环境变量文件" "OK" ".env 文件存在"
# 读取 .env 文件
$envContent = Get-Content ".env" -Raw
# 检查必需的环境变量
$requiredVars = @(
@{ Name = "DATABASE_URL"; Pattern = "DATABASE_URL=" }
)
# JWT_SECRET 或 NEXTAUTH_SECRET 至少需要一个
if ($envContent -match "JWT_SECRET=" -or $envContent -match "NEXTAUTH_SECRET=") {
Write-Check "JWT/Auth Secret" "OK" "已配置"
} else {
Write-Check "JWT/Auth Secret" "ERROR" "未配置 (需要 JWT_SECRET 或 NEXTAUTH_SECRET)"
}
foreach ($var in $requiredVars) {
if ($envContent -match $var.Pattern) {
Write-Check $var.Name "OK" "已配置"
} else {
Write-Check $var.Name "ERROR" "未配置"
}
}
# 检查可选但推荐的环境变量
$optionalVars = @(
@{ Name = "S3_ENDPOINT"; Pattern = "S3_ENDPOINT=" },
@{ Name = "S3_ACCESS_KEY"; Pattern = "S3_ACCESS_KEY=" },
@{ Name = "S3_SECRET_KEY"; Pattern = "S3_SECRET_KEY=" }
)
foreach ($var in $optionalVars) {
if ($envContent -match $var.Pattern) {
Write-Check $var.Name "OK" "已配置"
} else {
Write-Check $var.Name "WARN" "未配置(可选)"
}
}
} else {
Write-Check "环境变量文件" "ERROR" ".env 文件不存在"
}
Write-Host ""
# 2. 检查端口可用性
Write-Host "🔌 检查端口可用性..."
$ports = @(
@{ Port = 3000; Name = "Next.js 应用" },
@{ Port = 5432; Name = "PostgreSQL" },
@{ Port = 9000; Name = "MinIO API" },
@{ Port = 9001; Name = "MinIO Console" }
)
foreach ($p in $ports) {
$connection = Get-NetTCPConnection -LocalPort $p.Port -ErrorAction SilentlyContinue
if ($connection) {
$process = Get-Process -Id $connection.OwningProcess -ErrorAction SilentlyContinue
$processName = if ($process) { $process.ProcessName } else { "未知进程" }
Write-Check "$($p.Name) (端口 $($p.Port))" "OK" "已被 $processName 使用"
} else {
Write-Check "$($p.Name) (端口 $($p.Port))" "OK" "端口可用"
}
}
Write-Host ""
# 3. 检查依赖服务
Write-Host "🔗 检查依赖服务..."
# 检查 PostgreSQL
try {
$pgConnection = Test-NetConnection -ComputerName localhost -Port 5432 -WarningAction SilentlyContinue
if ($pgConnection.TcpTestSucceeded) {
Write-Check "PostgreSQL" "OK" "服务运行中"
} else {
Write-Check "PostgreSQL" "WARN" "服务未运行或无法连接"
}
} catch {
Write-Check "PostgreSQL" "WARN" "无法检测服务状态"
}
# 检查 MinIO
try {
$minioConnection = Test-NetConnection -ComputerName localhost -Port 9000 -WarningAction SilentlyContinue
if ($minioConnection.TcpTestSucceeded) {
Write-Check "MinIO" "OK" "服务运行中"
} else {
Write-Check "MinIO" "WARN" "服务未运行或无法连接"
}
} catch {
Write-Check "MinIO" "WARN" "无法检测服务状态"
}
Write-Host ""
# 4. 检查 Node.js 和 npm
Write-Host "📦 检查开发环境..."
try {
$nodeVersion = node --version 2>$null
if ($nodeVersion) {
Write-Check "Node.js" "OK" "版本 $nodeVersion"
} else {
Write-Check "Node.js" "ERROR" "未安装"
}
} catch {
Write-Check "Node.js" "ERROR" "未安装"
}
try {
$npmVersion = npm --version 2>$null
if ($npmVersion) {
Write-Check "npm" "OK" "版本 $npmVersion"
} else {
Write-Check "npm" "ERROR" "未安装"
}
} catch {
Write-Check "npm" "ERROR" "未安装"
}
# 检查 node_modules
if (Test-Path "node_modules") {
Write-Check "依赖包" "OK" "node_modules 存在"
} else {
Write-Check "依赖包" "WARN" "node_modules 不存在,请运行 npm install"
}
Write-Host ""
# 5. 检查 Docker(可选)
Write-Host "🐳 检查 Docker..."
try {
$dockerVersion = docker --version 2>$null
if ($dockerVersion) {
Write-Check "Docker" "OK" $dockerVersion
} else {
Write-Check "Docker" "WARN" "未安装(可选)"
}
} catch {
Write-Check "Docker" "WARN" "未安装(可选)"
}
try {
$composeVersion = docker-compose --version 2>$null
if ($composeVersion) {
Write-Check "Docker Compose" "OK" $composeVersion
} else {
Write-Check "Docker Compose" "WARN" "未安装(可选)"
}
} catch {
Write-Check "Docker Compose" "WARN" "未安装(可选)"
}
Write-Host ""
Write-Host "========================================"
if ($script:HasErrors) {
Write-Host " ❌ 配置验证失败,请修复上述错误"
Write-Host "========================================"
exit 1
} elseif ($script:HasWarnings) {
Write-Host " ⚠️ 配置验证通过,但有警告"
Write-Host "========================================"
exit 0
} else {
Write-Host " ✅ 配置验证通过"
Write-Host "========================================"
exit 0
}