diff --git a/package.json b/package.json index 0cd9775..c1b7454 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,9 @@ "deploy:cert-sync:run": "node ./script/deploy-cert-sync.cjs production run", "deploy:cert-sync:status": "node ./script/deploy-cert-sync.cjs production status", "deploy:cert-sync:config-check": "node ./script/deploy-cert-sync.cjs production config-check", + "deploy:cert-sync:cleanup-dry-run": "node ./script/deploy-cert-sync.cjs production cleanup-dry-run", + "deploy:cert-sync:cleanup": "node ./script/deploy-cert-sync.cjs production cleanup", + "deploy:gateway-probe": "node ./script/deploy-ops.cjs production gateway-probe", "ops:check-root-logs": "node ./script/check-root-logs.cjs", "ops:clean-root-logs": "node ./script/check-root-logs.cjs production --clean", diff --git a/script/deploy-cert-sync.cjs b/script/deploy-cert-sync.cjs index 5e02f98..3c5f83f 100644 --- a/script/deploy-cert-sync.cjs +++ b/script/deploy-cert-sync.cjs @@ -11,7 +11,8 @@ const { runSsh, } = require('./deploy-lib.cjs'); -const COMMANDS = new Set([ 'precheck', 'install', 'dry-run', 'run', 'status', 'config-check' ]); +const COMMANDS = new Set([ 'precheck', 'install', 'dry-run', 'run', 'status', 'config-check', 'cleanup-dry-run', 'cleanup' ]); + const LOCAL_SYNC_SCRIPT = path.join(ROOT, 'script', 'warehouse-cert-sync.cjs'); const LOCAL_DB_CONFIG = path.join(ROOT, 'database', 'config.js'); const LOCAL_PROD_CONFIG = path.join(ROOT, 'script', 'warehouse-cert-sync.config.prod.json'); @@ -20,14 +21,14 @@ function parseArgs(argv) { const options = { envName: 'production', command: '' }; const tokens = [ ...argv ]; if (tokens.length === 0) { - throw new Error('missing command: precheck | install | dry-run | run | status | config-check'); + throw new Error('missing command: precheck | install | dry-run | run | status | config-check | cleanup-dry-run | cleanup'); } if (!COMMANDS.has(tokens[0])) { options.envName = tokens[0]; tokens.shift(); } if (tokens.length === 0 || !COMMANDS.has(tokens[0])) { - throw new Error('missing command: precheck | install | dry-run | run | status | config-check'); + throw new Error('missing command: precheck | install | dry-run | run | status | config-check | cleanup-dry-run | cleanup'); } options.command = tokens[0]; return options; @@ -193,6 +194,17 @@ const seq = new Sequelize(client.database, client.username || client.user, clien } else { console.log("[DB_PROBE] domain_status distribution: (column missing, migration not applied; cert-sync falls back to no filter)"); } + const hasRetiredDomain = cols.some(r => r.cn === "retired_domain"); + console.log("[DB_PROBE] retired_domain column: " + (hasRetiredDomain ? "YES" : "NO")); + if (hasRetiredDomain) { + const [rCount] = await seq.query("SELECT COUNT(*) AS c FROM wms_warehouse_front_config WHERE retired_domain IS NOT NULL AND retired_domain <> ''"); + const [rPreview] = await seq.query("SELECT retired_domain AS d FROM wms_warehouse_front_config WHERE retired_domain IS NOT NULL AND retired_domain <> '' ORDER BY id ASC LIMIT 20"); + const names = rPreview.map(r => String(r.d)).join(","); + console.log("[DB_PROBE] retired_domain pending: retired=" + rCount[0].c + (names ? ": " + names : "")); + } else { + console.log("[DB_PROBE] retired_domain pending: (column missing, migration not applied; cleanup flow skipped)"); + } + } } catch (e) { console.log("[DB_PROBE] failed: " + e.message); @@ -201,7 +213,10 @@ const seq = new Sequelize(client.database, client.username || client.user, clien })(); `; -/** Read-only remote precheck. */ +/** + * Read-only remote precheck. + * @param {Object} config deploy 配置对象。 + */ function runPrecheck(config) { const deployRoot = config.deployRoot; const remoteScript = [ @@ -253,7 +268,10 @@ function runPrecheck(config) { console.log(runSsh(config, dbProbeScript, { stdio: 'pipe' }).trim()); } -/** Install: upload files, create thin shell wrapper, register root crontab. */ +/** + * Install: upload files, create thin shell wrapper, register root crontab. + * @param {Object} config deploy 配置对象。 + */ function runInstall(config) { const deployRoot = config.deployRoot; for (const [ file, label ] of [ @@ -347,21 +365,35 @@ function runInstall(config) { console.log(runSsh(config, installScript, { stdio: 'pipe' }).trim()); } -/** Run the remote wrapper with optional extra args; returns output + exit code. */ -function runRemoteWrapper(config, extraArgs = []) { +/** + * Run the remote wrapper with optional extra args; returns output + exit code. + * useFlock=true 时用与 crontab 相同的锁文件串行化(手工清理与每分钟定时任务互斥), + * flock 不可用时只告警并降级为不加锁执行(不阻断运维操作)。 + * @param {Object} config deploy 配置对象(含 deployRoot)。 + * @param {string[]} extraArgs 透传给远端包装脚本的额外参数。 + * @param {boolean} useFlock 是否用 flock 串行化执行,默认 false。 + * @return {string} 远端输出(已 trim)。 + */ +function runRemoteWrapper(config, extraArgs = [], useFlock = false) { const wrapperPath = `${config.deployRoot}/warehouse-cert-sync.sh`; + const runLine = useFlock + ? 'FLOCK_BIN=$(command -v flock || true); if [ -n "$FLOCK_BIN" ]; then "$FLOCK_BIN" -w 30 /var/lock/warehouse-cert-sync.lock bash "$WRAPPER" $ARGS; else echo "WARN: flock not found, run without lock"; bash "$WRAPPER" $ARGS; fi' + : 'bash "$WRAPPER" $ARGS'; const remoteScript = [ 'set -uo pipefail', `WRAPPER=${quoteForBash(wrapperPath)}`, `ARGS=${quoteForBash(extraArgs.join(' '))}`, 'if [ ! -f "$WRAPPER" ]; then echo "FATAL: wrapper missing: $WRAPPER"; exit 1; fi', - 'bash "$WRAPPER" $ARGS', + runLine, 'echo "CERT_SYNC_EXIT=$?"', ].join('\n'); return runSsh(config, remoteScript, { stdio: 'pipe' }).trim(); } -/** Status: crontab line, remote file list, log tail. */ +/** + * Status: crontab line, remote file list, log tail. + * @param {Object} config deploy 配置对象。 + */ function runStatus(config) { const deployRoot = config.deployRoot; const remoteScript = [ @@ -378,7 +410,10 @@ function runStatus(config) { console.log(runSsh(config, remoteScript, { stdio: 'pipe' }).trim()); } -/** 只读核对服务器 prod 配置文件:存在性 + JSON 合法性 + 内容展示(无凭据,可安全输出)。 */ +/** + * 只读核对服务器 prod 配置文件:存在性 + JSON 合法性 + 内容展示(无凭据,可安全输出)。 + * @param {Object} config deploy 配置对象。 + */ function runConfigCheck(config) { const deployRoot = config.deployRoot; const remoteScript = [ @@ -428,6 +463,16 @@ function main() { console.log(runRemoteWrapper(config, [])); return; } + if (options.command === 'cleanup-dry-run') { + console.log('[CLEANUP-DRY-RUN] remote retired-domain cleanup dry-run (read-only: no file deletes, no db updates, no reload)'); + console.log(runRemoteWrapper(config, [ '--cleanup-only', '--dry-run' ], true)); + return; + } + if (options.command === 'cleanup') { + console.log('[CLEANUP] remote retired-domain cleanup (real execution: deletes retired conf/cert files, then nginx -t + reload)'); + console.log(runRemoteWrapper(config, [ '--cleanup-only' ], true)); + return; + } if (options.command === 'status') { runStatus(config); return; diff --git a/script/warehouse-cert-sync.cjs b/script/warehouse-cert-sync.cjs index 71218af..fac138d 100644 --- a/script/warehouse-cert-sync.cjs +++ b/script/warehouse-cert-sync.cjs @@ -6,6 +6,13 @@ * 职责:扫描 wms_warehouse_front_config 中 sync_status=0(待同步)的记录, * 将 cert_pem / cert_key 内容写为证书文件,生成对应域名 server 块, * 逐条 nginx -t 校验后统一 reload,并回写部署状态。 + * 另外承担「旧域名下线」(两阶段域名切换的应用侧配套,应用不碰 nginx 文件): + * - 消费 retired_domain:删除旧域名残留的 server 块与证书文件,删干净后才清空该槽位; + * - 孤儿兜底清理:以目录实际文件为准做差集,回收因 retired_domain 单槽位丢失而残留的旧产物; + * - 统计口径:汇总的「待删 / 已删文件数」按绝对路径去重后输出;通道命中数(retired_domain / orphan) + * 单独列示,两者之和大于去重数属预期(dry-run 不落盘,两条通道都会看到同一文件); + * - 只要本轮删过文件,就先 nginx -t 再 reload;-t 失败回滚本轮删除并保留 retired_domain 待下轮重试。 + * * 设计要点: * - 每分钟被服务器侧 root crontab 调用(薄 shell 包装由部署 agent 适配), @@ -46,6 +53,9 @@ * --cert-dir <临时目录> --conf-dir <临时目录> --skip-nginx \ * --nginx-bin --reload-cmd <覆盖命令> * node script/warehouse-cert-sync.cjs --env production + * node script/warehouse-cert-sync.cjs --env production --dry-run + * node script/warehouse-cert-sync.cjs --env production --cleanup-only + * node script/warehouse-cert-sync.cjs --env production --cleanup-only --dry-run * * 退出码约定(供 crontab 告警): * 0 正常:无待处理行、本批全部成功、dry-run 完成; @@ -96,6 +106,14 @@ const KEY_FILE_MODE = 0o600; const TABLE_NAME = 'wms_warehouse_front_config'; +// ---- 旧域名下线 / 孤儿兜底清理相关常量 ---- +// conf 文件名形态:{domainSafe}{[_短哈希8]}.conf(与 buildConfFileName 产物一致,仅作第一道形态闸门) +const CONF_FILE_NAME_PATTERN = /^[A-Za-z0-9._-]+?(?:_[0-9a-f]{8})?\.conf$/; +// 证书 / 私钥文件名形态:{store_id}_{domainSafe}{[_短哈希8]}.pem|.key +const CERT_FILE_NAME_PATTERN = /^[A-Za-z0-9]+_[A-Za-z0-9._-]+?(?:_[0-9a-f]{8})?\.(?:pem|key)$/; +// server_name 指令解析(取第一条 server_name 的域名) +const SERVER_NAME_PATTERN = /^[ \t]*server_name[ \t]+([^;]+);/m; + /** * 单条处理结果统计。 * @typedef {Object} SyncStats @@ -110,6 +128,7 @@ const TABLE_NAME = 'wms_warehouse_front_config'; * @property {string} env 数据库环境标记:development | production。 * @property {string} configPath 生效的外部配置文件绝对路径。 * @property {boolean} dryRun 仅扫描与输出计划,不写文件、不改库。 + * @property {boolean} cleanupOnly 只做旧域名下线与孤儿兜底清理,不做证书同步(--cleanup-only)。 * @property {boolean} skipNginx 跳过 nginx -t / reload。 * @property {string} certDir 证书输出目录(绝对路径)。 * @property {string} confDir nginx server 块目录(绝对路径)。 @@ -159,7 +178,8 @@ function printUsage() { '', '选项:', ' --env 运行环境(必填,显式指定,不受终端 NODE_ENV 残留影响,同时决定数据库与配置文件)', - ' --dry-run 仅扫描并输出计划,不写文件、不改库', + ' --dry-run 仅扫描并输出计划,不写文件、不改库(含旧域名下线/孤儿清理的待删清单)', + ' --cleanup-only 只做旧域名下线与孤儿兜底清理,不做证书同步(供运维单独执行)', ' --cert-dir 证书输出目录(默认取配置文件或 ../.ssl,相对路径按 cwd 解析)', ' --conf-dir nginx server 块目录(默认取配置文件或 ../nginx_config,同样按 cwd 解析)', ' --upstream-include server 块 include 的 upstream 配置名(默认取配置文件或 nginx-upstream.conf)', @@ -232,6 +252,7 @@ function parseArgs(argv) { testCmd: '', limit: null, dryRun: false, + cleanupOnly: false, skipNginx: false, help: false, }; @@ -257,6 +278,8 @@ function parseArgs(argv) { cli.help = true; } else if (key === '--dry-run') { cli.dryRun = true; + } else if (key === '--cleanup-only') { + cli.cleanupOnly = true; } else if (key === '--skip-nginx') { cli.skipNginx = true; } else if (key === '--env') { @@ -295,6 +318,7 @@ function parseArgs(argv) { testCmd: '', limit: DEFAULT_LIMIT, dryRun: cli.dryRun, + cleanupOnly: cli.cleanupOnly, skipNginx: cli.skipNginx, help: true, }; @@ -320,6 +344,7 @@ function parseArgs(argv) { env, configPath, dryRun: cli.dryRun, + cleanupOnly: cli.cleanupOnly, skipNginx: cli.skipNginx, help: false, certDir: path.resolve(pickString(cli.certDir, 'CERT_SYNC_CERT_DIR', config.certDir, DEFAULT_CERT_DIR)), @@ -539,24 +564,58 @@ function shortHash(domain) { } /** - * 生成一条记录的文件名与落盘路径。 - * 命名规则:{store_id}_{domain};domain 做非法字符清洗,清洗后与原名不一致时 - * 追加 _哈希8 防不同域名清洗后碰撞。 + * 域名命名推导(写盘与下线清理共用的唯一入口)。 + * 命名规则:domain 做非法字符清洗得到 domainSafe;清洗后与原名不一致时 + * 追加 _短哈希8 防不同域名清洗后碰撞(如 *.a.com 会被清洗为 .a.com)。 + * @param {string} domainRaw 原始域名(已 trim)。 + * @return {{domainSafe: string, suffix: string}} 命名片段(suffix 为空串或 _短哈希8)。 + */ +function computeDomainNameParts(domainRaw) { + const domainSafe = sanitizeFileNameSegment(domainRaw); + const changed = domainSafe !== domainRaw; + return { + domainSafe, + suffix: changed ? `_${shortHash(domainRaw)}` : '', + }; +} + +/** + * 推导域名对应的 server 块文件名({domainSafe}{suffix}.conf)。 + * 写入(buildFileNames)与删除(旧域名下线 / 孤儿兜底清理)共用本推导, + * 保证「按名删除」与「按名写入」严格一致。 + * @param {string} domainRaw 原始域名(已 trim)。 + * @return {string} conf 文件名(不含目录)。 + */ +function buildConfFileName(domainRaw) { + const parts = computeDomainNameParts(domainRaw); + return `${parts.domainSafe}${parts.suffix}.conf`; +} + +/** + * 推导证书 / 私钥文件名前缀({store_id}_{domainSafe}{suffix})。 + * @param {number|string} storeId 仓库 ID。 + * @param {string} domainRaw 原始域名(已 trim)。 + * @return {string} baseName(不含扩展名与目录)。 + */ +function buildCertBaseName(storeId, domainRaw) { + const parts = computeDomainNameParts(domainRaw); + return `${storeId}_${parts.domainSafe}${parts.suffix}`; +} + +/** + * 生成一条记录的文件名与落盘路径(命名推导见 computeDomainNameParts)。 * @param {SyncArgs} args 解析后的参数集合。 * @param {number} storeId 仓库 ID。 * @param {string} domainRaw 原始域名(已 trim)。 * @return {{baseName: string, certPath: string, keyPath: string, confPath: string}} 文件路径集合。 */ function buildFileNames(args, storeId, domainRaw) { - const domainSafe = sanitizeFileNameSegment(domainRaw); - const changed = domainSafe !== domainRaw; - const suffix = changed ? `_${shortHash(domainRaw)}` : ''; - const baseName = `${storeId}_${domainSafe}${suffix}`; + const baseName = buildCertBaseName(storeId, domainRaw); return { baseName, certPath: path.join(args.certDir, `${baseName}.pem`), keyPath: path.join(args.certDir, `${baseName}.key`), - confPath: path.join(args.confDir, `${domainSafe}${suffix}.conf`), + confPath: path.join(args.confDir, buildConfFileName(domainRaw)), }; } @@ -887,6 +946,686 @@ async function handleReloadFailure(ctx, successRows, reloadError) { } } +// ===================================================================== +// 旧域名下线(retired_domain 消费)与孤儿兜底清理 +// +// 背景:前端独立部署改为两阶段域名切换后,应用侧只登记 pending,并在 TXT 校验 +// 通过后「提升」为新域名(domain 换成新域名、清空证书并重置同步状态),同时把 +// 旧域名写入 retired_domain(仅保留最近一个)。应用不碰 nginx 文件,因此旧域名的 +// server 块与证书文件必须由本脚本下线。 +// +// 红线: +// 1. 只允许删除 confDir / certDir 内的文件,删除前统一做目录前缀校验; +// 2. 只允许删除「本系统按命名规则生成」的文件(白名单识别,见 cleanupOrphans); +// 3. --dry-run 只打印待删清单,绝不删除文件、不写库、不 reload; +// 4. 删除失败(权限等)不中断整轮,只计入统计并 WARN。 +// ===================================================================== + +/** + * 探测 wms_warehouse_front_config 是否已有 retired_domain 列(两阶段域名切换新增)。 + * + * 脚本可能先于迁移部署:该列不存在时整段跳过下线 / 清理流程并 WARN, + * 与 domain_status 的降级策略同源(宁可不做,也不猜)。 + * @param {import('sequelize').Sequelize} sequelize Sequelize 实例。 + * @return {Promise} 存在返回 true;探测失败返回 false。 + */ +async function hasRetiredDomainColumn(sequelize) { + try { + const rows = await sequelize.query( + `SELECT COUNT(*) AS c FROM information_schema.columns + WHERE table_schema = DATABASE() AND table_name = ? AND column_name = 'retired_domain'`, + { replacements: [ TABLE_NAME ], type: Sequelize.QueryTypes.SELECT } + ); + return Number(rows && rows[0] && rows[0].c) > 0; + } catch (error) { + console.warn(`[CERT-SYNC] retired_domain 列探测失败(按无该列降级,跳过下线与孤儿清理): ${sanitizeErrorText(error.message)}`); + return false; + } +} + +/** + * 扫描待下线的旧域名记录(retired_domain 非空,按 id 升序,limit 条)。 + * @param {import('sequelize').Sequelize} sequelize Sequelize 实例。 + * @param {number} limit 单轮处理条数上限。 + * @return {Promise} 待下线记录数组(id / store_id / domain / retired_domain / retired_at)。 + */ +async function scanRetiredRows(sequelize, limit) { + return sequelize.query( + `SELECT id, store_id, domain, retired_domain, retired_at + FROM ${TABLE_NAME} + WHERE retired_domain IS NOT NULL AND retired_domain <> '' + ORDER BY id ASC + LIMIT ?`, + { replacements: [ limit ], type: Sequelize.QueryTypes.SELECT } + ); +} + +/** + * 读取「生效域名集合」= 允许存在 nginx server 块的域名。 + * domain_status 列缺失(迁移未执行)时降级为不按验证状态过滤。 + * 集合统一小写化:DNS 与 nginx server_name 匹配均不区分大小写, + * 避免 A.com / a.com 的大小写差异被误判为孤儿。 + * @param {import('sequelize').Sequelize} sequelize Sequelize 实例。 + * @param {boolean} domainStatusReady 是否存在 domain_status 列。 + * @return {Promise>} 小写化生效域名集合。 + */ +async function loadEffectiveDomains(sequelize, domainStatusReady) { + const verifiedFilter = domainStatusReady ? ' AND domain_status = 1' : ''; + const rows = await sequelize.query( + `SELECT DISTINCT domain FROM ${TABLE_NAME} + WHERE domain IS NOT NULL AND domain <> ''${verifiedFilter}`, + { type: Sequelize.QueryTypes.SELECT } + ); + const domains = new Set(); + for (const row of rows || []) { + const domain = String(row.domain || '').trim().toLowerCase(); + if (domain) { + domains.add(domain); + } + } + return domains; +} + +/** + * 读取「库内仍存在的证书文件名前缀集合」(不限域名验证状态)。 + * 孤儿证书兜底的安全闸门:只要某证书文件仍能对应表内任意一行的 + * (store_id, domain) 命名推导结果,就一律不删(可能只是 conf 尚未写出)。 + * @param {import('sequelize').Sequelize} sequelize Sequelize 实例。 + * @return {Promise>} baseName 集合({store_id}_{domainSafe}{suffix})。 + */ +async function loadKnownCertBaseNames(sequelize) { + const rows = await sequelize.query( + `SELECT DISTINCT store_id, domain FROM ${TABLE_NAME} + WHERE domain IS NOT NULL AND domain <> ''`, + { type: Sequelize.QueryTypes.SELECT } + ); + const baseNames = new Set(); + for (const row of rows || []) { + const domain = String(row.domain || '').trim(); + if (!domain) { + continue; + } + baseNames.add(buildCertBaseName(row.store_id, domain)); + } + return baseNames; +} + +/** + * 生成去重台账的路径键:win32 下路径大小写不敏感(D:/ 与 d:/ 指向同一文件),统一小写, + * 避免同一文件在去重台账里被算成两个文件。 + * @param {string} filePath 文件绝对路径。 + * @return {string} 去重键。 + */ +function cleanupPathKey(filePath) { + const resolved = path.resolve(filePath); + return process.platform === 'win32' ? resolved.toLowerCase() : resolved; +} + +/** + * 创建下线 / 清理统计对象(就地累加,供汇总打印与退出码判定使用)。 + * + * 两套口径不可混用: + * - 通道命中数(retiredConfs/retiredCerts/orphanConfs/orphanCerts):按「谁动的手」累加, + * 同一文件被两条通道各处理一次就计两次(dry-run 不落盘 → 会真实出现;真实运行不会); + * - 去重后文件数(cleanedPaths.size):按绝对路径去重,是「本轮待删 / 已删文件数」的唯一权威口径。 + * @return {Object} 统计对象。 + */ +function createCleanupStats() { + return { + scannedRetired: 0, // 扫描到的待下线行数 + clearedRows: 0, // 已清空 retired_domain 的行数 + retiredConfs: 0, // 下线通道命中的 conf 个数(不去重) + retiredCerts: 0, // 下线通道命中的证书文件个数(不去重) + orphanConfs: 0, // 孤儿通道命中的 conf 个数(不去重) + orphanCerts: 0, // 孤儿通道命中的证书文件个数(不去重) + planned: 0, // dry-run 计划行数(含通道重复列出;去重后文件数见 cleanedPaths.size) + cleanedPaths: new Map(), // 去重台账:路径键 → 首次列出/删除该路径的通道名(dry-run 与真实运行共用) + duplicateListings: 0, // dry-run 下被第二条通道重复列表的次数(解释「通道命中数之和 > 去重数」) + skippedRetired: 0, // 跳过的待下线条数(形态非法 / 数据冲突) + skippedOrphan: 0, // 孤儿清理跳过的文件数(白名单未命中 / 超单轮上限 / 不可判定) + deleteFailed: 0, // 删除失败文件数(权限等,非致命) + refusedPaths: 0, // 路径前缀校验拒绝数(红线护栏命中) + clearedDeferred: 0, // 因 nginx -t / reload 失败而延后清空的行数 + cleanupFailed: 0, // 清理阶段失败计数(nginx -t 或 reload 失败,用于退出码) + }; +} + +/** + * 格式化「去重后」口径摘要:`N 个文件(去重后;通道命中 retired_domain=X, orphan=Y)`。 + * + * 去重后文件数取 cleanedPaths 规模;通道命中数之和大于 N 属于预期: + * dry-run 不落盘,先列出的通道不会真的删掉文件,后一条通道仍会看到同一文件。 + * @param {Object} stats 统计对象。 + * @return {string} 摘要文本。 + */ +function formatCleanupDedupDetail(stats) { + const retiredHits = stats.retiredConfs + stats.retiredCerts; + const orphanHits = stats.orphanConfs + stats.orphanCerts; + return `${stats.cleanedPaths.size} 个文件(去重后;通道命中 retired_domain=${retiredHits}, orphan=${orphanHits})`; +} + +/** + * 路径前缀校验:解析后必须严格位于允许目录内(红线:禁止删除配置目录之外的路径)。 + * win32 下 path.relative 不区分大小写,POSIX 下区分。 + * @param {string} allowedDir 允许删除的目录(绝对路径)。 + * @param {string} filePath 待删除文件路径。 + * @return {boolean} 位于目录内返回 true。 + */ +function isPathInsideDir(allowedDir, filePath) { + const rel = path.relative(path.resolve(allowedDir), path.resolve(filePath)); + if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) { + return false; + } + return true; +} + +/** + * 读取文本文件;不存在或不可读返回 null(调用方按「不可判定」处理,不删除)。 + * @param {string} filePath 文件绝对路径。 + * @return {string|null} 文件内容或 null。 + */ +function readTextFileOrNull(filePath) { + try { + return fs.readFileSync(filePath, 'utf8'); + } catch (error) { + if (error && error.code !== 'ENOENT') { + console.warn(`[CERT-SYNC][WARN] 读取文件失败(按不可判定处理,不删除): ${filePath} - ${sanitizeErrorText(error.message)}`); + } + return null; + } +} + +/** + * 读取目录下的普通文件列表(目录不存在 / 不可读返回空数组并 WARN)。 + * @param {string} dirPath 目录绝对路径。 + * @return {Array<{name: string, fullPath: string}>} 文件列表。 + */ +function readDirSafe(dirPath) { + let entries = []; + try { + entries = fs.readdirSync(dirPath, { withFileTypes: true }); + } catch (error) { + console.warn(`[CERT-SYNC][WARN] 目录不可读(本轮跳过该目录扫描): ${dirPath} - ${sanitizeErrorText(error.message)}`); + return []; + } + const files = []; + for (const entry of entries) { + if (!entry.isFile()) { + continue; + } + files.push({ name: entry.name, fullPath: path.join(dirPath, entry.name) }); + } + return files; +} + +/** + * 从 conf 内容解析首个 server_name 的域名。 + * @param {string} content conf 文件内容。 + * @return {string|null} 域名(去引号与空白),解析不到返回 null。 + */ +function parseServerName(content) { + const matched = SERVER_NAME_PATTERN.exec(String(content || '')); + if (!matched) { + return null; + } + const firstToken = String(matched[1] || '').trim().split(/\s+/)[0] || ''; + const domain = firstToken.replace(/^["']|["']$/g, '').trim(); + return domain || null; +} + +/** + * 采集 conf 内容中引用的证书 / 私钥文件名前缀({store_id}_{domainSafe}{suffix})。 + * 用于在「删除后的 conf 集合」上判定证书文件是否仍被引用。 + * @param {string} content conf 文件内容。 + * @param {Set} target 采集结果集合(就地累加)。 + * @return {void} + */ +function collectCertBaseNames(content, target) { + const pattern = /([A-Za-z0-9]+_[A-Za-z0-9._-]+?)\.(?:pem|key)/g; + const text = String(content || ''); + let matched = pattern.exec(text); + while (matched) { + target.add(matched[1]); + matched = pattern.exec(text); + } +} + +/** + * 受控删除(下线与孤儿清理的唯一删除入口)。 + * + * 行为约定: + * 1. 目标必须位于 allowedDir 内(前缀校验),否则拒绝并 WARN(红线); + * 2. dry-run 只打印 [PLAN] DELETE,绝不落盘; + * 3. 文件不存在不算失败,只记 [SKIP] 日志; + * 4. 删除前缓存文件内容,供本轮 nginx -t 失败时回滚(尽量恢复磁盘原状); + * 5. 删除失败不抛异常,计入统计并 WARN,不中断整轮; + * 6. 计划 / 删除成功的路径登记入去重台账(cleanedPaths),供汇总输出去重口径。 + * @param {SyncContext} ctx 运行上下文。 + * @param {string} allowedDir 允许删除的目录(confDir 或 certDir)。 + * @param {string} filePath 目标文件绝对路径。 + * @param {'retired_domain'|'orphan'} reason 删除原因(审计日志用)。 + * @param {Object} stats 统计对象(就地累加)。 + * @param {Array<{path: string, content: Buffer, mode: number}>} rollbackEntries 回滚素材集合(就地累加)。 + * @return {'deleted'|'planned'|'missing'|'refused'|'failed'} 处理结果。 + */ +function deleteFileGuarded(ctx, allowedDir, filePath, reason, stats, rollbackEntries) { + if (!isPathInsideDir(allowedDir, filePath)) { + console.warn(`[CERT-SYNC][WARN] 拒绝删除配置目录之外的路径: ${filePath}(原因=${reason},允许目录=${allowedDir})`); + stats.refusedPaths++; + return 'refused'; + } + + let stat = null; + try { + stat = fs.statSync(filePath); + } catch (error) { + if (error && error.code === 'ENOENT') { + console.log(`[CERT-SYNC][SKIP] 待删文件不存在: ${filePath} 原因=${reason}`); + return 'missing'; + } + console.warn(`[CERT-SYNC][WARN] 读取待删文件属性失败: ${filePath} - ${sanitizeErrorText(error.message)}`); + stats.deleteFailed++; + return 'failed'; + } + if (!stat.isFile()) { + console.warn(`[CERT-SYNC][WARN] 待删目标不是普通文件,已跳过: ${filePath}`); + return 'missing'; + } + + const dedupKey = cleanupPathKey(filePath); + if (ctx.args.dryRun) { + // dry-run 绝不落盘 → 同一路径可能被后一条通道再次列出: + // 第二次列出时追加去重标注,避免运维按日志行数误判待删文件规模 + const listedBy = stats.cleanedPaths.get(dedupKey); + if (listedBy) { + stats.duplicateListings++; + console.log(`[CERT-SYNC][PLAN] DELETE ${filePath} 原因=${reason} [已由 ${listedBy} 通道列出,去重]`); + } else { + stats.cleanedPaths.set(dedupKey, reason); + console.log(`[CERT-SYNC][PLAN] DELETE ${filePath} 原因=${reason}`); + } + stats.planned++; + return 'planned'; + } + + try { + // 回滚素材:删除前缓存内容与权限(私钥内容只在内存中流转,绝不打印) + const content = fs.readFileSync(filePath); + // mode 只取低 9 位权限位(0o777),避免把文件类型位带进回滚写入 + // eslint-disable-next-line no-bitwise + rollbackEntries.push({ path: filePath, content, mode: stat.mode & 0o777 }); + + fs.unlinkSync(filePath); + } catch (error) { + console.warn(`[CERT-SYNC][WARN] 删除失败(不中断本轮): ${filePath} - ${sanitizeErrorText(error.message)}`); + stats.deleteFailed++; + return 'failed'; + } + // 去重台账只登记首次成功的删除(真实运行下第二条通道只会命中 [SKIP] 待删文件不存在) + if (!stats.cleanedPaths.has(dedupKey)) { + stats.cleanedPaths.set(dedupKey, reason); + } + console.log(`[CERT-SYNC] DELETE ${filePath} 原因=${reason}`); + return 'deleted'; +} + +/** + * 回滚本轮已删除的文件(nginx -t 失败时把磁盘尽量恢复成本轮开始前的状态)。 + * @param {Array<{path: string, content: Buffer, mode: number}>} rollbackEntries 回滚素材集合。 + * @return {number} 成功恢复的文件数。 + */ +function rollbackDeletedFiles(rollbackEntries) { + let restored = 0; + for (const entry of rollbackEntries) { + try { + writeFileAtomic(entry.path, entry.content, entry.mode); + restored++; + } catch (error) { + console.error(`[CERT-SYNC][WARN] 回滚失败,需人工恢复: ${entry.path} - ${sanitizeErrorText(error.message)}`); + } + } + return restored; +} + +/** + * 消费 retired_domain(下线主线):删除旧域名残留的 server 块与证书文件。 + * + * 产物路径完全按写入时的命名推导推算(与 buildFileNames 同源): + * - server 块:{confDir}/{domainSafe}{suffix}.conf + * - 证书文件:{certDir}/{store_id}_{domainSafe}{suffix}.pem 与 .key + * suffix 仅在「域名被清洗后与原名不一致」时出现(下划线 + 域名 md5 前 8 位), + * 例如 *.a.com 会被清洗为 .a.com,必须带 _短哈希8 才不会与 .a.com 的产物冲突。 + * + * 安全闸门(任一命中即整条跳过:不删文件、不清空槽位): + * 1. retired_domain 形态非法(空 / 超长 / 含非法字符); + * 2. retired_domain 与当前 domain 相同(数据异常,槽位指向生效域名); + * 3. retired_domain 仍在生效域名集合内(可能被其它仓库使用)。 + * 文件不存在不算失败(只记 [SKIP] 日志);权限等硬失败保留槽位待下一轮重试。 + * @param {SyncContext} ctx 运行上下文。 + * @param {Object} cleanupCtx 清理上下文(stats / effectiveDomains / handledConfPaths / rollbackEntries)。 + * @return {Promise>} 可清空槽位的行(reload 成功后统一清空)。 + */ +async function consumeRetiredDomains(ctx, cleanupCtx) { + const { args } = ctx; + const { stats, effectiveDomains, handledConfPaths, removedConfPaths, rollbackEntries } = cleanupCtx; + const rows = await scanRetiredRows(ctx.sequelize, args.limit); + stats.scannedRetired = rows.length; + const clearableRows = []; + + for (const row of rows) { + const currentDomain = String(row.domain || '').trim(); + const retiredRaw = String(row.retired_domain || '').trim(); + const rowLabel = `#${row.id} store=${row.store_id} retired=${retiredRaw || '(空)'}`; + + if (!retiredRaw || retiredRaw.length > DOMAIN_MAX_LENGTH || !DOMAIN_PATTERN.test(retiredRaw)) { + console.warn(`[CERT-SYNC][WARN] ${rowLabel} retired_domain 形态非法,跳过(不删文件、不清空槽位)`); + stats.skippedRetired++; + continue; + } + if (retiredRaw.toLowerCase() === currentDomain.toLowerCase()) { + console.warn(`[CERT-SYNC][WARN] ${rowLabel} retired_domain 与当前 domain 相同(数据异常),跳过以免误删生效产物`); + stats.skippedRetired++; + continue; + } + if (effectiveDomains.has(retiredRaw.toLowerCase())) { + console.warn(`[CERT-SYNC][WARN] ${rowLabel} retired_domain 仍在生效域名集合内(可能被其它仓库使用),跳过以免误删`); + stats.skippedRetired++; + continue; + } + + const confPath = path.join(args.confDir, buildConfFileName(retiredRaw)); + const certBaseName = buildCertBaseName(row.store_id, retiredRaw); + // 无论删除结果如何都登记为「本轮已处理」,避免孤儿清理对同一文件重复动作与重复日志 + handledConfPaths.add(confPath); + + const targets = [ + { dir: args.confDir, filePath: confPath, kind: 'conf' }, + { dir: args.certDir, filePath: path.join(args.certDir, `${certBaseName}.pem`), kind: 'cert' }, + { dir: args.certDir, filePath: path.join(args.certDir, `${certBaseName}.key`), kind: 'cert' }, + ]; + + let hardFailed = false; + for (const target of targets) { + const result = deleteFileGuarded(ctx, target.dir, target.filePath, 'retired_domain', stats, rollbackEntries); + if (result === 'failed') { + hardFailed = true; + continue; + } + if (result === 'deleted' || result === 'planned') { + if (target.kind === 'conf') { + stats.retiredConfs++; + removedConfPaths.add(target.filePath); + } else { + stats.retiredCerts++; + } + } + } + + if (hardFailed) { + console.warn(`[CERT-SYNC][WARN] ${rowLabel} 存在删除失败,保留 retired_domain 待下一轮重试`); + continue; + } + clearableRows.push({ row, retiredDomain: retiredRaw }); + } + + return clearableRows; +} + +/** + * 清空 retired_domain 槽位(retired_at 保留作审计)。 + * 防并发守卫:WHERE 带 id + retired_domain 双条件(与 markRowSuccess 同风格), + * 避免覆盖并发写入的新值(应用可能刚好又发生一次域名切换)。 + * @param {import('sequelize').Sequelize} sequelize Sequelize 实例。 + * @param {Object} row 记录对象(含 id)。 + * @param {string} retiredDomain 本轮消费的旧域名原值(并发守卫条件)。 + * @return {Promise} 受影响行数(0 表示已被并发修改,未清空)。 + */ +async function clearRetiredDomainSlot(sequelize, row, retiredDomain) { + const queryResult = await sequelize.query( + + `UPDATE ${TABLE_NAME} + SET retired_domain = NULL, + updated_at = NOW() + WHERE id = :id AND retired_domain = :retiredDomain`, + { replacements: { id: row.id, retiredDomain } } + ); + const metadata = Array.isArray(queryResult) ? queryResult[1] : null; + const affected = metadata && typeof metadata.affectedRows === 'number' ? Number(metadata.affectedRows) : null; + + return affected === null ? 1 : affected; +} + +/** + * 执行旧域名下线 + 孤儿兜底清理。 + * + * 执行顺序(关键:避免 reload 失败时误清空 retired_domain): + * 1. 删除旧域名 conf / 证书 + 孤儿差集删除(只删白名单命中的文件); + * 2. 只要本轮删过任何文件,就在删除后的配置树上执行 nginx -t,通过后再 reload; + * 3. nginx -t 失败 → 回滚本轮删除的文件并告警;reload 失败 → 直接告警; + * -t / reload 任一失败都不清空 retired_domain(槽位保留即待办标记,下一分钟自动重试, + * 等价于证书同步的 markRowReloadRetry「回退待处理 + 下一轮重试」策略); + * 4. 只有「本轮无文件变更」或「-t 通过且 reload 成功」才清空槽位。 + * @param {SyncContext} ctx 运行上下文。 + * @param {boolean} domainStatusReady 是否存在 domain_status 列。 + * @param {Object} stats 统计对象(就地累加)。 + * @return {Promise} 无返回值。 + */ +async function runCleanup(ctx, domainStatusReady, stats) { + const { args } = ctx; + const rollbackEntries = []; + const handledConfPaths = new Set(); + const removedConfPaths = new Set(); + const effectiveDomains = await loadEffectiveDomains(ctx.sequelize, domainStatusReady); + const knownBaseNames = await loadKnownCertBaseNames(ctx.sequelize); + const cleanupCtx = { stats, effectiveDomains, knownBaseNames, handledConfPaths, removedConfPaths, rollbackEntries }; + + console.log(`[CERT-SYNC] 下线清理开始: 生效域名 ${effectiveDomains.size} 个, 库内证书命名 ${knownBaseNames.size} 个, dry-run=${args.dryRun}`); + const clearableRows = await consumeRetiredDomains(ctx, cleanupCtx); + await cleanupOrphans(ctx, cleanupCtx); + + // 汇总统一改为「去重后」口径:dry-run 下同一文件可能被下线通道与孤儿通道各列一次, + // 直接相加各通道计数会虚增待删规模(发布前核对待删清单的关键输出) + const dedupedFiles = stats.cleanedPaths.size; + if (dedupedFiles === 0) { + console.log('[CERT-SYNC] 下线清理: 本轮无文件变更,跳过 nginx -t / reload'); + } else if (args.dryRun) { + console.log(`[CERT-SYNC][PLAN] 下线清理: 待删 ${formatCleanupDedupDetail(stats)}、不执行 nginx -t / reload、不写库`); + return; + } else if (args.skipNginx || ctx.nginxMissing) { + console.warn(`[CERT-SYNC] 下线清理: 已跳过 nginx -t / reload(${args.skipNginx ? '--skip-nginx' : '本机未检测到 nginx'}),文件删除已生效,清库继续`); + console.log(`[CERT-SYNC] 下线清理: 删除 ${formatCleanupDedupDetail(stats)}`); + } else { + const testResult = runNginxCommand(ctx.testCommand); + if (!testResult.ok) { + console.error(`[CERT-SYNC][RELOAD-FAIL] 下线清理后 nginx -t 失败: ${sanitizeErrorText(testResult.stderr || testResult.stdout)}`); + const restored = rollbackDeletedFiles(rollbackEntries); + if (rollbackEntries.length > 0) { + const retest = runNginxCommand(ctx.testCommand); + const retestTip = retest.ok + ? '(回滚后 nginx -t 已恢复)' + : `(回滚后 nginx -t 仍失败,请检查其它配置: ${sanitizeErrorText(retest.stderr || retest.stdout)})`; + console.error(`[CERT-SYNC][RELOAD-FAIL] 已回滚 ${restored}/${rollbackEntries.length} 个文件${retestTip}`); + } + stats.cleanupFailed++; + stats.clearedDeferred += clearableRows.length; + console.warn(`[CERT-SYNC][WARN] 本轮不清空 retired_domain(${clearableRows.length} 条保留,下一轮自动重试)`); + return; + } + const reloadResult = runNginxCommand(ctx.reloadCommand); + if (!reloadResult.ok) { + console.error(`[CERT-SYNC][RELOAD-FAIL] nginx -s reload 失败: ${sanitizeErrorText(reloadResult.stderr || reloadResult.stdout)}`); + stats.cleanupFailed++; + stats.clearedDeferred += clearableRows.length; + console.warn(`[CERT-SYNC][WARN] 本轮不清空 retired_domain(${clearableRows.length} 条保留,下一轮自动重试;文件已删除,重试幂等)`); + return; + } + console.log(`[CERT-SYNC] 下线清理: 删除 ${formatCleanupDedupDetail(stats)}`); + console.log('[CERT-SYNC] 下线清理: nginx -t 通过 + nginx -s reload 成功'); + } + + for (const item of clearableRows) { + const affected = await clearRetiredDomainSlot(ctx.sequelize, item.row, item.retiredDomain); + if (affected === 0) { + console.warn(`[CERT-SYNC][WARN] #${item.row.id} retired_domain 已被并发修改,本轮跳过清空(下一轮按新值处理)`); + stats.skippedRetired++; + continue; + } + stats.clearedRows++; + } +} + +/** + * 打印下线 / 清理统计(dry-run 时以待删口径输出)。 + * @param {Object} stats 统计对象。 + * @param {SyncArgs} args 参数集合。 + * @return {void} + */ +function printCleanupSummary(stats, args) { + const prefix = args.dryRun ? '[CERT-SYNC][PLAN] ' : '[CERT-SYNC] '; + const verb = args.dryRun ? '待删除' : '删除'; + console.log(`${prefix}旧域名下线: 处理 ${stats.scannedRetired} 条, ${verb} conf ${stats.retiredConfs} 个, 证书文件 ${stats.retiredCerts} 个, 跳过 ${stats.skippedRetired} 条`); + console.log(`${prefix}孤儿兜底清理: ${verb} conf ${stats.orphanConfs} 个, 证书文件 ${stats.orphanCerts} 个, 白名单未命中/超上限跳过 ${stats.skippedOrphan} 个`); + console.log(`${prefix}下线清理汇总: ${verb} ${formatCleanupDedupDetail(stats)}(其中通道重复列出 ${stats.duplicateListings} 行), 清空 retired_domain ${stats.clearedRows} 条(延后 ${stats.clearedDeferred} 条), 删除失败 ${stats.deleteFailed} 个, 路径越界拒绝 ${stats.refusedPaths} 个, 清理阶段失败 ${stats.cleanupFailed}`); +} + +/** + * 判定清理阶段是否存在需要告警的失败(统一退出码口径:删除失败 / 路径越界 / -t 或 reload 失败)。 + * @param {Object} stats 清理统计对象。 + * @return {boolean} 存在失败返回 true。 + */ +function hasCleanupFailure(stats) { + return stats.deleteFailed > 0 || stats.refusedPaths > 0 || stats.cleanupFailed > 0; +} + +/** + * 孤儿兜底清理:以目录实际文件为准做差集,回收 retired_domain 单槽位(只记最近一个) + * 丢失中间域名后残留的产物。 + * + * conf 白名单识别(三条全部命中才认为是本系统生成、才允许删除): + * 1. 文件名形态命中 CONF_FILE_NAME_PATTERN({domainSafe}{[_短哈希8]}.conf); + * 2. 文件内容包含本系统 upstreamInclude 路径字符串(本系统 server 块的固有标记); + * 3. 文件内容可解析出 server_name ,domain 通过域名白名单校验, + * 且用该 domain 反推的 conf 文件名与实际文件名完全一致(命名可回推一致)。 + * 未命中的文件一律不动(他人手工 conf、宝塔自身文件等),只计入跳过统计; + * 命中白名单且 domain 不在生效集合内(按小写比较)→ 列入待删。 + * + * 证书 / 私钥文件:仅处理文件名严格命中 CERT_FILE_NAME_PATTERN 的,且需同时满足 + * - 在「删除后的 conf 集合」上不被任何存活 conf 引用(引用判定覆盖全部 conf, + * 含未通过白名单的文件,避免删掉他人 conf 仍在使用的证书); + * - 不是库内任意一行 (store_id, domain) 的命名推导产物(可能只是 conf 尚未写出)。 + * 存在不可读 conf 时本轮整体跳过证书清理(无法证明「未被引用」,宁可不删)。 + * + * 单轮删除数量上限为 --limit,超出部分下一轮继续(cron 每分钟一次,可自然收敛)。 + * @param {SyncContext} ctx 运行上下文。 + * @param {Object} cleanupCtx 清理上下文(stats / effectiveDomains / knownBaseNames / handledConfPaths / rollbackEntries)。 + * @return {Promise} 无返回值。 + */ +async function cleanupOrphans(ctx, cleanupCtx) { + const { args } = ctx; + const { stats, effectiveDomains, knownBaseNames, handledConfPaths, removedConfPaths, rollbackEntries } = cleanupCtx; + + // ---------- 第一遍:conf 目录扫描 + 白名单判定 ---------- + const confEntries = readDirSafe(args.confDir); + const confContents = new Map(); + const orphanConfs = []; + let unreadableConf = false; + + for (const entry of confEntries) { + if (!/\.conf$/i.test(entry.name)) { + continue; + } + if (handledConfPaths.has(entry.fullPath)) { + continue; + } + const content = readTextFileOrNull(entry.fullPath); + if (content === null) { + unreadableConf = true; + continue; + } + confContents.set(entry.fullPath, content); + if (!CONF_FILE_NAME_PATTERN.test(entry.name)) { + stats.skippedOrphan++; + continue; + } + if (!content.includes(args.upstreamInclude)) { + stats.skippedOrphan++; + continue; + } + const domain = parseServerName(content); + if (!domain || domain.length > DOMAIN_MAX_LENGTH || !DOMAIN_PATTERN.test(domain)) { + stats.skippedOrphan++; + continue; + } + if (buildConfFileName(domain) !== entry.name) { + stats.skippedOrphan++; + continue; + } + if (effectiveDomains.has(domain.toLowerCase())) { + continue; + } + if (orphanConfs.length >= args.limit) { + stats.skippedOrphan++; + continue; + } + orphanConfs.push({ fullPath: entry.fullPath, domain }); + } + + // ---------- 第二遍:删除孤儿 conf ---------- + for (const item of orphanConfs) { + const result = deleteFileGuarded(ctx, args.confDir, item.fullPath, 'orphan', stats, rollbackEntries); + handledConfPaths.add(item.fullPath); + if (result === 'deleted' || result === 'planned') { + stats.orphanConfs++; + removedConfPaths.add(item.fullPath); + } + } + + // ---------- 第三遍:证书引用判定(在删除后的 conf 集合上判断) ---------- + if (unreadableConf) { + console.warn('[CERT-SYNC][WARN] conf 目录存在不可读文件,无法证明证书未被引用,本轮跳过证书孤儿清理'); + stats.skippedOrphan++; + return; + } + const referencedBaseNames = new Set(); + for (const entry of confEntries) { + if (!/\.conf$/i.test(entry.name)) { + continue; + } + if (removedConfPaths.has(entry.fullPath) || !fs.existsSync(entry.fullPath)) { + continue; + } + const content = confContents.has(entry.fullPath) ? confContents.get(entry.fullPath) : readTextFileOrNull(entry.fullPath); + if (content === null) { + continue; + } + collectCertBaseNames(content, referencedBaseNames); + } + + // ---------- 第四遍:证书 / 私钥文件差集删除 ---------- + let orphanCertCount = 0; + for (const entry of readDirSafe(args.certDir)) { + if (!CERT_FILE_NAME_PATTERN.test(entry.name)) { + continue; + } + const baseName = entry.name.replace(/\.(pem|key)$/i, ''); + if (referencedBaseNames.has(baseName)) { + continue; + } + if (knownBaseNames.has(baseName)) { + continue; + } + if (orphanCertCount >= args.limit) { + stats.skippedOrphan++; + continue; + } + const result = deleteFileGuarded(ctx, args.certDir, entry.fullPath, 'orphan', stats, rollbackEntries); + if (result === 'deleted' || result === 'planned') { + stats.orphanCerts++; + orphanCertCount++; + } + } +} + /** * 主流程:解析参数 → 连接数据库 → 扫描 → 逐条处理 → reload → 回写状态。 * @return {Promise} 无返回值,通过 process.exitCode 表达执行结果。 @@ -898,7 +1637,7 @@ async function main() { return; } - console.log(`[CERT-SYNC] 开始: env=${args.env} dry-run=${args.dryRun} skip-nginx=${args.skipNginx} limit=${args.limit}`); + console.log(`[CERT-SYNC] 开始: env=${args.env} dry-run=${args.dryRun} cleanup-only=${args.cleanupOnly} skip-nginx=${args.skipNginx} limit=${args.limit}`); console.log(`[CERT-SYNC] 外部配置文件: ${args.configPath}`); console.log(`[CERT-SYNC] 证书目录: ${args.certDir}`); console.log(`[CERT-SYNC] server 块目录: ${args.confDir}`); @@ -951,9 +1690,31 @@ async function main() { console.warn('[CERT-SYNC] 未检测到 wms_warehouse_front_config.domain_status 列(迁移尚未执行),本轮降级为不按域名验证状态过滤'); } + // 旧域名下线 + 孤儿兜底清理(两阶段域名切换的 nginx 侧配套): + // 放在证书同步之前执行,避免同一轮内「写新 conf」与「删旧 conf」互相干扰; + // 本流程自带一次 nginx -t / reload(仅在本轮确实删过文件时),与下面的同步流程互不耦合。 + const cleanupStats = createCleanupStats(); + const retiredDomainReady = await hasRetiredDomainColumn(sequelize); + if (!retiredDomainReady) { + console.warn('[CERT-SYNC] 未检测到 wms_warehouse_front_config.retired_domain 列(迁移尚未执行),本轮跳过旧域名下线与孤儿兜底清理'); + } else { + await runCleanup(ctx, domainStatusReady, cleanupStats); + printCleanupSummary(cleanupStats, args); + } + + // --cleanup-only:只做下线 / 清理,不做证书同步(供运维单独执行) + if (args.cleanupOnly) { + console.log(`[CERT-SYNC][SUMMARY] --cleanup-only 完成: 清空 retired_domain ${cleanupStats.clearedRows} 条, ${args.dryRun ? '待删' : '删除'} ${formatCleanupDedupDetail(cleanupStats)}, 通道命中明细(不去重)conf ${cleanupStats.retiredConfs + cleanupStats.orphanConfs} 个 / 证书文件 ${cleanupStats.retiredCerts + cleanupStats.orphanCerts} 个, 删除失败 ${cleanupStats.deleteFailed} 个`); + process.exitCode = hasCleanupFailure(cleanupStats) ? EXIT_BUSINESS_FAILURE : EXIT_OK; + return; + } + const rows = await scanPendingRows(sequelize, args.limit, domainStatusReady); if (!rows || rows.length === 0) { console.log('[CERT-SYNC] 无待同步记录,静默退出'); + if (hasCleanupFailure(cleanupStats)) { + process.exitCode = EXIT_BUSINESS_FAILURE; + } return; } @@ -980,7 +1741,7 @@ async function main() { } console.log(`[CERT-SYNC][SUMMARY] total=${stats.total} success=${stats.success} failed=${stats.failed}`); - if (stats.failed > 0) { + if (stats.failed > 0 || hasCleanupFailure(cleanupStats)) { process.exitCode = EXIT_BUSINESS_FAILURE; } else { process.exitCode = EXIT_OK;