mirror of
https://git.oceanpay.cc/danial/kami_apple_exchage.git
synced 2025-12-18 22:29:09 +00:00
- Create README.md for deployment instructions including environment requirements and setup steps. - Implement deploy.sh script for automated deployment of development and production environments. - Add combined Docker Compose configuration for frontend and backend services. - Include Redis configuration file for optimized memory management and persistence. - Update frontend Dockerfile to handle Next.js asset paths and static files. - Remove obsolete deployment files and configurations from frontend directory.
204 lines
5.6 KiB
Bash
204 lines
5.6 KiB
Bash
#!/bin/bash
|
|
|
|
# Apple Gift Card Exchange Platform - 部署脚本
|
|
# 支持开发环境和生产环境的 Docker 部署
|
|
|
|
set -e # 遇到错误立即退出
|
|
|
|
# 颜色输出
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 打印带颜色的信息
|
|
print_info() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# 检查 Docker 是否安装
|
|
check_docker() {
|
|
if ! command -v docker &> /dev/null; then
|
|
print_error "Docker 未安装,请先安装 Docker"
|
|
exit 1
|
|
fi
|
|
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
|
|
print_error "Docker Compose 未安装,请先安装 Docker Compose"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# 检查环境文件
|
|
check_env_files() {
|
|
if [ ! -f "backend/.env" ]; then
|
|
print_warning "backend/.env 文件不存在,使用默认配置"
|
|
cp backend/.env.example backend/.env 2>/dev/null || true
|
|
fi
|
|
|
|
if [ ! -f "frontend/.env.local" ]; then
|
|
print_warning "frontend/.env.local 文件不存在,使用默认配置"
|
|
cp frontend/env.example frontend/.env.local 2>/dev/null || true
|
|
fi
|
|
}
|
|
|
|
# 创建必要的目录
|
|
create_directories() {
|
|
print_info "创建必要的目录..."
|
|
mkdir -p deploy/ssl
|
|
mkdir -p deploy/logs
|
|
mkdir -p backend/logs
|
|
mkdir -p backend/screenshots
|
|
mkdir -p backend/data
|
|
chmod 755 deploy/ssl deploy/logs backend/logs backend/screenshots backend/data
|
|
}
|
|
|
|
# 生成 SSL 证书(自签名)
|
|
generate_ssl_cert() {
|
|
if [ ! -f "deploy/ssl/cert.pem" ] || [ ! -f "deploy/ssl/key.pem" ]; then
|
|
print_info "生成自签名 SSL 证书..."
|
|
openssl req -x509 -newkey rsa:4096 -keyout deploy/ssl/key.pem -out deploy/ssl/cert.pem -days 365 -nodes -subj "/C=CN/ST=Beijing/L=Beijing/O=Apple Exchange/CN=localhost" 2>/dev/null || {
|
|
print_warning "无法生成 SSL 证书,请手动安装或使用 HTTP"
|
|
}
|
|
fi
|
|
}
|
|
|
|
# 清理旧容器和镜像
|
|
cleanup() {
|
|
print_info "清理旧容器和镜像..."
|
|
docker-compose -f deploy/docker-compose.combined.yml down --remove-orphans 2>/dev/null || true
|
|
docker system prune -f 2>/dev/null || true
|
|
}
|
|
|
|
# 构建和启动服务
|
|
start_services() {
|
|
local env="$1"
|
|
print_info "启动 $env 环境服务..."
|
|
|
|
case $env in
|
|
"dev")
|
|
print_info "启动开发环境..."
|
|
docker-compose -f deploy/docker-compose.combined.yml --profile dev up -d
|
|
;;
|
|
"prod")
|
|
print_info "启动生产环境..."
|
|
docker-compose -f deploy/docker-compose.combined.yml --profile production up -d
|
|
;;
|
|
"monitoring")
|
|
print_info "启动包含监控的环境..."
|
|
docker-compose -f deploy/docker-compose.combined.yml --profile monitoring --profile production up -d
|
|
;;
|
|
*)
|
|
print_error "未知的环境: $env"
|
|
echo "可用环境: dev, prod, monitoring"
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# 等待服务启动
|
|
wait_for_services() {
|
|
print_info "等待服务启动..."
|
|
sleep 30
|
|
|
|
# 检查前端服务
|
|
if curl -f http://localhost:3000/ >/dev/null 2>&1; then
|
|
print_success "前端服务已启动: http://localhost:3000"
|
|
else
|
|
print_warning "前端服务启动失败或仍在启动中"
|
|
fi
|
|
|
|
# 检查后端 API
|
|
if curl -f http://localhost:8000/api/v1/health/liveness >/dev/null 2>&1; then
|
|
print_success "后端 API 已启动: http://localhost:8000"
|
|
print_success "API 文档: http://localhost:8000/docs"
|
|
else
|
|
print_warning "后端 API 启动失败或仍在启动中"
|
|
fi
|
|
|
|
# 检查数据库
|
|
if docker exec apple-exchange-db pg_isready -U postgres >/dev/null 2>&1; then
|
|
print_success "数据库已启动"
|
|
else
|
|
print_warning "数据库启动失败或仍在启动中"
|
|
fi
|
|
|
|
# 检查 Redis
|
|
if docker exec apple-exchange-redis redis-cli ping >/dev/null 2>&1; then
|
|
print_success "Redis 已启动"
|
|
else
|
|
print_warning "Redis 启动失败或仍在启动中"
|
|
fi
|
|
}
|
|
|
|
# 显示服务状态
|
|
show_status() {
|
|
print_info "服务状态:"
|
|
docker-compose -f deploy/docker-compose.combined.yml ps
|
|
}
|
|
|
|
# 显示访问信息
|
|
show_access_info() {
|
|
echo ""
|
|
print_success "部署完成!"
|
|
echo ""
|
|
echo "=== 访问地址 ==="
|
|
echo "前端应用: http://localhost:3000"
|
|
echo "后端 API: http://localhost:8000"
|
|
echo "API 文档: http://localhost:8000/docs"
|
|
echo ""
|
|
echo "=== 监控服务 (如果启用) ==="
|
|
echo "Celery Flower: http://localhost:5555"
|
|
echo ""
|
|
echo "=== 数据库访问 ==="
|
|
echo "PostgreSQL: localhost:5432"
|
|
echo "Redis: localhost:6379"
|
|
echo ""
|
|
echo "=== 管理命令 ==="
|
|
echo "查看日志: docker-compose -f deploy/docker-compose.combined.yml logs -f [service]"
|
|
echo "停止服务: docker-compose -f deploy/docker-compose.combined.yml down"
|
|
echo "重启服务: docker-compose -f deploy/docker-compose.combined.yml restart [service]"
|
|
echo ""
|
|
}
|
|
|
|
# 主函数
|
|
main() {
|
|
local env="${1:-dev}"
|
|
|
|
print_info "Apple Gift Card Exchange Platform 部署脚本"
|
|
print_info "环境: $env"
|
|
echo ""
|
|
|
|
check_docker
|
|
check_env_files
|
|
create_directories
|
|
generate_ssl_cert
|
|
|
|
if [ "$2" = "--cleanup" ]; then
|
|
cleanup
|
|
fi
|
|
|
|
start_services "$env"
|
|
wait_for_services
|
|
show_status
|
|
show_access_info
|
|
}
|
|
|
|
# 解析命令行参数
|
|
if [ $# -eq 0 ]; then
|
|
main "dev"
|
|
else
|
|
main "$1" "$2"
|
|
fi |