imetting/start-external.sh

221 lines
5.5 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/bash
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
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"
}
print_banner() {
echo -e "${BLUE}"
cat << "EOF"
_ __ __ _ _
(_) \/ | ___ ___| |_(_)_ __ __ _
| | |\/| |/ _ \/ _ \ __| | '_ \ / _` |
| | | | | __/ __/ |_| | | | | (_| |
|_|_| |_|\___|\___|\__|_|_| |_|\__, |
|___/
External Middleware Deployment
EOF
echo -e "${NC}"
}
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
check_dependencies() {
print_info "检查系统依赖..."
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
print_success "系统依赖检查通过"
}
init_compose_cmd() {
if docker compose version &> /dev/null; then
COMPOSE_CMD="docker compose"
else
COMPOSE_CMD="docker-compose"
fi
}
check_env_files() {
print_info "检查环境变量配置..."
if [ ! -f .env ]; then
print_warning ".env 文件不存在,从模板创建..."
cp .env.example .env
print_warning "请编辑 .env 文件,配置外部 MySQL / Redis、访问端口、BASE_URL 等参数"
print_warning "按任意键继续,或 Ctrl+C 退出..."
read -n 1 -s
fi
print_success "环境变量文件检查完成"
}
load_external_env() {
print_info "加载外部数据库与 Redis 配置..."
set -a
source .env
set +a
DB_HOST="${DB_HOST:-${MYSQL_HOST:-}}"
DB_PORT="${DB_PORT:-${MYSQL_PORT:-3306}}"
DB_USER="${DB_USER:-${MYSQL_USER:-}}"
DB_PASSWORD="${DB_PASSWORD:-${MYSQL_PASSWORD:-}}"
DB_NAME="${DB_NAME:-${MYSQL_DATABASE:-}}"
REDIS_PORT="${REDIS_PORT:-6379}"
REDIS_DB="${REDIS_DB:-0}"
REDIS_PASSWORD="${REDIS_PASSWORD:-}"
local required_vars=(DB_HOST DB_USER DB_PASSWORD DB_NAME REDIS_HOST)
for var_name in "${required_vars[@]}"; do
if [ -z "${!var_name}" ]; then
print_error ".env 缺少必填配置: ${var_name}"
exit 1
fi
done
print_success "外部中间件配置已加载"
}
create_directories() {
print_info "创建必要的目录..."
mkdir -p data/uploads
mkdir -p data/logs/backend
mkdir -p data/logs/frontend
mkdir -p backups
print_success "目录创建完成"
}
create_override_file() {
OVERRIDE_FILE="$(mktemp /tmp/imeeting-external-compose.XXXXXX.yml)"
trap 'rm -f "$OVERRIDE_FILE"' EXIT
cat > "$OVERRIDE_FILE" <<EOF
services:
backend:
environment:
DB_HOST: "${DB_HOST}"
DB_PORT: "${DB_PORT}"
DB_USER: "${DB_USER}"
DB_PASSWORD: "${DB_PASSWORD}"
DB_NAME: "${DB_NAME}"
REDIS_HOST: "${REDIS_HOST}"
REDIS_PORT: "${REDIS_PORT}"
REDIS_DB: "${REDIS_DB}"
REDIS_PASSWORD: "${REDIS_PASSWORD}"
EOF
if [ -n "${BASE_URL:-}" ]; then
cat >> "$OVERRIDE_FILE" <<EOF
BASE_URL: "${BASE_URL}"
EOF
fi
}
start_services() {
print_info "启动应用服务(不启动 MySQL/Redis..."
$COMPOSE_CMD -f docker-compose.yml -f "$OVERRIDE_FILE" up -d --build --no-deps backend frontend
print_success "应用服务启动命令已执行"
}
wait_for_health() {
print_info "等待应用服务启动..."
local max_wait=120
local waited=0
while [ $waited -lt $max_wait ]; do
local healthy_count
healthy_count=$($COMPOSE_CMD -f docker-compose.yml -f "$OVERRIDE_FILE" ps --format json 2>/dev/null | grep -c '"Health":"healthy"' || echo "0")
if [ "$healthy_count" -eq 2 ]; then
print_success "前后端服务已就绪"
return 0
fi
echo -ne "\r等待中... (${waited}s/${max_wait}s) 健康: ${healthy_count}/2"
sleep 5
waited=$((waited + 5))
done
echo ""
print_warning "服务启动超时,请手动检查状态"
return 1
}
show_status() {
print_info "服务状态:"
$COMPOSE_CMD ps backend frontend
}
show_access_info() {
echo ""
print_success "==================================="
print_success " iMeeting 外部中间件模式部署完成!"
print_success "==================================="
echo ""
echo -e "${GREEN}访问地址:${NC}"
echo -e " HTTP访问: ${BLUE}http://localhost${NC}"
echo -e " API文档: ${BLUE}http://localhost/docs${NC}"
echo ""
echo -e "${YELLOW}当前模式:${NC}"
echo -e " 仅启动: ${BLUE}backend + frontend${NC}"
echo -e " 外部依赖: ${BLUE}根目录 .env 中配置的 MySQL / Redis${NC}"
echo ""
echo -e "${YELLOW}常用命令:${NC}"
echo -e " 查看日志: ${BLUE}$COMPOSE_CMD logs -f backend frontend${NC}"
echo -e " 停止服务: ${BLUE}./stop-external.sh${NC}"
echo -e " 查看状态: ${BLUE}$COMPOSE_CMD ps backend frontend${NC}"
echo ""
}
main() {
print_banner
check_dependencies
init_compose_cmd
check_env_files
load_external_env
create_directories
create_override_file
start_services
echo ""
wait_for_health
echo ""
show_status
show_access_info
}
main "$@"