Files
deerflow-code/deploy/linux-docker-offline-deerflow-only/release-template/OFFLINE_INSTALL.sh
T

220 lines
7.5 KiB
Bash
Raw 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.
#!/usr/bin/env bash
set -Eeuo pipefail
BUNDLE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPOSE_FILE="$BUNDLE_DIR/compose/docker-compose.yml"
GENERATED_ENV="$BUNDLE_DIR/data/generated/compose.env"
DEPLOY_ENV="$BUNDLE_DIR/config/deploy.env"
STATE_DIR="$BUNDLE_DIR/data/state"
IMAGE_LIST="$BUNDLE_DIR/images/deerflow-image-list.txt"
IMAGE_ARCHIVE="$BUNDLE_DIR/images/deerflow-images-linux-amd64.tar.gz"
PROJECT_NAME="cmzs-deerflow"
COMPOSE_CMD=()
say() { printf '\n[%s] %s\n' "$(date '+%F %T')" "$*"; }
die() { printf '错误: %s\n' "$*" >&2; exit 1; }
usage() {
cat <<'EOF'
用法:
./OFFLINE_INSTALL.sh # 等同 --start
./OFFLINE_INSTALL.sh --start # 按 deploy.env 渲染并启动
./OFFLINE_INSTALL.sh --stop # 只停止,不删除容器、镜像、网络或数据卷
./OFFLINE_INSTALL.sh --restart # 直接加载 source/ 下最新前后台代码并重启
./OFFLINE_INSTALL.sh --status # 查看服务状态与访问地址
./OFFLINE_INSTALL.sh --logs [服务名]
./OFFLINE_INSTALL.sh --office-check # 不连接数据库,检查 Word/Excel/PPT 沙箱能力
EOF
}
detect_compose() {
# 目标离线服务器使用独立的 docker-compose 命令;同时保留 Compose v2
# 插件回退,便于同一部署包在两类 Docker 主机上使用。
if command -v docker-compose >/dev/null 2>&1; then
COMPOSE_CMD=(docker-compose)
elif docker compose version >/dev/null 2>&1; then
COMPOSE_CMD=(docker compose)
else
die "未安装 docker-compose,也没有 docker compose 插件"
fi
"${COMPOSE_CMD[@]}" version >/dev/null 2>&1 || die "Docker Compose 不可用"
}
require_tools() {
command -v docker >/dev/null 2>&1 || die "未安装 Docker"
docker info >/dev/null 2>&1 || die "Docker 服务不可用,或当前用户无权访问 Docker"
detect_compose
command -v gzip >/dev/null 2>&1 || die "缺少 gzip"
command -v sha256sum >/dev/null 2>&1 || die "缺少 sha256sum"
command -v curl >/dev/null 2>&1 || die "缺少 curl"
}
compose() {
[[ -f "$GENERATED_ENV" ]] || die "尚未生成运行配置,请先执行 --start"
"${COMPOSE_CMD[@]}" --project-name "$PROJECT_NAME" --env-file "$GENERATED_ENV" -f "$COMPOSE_FILE" "$@"
}
images_missing() {
local image
while IFS= read -r image; do
[[ -z "$image" || "$image" == \#* ]] && continue
docker image inspect "$image" >/dev/null 2>&1 || return 0
done < "$IMAGE_LIST"
return 1
}
load_images_if_missing() {
if ! images_missing; then
say "所需 DeerFlow 镜像已存在,跳过校验与 docker load"
return 0
fi
say "校验 DeerFlow 离线镜像文件"
(cd "$BUNDLE_DIR" && sha256sum -c SHA256SUMS)
say "加载 DeerFlow 前端和后端镜像"
gzip -dc "$IMAGE_ARCHIVE" | docker load
}
backend_image() {
sed -n '/^[^#[:space:]]/ {p;q;}' "$IMAGE_LIST"
}
render_runtime() {
mkdir -p "$STATE_DIR" "$BUNDLE_DIR/data/generated"
chmod 0600 "$DEPLOY_ENV"
say "校验 deploy.env 并生成运行配置"
docker run --rm --network none \
-v "$BUNDLE_DIR:/bundle" \
"$(backend_image)" python /bundle/scripts/render_runtime.py /bundle
}
install_wheelhouse() {
[[ -d "$BUNDLE_DIR/source/wheelhouse" ]] || die "缺少 source/wheelhouse"
[[ -f "$BUNDLE_DIR/source/wheelhouse/requirements.txt" ]] || \
die "缺少 source/wheelhouse/requirements.txt"
say "检查 source/wheelhouse 并离线安装新增 Python 依赖"
compose run --rm --no-deps deerflow-backend \
/opt/venv/bin/python /app/offline-deploy/install_wheelhouse.py \
/app/wheelhouse /app/backend/.deer-flow/python-vendor
}
preflight_mysql() {
say "检查 DeerFlow 外部 MySQL"
docker run --rm --network host \
--add-host host.docker.internal:host-gateway \
-v "$BUNDLE_DIR:/bundle:ro" \
"$(backend_image)" python /bundle/scripts/preflight_mysql.py /bundle/config/deploy.env
}
generated_value() {
local key="$1"
sed -n "s/^${key}=//p" "$GENERATED_ENV" | tail -n 1
}
wait_http() {
local name="$1" url="$2" attempts="${3:-90}" code i
for ((i=1; i<=attempts; i++)); do
code="$(curl -sS -o /dev/null -w '%{http_code}' "$url" 2>/dev/null || true)"
if [[ "$code" =~ ^(200|204|301|302)$ ]]; then
printf '%s 就绪(HTTP %s)\n' "$name" "$code"
return 0
fi
sleep 3
done
compose ps || true
compose logs --tail=160 || true
die "$name 启动超时: $url"
}
start_all() {
[[ -f "$DEPLOY_ENV" ]] || die "缺少 $DEPLOY_ENV"
load_images_if_missing
render_runtime
preflight_mysql
install_wheelhouse
say "启动 DeerFlow(禁止在线拉取和现场构建)"
# 镜像已由 load_images_if_missing 校验并加载。这里不用 Compose v2 专属的
# `--pull never`,以兼容服务器上的 docker-compose v1。
compose up -d --no-build
wait_http "DeerFlow 后端" "http://127.0.0.1:$(generated_value BACKEND_PORT)/health" 120
wait_http "DeerFlow 页面" "http://127.0.0.1:$(generated_value FRONTEND_PORT)/" 60
say "部署完成"
printf 'DeerFlow: http://%s:%s\n' "$(generated_value PUBLIC_HOST)" "$(generated_value FRONTEND_PORT)"
compose ps
}
stop_all() {
if [[ ! -f "$GENERATED_ENV" ]]; then
say "没有已生成的运行配置,无需停止"
return 0
fi
say "只停止容器;保留容器、镜像、网络和全部数据卷"
compose stop
}
restart_all() {
[[ -f "$DEPLOY_ENV" ]] || die "缺少 $DEPLOY_ENV"
[[ -s "$GENERATED_ENV" ]] || die "缺少运行配置,请先执行 --start"
[[ -d "$BUNDLE_DIR/source/backend/app" ]] || die "缺少 source/backend/app"
[[ -d "$BUNDLE_DIR/source/backend/packages" ]] || die "缺少 source/backend/packages"
[[ -d "$BUNDLE_DIR/source/backend/tests" ]] || die "缺少 source/backend/tests"
[[ -s "$BUNDLE_DIR/source/frontend/dist/index.html" ]] || \
die "缺少 source/frontend/dist/index.html;请先完成前端 pnpm build"
# --restart 不做 MySQL 预检,也不 load/build/pull 镜像;但会重新渲染
# deploy.env,让新增模型和配置修改可随同源码在一次重启中生效。
render_runtime
install_wheelhouse
say "检查并加载 source/ 下最新前后台代码"
compose run --rm --no-deps deerflow-backend /opt/venv/bin/python -c \
'import app.gateway.app, deerflow; print("后端源码检查通过")'
say "重建并重启后端容器(保留镜像、数据库和数据卷)"
compose up -d --no-build --no-deps --force-recreate deerflow-backend
wait_http "DeerFlow 后端" \
"http://127.0.0.1:$(generated_value BACKEND_PORT)/health" 90
say "重启前端"
compose up -d --no-build --no-deps --force-recreate deerflow-frontend
wait_http "DeerFlow 页面" \
"http://127.0.0.1:$(generated_value FRONTEND_PORT)/" 45
say "最新源码已生效"
compose ps
}
status_all() {
if [[ ! -f "$GENERATED_ENV" ]]; then
say "尚未执行过 --start"
return 0
fi
compose ps
printf '\nDeerFlow: http://%s:%s\n' "$(generated_value PUBLIC_HOST)" "$(generated_value FRONTEND_PORT)"
}
office_check() {
load_images_if_missing
say "检查本地沙箱的 Word / Excel / PowerPoint / PDF 处理能力"
docker run --rm --network none "$(backend_image)" \
/opt/venv/bin/python /app/backend/scripts/office_runtime_selfcheck.py
}
main() {
local mode="${1:---start}"
require_tools
case "$mode" in
--start) start_all ;;
--stop) stop_all ;;
--restart) restart_all ;;
--status) status_all ;;
--office-check) office_check ;;
--logs)
shift
if [[ $# -gt 0 ]]; then compose logs --tail=200 -f "$1"; else compose logs --tail=200 -f; fi
;;
--help|-h) usage ;;
*) usage; die "未知参数: $mode" ;;
esac
}
main "$@"