Files

VDE 外部服务模板(vde-service 0.1)

P4-13(WP-P4-06 范围外):可直接复制的外部服务模板。模板不加入 VDE 默认构建, 每个模板是独立 CMake/脚本工程;只经 vde-service 文件协议与宿主交互,不链接 VDE 内核/插件 ABI。

目录

路径 内容
cpp/ C++17 echo 服务 + vde-service.json + 独立 CMake(仅标准库)
python/ Python 3 标准库 echo 服务 + 清单 + Windows 原生启动垫片 launcher/
container/ 真实 docker/podman 隔离模板:Dockerfile、容器清单、跨平台 e2e 脚本

本地(无容器)快速验证

# C++ 服务(独立构建,产物 <build>/service/)
cmake -S templates/external-service/cpp -B build/ext-service-cpp -G Ninja
cmake --build build/ext-service-cpp

# 直接进程调用(request/response JSON 文件交换)
# shell 中:
python - <<'PY'
import json, pathlib, subprocess, sys
work = pathlib.Path("build/ext-service-cpp/service")
req = work / "request.json"
req.write_text(json.dumps({
  "protocol": "vde-service/0.1", "request_id": "local-1", "operation": "echo",
  "output": str(work / "output.txt"), "args": {"message": "hello"}}), encoding="utf-8")
rc = subprocess.call([str(work / "vde_service_echo"), "--request", str(req),
                      "--response", str(work / "response.json")])
assert rc == 0, rc
print((work / "response.json").read_text(encoding="utf-8"))
PY

Python 模板在 Windows 上通过 launcher/vde_service_echo_launch.exe 启动(CreateProcess 不能直接执行 .py); POSIX 可直接把 .py 写入清单 executable(需 shebang + 可执行位)。构建/直调说明见各子目录 README。

与宿主/PluginManager 集成

  • 把模板目录(含 vde-service.json 与可执行文件)放入服务搜索目录, 调用 PluginManager::scan_service_directory(dir) 发现;
  • PluginManager::invoke_external_service(service_id, request_json, opts, &result) 完成 工作目录/request.json/response.json/超时 kill/输出白名单校验;
  • 模板 echo 的 service id 为 com.vde.template.echo.cpp / com.vde.template.echo.python, capability 为 service:template.echo;替换业务逻辑时保留协议字段与退出码。

测试与证据

  • 本地 e2e(C++ gtest,CTest test_external_service_templates):构建/发现 C++ 与 Python 模板, 经 PluginManager 验证成功、错误(unsupported_operation)、超时路径;不需要容器;
  • 容器 e2e(CTest VDE_ExternalServiceTemplatesContainerSmoke):仅当 PATH 中存在 docker 或 podman 时构建镜像并运行 container/real_container_e2e.{sh,ps1};否则 SKIP(退出码 77) 并记录“真实容器 e2e 环境阻塞”(本工作树没有容器运行时);
  • 容器要求:Linux 容器模式、可拉取基础镜像(gcc/debian/python)、可运行 --network none/ --read-only/tmpfs/volume;Windows 需 Docker Desktop(Linux containers) 或 podman,PowerShell 7。

已知限制

  • 模板只做 echo 演示,不包含真实算法;生产实现建议使用正式 JSON/schema 校验与结构化日志;
  • 超时由宿主/runner 强制 kill;Python 模板在 Windows 上的垫片杀进程时子 Python 进程不保证级联终止(详见协议 §7.1/§7.3);
  • 真实容器 e2e 需要宿主环境,不能以契约冒烟测试替代隔离验证。