#!/bin/bash # # DataOps Platform 启动脚本 # 使用 supervisorctl 启动 gunicorn 服务 # set -e # 配置变量 APP_NAME="dataops-platform" APP_DIR="/opt/dataops-platform" VENV_DIR="${APP_DIR}/venv" LOG_DIR="${APP_DIR}/logs" # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo_info() { echo -e "${GREEN}[INFO]${NC} $1" } echo_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } echo_error() { echo -e "${RED}[ERROR]${NC} $1" } # 检查虚拟环境是否存在 check_venv() { if [ ! -d "${VENV_DIR}" ]; then echo_error "虚拟环境不存在: ${VENV_DIR}" echo_info "请先运行部署脚本创建虚拟环境" exit 1 fi } # 检查 supervisor 是否运行 check_supervisor() { if ! pgrep -x "supervisord" > /dev/null; then echo_warn "supervisord 未运行,正在启动..." sudo supervisord -c /etc/supervisor/supervisord.conf sleep 2 fi } # 启动应用 start_app() { echo_info "正在启动 ${APP_NAME}..." # 检查应用状态 status=$(sudo supervisorctl status ${APP_NAME} 2>/dev/null | awk '{print $2}' || echo "UNKNOWN") if [ "$status" = "RUNNING" ]; then echo_warn "${APP_NAME} 已经在运行中" sudo supervisorctl status ${APP_NAME} return 0 fi # 启动应用 sudo supervisorctl start ${APP_NAME} # 等待启动 sleep 3 # 检查启动状态 status=$(sudo supervisorctl status ${APP_NAME} | awk '{print $2}') if [ "$status" = "RUNNING" ]; then echo_info "${APP_NAME} 启动成功!" sudo supervisorctl status ${APP_NAME} else echo_error "${APP_NAME} 启动失败!" echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log" exit 1 fi } # 健康检查 health_check() { echo_info "正在进行健康检查..." sleep 2 # 尝试访问健康检查接口 response=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:80/api/system/health 2>/dev/null || echo "000") if [ "$response" = "200" ]; then echo_info "健康检查通过! HTTP 状态码: ${response}" else echo_warn "健康检查返回状态码: ${response}" echo_info "服务可能需要更多时间启动,请稍后手动检查" fi } # 主函数 main() { echo "==========================================" echo " DataOps Platform 启动脚本" echo "==========================================" check_venv check_supervisor start_app health_check echo "" echo_info "启动完成!" echo_info "访问地址: http://localhost:80" echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log" } main "$@"