start_dataops.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/bash
  2. #
  3. # DataOps Platform 启动脚本
  4. # 使用 supervisorctl 启动 gunicorn 服务
  5. #
  6. set -e
  7. # 配置变量
  8. APP_NAME="dataops-platform"
  9. APP_DIR="/opt/dataops-platform"
  10. VENV_DIR="${APP_DIR}/venv"
  11. LOG_DIR="${APP_DIR}/logs"
  12. # 颜色输出
  13. RED='\033[0;31m'
  14. GREEN='\033[0;32m'
  15. YELLOW='\033[1;33m'
  16. NC='\033[0m' # No Color
  17. echo_info() {
  18. echo -e "${GREEN}[INFO]${NC} $1"
  19. }
  20. echo_warn() {
  21. echo -e "${YELLOW}[WARN]${NC} $1"
  22. }
  23. echo_error() {
  24. echo -e "${RED}[ERROR]${NC} $1"
  25. }
  26. # 检查虚拟环境是否存在
  27. check_venv() {
  28. if [ ! -d "${VENV_DIR}" ]; then
  29. echo_error "虚拟环境不存在: ${VENV_DIR}"
  30. echo_info "请先运行部署脚本创建虚拟环境"
  31. exit 1
  32. fi
  33. }
  34. # 检查 supervisor 是否运行
  35. check_supervisor() {
  36. if ! pgrep -x "supervisord" > /dev/null; then
  37. echo_warn "supervisord 未运行,正在启动..."
  38. sudo supervisord -c /etc/supervisor/supervisord.conf
  39. sleep 2
  40. fi
  41. }
  42. # 启动应用
  43. start_app() {
  44. echo_info "正在启动 ${APP_NAME}..."
  45. # 检查应用状态
  46. status=$(sudo supervisorctl status ${APP_NAME} 2>/dev/null | awk '{print $2}' || echo "UNKNOWN")
  47. if [ "$status" = "RUNNING" ]; then
  48. echo_warn "${APP_NAME} 已经在运行中"
  49. sudo supervisorctl status ${APP_NAME}
  50. return 0
  51. fi
  52. # 启动应用
  53. sudo supervisorctl start ${APP_NAME}
  54. # 等待启动
  55. sleep 3
  56. # 检查启动状态
  57. status=$(sudo supervisorctl status ${APP_NAME} | awk '{print $2}')
  58. if [ "$status" = "RUNNING" ]; then
  59. echo_info "${APP_NAME} 启动成功!"
  60. sudo supervisorctl status ${APP_NAME}
  61. else
  62. echo_error "${APP_NAME} 启动失败!"
  63. echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log"
  64. exit 1
  65. fi
  66. }
  67. # 健康检查
  68. health_check() {
  69. echo_info "正在进行健康检查..."
  70. sleep 2
  71. # 尝试访问健康检查接口
  72. response=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:80/api/system/health 2>/dev/null || echo "000")
  73. if [ "$response" = "200" ]; then
  74. echo_info "健康检查通过! HTTP 状态码: ${response}"
  75. else
  76. echo_warn "健康检查返回状态码: ${response}"
  77. echo_info "服务可能需要更多时间启动,请稍后手动检查"
  78. fi
  79. }
  80. # 主函数
  81. main() {
  82. echo "=========================================="
  83. echo " DataOps Platform 启动脚本"
  84. echo "=========================================="
  85. check_venv
  86. check_supervisor
  87. start_app
  88. health_check
  89. echo ""
  90. echo_info "启动完成!"
  91. echo_info "访问地址: http://localhost:80"
  92. echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log"
  93. }
  94. main "$@"