合并远端并行的 worker unit 渲染实现
Project CI / Backend tests (pull_request) Failing after 11s
Project CI / Repository checks (pull_request) Failing after 12s
Project CI / Frontend tests (pull_request) Failing after 1m12s
Project CI / Native shell tests (pull_request) Successful in 2m4s

远端 798098e06 与本地 299544719 并行修复了同一问题(deploy 原文安装
unit 覆盖自定义路径)。合并采纳远端实现为准:python3 字面量渲染(含
模板占位路径存在性校验)、install_worker_systemd_units 参数化、Jenkins
双流水线暴露并透传 CONTROLLER/BGFILTER env 参数、guardrails 门禁——
其中流水线透传补上了本地版本刻意未覆盖的传输层缺口。保留本地独有的
readyz 派生修复、connect 重连与冷启动宽限、契约文档收口;删除本地
sed 渲染实现与合并产生的重复参数定义;decision-log 双条目并存。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-23 06:47:27 +00:00
8 changed files with 272 additions and 114 deletions
@@ -4426,3 +4426,10 @@
- 平台差异(Windows 开发环境):连接已关闭的 loopback 端口不回 RST 而是挂到 connect timeout,错误呈现为 `deadline_exceeded``is_connect` 为真;重连判定只看 connect 分类,不看错误码。生产 Linux 即时拒绝,呈现 `internal_error`
- 安全不变量测试:除配额 / 跨窗 / deadline 地板路径外,专项覆盖「TCP 已 accept、未回任何 HTTP 字节即断开 → 不得发起第二次连接」,以 mock listener 的 accept 计数证明父侧未重连。
- 影响范围:`server-rs/crates/api-server/src/bgfilter_worker.rs``state.rs``editor_project.rs`、调度方案 §5.1/§7/§9.3/§11、数据契约 247-248 行、运维文档及各编辑器专题文档的旧禁令措辞统一改为「不重试已被 worker 接收的内部 RPC」。
## 2026-07-23 生产 API 发布按实际路径渲染 worker systemd unit
- 背景:Server-Provision 支持自定义 current link、API env 和角色 env,并在首次安装时渲染三个 worker unitAPI deploy 为下发随 release 更新的 unit 又原样覆盖目标机配置,导致自定义路径在下一次发布时退回模板默认值。
- 决策:`production-api-deploy.sh` 继续随 release 安装默认命名的 BgFilter、external-generation worker 和 controller unit,但安装前必须用本次部署参数渲染临时文件;新增 `--controller-env-file` 补齐 controller 专属 env 输入。release 内模板保持默认路径,供 provision 和 deploy 共同作为单一模板来源;自定义服务名仍由目标机自行管理,不强制覆盖。
- 影响范围:`scripts/deploy/production-api-deploy.sh``scripts/check-production-api-deploy.mjs``jenkins/Jenkinsfile.production-api-deploy``jenkins/Jenkinsfile.production-full-build-and-deploy``scripts/check-production-ops-guardrails.mjs`、生产运维文档和 worker systemd 发布契约。
- 验证方式:`bash -n scripts/deploy/production-api-deploy.sh``node --check scripts/check-production-api-deploy.mjs``npm run check:production-api-deploy``npm run check:production-ops``npm run check:encoding``git diff --check`
@@ -3312,3 +3312,11 @@
- 验证与回滚:重启后先跑真实 PR 的四个 job,再清理旧镜像。失败时先把 workflow `runs-on` 改回 `ubuntu-latest`,再恢复 runner config 备份并重启;不在 Git、共享文档或日志中记录 config 备份路径、注册信息或 token。
- 重启边界:`docker restart --timeout 660` 只设置容器停止宽限,不能替代 Runner drain。rootless DinD supervisor 可能与 runner 同时停止内层 dockerd,使仍在收尾的 job 因连接关闭被标记失败;切换前必须同时确认 Gitea 没有 `in_progress` run 且内层 `docker ps` 为空。误触发时只重跑受影响的失败 job,不重跑已成功项。
- 关联:`deploy/container/README.md``docs/【开发运维】本地开发验证与生产运维-2026-05-15.md``docs/project-memory/shared-memory/development-workflow.md`
## 生产 API 发布重装 worker unit 不能丢失自定义路径(2026-07-23)
- 现象:Server-Provision 已按自定义 current link 和 env 路径安装 worker systemd unit,但下一次 API 发布后,worker 可能重新读取 `/opt/genarrative/current``/etc/genarrative/*.env`;默认路径仍有旧 release 时,服务 active 和部署成功都不能证明新二进制已运行。
- 原因:发布包中的 BgFilter、external-generation worker 和 controller unit 是带默认路径的模板;deploy 若直接 `install` 原文件,会覆盖 provision 已渲染的目标机 unit。external-generation 专属 env 还是可选加载,错误路径可能不会阻止服务进入 active。
- 处理:API deploy 安装三个 unit 前必须按本次 current、API env 和各角色 env 参数渲染临时文件,安装后保留 release 内原始模板不变;controller 自定义 env 由 `--controller-env-file` 显式传入。API Deploy 与 Full Job 必须同步暴露并透传 controller/BgFilter env,不能让流水线回退默认路径。自定义服务名表示沿用目标机自管 unit,不进入默认 unit 安装分支。
- 验证:部署 guard 使用临时自定义绝对路径,直接读取实际安装目录中的三个 unit,核对 `WorkingDirectory``ExecStart`、共享 API env 与角色 env,不能只用 fake `systemctl is-active` 判绿。
- 关联:`scripts/deploy/production-api-deploy.sh``scripts/check-production-api-deploy.mjs``scripts/jenkins-server-provision.sh``docs/【开发运维】本地开发验证与生产运维-2026-05-15.md`
File diff suppressed because one or more lines are too long
+5 -1
View File
@@ -24,6 +24,8 @@ pipeline {
string(name: 'HEALTH_URL', defaultValue: 'http://127.0.0.1:8082/readyz', description: '本机 readiness 检查地址')
string(name: 'API_ENV_FILE', defaultValue: '/etc/genarrative/api-server.env', description: 'api-server 环境文件')
string(name: 'WORKER_ENV_FILE', defaultValue: '/etc/genarrative/external-generation-worker.env', description: '外部生成 worker 环境文件')
string(name: 'CONTROLLER_ENV_FILE', defaultValue: '/etc/genarrative/external-generation-controller.env', description: '外部生成 controller 环境文件')
string(name: 'BGFILTER_WORKER_ENV_FILE', defaultValue: '/etc/genarrative/bgfilter-worker.env', description: 'BgFilter worker 环境文件')
string(name: 'DATABASE', defaultValue: 'genarrative-prod', description: 'api-server 连接的 SpacetimeDB database')
string(name: 'SPACETIME_SERVER_URL', defaultValue: 'http://127.0.0.1:3101', description: 'api-server 连接的 SpacetimeDB server URL')
}
@@ -53,7 +55,7 @@ pipeline {
if (!(params.DATABASE.trim() ==~ /^[a-z0-9]+(-[a-z0-9]+)*$/)) {
error("DATABASE 必须匹配 ^[a-z0-9]+(-[a-z0-9]+)*\$: ${params.DATABASE}")
}
[API_ENV_FILE: params.API_ENV_FILE, WORKER_ENV_FILE: params.WORKER_ENV_FILE].each { name, value ->
[API_ENV_FILE: params.API_ENV_FILE, WORKER_ENV_FILE: params.WORKER_ENV_FILE, CONTROLLER_ENV_FILE: params.CONTROLLER_ENV_FILE, BGFILTER_WORKER_ENV_FILE: params.BGFILTER_WORKER_ENV_FILE].each { name, value ->
def envFile = value?.trim()
if (!envFile || envFile == '/' || !(envFile ==~ /^\/(?!.*\.\.)[A-Za-z0-9._\/-]+$/)) {
error("${name} 必须是 Linux 绝对路径且不能包含 ..: ${envFile}")
@@ -117,6 +119,8 @@ pipeline {
--health-url "${HEALTH_URL}" \
--api-env-file "${API_ENV_FILE:-/etc/genarrative/api-server.env}" \
--worker-env-file "${WORKER_ENV_FILE:-/etc/genarrative/external-generation-worker.env}" \
--controller-env-file "${CONTROLLER_ENV_FILE:-/etc/genarrative/external-generation-controller.env}" \
--bgfilter-worker-env-file "${BGFILTER_WORKER_ENV_FILE:-/etc/genarrative/bgfilter-worker.env}" \
--database "${DATABASE}" \
--spacetime-server-url "${SPACETIME_SERVER_URL:-http://127.0.0.1:3101}"
'
@@ -41,6 +41,8 @@ pipeline {
string(name: 'SPACETIME_RUN_AS_USER', defaultValue: 'spacetimedb', description: 'Stdb 发布使用的本机用户')
string(name: 'API_ENV_FILE', defaultValue: '/etc/genarrative/api-server.env', description: 'API 与 Stdb publish 共用的 api-server 环境文件')
string(name: 'WORKER_ENV_FILE', defaultValue: '/etc/genarrative/external-generation-worker.env', description: 'API 与 Stdb publish 共用的 worker 环境文件')
string(name: 'CONTROLLER_ENV_FILE', defaultValue: '/etc/genarrative/external-generation-controller.env', description: 'API deploy 使用的外部生成 controller 环境文件')
string(name: 'BGFILTER_WORKER_ENV_FILE', defaultValue: '/etc/genarrative/bgfilter-worker.env', description: 'API deploy 使用的 BgFilter worker 环境文件')
choice(name: 'STDB_API_ROLLOUT_MODE', choices: ['normal', 'pause-after-stdb'], description: '定时任务默认 normal 完整发布 dev;人工维护窗口才选择 pause-after-stdb')
string(name: 'STDB_API_ROLLOUT_APPROVERS', defaultValue: '', description: 'pause-after-stdb 必填:允许放行 API 的 Jenkins 用户/组,多个值用逗号分隔')
booleanParam(name: 'EXIT_MAINTENANCE_MODE_AFTER_COMPLETION', defaultValue: true, description: '完整发布成功后是否退出维护模式;取消勾选会保留维护页,便于内网验收后人工恢复公网')
@@ -233,6 +235,8 @@ pipeline {
booleanParam(name: 'KEEP_MAINTENANCE_MODE', value: true),
string(name: 'API_ENV_FILE', value: params.API_ENV_FILE ?: '/etc/genarrative/api-server.env'),
string(name: 'WORKER_ENV_FILE', value: params.WORKER_ENV_FILE ?: '/etc/genarrative/external-generation-worker.env'),
string(name: 'CONTROLLER_ENV_FILE', value: params.CONTROLLER_ENV_FILE ?: '/etc/genarrative/external-generation-controller.env'),
string(name: 'BGFILTER_WORKER_ENV_FILE', value: params.BGFILTER_WORKER_ENV_FILE ?: '/etc/genarrative/bgfilter-worker.env'),
string(name: 'DATABASE', value: params.DATABASE),
string(name: 'SPACETIME_SERVER_URL', value: params.SPACETIME_SERVER_URL ?: ''),
]
+91 -61
View File
@@ -352,63 +352,87 @@ function assertDeployCopiesPingoraDirectReleaseDependencies() {
),
'API deploy 必须把随包 BgFilter worker 单元安装到 systemd unit 目录。',
);
// 安装的 unit 必须按本次部署参数渲染(fixture 全程使用自定义路径):
// 含 fixture 的 current 链接与 env 路径,不得残留模板默认字面量——
// 否则自定义 --current-link/env 部署会让服务启动旧二进制或加载默认配置。
const renderedUnits = [
['genarrative-bgfilter-worker.service', fixture.bgfilterWorkerEnvFile],
[
const externalGenerationWorkerUnit = readFileSync(
path.join(
fixture.systemdUnitDir,
'genarrative-external-generation-worker@.service',
fixture.externalGenerationWorkerEnvFile,
],
[
),
'utf8',
);
const externalGenerationControllerUnit = readFileSync(
path.join(
fixture.systemdUnitDir,
'genarrative-external-generation-controller.service',
fixture.controllerEnvFile,
],
];
for (const [unitName, roleEnvFile] of renderedUnits) {
const unitContent = readFileSync(
path.join(fixture.systemdUnitDir, unitName),
'utf8',
);
assertIncludes(
unitContent,
`WorkingDirectory=${fixture.currentLink}`,
`${unitName} 的工作目录必须渲染为 --current-link。`,
);
assertIncludes(
unitContent,
`${fixture.currentLink}/api-server`,
`${unitName} 的 ExecStart 必须指向 --current-link 下的二进制。`,
);
assertIncludes(
unitContent,
`EnvironmentFile=${fixture.apiEnvFile}`,
`${unitName} 必须加载 --api-env-file 指定的共享 env。`,
);
assertIncludes(
unitContent,
roleEnvFile,
`${unitName} 必须加载本角色 env 参数指定的文件。`,
);
for (const templateDefault of [
'/opt/genarrative/current',
'/etc/genarrative/api-server.env',
'/etc/genarrative/bgfilter-worker.env',
'/etc/genarrative/external-generation-worker.env',
'/etc/genarrative/external-generation-controller.env',
]) {
if (unitContent.includes(templateDefault)) {
failures.push(
`${unitName} 安装后不得残留模板默认路径 ${templateDefault}unit 必须按部署参数渲染。`,
);
}
}
}
),
'utf8',
);
const bgfilterUnit = readFileSync(
path.join(fixture.systemdUnitDir, 'genarrative-bgfilter-worker.service'),
'utf8',
);
for (const [label, unit] of [
['外部生成 worker unit', externalGenerationWorkerUnit],
['外部生成 controller unit', externalGenerationControllerUnit],
['BgFilter worker unit', bgfilterUnit],
]) {
assertIncludes(
unit,
`WorkingDirectory=${fixture.currentLink}`,
`${label} 必须使用本次部署的自定义 current link。`,
);
assertIncludes(
unit,
`${fixture.currentLink}/api-server`,
`${label} 必须启动本次部署的自定义 current link 下的二进制。`,
);
assertIncludes(
unit,
`EnvironmentFile=${fixture.apiEnvFile}`,
`${label} 必须加载本次部署指定的 API env。`,
);
assertNotIncludes(
unit,
'/opt/genarrative/current',
`${label} 安装后不得继续引用模板默认 current link。`,
);
assertNotIncludes(
unit,
'/etc/genarrative/api-server.env',
`${label} 安装后不得继续引用模板默认 API env。`,
);
}
assertIncludes(
externalGenerationWorkerUnit,
`EnvironmentFile=-${fixture.externalGenerationWorkerEnvFile}`,
'外部生成 worker unit 必须加载本次部署指定的专属 env。',
);
assertNotIncludes(
externalGenerationWorkerUnit,
'/etc/genarrative/external-generation-worker.env',
'外部生成 worker unit 安装后不得继续引用模板默认专属 env。',
);
assertIncludes(
externalGenerationControllerUnit,
`EnvironmentFile=-${fixture.externalGenerationControllerEnvFile}`,
'外部生成 controller unit 必须加载本次部署指定的专属 env。',
);
assertNotIncludes(
externalGenerationControllerUnit,
'/etc/genarrative/external-generation-controller.env',
'外部生成 controller unit 安装后不得继续引用模板默认专属 env。',
);
assertIncludes(
bgfilterUnit,
`EnvironmentFile=${fixture.bgfilterWorkerEnvFile}`,
'BgFilter unit 必须加载本次部署指定的专属 env。',
);
assertNotIncludes(
bgfilterUnit,
'/etc/genarrative/bgfilter-worker.env',
'BgFilter unit 安装后不得继续引用模板默认专属 env。',
);
const sharedEnvIndex = bgfilterUnit.indexOf(
`EnvironmentFile=${fixture.apiEnvFile}`,
);
@@ -2046,16 +2070,16 @@ function prepareFixture(name) {
'etc',
'external-generation-worker.env',
);
const externalGenerationControllerEnvFile = path.join(
root,
'etc',
'external-generation-controller.env',
);
const bgfilterWorkerEnvFile = path.join(
root,
'etc',
'bgfilter-worker.env',
);
const controllerEnvFile = path.join(
root,
'etc',
'external-generation-controller.env',
);
const bgfilterTokenFile = path.join(root, 'etc', 'bgfilter-worker.token');
const pingoraEnvFile = path.join(root, 'etc', 'pingora-gateway.env');
const maintenanceFile = path.join(root, 'maintenance', 'enabled');
@@ -2102,8 +2126,8 @@ function prepareFixture(name) {
'utf8',
);
writeFileSync(
controllerEnvFile,
'GENARRATIVE_EXTERNAL_GENERATION_CONTROLLER_PLACEHOLDER=1\n',
externalGenerationControllerEnvFile,
'GENARRATIVE_EXTERNAL_GENERATION_CONTROLLER_MAX_WORKERS=4\n',
'utf8',
);
writeFileSync(
@@ -2461,8 +2485,8 @@ function prepareFixture(name) {
currentLink,
apiEnvFile,
externalGenerationWorkerEnvFile,
externalGenerationControllerEnvFile,
bgfilterWorkerEnvFile,
controllerEnvFile,
bgfilterTokenFile,
pingoraEnvFile,
maintenanceFile,
@@ -2538,10 +2562,10 @@ function runDeploy(fixture, options = {}) {
options.apiEnvFile ?? fixture.apiEnvFile,
'--worker-env-file',
fixture.externalGenerationWorkerEnvFile,
'--controller-env-file',
fixture.externalGenerationControllerEnvFile,
'--bgfilter-worker-env-file',
fixture.bgfilterWorkerEnvFile,
'--controller-env-file',
options.controllerEnvFile ?? fixture.controllerEnvFile,
// 缺省不传 --bgfilter-worker-health-url,与真实 Jenkins 调用一致:
// 让预检从已校验的 worker env HOST/PORT 派生 readiness URL。
...(options.bgfilterWorkerHealthUrl === undefined
@@ -2663,6 +2687,12 @@ function assertIncludes(content, needle, reason) {
}
}
function assertNotIncludes(content, needle, reason) {
if (content.includes(needle)) {
failures.push(`${reason} 不应包含: ${needle}`);
}
}
function assertPingoraEnvProductionDefaults(filePath, label) {
if (!existsSync(filePath)) {
return;
@@ -233,6 +233,12 @@ const checks = [
reason:
'API readiness 单次请求必须有超时,避免端口已建立但服务尚未响应时绕过重试上限无限挂起。',
},
{
file: 'scripts/deploy/production-api-deploy.sh',
includes:
'on_exit() {\n local exit_code=$?\n cleanup_rendered_systemd_unit',
reason: 'API deploy 被中断时必须清理尚未安装完成的临时 systemd unit。',
},
{
file: 'jenkins/Jenkinsfile.production-api-deploy',
includes:
@@ -244,6 +250,54 @@ const checks = [
includes: 'maintenance_deploy_args+=(--keep-maintenance-mode)',
reason: 'API Deploy Job 必须把保持维护参数传给发布产物内的部署脚本。',
},
{
file: 'jenkins/Jenkinsfile.production-api-deploy',
includes:
"string(name: 'CONTROLLER_ENV_FILE', defaultValue: '/etc/genarrative/external-generation-controller.env'",
reason: 'API Deploy Job 必须暴露外部生成 controller env 路径。',
},
{
file: 'jenkins/Jenkinsfile.production-api-deploy',
includes:
"string(name: 'BGFILTER_WORKER_ENV_FILE', defaultValue: '/etc/genarrative/bgfilter-worker.env'",
reason: 'API Deploy Job 必须暴露 BgFilter worker env 路径。',
},
{
file: 'jenkins/Jenkinsfile.production-api-deploy',
includes:
'--controller-env-file "${CONTROLLER_ENV_FILE:-/etc/genarrative/external-generation-controller.env}"',
reason: 'API Deploy Job 必须把 controller env 路径传给发布脚本。',
},
{
file: 'jenkins/Jenkinsfile.production-api-deploy',
includes:
'--bgfilter-worker-env-file "${BGFILTER_WORKER_ENV_FILE:-/etc/genarrative/bgfilter-worker.env}"',
reason: 'API Deploy Job 必须把 BgFilter worker env 路径传给发布脚本。',
},
{
file: 'jenkins/Jenkinsfile.production-full-build-and-deploy',
includes:
"string(name: 'CONTROLLER_ENV_FILE', defaultValue: '/etc/genarrative/external-generation-controller.env'",
reason: 'Full Job 必须暴露外部生成 controller env 路径。',
},
{
file: 'jenkins/Jenkinsfile.production-full-build-and-deploy',
includes:
"string(name: 'BGFILTER_WORKER_ENV_FILE', defaultValue: '/etc/genarrative/bgfilter-worker.env'",
reason: 'Full Job 必须暴露 BgFilter worker env 路径。',
},
{
file: 'jenkins/Jenkinsfile.production-full-build-and-deploy',
includes:
"string(name: 'CONTROLLER_ENV_FILE', value: params.CONTROLLER_ENV_FILE ?: '/etc/genarrative/external-generation-controller.env')",
reason: 'Full Job 必须把 controller env 路径传给 API Deploy Job。',
},
{
file: 'jenkins/Jenkinsfile.production-full-build-and-deploy',
includes:
"string(name: 'BGFILTER_WORKER_ENV_FILE', value: params.BGFILTER_WORKER_ENV_FILE ?: '/etc/genarrative/bgfilter-worker.env')",
reason: 'Full Job 必须把 BgFilter worker env 路径传给 API Deploy Job。',
},
{
file: 'scripts/jenkins-server-provision.sh',
includes: 'ensure_runtime_bootstrap_secret_file_env',
+101 -50
View File
@@ -5,7 +5,7 @@ set -euo pipefail
usage() {
cat <<'EOF'
用法:
./scripts/deploy/production-api-deploy.sh --source-dir build/<version> [--version <version>] [--release-root /opt/genarrative/releases] [--current-link /opt/genarrative/current] [--service genarrative-api.service] [--pingora-service genarrative-pingora-gateway.service] [--require-pingora-gateway] [--bgfilter-worker-service genarrative-bgfilter-worker.service] [--bgfilter-worker-health-url <url>] [--bgfilter-worker-env-file /etc/genarrative/bgfilter-worker.env] [--no-bgfilter-worker] [--worker-service-pattern 'genarrative-external-generation-worker@*.service'] [--no-worker-services] [--worker-controller-service genarrative-external-generation-controller.service] [--no-worker-controller] [--health-url http://127.0.0.1:8082/readyz] [--api-env-file /etc/genarrative/api-server.env] [--worker-env-file /etc/genarrative/external-generation-worker.env] [--controller-env-file /etc/genarrative/external-generation-controller.env] [--database genarrative-prod] [--spacetime-server-url http://127.0.0.1:3101] [--keep-maintenance-mode]
./scripts/deploy/production-api-deploy.sh --source-dir build/<version> [--version <version>] [--release-root /opt/genarrative/releases] [--current-link /opt/genarrative/current] [--service genarrative-api.service] [--pingora-service genarrative-pingora-gateway.service] [--require-pingora-gateway] [--bgfilter-worker-service genarrative-bgfilter-worker.service] [--bgfilter-worker-health-url <url>] [--bgfilter-worker-env-file /etc/genarrative/bgfilter-worker.env] [--no-bgfilter-worker] [--worker-service-pattern 'genarrative-external-generation-worker@*.service'] [--no-worker-services] [--worker-controller-service genarrative-external-generation-controller.service] [--controller-env-file /etc/genarrative/external-generation-controller.env] [--no-worker-controller] [--health-url http://127.0.0.1:8082/readyz] [--api-env-file /etc/genarrative/api-server.env] [--worker-env-file /etc/genarrative/external-generation-worker.env] [--database genarrative-prod] [--spacetime-server-url http://127.0.0.1:3101] [--keep-maintenance-mode]
说明:
进入维护模式,校验并发布 api-server 单文件,更新 current 链接,重启 systemd 服务并执行 readiness 检查。
@@ -37,6 +37,10 @@ require_absolute_path() {
echo "[production-api-deploy] ${label} 必须使用绝对路径: ${value}" >&2
exit 1
fi
if [[ "${value}" == *$'\n'* || "${value}" == *$'\r'* ]]; then
echo "[production-api-deploy] ${label} 不能包含换行符。" >&2
exit 1
fi
}
validate_spacetime_database_name() {
@@ -897,21 +901,11 @@ ensure_default_worker_service() {
systemctl enable --now "${default_service}"
}
escape_sed_replacement() {
printf "%s" "$1" | sed "s/[&|]/\\\\&/g"
}
# 与 provision 的 render_*_service 同语义:unit 文件在仓库里是模板,安装前必须把
# 默认 current 链接与 env 路径渲染为本次部署参数,自定义 --current-link/env 时
# 不得让服务启动旧二进制或加载默认 env(默认参数下渲染输出与模板逐字节一致)。
install_release_systemd_unit() {
local source_path="$1"
local unit_name="$2"
local label="$3"
shift 3
local unit_dir="${GENARRATIVE_SYSTEMD_UNIT_DIR:-/etc/systemd/system}"
local sed_args=()
local template_default replacement rendered
if [[ ! -f "${source_path}" ]]; then
echo "[production-api-deploy] 发布产物缺少${label}: ${source_path}" >&2
@@ -921,27 +915,61 @@ install_release_systemd_unit() {
echo "[production-api-deploy] systemd unit 目录必须使用绝对路径: ${unit_dir}" >&2
return 1
fi
while [[ $# -gt 0 ]]; do
template_default="$1"
replacement="${2:?unit 渲染替换必须成对提供模板默认值与实际值}"
shift 2
if [[ -z "${replacement}" ]]; then
echo "[production-api-deploy] ${label} 渲染值不能为空: ${template_default}" >&2
return 1
fi
sed_args+=(-e "s|${template_default}|$(escape_sed_replacement "${replacement}")|g")
done
rendered="$(mktemp)"
if [[ "${#sed_args[@]}" -gt 0 ]]; then
sed "${sed_args[@]}" "${source_path}" > "${rendered}"
else
cat "${source_path}" > "${rendered}"
fi
echo "[production-api-deploy] 安装${label}: ${unit_name}"
run_privileged install -d -m 0755 "${unit_dir}"
run_privileged install -m 0644 "${rendered}" "${unit_dir}/${unit_name}"
rm -f "${rendered}"
if ! run_privileged install -d -m 0755 "${unit_dir}"; then
return 1
fi
run_privileged install -m 0644 "${source_path}" "${unit_dir}/${unit_name}"
}
render_and_install_release_systemd_unit() {
local source_path="$1"
local unit_name="$2"
local label="$3"
local rendered_path
local install_status=0
shift 3
if [[ ! -f "${source_path}" ]]; then
echo "[production-api-deploy] 发布产物缺少${label}: ${source_path}" >&2
return 1
fi
if [[ "$#" -eq 0 || $(( $# % 2 )) -ne 0 ]]; then
echo "[production-api-deploy] ${label} 缺少成对的模板路径和实际路径。" >&2
return 1
fi
RENDERED_SYSTEMD_UNIT_FILE="$(mktemp)"
rendered_path="${RENDERED_SYSTEMD_UNIT_FILE}"
if ! python3 - "${source_path}" "${rendered_path}" "$@" <<'PY'
import re
import sys
from pathlib import Path
source_path = Path(sys.argv[1])
rendered_path = Path(sys.argv[2])
replacements = sys.argv[3:]
content = source_path.read_text(encoding="utf-8")
replacement_map = dict(zip(replacements[::2], replacements[1::2]))
for template_path in replacement_map:
if template_path not in content:
raise SystemExit(f"systemd unit 模板缺少占位路径: {template_path}")
pattern = re.compile("|".join(re.escape(path) for path in replacement_map))
content = pattern.sub(lambda match: replacement_map[match.group(0)], content)
rendered_path.write_text(content, encoding="utf-8")
PY
then
cleanup_rendered_systemd_unit
return 1
fi
install_release_systemd_unit "${rendered_path}" "${unit_name}" "${label}" || install_status=$?
cleanup_rendered_systemd_unit
return "${install_status}"
}
install_worker_systemd_units() {
@@ -949,38 +977,43 @@ install_worker_systemd_units() {
local pattern="$2"
local controller_service="$3"
local bgfilter_service="$4"
local current_link="$5"
local api_env_file="$6"
local worker_env_file="$7"
local controller_env_file="$8"
local bgfilter_env_file="$9"
local installed_any=0
if [[ "${bgfilter_service}" == "genarrative-bgfilter-worker.service" ]]; then
install_release_systemd_unit \
render_and_install_release_systemd_unit \
"${release_dir}/deploy/systemd/genarrative-bgfilter-worker.service" \
"genarrative-bgfilter-worker.service" \
"BgFilter worker systemd 单元" \
"/opt/genarrative/current" "${CURRENT_LINK}" \
"/etc/genarrative/api-server.env" "${API_ENV_FILE}" \
"/etc/genarrative/bgfilter-worker.env" "${BGFILTER_WORKER_ENV_FILE}"
"/opt/genarrative/current" "${current_link}" \
"/etc/genarrative/api-server.env" "${api_env_file}" \
"/etc/genarrative/bgfilter-worker.env" "${bgfilter_env_file}"
installed_any=1
fi
if [[ "${pattern}" == "genarrative-external-generation-worker@*.service" ]]; then
install_release_systemd_unit \
render_and_install_release_systemd_unit \
"${release_dir}/deploy/systemd/genarrative-external-generation-worker@.service" \
"genarrative-external-generation-worker@.service" \
"外部生成 worker systemd 模板" \
"/opt/genarrative/current" "${CURRENT_LINK}" \
"/etc/genarrative/api-server.env" "${API_ENV_FILE}" \
"/etc/genarrative/external-generation-worker.env" "${WORKER_ENV_FILE}"
"/opt/genarrative/current" "${current_link}" \
"/etc/genarrative/api-server.env" "${api_env_file}" \
"/etc/genarrative/external-generation-worker.env" "${worker_env_file}"
installed_any=1
fi
if [[ "${controller_service}" == "genarrative-external-generation-controller.service" ]]; then
install_release_systemd_unit \
render_and_install_release_systemd_unit \
"${release_dir}/deploy/systemd/genarrative-external-generation-controller.service" \
"genarrative-external-generation-controller.service" \
"外部生成 worker controller systemd 单元" \
"/opt/genarrative/current" "${CURRENT_LINK}" \
"/etc/genarrative/api-server.env" "${API_ENV_FILE}" \
"/etc/genarrative/external-generation-controller.env" "${CONTROLLER_ENV_FILE}"
"/opt/genarrative/current" "${current_link}" \
"/etc/genarrative/api-server.env" "${api_env_file}" \
"/etc/genarrative/external-generation-controller.env" "${controller_env_file}"
installed_any=1
fi
@@ -1143,6 +1176,7 @@ MAINTENANCE_FILE="${GENARRATIVE_MAINTENANCE_FILE:-/var/lib/genarrative/maintenan
CURRENT_LINK_SWITCHED=0
RELEASE_DIR=""
STAGING_RELEASE_DIR=""
RENDERED_SYSTEMD_UNIT_FILE=""
while [[ $# -gt 0 ]]; do
case "$1" in
@@ -1194,6 +1228,10 @@ while [[ $# -gt 0 ]]; do
WORKER_CONTROLLER_SERVICE="${2:?缺少 --worker-controller-service 的值}"
shift 2
;;
--controller-env-file)
CONTROLLER_ENV_FILE="${2:?缺少 --controller-env-file 的值}"
shift 2
;;
--no-worker-controller)
WORKER_CONTROLLER_SERVICE=""
shift
@@ -1227,10 +1265,6 @@ while [[ $# -gt 0 ]]; do
WORKER_ENV_FILE="${2:?缺少 --worker-env-file 的值}"
shift 2
;;
--controller-env-file)
CONTROLLER_ENV_FILE="${2:?缺少 --controller-env-file 的值}"
shift 2
;;
--database)
DATABASE="${2:?缺少 --database 的值}"
shift 2
@@ -1254,12 +1288,12 @@ require_absolute_path "${API_ENV_FILE}" "--api-env-file"
if [[ -n "${WORKER_ENV_FILE}" ]]; then
require_absolute_path "${WORKER_ENV_FILE}" "--worker-env-file"
fi
if [[ -n "${BGFILTER_WORKER_ENV_FILE}" ]]; then
require_absolute_path "${BGFILTER_WORKER_ENV_FILE}" "--bgfilter-worker-env-file"
fi
if [[ -n "${CONTROLLER_ENV_FILE}" ]]; then
require_absolute_path "${CONTROLLER_ENV_FILE}" "--controller-env-file"
fi
if [[ -n "${BGFILTER_WORKER_ENV_FILE}" ]]; then
require_absolute_path "${BGFILTER_WORKER_ENV_FILE}" "--bgfilter-worker-env-file"
fi
if [[ -n "${DATABASE}" ]]; then
validate_spacetime_database_name "${DATABASE}"
@@ -1312,8 +1346,16 @@ cleanup_staging_release() {
fi
}
cleanup_rendered_systemd_unit() {
if [[ -n "${RENDERED_SYSTEMD_UNIT_FILE:-}" && -f "${RENDERED_SYSTEMD_UNIT_FILE}" ]]; then
rm -f "${RENDERED_SYSTEMD_UNIT_FILE}"
fi
RENDERED_SYSTEMD_UNIT_FILE=""
}
on_exit() {
local exit_code=$?
cleanup_rendered_systemd_unit
if [[ "${exit_code}" -ne 0 && "${DEPLOY_COMPLETED}" -ne 1 ]]; then
cleanup_staging_release
if [[ "${MAINTENANCE_ENABLED_BY_DEPLOY}" -eq 1 && "${CURRENT_LINK_SWITCHED}" -ne 1 ]]; then
@@ -1620,7 +1662,16 @@ if [[ "${PINGORA_INCLUDED}" -eq 1 ]]; then
ensure_pingora_shadow_service "${PINGORA_SERVICE_NAME}" "${PINGORA_SHADOW_ENV_FILE}"
fi
install_worker_systemd_units "${RELEASE_DIR}" "${WORKER_SERVICE_PATTERN}" "${WORKER_CONTROLLER_SERVICE}" "${BGFILTER_WORKER_SERVICE}"
install_worker_systemd_units \
"${RELEASE_DIR}" \
"${WORKER_SERVICE_PATTERN}" \
"${WORKER_CONTROLLER_SERVICE}" \
"${BGFILTER_WORKER_SERVICE}" \
"${CURRENT_LINK}" \
"${API_ENV_FILE}" \
"${WORKER_ENV_FILE}" \
"${CONTROLLER_ENV_FILE}" \
"${BGFILTER_WORKER_ENV_FILE}"
restart_and_wait_for_bgfilter_worker "${BGFILTER_WORKER_SERVICE}" "${BGFILTER_WORKER_HEALTH_URL}"