Files
dsh_shenxian/scripts/runtime-baseline.cjs
admin 452924d89c feat(config): 涉密内容外置到配置目录(档案 140)
把散落在代码里的真实部署值统一收进 config/,代码改为引用配置,
使仓库副本/开源导出不再带出生产域名、IP、内网路径与凭据。

新增 config/:platform.env.example(模板)· load.sh(shell 加载器)·
index.cjs(node 加载器)· README.md(键一览与优先级)。
真实值放 config/platform.env —— 已 .gitignore 排除,不入库、不进导出。

TS 侧新增 src/platform-paths.ts 作部署路径的唯一解析处(零副作用):
platformDir/stateDir/backupDir/artifactDir/installDir/scriptPath。
config.ts 接入这些字段;内置中继种子由生产 URL 改为空(改由
DSHS_OVERLAY_BOOTSTRAP_SEEDS 提供)。修掉 5 处硬编码绝对路径,
src/** 注释中性化 116 行/53 文件。

scripts/** 36 个内部运维脚本:真令牌/PG 口令/隧道目标/主机号/路径
一律改从配置取;web/wake.html 的注册域白名单改为运行时从
location.hostname 推导;test/** 夹具 119 行/13 文件改 RFC 2606/5737
保留值,并把「内置种子必须为空」固化为回归断言。

取证:tsc 0 错;npm test 373/375(唯一失败 lease 属既有);
全仓扫描(大小写不敏感)代码面涉密标识 = 0;已部署 47 并零回归
(/opt/dsh/* 未搬家,/var/lib/dshs/platform 未被误建)。
2026-09-19 15:12:19 +08:00

60 lines
2.7 KiB
JavaScript
Raw Permalink 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 node
const cfg = require('../config/index.cjs')
/**
* 运行时版本基线巡检(档案 44)。
*
* 目的:**保证实例共享的基础运行时(Python / pip / node / npm)版本稳定**,
* 避免"版本差异导致插件功能无法使用"。
*
* 背景:运行时是平台在 `/usr/local/dsh-runtime/` 装的可移植发行版,
* 实例内 `/usr` 只读 → 理论上改不了;本脚本是**观测兜底** ——
* 一旦版本偏离基线(例:有人在宿主上手动装/换了版本),立刻告警,而不是等插件坏掉才发现。
*
* 用法:
* node runtime-baseline.cjs # 对比,偏离则退出码 1(cron 会记日志)
* node runtime-baseline.cjs --accept # 主动把当前版本写入基线(升级运行时后执行)
*/
const { execFileSync } = require('node:child_process')
const { existsSync, readFileSync, writeFileSync } = require('node:fs')
const BASE = require('node:path').join(cfg.stateDir(), 'runtime-baseline.json')
const ACCEPT = process.argv.includes('--accept')
const run = (cmd, args) => {
try { return execFileSync(cmd, args, { encoding: 'utf8', timeout: 20000 }).trim().split('\n')[0] }
catch (e) { return `ERR:${String(e.message).split('\n')[0].slice(0, 60)}` }
}
const ver = (cmd, args) => (run(cmd, args).match(/\d+\.\d+(\.\d+)?/) ?? ['?'])[0]
const probe = () => ({
rg: ver('/usr/local/bin/rg', ['--version']),
jq: ver('/usr/local/bin/jq', ['--version']),
ffmpeg: (run('/usr/local/bin/ffmpeg', ['-version']).match(/ffmpeg version (\S+)/)?.[1] ?? '?'),
python3: ver('/usr/local/bin/python3', ['-V']),
pip3: ver('/usr/local/bin/pip3', ['-V']),
node: ver('/usr/local/bin/node', ['-v']),
npm: ver('/usr/local/bin/npm', ['-v']),
runtimePinned: existsSync('/usr/local/dsh-runtime/VERSION')
? (readFileSync('/usr/local/dsh-runtime/VERSION', 'utf8').match(/^python_version=(.+)$/m)?.[1] ?? '?')
: 'MISSING',
})
const now = probe()
if (ACCEPT || !existsSync(BASE)) {
writeFileSync(BASE, JSON.stringify({ acceptedAt: new Date().toISOString(), versions: now }, null, 2) + '\n')
console.log(`==> 基线已写入 ${BASE}`)
console.log(' ', JSON.stringify(now))
process.exit(0)
}
const base = JSON.parse(readFileSync(BASE, 'utf8'))
const drift = Object.keys(now).filter((k) => base.versions[k] !== now[k])
if (drift.length === 0) {
console.log(`ok 运行时版本与基线一致:${JSON.stringify(now)}`)
process.exit(0)
}
console.log('!! 运行时版本漂移(档案 44)')
for (const k of drift) console.log(` ${k}: 基线 ${base.versions[k]} → 现在 ${now[k]}`)
console.log(` 基线时间 ${base.acceptedAt};若本次是有意升级,执行:node runtime-baseline.cjs --accept`)
process.exit(1)