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

192 lines
5.0 KiB
Bash
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.

#!/bin/bash
# ============================================
# 配置验证脚本 (Linux/Mac)
# ============================================
# 功能:
# - 验证环境变量配置
# - 验证端口可用性
# - 验证依赖服务连接
# 使用:
# ./scripts/validate-config.sh
# ============================================
HAS_ERRORS=false
HAS_WARNINGS=false
write_check() {
local name="$1"
local status="$2"
local message="$3"
case "$status" in
"OK") icon="✅" ;;
"WARN") icon="⚠️"; HAS_WARNINGS=true ;;
"ERROR") icon="❌"; HAS_ERRORS=true ;;
*) icon="ℹ️" ;;
esac
echo " $icon $name : $message"
}
echo ""
echo "========================================"
echo " 配置验证脚本"
echo "========================================"
echo ""
# 1. 检查环境变量文件
echo "📋 检查环境变量..."
if [ -f ".env" ]; then
write_check "环境变量文件" "OK" ".env 文件存在"
# 检查必需的环境变量
if grep -q "DATABASE_URL=" .env; then
write_check "DATABASE_URL" "OK" "已配置"
else
write_check "DATABASE_URL" "ERROR" "未配置"
fi
if grep -q "JWT_SECRET=" .env; then
write_check "JWT_SECRET" "OK" "已配置"
else
write_check "JWT_SECRET" "ERROR" "未配置"
fi
# 检查可选环境变量
if grep -q "S3_ENDPOINT=" .env; then
write_check "S3_ENDPOINT" "OK" "已配置"
else
write_check "S3_ENDPOINT" "WARN" "未配置(可选)"
fi
if grep -q "S3_ACCESS_KEY=" .env; then
write_check "S3_ACCESS_KEY" "OK" "已配置"
else
write_check "S3_ACCESS_KEY" "WARN" "未配置(可选)"
fi
else
write_check "环境变量文件" "ERROR" ".env 文件不存在"
fi
echo ""
# 2. 检查端口可用性
echo "🔌 检查端口可用性..."
check_port() {
local port="$1"
local name="$2"
if command -v lsof &> /dev/null; then
if lsof -i ":$port" &> /dev/null; then
process=$(lsof -i ":$port" | tail -1 | awk '{print $1}')
write_check "$name (端口 $port)" "OK" "已被 $process 使用"
else
write_check "$name (端口 $port)" "OK" "端口可用"
fi
elif command -v netstat &> /dev/null; then
if netstat -tuln | grep -q ":$port "; then
write_check "$name (端口 $port)" "OK" "端口已被使用"
else
write_check "$name (端口 $port)" "OK" "端口可用"
fi
else
write_check "$name (端口 $port)" "WARN" "无法检测端口状态"
fi
}
check_port 3000 "Next.js 应用"
check_port 5432 "PostgreSQL"
check_port 9000 "MinIO API"
check_port 9001 "MinIO Console"
echo ""
# 3. 检查依赖服务
echo "🔗 检查依赖服务..."
# 检查 PostgreSQL
if command -v pg_isready &> /dev/null; then
if pg_isready -h localhost -p 5432 &> /dev/null; then
write_check "PostgreSQL" "OK" "服务运行中"
else
write_check "PostgreSQL" "WARN" "服务未运行或无法连接"
fi
elif nc -z localhost 5432 2>/dev/null; then
write_check "PostgreSQL" "OK" "端口可访问"
else
write_check "PostgreSQL" "WARN" "服务未运行或无法连接"
fi
# 检查 MinIO
if curl -s http://localhost:9000/minio/health/live &> /dev/null; then
write_check "MinIO" "OK" "服务运行中"
elif nc -z localhost 9000 2>/dev/null; then
write_check "MinIO" "OK" "端口可访问"
else
write_check "MinIO" "WARN" "服务未运行或无法连接"
fi
echo ""
# 4. 检查开发环境
echo "📦 检查开发环境..."
if command -v node &> /dev/null; then
node_version=$(node --version)
write_check "Node.js" "OK" "版本 $node_version"
else
write_check "Node.js" "ERROR" "未安装"
fi
if command -v npm &> /dev/null; then
npm_version=$(npm --version)
write_check "npm" "OK" "版本 $npm_version"
else
write_check "npm" "ERROR" "未安装"
fi
if [ -d "node_modules" ]; then
write_check "依赖包" "OK" "node_modules 存在"
else
write_check "依赖包" "WARN" "node_modules 不存在,请运行 npm install"
fi
echo ""
# 5. 检查 Docker(可选)
echo "🐳 检查 Docker..."
if command -v docker &> /dev/null; then
docker_version=$(docker --version)
write_check "Docker" "OK" "$docker_version"
else
write_check "Docker" "WARN" "未安装(可选)"
fi
if command -v docker-compose &> /dev/null; then
compose_version=$(docker-compose --version)
write_check "Docker Compose" "OK" "$compose_version"
else
write_check "Docker Compose" "WARN" "未安装(可选)"
fi
echo ""
echo "========================================"
if [ "$HAS_ERRORS" = true ]; then
echo " ❌ 配置验证失败,请修复上述错误"
echo "========================================"
exit 1
elif [ "$HAS_WARNINGS" = true ]; then
echo " ⚠️ 配置验证通过,但有警告"
echo "========================================"
exit 0
else
echo " ✅ 配置验证通过"
echo "========================================"
exit 0
fi