QGIS的docker管理
This commit is contained in:
+203
-169
@@ -1,202 +1,236 @@
|
||||
"""
|
||||
QGIS 环境检测与子进程配置模块。
|
||||
QGIS 环境配置模块 — Docker 模式。
|
||||
|
||||
主进程运行在 Python 3.10,无法直接加载 QGIS 的 Python 3.12 C 扩展。
|
||||
所有 QGIS 操作通过 subprocess 调用 QGIS Python 3.12 执行。
|
||||
所有 QGIS 操作通过 docker exec 在容器内执行,
|
||||
主进程(Python 3.10)无需安装 QGIS 或处理 DLL 加载。
|
||||
|
||||
容器内使用 qgis/qgis 官方镜像(QGIS 3.x + Python 3)。
|
||||
|
||||
本模块提供:
|
||||
- get_qgis_python_path(): 检测 QGIS Python 3.12 解释器路径
|
||||
- build_qgis_subprocess_env(): 构建子进程完整环境变量(替代 bat 包装器)
|
||||
- is_qgis_available(): 检查 QGIS 是否可用
|
||||
- get_runner_script(): 获取 qgis_runner.py 的路径
|
||||
- get_docker_container(): 获取 Docker 容器名称
|
||||
- get_docker_project_dir(): 获取容器内项目根目录
|
||||
- build_docker_exec_cmd(): 构建 docker exec 命令
|
||||
- build_docker_volume_mounts(): 构建 docker run 卷挂载参数
|
||||
- get_runner_script(): 获取 qgis_runner.py 的绝对路径
|
||||
- get_container_python_path(): 获取容器内 Python 解释器路径
|
||||
"""
|
||||
import os
|
||||
import platform
|
||||
from pathlib import Path
|
||||
|
||||
from app.config.paths import get_logger
|
||||
|
||||
logger = get_logger("qgis.env")
|
||||
|
||||
IS_WINDOWS = platform.system() == "Windows"
|
||||
|
||||
def get_qgis_python_path(qgis_root: str = None) -> str | None:
|
||||
|
||||
# ============================================================
|
||||
# Docker 容器配置
|
||||
# ============================================================
|
||||
|
||||
def get_docker_container() -> str:
|
||||
"""获取 Docker 容器名称/ID"""
|
||||
try:
|
||||
from config import settings
|
||||
container = getattr(settings, "QGIS_DOCKER_CONTAINER", "") or ""
|
||||
if container.strip():
|
||||
return container.strip()
|
||||
except Exception:
|
||||
pass
|
||||
# 环境变量覆盖
|
||||
env_container = os.environ.get("QGIS_DOCKER_CONTAINER", "")
|
||||
if env_container.strip():
|
||||
return env_container.strip()
|
||||
return "qgis-server"
|
||||
|
||||
|
||||
def get_docker_project_dir() -> str:
|
||||
"""获取容器内项目根目录(代码挂载目标路径)"""
|
||||
try:
|
||||
from config import settings
|
||||
project_dir = getattr(settings, "QGIS_DOCKER_PROJECT_DIR", "") or ""
|
||||
if project_dir.strip():
|
||||
return project_dir.strip()
|
||||
except Exception:
|
||||
pass
|
||||
env_dir = os.environ.get("QGIS_DOCKER_PROJECT_DIR", "")
|
||||
if env_dir.strip():
|
||||
return env_dir.strip()
|
||||
return "/app"
|
||||
|
||||
|
||||
def get_container_python_path() -> str:
|
||||
"""获取容器内 Python 解释器路径"""
|
||||
try:
|
||||
from config import settings
|
||||
python_path = getattr(settings, "QGIS_DOCKER_PYTHON", "") or ""
|
||||
if python_path.strip():
|
||||
return python_path.strip()
|
||||
except Exception:
|
||||
pass
|
||||
env_path = os.environ.get("QGIS_DOCKER_PYTHON", "")
|
||||
if env_path.strip():
|
||||
return env_path.strip()
|
||||
# qgis/qgis 官方镜像默认路径
|
||||
return "/usr/bin/python3"
|
||||
|
||||
|
||||
# ============================================================
|
||||
# docker exec 命令构建
|
||||
# ============================================================
|
||||
|
||||
def build_docker_exec_cmd(python_path: str, runner_script: str, json_file: str) -> list:
|
||||
"""
|
||||
检测 QGIS 自带的 Python 3.12 解释器路径。
|
||||
构建 docker exec 命令。
|
||||
|
||||
Windows: {QGIS_ROOT}/apps/Python312/python3.exe
|
||||
Args:
|
||||
python_path: 容器内 Python 路径(如 /usr/bin/python3)
|
||||
runner_script: 容器内 runner 脚本路径
|
||||
json_file: 容器内 JSON 请求文件路径
|
||||
|
||||
Returns:
|
||||
解释器绝对路径,不存在则返回 None
|
||||
docker exec 命令列表
|
||||
"""
|
||||
if qgis_root is None:
|
||||
qgis_root = _detect_qgis_root()
|
||||
if qgis_root is None:
|
||||
return None
|
||||
container = get_docker_container()
|
||||
project_dir = get_docker_project_dir()
|
||||
|
||||
import platform
|
||||
if platform.system() == "Windows":
|
||||
for name in ("python3.exe", "python.exe"):
|
||||
candidate = os.path.join(qgis_root, "apps", "Python312", name)
|
||||
if os.path.isfile(candidate):
|
||||
logger.info(f"检测到 QGIS Python: {candidate}")
|
||||
return candidate
|
||||
logger.warning(f"未找到 QGIS Python 3.12")
|
||||
return None
|
||||
else:
|
||||
import shutil
|
||||
# Qt 平台插件(从配置读取,避免硬编码)
|
||||
try:
|
||||
from config import settings
|
||||
qt_platform = getattr(settings, "QGIS_DOCKER_QT_PLATFORM", "offscreen")
|
||||
except Exception:
|
||||
qt_platform = "offscreen"
|
||||
|
||||
# 优先检查环境变量
|
||||
env_python = os.environ.get("QGIS_PYTHON_PATH")
|
||||
if env_python and os.path.isfile(env_python):
|
||||
logger.info(f"QGIS_PYTHON_PATH 指定: {env_python}")
|
||||
return env_python
|
||||
|
||||
# 搜索标准 Linux QGIS 安装路径
|
||||
linux_paths = [
|
||||
"/usr/bin/qgis", # Ubuntu/Debian apt 安装
|
||||
"/usr/bin/qgis.bin",
|
||||
"/opt/QGIS/apps/Python312/bin/python3", # 独立安装器
|
||||
"/opt/QGIS/apps/Python3/bin/python3",
|
||||
"/usr/libexec/qgis/python3", # Fedora/RHEL
|
||||
]
|
||||
for candidate in linux_paths:
|
||||
if os.path.isfile(candidate):
|
||||
logger.info(f"检测到 QGIS 可执行文件: {candidate}")
|
||||
return candidate
|
||||
|
||||
# 回退到系统 Python(如果 qgis.core 可导入)
|
||||
sys_python = shutil.which("python3") or shutil.which("python")
|
||||
if sys_python:
|
||||
logger.info(f"Linux 环境,使用系统 Python: {sys_python}")
|
||||
return sys_python
|
||||
return None
|
||||
cmd = [
|
||||
"docker", "exec",
|
||||
"-w", project_dir,
|
||||
"-e", f"QT_QPA_PLATFORM={qt_platform}",
|
||||
container,
|
||||
python_path, runner_script, json_file,
|
||||
]
|
||||
return cmd
|
||||
|
||||
|
||||
def build_qgis_subprocess_env(qgis_root: str = None) -> dict:
|
||||
# ============================================================
|
||||
# docker run 卷挂载(首次启动容器用)
|
||||
# ============================================================
|
||||
|
||||
def get_host_file_store() -> str:
|
||||
"""获取主机端文件输出目录"""
|
||||
try:
|
||||
from config import settings
|
||||
fs = getattr(settings, "FILE_STORE_DIR", "") or ""
|
||||
if fs.strip():
|
||||
return fs.strip().replace("\\", "/")
|
||||
except Exception:
|
||||
pass
|
||||
return "G:/files"
|
||||
|
||||
|
||||
def get_container_file_store() -> str:
|
||||
"""获取容器内文件输出目录(Linux 路径)"""
|
||||
# 挂载时主机 G:/files → 容器内 /files
|
||||
host = get_host_file_store()
|
||||
# 取最后一段目录名作为容器内路径
|
||||
tail = host.rstrip("/").rsplit("/", 1)[-1]
|
||||
return f"/{tail}"
|
||||
|
||||
|
||||
def map_host_to_container(path: str) -> str:
|
||||
"""将主机路径映射为容器内路径。
|
||||
优先匹配 file_store(G:/files → /files),其次匹配项目根目录(F:/project/... → /app)。
|
||||
"""
|
||||
构建 QGIS Python 3.12 子进程的完整环境变量(替代 bat 包装器)。
|
||||
normalized = path.replace("\\", "/")
|
||||
|
||||
策略:
|
||||
1. 从 os.environ 继承,移除 venv 污染(PYTHONPATH/VIRTUAL_ENV 等)
|
||||
2. 将 QGIS 的 bin 目录前置到 PATH(Qt5→qgis→gdal 顺序,与原 bat 一致)
|
||||
3. 设置 PYTHONPATH / QGIS_PREFIX_PATH / QT_PLUGIN_PATH / GDAL_DATA / PROJ_DATA
|
||||
4. 子进程 _setup_environment() 负责 os.add_dll_directory + ctypes 预加载
|
||||
# 1. file_store 路径映射
|
||||
host_fs = get_host_file_store()
|
||||
container_fs = get_container_file_store()
|
||||
if normalized.lower().startswith(host_fs.lower()):
|
||||
return container_fs + normalized[len(host_fs):]
|
||||
|
||||
# 2. 项目根目录映射
|
||||
project_root = str(Path(__file__).parent.parent.parent.parent).replace("\\", "/")
|
||||
project_dir = get_docker_project_dir()
|
||||
if normalized.lower().startswith(project_root.lower()):
|
||||
return project_dir + normalized[len(project_root):]
|
||||
|
||||
return normalized
|
||||
|
||||
|
||||
def map_container_to_host(path: str) -> str:
|
||||
"""将容器内路径映射回主机路径"""
|
||||
host = get_host_file_store()
|
||||
container = get_container_file_store()
|
||||
if path.startswith(container):
|
||||
return host + path[len(container):]
|
||||
return path
|
||||
|
||||
|
||||
def build_docker_volume_mounts() -> list:
|
||||
"""
|
||||
import platform
|
||||
构建 Docker 卷挂载参数列表(用于 docker run)。
|
||||
|
||||
# 继承当前环境,清理 venv 污染
|
||||
env = dict(os.environ)
|
||||
venv_root = env.get("VIRTUAL_ENV", "").lower()
|
||||
挂载内容:
|
||||
1. 项目代码目录 → 容器内 /app(只读)
|
||||
2. 输出文件目录 → 容器内 /files(读写)
|
||||
"""
|
||||
project_root = str(Path(__file__).parent.parent.parent.parent)
|
||||
host_file_store = get_host_file_store()
|
||||
container_file_store = get_container_file_store()
|
||||
project_dir = get_docker_project_dir()
|
||||
|
||||
for key in (
|
||||
"PYTHONPATH",
|
||||
"PYTHONHOME",
|
||||
"VIRTUAL_ENV",
|
||||
"PYTHONDONTWRITEBYTECODE",
|
||||
):
|
||||
env.pop(key, None)
|
||||
|
||||
if venv_root:
|
||||
path_parts = env.get("PATH", "").split(";")
|
||||
clean = []
|
||||
for p in path_parts:
|
||||
if not p or venv_root in p.lower():
|
||||
continue
|
||||
clean.append(p)
|
||||
env["PATH"] = ";".join(clean)
|
||||
|
||||
# 检测 QGIS 安装路径
|
||||
if qgis_root is None:
|
||||
qgis_root = _detect_qgis_root() or "D:/QGIS"
|
||||
|
||||
env["QGIS_ROOT"] = qgis_root
|
||||
|
||||
if platform.system() != "Windows":
|
||||
return env
|
||||
|
||||
qgis_app_dir = os.path.join(qgis_root, "apps", "qgis-ltr")
|
||||
if not os.path.isdir(qgis_app_dir):
|
||||
qgis_app_dir = os.path.join(qgis_root, "apps", "qgis")
|
||||
if not os.path.isdir(qgis_app_dir):
|
||||
raise RuntimeError(
|
||||
f"未找到 QGIS 应用目录: {qgis_root}\\apps\\qgis-ltr 或 qgis"
|
||||
)
|
||||
|
||||
# 前置 QGIS bin 目录到 PATH(Qt5 必须在最前面,QGIS 依赖它)
|
||||
qgis_bin = os.path.join(qgis_app_dir, "bin")
|
||||
qt5_bin = os.path.join(qgis_root, "apps", "Qt5", "bin")
|
||||
gdal_lib = os.path.join(qgis_root, "apps", "gdal", "lib")
|
||||
qt5_plugins = os.path.join(qgis_root, "apps", "Qt5", "plugins")
|
||||
qtplugins = os.path.join(qgis_app_dir, "qtplugins")
|
||||
qgis_python_dir = os.path.join(qgis_app_dir, "python")
|
||||
|
||||
env["PATH"] = f"{qt5_bin};{qgis_bin};{gdal_lib};{env.get('PATH', '')}"
|
||||
|
||||
# QGIS 核心环境变量(与 bat 包装器保持一致)
|
||||
env["PYTHONPATH"] = qgis_python_dir
|
||||
env["QGIS_PREFIX_PATH"] = qgis_app_dir
|
||||
env["QT_PLUGIN_PATH"] = f"{qtplugins};{qt5_plugins}"
|
||||
|
||||
# GDAL / PROJ 数据目录(避免系统旧版 proj.db 干扰)
|
||||
gdal_data = os.path.join(qgis_root, "apps", "gdal", "share", "gdal")
|
||||
if os.path.isdir(gdal_data):
|
||||
env["GDAL_DATA"] = gdal_data
|
||||
|
||||
for pd in (
|
||||
os.path.join(qgis_app_dir, "share", "proj"),
|
||||
os.path.join(qgis_root, "share", "proj"),
|
||||
os.path.join(qgis_root, "apps", "gdal", "share", "proj"),
|
||||
):
|
||||
if os.path.isfile(os.path.join(pd, "proj.db")):
|
||||
env["PROJ_DATA"] = pd
|
||||
break
|
||||
|
||||
# UTF-8 / GDAL 编码辅助变量
|
||||
env["PYTHONUTF8"] = "1"
|
||||
env["GDAL_FILENAME_IS_UTF8"] = "YES"
|
||||
env["VSI_CACHE"] = "TRUE"
|
||||
env["VSI_CACHE_SIZE"] = "1000000"
|
||||
|
||||
logger.debug(
|
||||
f"QGIS subprocess env built: root={qgis_root}, app={qgis_app_dir}, "
|
||||
f"PATH prefixed with qgis_bin/qt5_bin/gdal_lib"
|
||||
)
|
||||
return env
|
||||
mounts = [
|
||||
f"{project_root}:{project_dir}:ro",
|
||||
f"{host_file_store}:{container_file_store}",
|
||||
]
|
||||
return mounts
|
||||
|
||||
|
||||
def is_qgis_available(qgis_root: str = None) -> bool:
|
||||
"""检查 QGIS 环境是否可用"""
|
||||
return get_qgis_python_path(qgis_root) is not None
|
||||
def build_docker_run_cmd(image: str = None) -> list:
|
||||
"""
|
||||
构建 docker run 启动命令(首次部署用)。
|
||||
|
||||
Returns:
|
||||
docker run 命令列表
|
||||
"""
|
||||
if image is None:
|
||||
try:
|
||||
from config import settings
|
||||
image = getattr(settings, "QGIS_DOCKER_IMAGE", "") or ""
|
||||
if not image.strip():
|
||||
image = "qgis/qgis:latest"
|
||||
except Exception:
|
||||
image = "qgis/qgis:latest"
|
||||
|
||||
# 容器保活命令(从配置读取)
|
||||
try:
|
||||
from config import settings
|
||||
keep_alive = getattr(settings, "QGIS_DOCKER_KEEP_ALIVE", "sleep infinity")
|
||||
except Exception:
|
||||
keep_alive = "sleep infinity"
|
||||
|
||||
container = get_docker_container()
|
||||
mounts = build_docker_volume_mounts()
|
||||
|
||||
cmd = [
|
||||
"docker", "run", "-d",
|
||||
"--name", container,
|
||||
"--restart", "unless-stopped",
|
||||
]
|
||||
for m in mounts:
|
||||
cmd.extend(["-v", m])
|
||||
|
||||
cmd.append(image)
|
||||
# 保活命令
|
||||
cmd.extend(keep_alive.split())
|
||||
return cmd
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Runner 脚本路径
|
||||
# ============================================================
|
||||
|
||||
def get_runner_script() -> str:
|
||||
"""获取 qgis_runner.py 的绝对路径"""
|
||||
"""获取 qgis_runner.py 的绝对路径(主机端)"""
|
||||
return str(Path(__file__).parent / "qgis_runner.py")
|
||||
|
||||
|
||||
def _detect_qgis_root() -> str | None:
|
||||
"""
|
||||
自动检测 QGIS 安装根目录。
|
||||
|
||||
优先级:
|
||||
1. 环境变量 QGIS_ROOT
|
||||
2. Windows 默认路径 D:/QGIS
|
||||
3. Linux 常见路径
|
||||
"""
|
||||
env_root = os.environ.get("QGIS_ROOT")
|
||||
if env_root and os.path.isdir(env_root):
|
||||
return env_root
|
||||
|
||||
import platform
|
||||
if platform.system() == "Windows":
|
||||
for candidate in ["D:/QGIS", "C:/OSGeo4W", "C:/QGIS"]:
|
||||
if os.path.isdir(candidate):
|
||||
logger.info(f"检测到 QGIS 根目录: {candidate}")
|
||||
return candidate
|
||||
else:
|
||||
for candidate in ["/usr", "/opt/QGIS", "/home/QGIS"]:
|
||||
if os.path.isdir(candidate):
|
||||
logger.info(f"检测到 QGIS 根目录: {candidate}")
|
||||
return candidate
|
||||
|
||||
logger.warning("未检测到 QGIS 安装目录")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user