feat: support minio storage

This commit is contained in:
wizardchen
2025-08-13 21:56:20 +08:00
committed by lyingbug
parent 621d9aad37
commit 8b43931886
29 changed files with 2735 additions and 1494 deletions
+106 -63
View File
@@ -80,18 +80,20 @@ check_env_file() {
if [ -z "$DB_DRIVER" ]; then missing_vars+=("DB_DRIVER"); fi
if [ -z "$STORAGE_TYPE" ]; then missing_vars+=("STORAGE_TYPE"); fi
if [ ${#missing_vars[@]} -gt 0 ]; then
log_warning "以下环境变量未设置,将使用默认值: ${missing_vars[*]}"
else
log_success "所有必要的环境变量已设置"
fi
return 0
}
# 安装Ollama(根据平台不同采用不同方法)
install_ollama() {
log_info "Ollama未安装,正在安装..."
# 检查是否为远程服务
get_ollama_base_url
if [ $IS_REMOTE -eq 1 ]; then
log_info "检测到远程Ollama服务配置,无需在本地安装Ollama"
return 0
fi
log_info "本地Ollama未安装,正在安装..."
OS=$(uname)
if [ "$OS" = "Darwin" ]; then
@@ -114,18 +116,54 @@ install_ollama() {
fi
if [ $? -eq 0 ]; then
log_success "Ollama安装完成"
log_success "本地Ollama安装完成"
return 0
else
log_error "Ollama安装失败"
log_error "本地Ollama安装失败"
return 1
fi
}
# 获取Ollama基础URL,检查是否为远程服务
get_ollama_base_url() {
check_env_file
# 从环境变量获取Ollama基础URL
OLLAMA_URL=${OLLAMA_BASE_URL:-"http://host.docker.internal:11434"}
# 提取主机部分
OLLAMA_HOST=$(echo "$OLLAMA_URL" | sed -E 's|^https?://||' | sed -E 's|:[0-9]+$||' | sed -E 's|/.*$||')
# 提取端口部分
OLLAMA_PORT=$(echo "$OLLAMA_URL" | grep -oE ':[0-9]+' | grep -oE '[0-9]+' || echo "11434")
# 检查是否为localhost或127.0.0.1
IS_REMOTE=0
if [ "$OLLAMA_HOST" = "localhost" ] || [ "$OLLAMA_HOST" = "127.0.0.1" ] || [ "$OLLAMA_HOST" = "host.docker.internal" ]; then
IS_REMOTE=0 # 本地服务
else
IS_REMOTE=1 # 远程服务
fi
}
# 启动Ollama服务
start_ollama() {
log_info "正在检查Ollama服务..."
# 提取主机和端口
get_ollama_base_url
log_info "Ollama服务地址: $OLLAMA_URL"
if [ $IS_REMOTE -eq 1 ]; then
log_info "检测到远程Ollama服务,将直接使用远程服务,不进行本地安装和启动"
# 检查远程服务是否可用
if curl -s "$OLLAMA_URL/api/tags" &> /dev/null; then
log_success "远程Ollama服务可访问"
return 0
else
log_warning "远程Ollama服务不可访问,请确认服务地址正确且已启动"
return 1
fi
fi
# 以下为本地服务的处理
# 检查Ollama是否已安装
if ! command -v ollama &> /dev/null; then
install_ollama
@@ -135,19 +173,19 @@ start_ollama() {
fi
# 检查Ollama服务是否已运行
if curl -s http://localhost:11434/api/tags &> /dev/null; then
log_success "Ollama服务已经在运行"
if curl -s "http://localhost:$OLLAMA_PORT/api/tags" &> /dev/null; then
log_success "本地Ollama服务已经在运行,端口:$OLLAMA_PORT"
else
log_info "启动Ollama服务..."
log_info "启动本地Ollama服务..."
# 注意:官方推荐使用 systemctl 或 launchctl 管理服务,直接后台运行仅用于临时场景
systemctl restart ollama || (ollama serve > /dev/null 2>&1 &)
systemctl restart ollama || (ollama serve > /dev/null 2>&1 < /dev/null &)
# 等待服务启动
MAX_RETRIES=30
COUNT=0
while [ $COUNT -lt $MAX_RETRIES ]; do
if curl -s http://localhost:11434/api/tags &> /dev/null; then
log_success "Ollama服务已成功启动"
if curl -s "http://localhost:$OLLAMA_PORT/api/tags" &> /dev/null; then
log_success "本地Ollama服务已成功启动,端口:$OLLAMA_PORT"
break
fi
echo -ne "等待Ollama服务启动... ($COUNT/$MAX_RETRIES)\r"
@@ -157,12 +195,12 @@ start_ollama() {
echo "" # 换行
if [ $COUNT -eq $MAX_RETRIES ]; then
log_error "Ollama服务启动失败"
log_error "本地Ollama服务启动失败"
return 1
fi
fi
log_success "Ollama服务地址: http://localhost:11434"
log_success "本地Ollama服务地址: http://localhost:$OLLAMA_PORT"
return 0
}
@@ -170,9 +208,17 @@ start_ollama() {
stop_ollama() {
log_info "正在停止Ollama服务..."
# 检查是否为远程服务
get_ollama_base_url
if [ $IS_REMOTE -eq 1 ]; then
log_info "检测到远程Ollama服务,无需在本地停止"
return 0
fi
# 检查Ollama是否已安装
if ! command -v ollama &> /dev/null; then
log_info "Ollama未安装,无需停止"
log_info "本地Ollama未安装,无需停止"
return 0
fi
@@ -184,9 +230,9 @@ stop_ollama() {
else
pkill -f "ollama serve"
fi
log_success "Ollama服务已停止"
log_success "本地Ollama服务已停止"
else
log_info "Ollama服务未运行"
log_info "本地Ollama服务未运行"
fi
return 0
@@ -217,6 +263,20 @@ check_docker() {
return 0
}
check_platform() {
# 检测当前系统平台
log_info "检测系统平台信息..."
if [ "$(uname -m)" = "x86_64" ]; then
export PLATFORM="linux/amd64"
elif [ "$(uname -m)" = "aarch64" ] || [ "$(uname -m)" = "arm64" ]; then
export PLATFORM="linux/arm64"
else
log_warning "未识别的平台类型:$(uname -m),将使用默认平台 linux/amd64"
export PLATFORM="linux/amd64"
fi
log_info "当前平台:$PLATFORM"
}
# 启动Docker容器
start_docker() {
log_info "正在启动Docker容器..."
@@ -233,18 +293,8 @@ start_docker() {
# 读取.env文件
source "$PROJECT_ROOT/.env"
storage_type=${STORAGE_TYPE:-local}
# 检测当前系统平台
log_info "检测系统平台信息..."
if [ "$(uname -m)" = "x86_64" ]; then
export PLATFORM="linux/amd64"
elif [ "$(uname -m)" = "aarch64" ] || [ "$(uname -m)" = "arm64" ]; then
export PLATFORM="linux/arm64"
else
log_warning "未识别的平台类型:$(uname -m),将使用默认平台 linux/amd64"
export PLATFORM="linux/amd64"
fi
log_info "当前平台:$PLATFORM"
check_platform
# 进入项目根目录再执行docker-compose命令
cd "$PROJECT_ROOT"
@@ -258,20 +308,6 @@ start_docker() {
return 1
fi
# 如果存储类型是minio,则启动MinIO服务
if [ "$storage_type" == "minio" ]; then
log_info "检测到MinIO存储配置,启动MinIO服务..."
# *** 修改点: 使用新的 docker compose 命令 ***
docker compose -f ./docker/docker-compose.minio.yml up --build -d
if [ $? -ne 0 ]; then
log_error "MinIO服务启动失败"
return 1
fi
log_success "MinIO服务已启动"
else
log_info "使用本地存储,不启动MinIO服务"
fi
log_success "所有Docker容器已成功启动"
# 显示容器状态
@@ -304,12 +340,6 @@ stop_docker() {
return 1
fi
# 如果存在minio配置,也停止minio
if [ -f "$PROJECT_ROOT/docker/docker-compose.minio.yml" ]; then
# *** 修改点: 使用新的 docker compose 命令 ***
docker compose -f "./docker/docker-compose.minio.yml" down
fi
log_success "所有Docker容器已停止"
return 0
}
@@ -354,6 +384,8 @@ restart_container() {
return 1
fi
check_platform
# 进入项目根目录再执行docker-compose命令
cd "$PROJECT_ROOT"
@@ -369,7 +401,7 @@ restart_container() {
# 构建并重启容器
log_info "正在重新构建容器 '$container_name'..."
# *** 修改点: 使用新的 docker compose 命令 ***
docker compose build "$container_name"
PLATFORM=$PLATFORM docker compose build "$container_name"
if [ $? -ne 0 ]; then
log_error "容器 '$container_name' 构建失败"
return 1
@@ -377,7 +409,7 @@ restart_container() {
log_info "正在重启容器 '$container_name'..."
# *** 修改点: 使用新的 docker compose 命令 ***
docker compose up -d --no-deps "$container_name"
PLATFORM=$PLATFORM docker compose up -d --no-deps "$container_name"
if [ $? -ne 0 ]; then
log_error "容器 '$container_name' 重启失败"
return 1
@@ -401,17 +433,28 @@ check_environment() {
# 检查.env文件
check_env_file
# 检查Ollama
if command -v ollama &> /dev/null; then
log_success "Ollama已安装"
if curl -s http://localhost:11434/api/tags &> /dev/null; then
version=$(curl -s http://localhost:11434/api/tags | grep -o '"version":"[^"]*"' | cut -d'"' -f4)
log_success "Ollama服务正在运行,版本: $version"
get_ollama_base_url
if [ $IS_REMOTE -eq 1 ]; then
log_info "检测到远程Ollama服务配置"
if curl -s "$OLLAMA_URL/api/tags" &> /dev/null; then
version=$(curl -s "$OLLAMA_URL/api/tags" | grep -o '"version":"[^"]*"' | cut -d'"' -f4)
log_success "远程Ollama服务可访问,版本: $version"
else
log_warning "Ollama已安装但服务未运行"
log_warning "远程Ollama服务不可访问,请确认服务地址正确且已启动"
fi
else
log_warning "Ollama未安装"
if command -v ollama &> /dev/null; then
log_success "本地Ollama已安装"
if curl -s "http://localhost:$OLLAMA_PORT/api/tags" &> /dev/null; then
version=$(curl -s "http://localhost:$OLLAMA_PORT/api/tags" | grep -o '"version":"[^"]*"' | cut -d'"' -f4)
log_success "本地Ollama服务正在运行,版本: $version"
else
log_warning "本地Ollama已安装但服务未运行"
fi
else
log_warning "本地Ollama未安装"
fi
fi
# 检查磁盘空间
@@ -580,7 +623,7 @@ else
fi
elif [ "$START_OLLAMA" = true ] && [ $OLLAMA_RESULT -eq 0 ]; then
log_success "Ollama服务启动完成,可通过以下地址访问:"
echo -e "${GREEN} - Ollama API: http://localhost:11434${NC}"
echo -e "${GREEN} - Ollama API: http://localhost:$OLLAMA_PORT${NC}"
elif [ "$START_DOCKER" = true ] && [ $DOCKER_RESULT -eq 0 ]; then
log_success "Docker容器启动完成,可通过以下地址访问:"
echo -e "${GREEN} - 前端界面: http://localhost${NC}"