Files
BigDataTool/docker-entrypoint.sh
2025-08-11 09:34:45 +08:00

237 lines
5.8 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
# BigDataTool Docker 启动脚本
# 用于容器化部署的入口脚本
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 日志函数
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# 显示启动信息
show_banner() {
echo "======================================"
echo " BigDataTool Container Startup"
echo "======================================"
echo "Version: 2.0"
echo "Python: $(python --version)"
echo "Working Directory: $(pwd)"
echo "User: $(whoami)"
echo "======================================"
}
# 检查环境变量
check_environment() {
log_info "检查环境变量..."
# 设置默认值
export FLASK_ENV=${FLASK_ENV:-production}
export FLASK_DEBUG=${FLASK_DEBUG:-False}
export SECRET_KEY=${SECRET_KEY:-$(python -c "import secrets; print(secrets.token_hex(32))")}
export DATABASE_URL=${DATABASE_URL:-sqlite:///config_groups.db}
log_info "FLASK_ENV: $FLASK_ENV"
log_info "FLASK_DEBUG: $FLASK_DEBUG"
log_info "数据库URL: $DATABASE_URL"
# 检查必要的环境变量
if [ -z "$SECRET_KEY" ]; then
log_warn "SECRET_KEY 未设置,使用随机生成的密钥"
fi
}
# 初始化数据库
initialize_database() {
log_info "初始化数据库..."
# 检查数据库文件是否存在
if [ ! -f "config_groups.db" ]; then
log_info "数据库文件不存在,将自动创建"
python -c "
from modules.database import ensure_database
if ensure_database():
print('数据库初始化成功')
else:
print('数据库初始化失败')
exit(1)
"
if [ $? -eq 0 ]; then
log_success "数据库初始化完成"
else
log_error "数据库初始化失败"
exit 1
fi
else
log_info "数据库文件已存在"
fi
}
# 创建必要目录
create_directories() {
log_info "创建必要目录..."
# 创建日志目录
if [ ! -d "logs" ]; then
mkdir -p logs
log_info "创建日志目录: logs"
fi
# 创建配置目录
if [ ! -d "config" ]; then
mkdir -p config
log_info "创建配置目录: config"
fi
# 设置权限
chmod -R 755 logs config || true
}
# 健康检查函数
health_check() {
log_info "执行健康检查..."
local max_attempts=30
local attempt=1
while [ $attempt -le $max_attempts ]; do
if wget --no-verbose --tries=1 --spider http://localhost:5000/api/health >/dev/null 2>&1; then
log_success "应用健康检查通过"
return 0
fi
log_info "等待应用启动... ($attempt/$max_attempts)"
sleep 2
((attempt++))
done
log_error "健康检查失败,应用可能未正常启动"
return 1
}
# 信号处理函数
cleanup() {
log_warn "收到退出信号,正在清理..."
# 这里可以添加清理逻辑
# 例如:保存缓存、关闭数据库连接等
log_info "清理完成,退出应用"
exit 0
}
# 设置信号处理
trap cleanup SIGTERM SIGINT
# 主启动函数
start_application() {
log_info "启动BigDataTool应用..."
# 根据环境变量选择启动方式
if [ "$FLASK_ENV" = "development" ]; then
log_info "以开发模式启动"
python app.py
else
log_info "以生产模式启动"
# 检查是否安装了gunicorn
if command -v gunicorn >/dev/null 2>&1; then
log_info "使用Gunicorn启动应用"
exec gunicorn \
--bind 0.0.0.0:5000 \
--workers 4 \
--worker-class sync \
--worker-connections 1000 \
--max-requests 1000 \
--max-requests-jitter 50 \
--timeout 120 \
--keep-alive 5 \
--log-level info \
--access-logfile - \
--error-logfile - \
app:app
else
log_warn "Gunicorn未安装使用Flask开发服务器"
python app.py
fi
fi
}
# 显示帮助信息
show_help() {
echo "BigDataTool Docker 启动脚本"
echo ""
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " start 启动应用(默认)"
echo " health-check 执行健康检查"
echo " init-db 仅初始化数据库"
echo " shell 进入交互式shell"
echo " help 显示此帮助信息"
echo ""
echo "环境变量:"
echo " FLASK_ENV Flask运行环境 (development/production)"
echo " FLASK_DEBUG 是否启用调试模式 (True/False)"
echo " SECRET_KEY 应用密钥"
echo " DATABASE_URL 数据库连接URL"
echo ""
}
# 主逻辑
main() {
case "${1:-start}" in
start)
show_banner
check_environment
create_directories
initialize_database
start_application
;;
health-check)
health_check
;;
init-db)
log_info "仅初始化数据库模式"
check_environment
create_directories
initialize_database
log_success "数据库初始化完成"
;;
shell)
log_info "进入交互式shell"
exec /bin/bash
;;
help|--help|-h)
show_help
;;
*)
log_error "未知选项: $1"
show_help
exit 1
;;
esac
}
# 执行主函数
main "$@"