aidress/entrypoint.sh
2025-03-21 23:02:15 +08:00

61 lines
2.0 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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
set -e
# 显示当前环境
echo "============================================"
echo "容器启动 - 环境: ${ENV:-production}"
echo "============================================"
# 检查是否存在.env文件
if [ -f .env ]; then
echo "从.env文件加载环境变量..."
export $(grep -v '^#' .env | xargs)
fi
# 检查关键环境变量
echo "检查关键环境变量..."
MISSING_VARS=0
# 检查数据库配置
if [ -z "$DB_HOST" ]; then echo "警告: DB_HOST 未设置"; MISSING_VARS=$((MISSING_VARS+1)); fi
if [ -z "$DB_USER" ]; then echo "警告: DB_USER 未设置"; MISSING_VARS=$((MISSING_VARS+1)); fi
if [ -z "$DB_PASSWORD" ]; then echo "警告: DB_PASSWORD 未设置"; MISSING_VARS=$((MISSING_VARS+1)); fi
if [ -z "$DB_NAME" ]; then echo "警告: DB_NAME 未设置"; MISSING_VARS=$((MISSING_VARS+1)); fi
# 检查API密钥
if [ -z "$DASHSCOPE_API_KEY" ]; then echo "警告: DASHSCOPE_API_KEY 未设置"; fi
if [ -z "$QCLOUD_SECRET_ID" ]; then echo "警告: QCLOUD_SECRET_ID 未设置"; fi
if [ -z "$QCLOUD_SECRET_KEY" ]; then echo "警告: QCLOUD_SECRET_KEY 未设置"; fi
# 检查依赖
echo "检查Python依赖..."
python3 check_dependencies.py
# 如果主要依赖都在,尝试连接数据库
if [ $MISSING_VARS -eq 0 ]; then
echo "尝试连接到数据库..."
MAX_RETRIES=5
RETRY_COUNT=0
until PGPASSWORD=$DB_PASSWORD mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -e "SELECT 1" > /dev/null 2>&1; do
RETRY_COUNT=$((RETRY_COUNT+1))
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
echo "无法连接到数据库 - 达到最大重试次数"
break
fi
echo "无法连接到数据库将在5秒后重试... ($RETRY_COUNT/$MAX_RETRIES)"
sleep 5
done
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
echo "数据库连接成功!"
# 应用数据库迁移
echo "应用数据库迁移..."
python3 create_migration.py upgrade
fi
fi
# 启动应用
echo "启动API服务..."
exec "$@"