- 发布/回退链路新增固定文件排除与保护:warehouse-cert-sync 两份环境配置不进发布包、清理时暂存/放回,服务器副本不被覆盖/删除(deploy-lib 独立常量 + 共用守卫函数) - deploy-cert-sync 新增 config-check 只读核对;install 对 prod 配置首次放置一次、之后永不覆盖;dev 配置永不上传生产 - 薄包装简化:nginx 二进制/upstream/reload 全部收敛到 prod 配置,包装仅保留 cwd+依赖+DB 运行时引导 - 新增 warehouse-cert-sync.cjs 与 dev/prod 双环境配置;package.json 增加 deploy:cert-sync:config-check
964 lines
40 KiB
JavaScript
964 lines
40 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 仓库前端证书同步脚本(nginx 部署器)。
|
||
*
|
||
* 职责:扫描 wms_warehouse_front_config 中 sync_status=0(待同步)的记录,
|
||
* 将 cert_pem / cert_key 内容写为证书文件,生成对应域名 server 块,
|
||
* 逐条 nginx -t 校验后统一 reload,并回写部署状态。
|
||
*
|
||
* 设计要点:
|
||
* - 每分钟被服务器侧 root crontab 调用(薄 shell 包装由部署 agent 适配),
|
||
* 必须幂等、可重入:重复执行会覆盖同名文件与 conf,无副作用。
|
||
* - 逐条失败隔离:单条写文件失败 / nginx -t 失败只回写该条
|
||
* sync_status=2 + last_sync_error + sync_attempts+1,不影响其他条目;
|
||
* nginx -t 失败时撤销本条 server 块,保证不拖垮整体 nginx。
|
||
* - reload 策略:本批有成功条目时执行一次 nginx -s reload;
|
||
* reload 失败时本批成功条目回退 sync_status=0(下分钟自动重试,
|
||
* sync_attempts 达到 RELOAD_FAILURE_MAX_RETRY 后转 sync_status=2 终止重试)。
|
||
* - 敏感红线:任何日志/错误输出不得打印 cert_pem / cert_key 内容,
|
||
* last_sync_error 统一做 PEM 片段脱敏 + 500 字符截断。
|
||
* - 文件权限:私钥 0600、证书/conf 0644;写文件采用「临时文件 + rename」原子写。
|
||
* - DB 读写全部参数化(sequelize replacements),禁止字符串拼接 SQL。
|
||
* - 数据库目标显式处理:--env 决定 WMS_DB_TARGET,先于 require 设置,
|
||
* 覆盖终端残留的 NODE_ENV,避免误连生产库。
|
||
*
|
||
* 部署约定(供部署 agent):
|
||
* - 脚本以部署根目录为 cwd 运行(薄 shell 包装中先 cd 到部署根目录)。
|
||
* - 双环境外部配置文件(随代码 git 提交,发布时严禁覆盖/删除/改写):
|
||
* script/warehouse-cert-sync.config.dev.json 开发环境(本机宝塔 Windows nginx)
|
||
* script/warehouse-cert-sync.config.prod.json 生产环境(服务器宝塔构建 nginx,systemd)
|
||
* 按 --env 自动选择;所选配置缺失或 JSON 非法一律致命退出(exit 1),
|
||
* 生产环境宁可不跑也不猜,开发环境同样显式报错而非静默回退。
|
||
* - 参数优先级:CLI 参数 > 环境变量 > 外部配置文件 > 内置默认。
|
||
* - 证书目录 / nginx server 块目录 / upstream include / nginx 二进制 /
|
||
* reload 与 -t 命令默认全部来自配置文件;生产配置中 certDir/confDir 相对
|
||
* cwd(部署根 /www/wwwroot/qimen)解析为 ../.ssl 与 ../nginx_config,
|
||
* 目录不存在时自动递归创建;nginx 主配置由运维自行 include ../nginx_config/*.conf,
|
||
* 本脚本不管理主配置。
|
||
* - 自定义命令占位符:{nginxBin} 与 {nginxPrefix}(-p 前缀目录,win32 宝塔 nginx
|
||
* 防 cwd 劫持),替换时自动按平台加引号;覆盖命令经 shell 执行。
|
||
* - 建议 crontab 每分钟调用一次薄 shell 包装(crontab 安装由部署 agent 负责)。
|
||
*
|
||
* 用法:
|
||
* node script/warehouse-cert-sync.cjs --env development --skip-nginx --dry-run
|
||
* node script/warehouse-cert-sync.cjs --env development \
|
||
* --cert-dir <临时目录> --conf-dir <临时目录> --skip-nginx \
|
||
* --nginx-bin <fake-nginx> --reload-cmd <覆盖命令>
|
||
* node script/warehouse-cert-sync.cjs --env production
|
||
*
|
||
* 退出码约定(供 crontab 告警):
|
||
* 0 正常:无待处理行、本批全部成功、dry-run 完成;
|
||
* 1 致命:参数非法 / DB 连接失败 / 目录不可建 / 生产环境 nginx 缺失;
|
||
* 2 业务级失败:本批存在条目处理失败或 nginx reload 失败(详情已回写 DB)。
|
||
*/
|
||
|
||
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const crypto = require('node:crypto');
|
||
const { spawnSync } = require('node:child_process');
|
||
|
||
const Sequelize = require('sequelize');
|
||
|
||
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
||
const DB_CONFIG_PATH = path.join(PROJECT_ROOT, 'database', 'config.js');
|
||
|
||
// 部署约定默认目录:相对脚本运行时 cwd(部署根目录)的上一级,
|
||
// path.resolve(process.cwd(), ...) 在运行时解析为绝对路径;
|
||
// nginx 主配置由运维自行 include 该 conf 目录,本脚本不管理主配置。
|
||
const DEFAULT_CERT_DIR = path.resolve(process.cwd(), '..', '.ssl');
|
||
const DEFAULT_CONF_DIR = path.resolve(process.cwd(), '..', 'nginx_config');
|
||
const DEFAULT_UPSTREAM_INCLUDE = 'nginx-upstream.conf';
|
||
const DEFAULT_NGINX_BIN = 'nginx';
|
||
const DEFAULT_LIMIT = 50;
|
||
|
||
// 双环境外部配置文件:按 --env 选择(文件随代码 git 提交,发布时不得覆盖删除)。
|
||
// development → 本机宝塔 Windows nginx(nginx.exe + -p 前缀);production → 服务器宝塔构建 nginx(systemd)。
|
||
const ENV_CONFIG_FILE = {
|
||
development: path.join(__dirname, 'warehouse-cert-sync.config.dev.json'),
|
||
production: path.join(__dirname, 'warehouse-cert-sync.config.prod.json'),
|
||
};
|
||
|
||
const NGINX_CMD_TIMEOUT_MS = 30 * 1000;
|
||
const LAST_SYNC_ERROR_MAX_LENGTH = 500;
|
||
const RELOAD_FAILURE_MAX_RETRY = 5;
|
||
const DOMAIN_MAX_LENGTH = 253;
|
||
const DOMAIN_PATTERN = /^[A-Za-z0-9*._-]+$/;
|
||
const PEM_CERT_MARK = 'BEGIN CERTIFICATE';
|
||
const PEM_KEY_MARK = 'PRIVATE KEY';
|
||
|
||
const EXIT_OK = 0;
|
||
const EXIT_FATAL = 1;
|
||
const EXIT_BUSINESS_FAILURE = 2;
|
||
|
||
const CERT_FILE_MODE = 0o644;
|
||
const KEY_FILE_MODE = 0o600;
|
||
|
||
const TABLE_NAME = 'wms_warehouse_front_config';
|
||
|
||
/**
|
||
* 单条处理结果统计。
|
||
* @typedef {Object} SyncStats
|
||
* @property {number} total 本批扫描到的待同步条数。
|
||
* @property {number} success 成功条数。
|
||
* @property {number} failed 失败条数。
|
||
*/
|
||
|
||
/**
|
||
* 命令行参数集合。
|
||
* @typedef {Object} SyncArgs
|
||
* @property {string} env 数据库环境标记:development | production。
|
||
* @property {string} configPath 生效的外部配置文件绝对路径。
|
||
* @property {boolean} dryRun 仅扫描与输出计划,不写文件、不改库。
|
||
* @property {boolean} skipNginx 跳过 nginx -t / reload。
|
||
* @property {string} certDir 证书输出目录(绝对路径)。
|
||
* @property {string} confDir nginx server 块目录(绝对路径)。
|
||
* @property {string} upstreamInclude upstream 配置文件 include 名。
|
||
* @property {number} limit 单次处理条数上限。
|
||
* @property {string} nginxBin nginx 可执行文件路径。
|
||
* @property {string} nginxPrefix nginx -p 前缀目录(空串表示不启用)。
|
||
* @property {string} reloadCmd nginx reload 命令覆盖(空串按系统类型推导)。
|
||
* @property {string} testCmd nginx -t 校验命令覆盖(空串按系统类型推导)。
|
||
* @property {boolean} help 是否打印帮助。
|
||
*/
|
||
|
||
/**
|
||
* nginx 命令执行描述(由 buildNginxCommand 统一产出)。
|
||
* @typedef {Object} NginxCommandDesc
|
||
* @property {string} command 命令字符串(shell 模式)或可执行文件路径(直连模式)。
|
||
* @property {string[]} args 直连模式的参数数组(shell 模式为空数组)。
|
||
* @property {boolean} shell 是否经 shell 执行(true 时 command 为完整命令字符串)。
|
||
*/
|
||
|
||
/**
|
||
* 脚本运行上下文(参数、DB 连接、nginx 可用性等)。
|
||
* @typedef {Object} SyncContext
|
||
* @property {SyncArgs} args 解析后的命令行参数。
|
||
* @property {import('sequelize').Sequelize} sequelize Sequelize 实例。
|
||
* @property {boolean} nginxMissing nginx 二进制缺失(开发环境自动降级)。
|
||
* @property {NginxCommandDesc} testCommand nginx -t 校验命令执行描述。
|
||
* @property {NginxCommandDesc} reloadCommand nginx reload 命令执行描述。
|
||
*/
|
||
|
||
/**
|
||
* 打印帮助信息(stdout)。
|
||
* @return {void}
|
||
*/
|
||
function printUsage() {
|
||
const text = [
|
||
'仓库前端证书同步脚本(nginx 部署器)',
|
||
'',
|
||
'用法: node script/warehouse-cert-sync.cjs --env <development|production> [选项]',
|
||
'',
|
||
'环境配置文件(按 --env 自动加载,随代码提交,发布时不得覆盖删除):',
|
||
' script/warehouse-cert-sync.config.dev.json 开发环境(本机宝塔 Windows nginx)',
|
||
' script/warehouse-cert-sync.config.prod.json 生产环境(服务器宝塔构建 nginx,systemd)',
|
||
' 所选配置缺失或 JSON 非法将致命退出(exit 1),生产环境宁可不跑也不猜。',
|
||
'',
|
||
'参数优先级: CLI 参数 > 环境变量 > 外部配置文件 > 内置默认',
|
||
'',
|
||
'选项:',
|
||
' --env <env> 运行环境(必填,显式指定,不受终端 NODE_ENV 残留影响,同时决定数据库与配置文件)',
|
||
' --dry-run 仅扫描并输出计划,不写文件、不改库',
|
||
' --cert-dir <path> 证书输出目录(默认取配置文件或 ../.ssl,相对路径按 cwd 解析)',
|
||
' --conf-dir <path> nginx server 块目录(默认取配置文件或 ../nginx_config,同样按 cwd 解析)',
|
||
' --upstream-include <v> server 块 include 的 upstream 配置名(默认取配置文件或 nginx-upstream.conf)',
|
||
' --skip-nginx 跳过 nginx -t 校验与 nginx reload(开发/测试用)',
|
||
' --nginx-bin <path> nginx 可执行文件路径(默认取配置文件或 PATH 中的 nginx)',
|
||
' --nginx-prefix <path> nginx -p 前缀目录(win32 宝塔 nginx 防 cwd 劫持,默认取配置文件)',
|
||
' --reload-cmd <cmd> reload 命令覆盖(默认按系统类型推导 "{nginxBin} -s reload";',
|
||
' 支持 {nginxBin} / {nginxPrefix} 占位符且自动按平台加引号;',
|
||
' linux 可传 "systemctl reload nginx")',
|
||
' --test-cmd <cmd> nginx -t 校验命令覆盖(默认按系统类型推导 "{nginxBin} -t",语义同上)',
|
||
' --limit <n> 单次处理条数上限(默认 50)',
|
||
' --help, -h 打印本帮助',
|
||
'',
|
||
'环境变量(优先级高于配置文件、低于命令行参数):',
|
||
' CERT_SYNC_ENV / CERT_SYNC_CERT_DIR / CERT_SYNC_CONF_DIR / CERT_SYNC_UPSTREAM_INCLUDE',
|
||
' CERT_SYNC_LIMIT / CERT_SYNC_NGINX_BIN / CERT_SYNC_NGINX_PREFIX',
|
||
' CERT_SYNC_RELOAD_CMD / CERT_SYNC_TEST_CMD',
|
||
' WMS_DB_TARGET / DB_HOST / DB_PORT / DB_USER / DB_PASSWORD / DB_NAME(透传 database/config.js)',
|
||
'',
|
||
'退出码: 0 正常;1 致命错误;2 业务级失败(条目失败或 reload 失败,供 cron 告警)',
|
||
].join('\n');
|
||
console.log(text);
|
||
}
|
||
|
||
/**
|
||
* 加载 --env 对应的外部配置文件(双环境外部配置文件机制)。
|
||
* 文件随代码 git 提交;所选配置缺失或 JSON 非法一律视为致命错误,
|
||
* 生产环境宁可不跑也不猜,开发环境同样显式报错而非静默回退内置默认。
|
||
* @param {string} env 数据库环境标记:development | production。
|
||
* @return {{configPath: string, config: Object}} 配置文件绝对路径与解析后的配置对象。
|
||
* @throws {Error} 配置文件缺失、JSON 非法或顶层结构非对象时抛出异常(触发退出码 1)。
|
||
*/
|
||
function loadEnvConfig(env) {
|
||
const configPath = ENV_CONFIG_FILE[env];
|
||
let rawText = '';
|
||
try {
|
||
rawText = fs.readFileSync(configPath, 'utf8');
|
||
} catch (error) {
|
||
throw new Error(`环境配置文件缺失: ${configPath}(--env=${env} 必须搭配对应配置文件;生产环境宁可不跑也不猜,请恢复该文件或改用显式 CLI 参数覆盖)`);
|
||
}
|
||
let config = null;
|
||
try {
|
||
config = JSON.parse(rawText);
|
||
} catch (error) {
|
||
throw new Error(`环境配置文件 JSON 非法: ${configPath} - ${error.message}`);
|
||
}
|
||
if (!config || typeof config !== 'object' || Array.isArray(config)) {
|
||
throw new Error(`环境配置文件结构非法: ${configPath}(顶层必须是 JSON 对象)`);
|
||
}
|
||
return { configPath, config };
|
||
}
|
||
|
||
/**
|
||
* 解析命令行参数:CLI 参数 > 环境变量 > 外部配置文件 > 内置默认(优先级从高到低)。
|
||
* 先解析 CLI 原始值,再按 --env 加载外部配置文件作为默认值来源,
|
||
* 最后统一应用优先级回填缺省字段(目录类值经 path.resolve 解析为绝对路径)。
|
||
* @param {string[]} argv process.argv.slice(2) 结果。
|
||
* @return {SyncArgs} 解析后的参数集合。
|
||
* @throws {Error} 参数非法或配置文件缺失/非法时抛出异常(触发退出码 1)。
|
||
*/
|
||
function parseArgs(argv) {
|
||
const cli = {
|
||
env: '',
|
||
certDir: '',
|
||
confDir: '',
|
||
upstreamInclude: '',
|
||
nginxBin: '',
|
||
nginxPrefix: '',
|
||
reloadCmd: '',
|
||
testCmd: '',
|
||
limit: null,
|
||
dryRun: false,
|
||
skipNginx: false,
|
||
help: false,
|
||
};
|
||
|
||
for (let index = 0; index < argv.length; index++) {
|
||
const item = argv[index];
|
||
const eqIndex = item.indexOf('=');
|
||
const key = eqIndex >= 0 ? item.slice(0, eqIndex) : item;
|
||
const inlineValue = eqIndex >= 0 ? item.slice(eqIndex + 1) : null;
|
||
|
||
const readValue = () => {
|
||
if (inlineValue !== null) {
|
||
return inlineValue;
|
||
}
|
||
if (index + 1 >= argv.length) {
|
||
throw new Error(`参数 ${key} 缺少值`);
|
||
}
|
||
index++;
|
||
return argv[index];
|
||
};
|
||
|
||
if (key === '--help' || key === '-h') {
|
||
cli.help = true;
|
||
} else if (key === '--dry-run') {
|
||
cli.dryRun = true;
|
||
} else if (key === '--skip-nginx') {
|
||
cli.skipNginx = true;
|
||
} else if (key === '--env') {
|
||
cli.env = String(readValue() || '').trim();
|
||
} else if (key === '--cert-dir') {
|
||
cli.certDir = String(readValue() || '').trim();
|
||
} else if (key === '--conf-dir') {
|
||
cli.confDir = String(readValue() || '').trim();
|
||
} else if (key === '--upstream-include') {
|
||
cli.upstreamInclude = String(readValue() || '').trim();
|
||
} else if (key === '--nginx-bin') {
|
||
cli.nginxBin = String(readValue() || '').trim();
|
||
} else if (key === '--nginx-prefix') {
|
||
cli.nginxPrefix = String(readValue() || '').trim();
|
||
} else if (key === '--reload-cmd') {
|
||
cli.reloadCmd = String(readValue() || '').trim();
|
||
} else if (key === '--test-cmd') {
|
||
cli.testCmd = String(readValue() || '').trim();
|
||
} else if (key === '--limit') {
|
||
cli.limit = Number(readValue());
|
||
} else {
|
||
throw new Error(`未知参数: ${item}`);
|
||
}
|
||
}
|
||
|
||
if (cli.help) {
|
||
return {
|
||
env: cli.env || String(process.env.CERT_SYNC_ENV || '').trim(),
|
||
configPath: '',
|
||
certDir: '',
|
||
confDir: '',
|
||
upstreamInclude: '',
|
||
nginxBin: '',
|
||
nginxPrefix: '',
|
||
reloadCmd: '',
|
||
testCmd: '',
|
||
limit: DEFAULT_LIMIT,
|
||
dryRun: cli.dryRun,
|
||
skipNginx: cli.skipNginx,
|
||
help: true,
|
||
};
|
||
}
|
||
|
||
const env = cli.env || String(process.env.CERT_SYNC_ENV || '').trim();
|
||
if (env !== 'development' && env !== 'production') {
|
||
throw new Error('必须通过 --env 显式指定运行环境:development | production');
|
||
}
|
||
|
||
// 外部配置文件作为默认值来源(优先级低于 CLI 参数与环境变量)
|
||
const { configPath, config } = loadEnvConfig(env);
|
||
|
||
// 优先级:CLI 参数 > 环境变量 > 外部配置文件 > 内置默认
|
||
const pickString = (cliValue, envName, configValue, fallback) => {
|
||
const envValue = String(process.env[envName] || '').trim();
|
||
const configString = configValue === undefined || configValue === null ? '' : String(configValue).trim();
|
||
const value = cliValue || envValue || configString || fallback;
|
||
return String(value).trim();
|
||
};
|
||
|
||
const args = {
|
||
env,
|
||
configPath,
|
||
dryRun: cli.dryRun,
|
||
skipNginx: cli.skipNginx,
|
||
help: false,
|
||
certDir: path.resolve(pickString(cli.certDir, 'CERT_SYNC_CERT_DIR', config.certDir, DEFAULT_CERT_DIR)),
|
||
confDir: path.resolve(pickString(cli.confDir, 'CERT_SYNC_CONF_DIR', config.confDir, DEFAULT_CONF_DIR)),
|
||
upstreamInclude: pickString(cli.upstreamInclude, 'CERT_SYNC_UPSTREAM_INCLUDE', config.upstreamInclude, DEFAULT_UPSTREAM_INCLUDE),
|
||
nginxBin: pickString(cli.nginxBin, 'CERT_SYNC_NGINX_BIN', config.nginxBin, DEFAULT_NGINX_BIN),
|
||
nginxPrefix: pickString(cli.nginxPrefix, 'CERT_SYNC_NGINX_PREFIX', config.nginxPrefix, ''),
|
||
reloadCmd: pickString(cli.reloadCmd, 'CERT_SYNC_RELOAD_CMD', config.reloadCmd, ''),
|
||
testCmd: pickString(cli.testCmd, 'CERT_SYNC_TEST_CMD', config.testCmd, ''),
|
||
limit: cli.limit !== null ? cli.limit : (Number(process.env.CERT_SYNC_LIMIT) || DEFAULT_LIMIT),
|
||
};
|
||
|
||
if (!Number.isInteger(args.limit) || args.limit <= 0) {
|
||
throw new Error(`--limit 必须是正整数,当前值: ${args.limit}`);
|
||
}
|
||
if (!args.upstreamInclude) {
|
||
throw new Error('--upstream-include 不能为空');
|
||
}
|
||
if (!args.nginxBin) {
|
||
throw new Error('--nginx-bin 不能为空');
|
||
}
|
||
|
||
return args;
|
||
}
|
||
|
||
/**
|
||
* 按 --env 显式设置数据库连接目标,覆盖终端残留的 NODE_ENV。
|
||
* WMS_DB_TARGET 优先级高于 NODE_ENV(database/config.js 约定),
|
||
* 在 require database/config.js 之前调用本函数即可消除残留风险。
|
||
* @param {SyncArgs} args 解析后的参数集合。
|
||
* @return {void}
|
||
*/
|
||
function applyDbTarget(args) {
|
||
const target = args.env === 'production' ? 'production' : 'development';
|
||
process.env.WMS_DB_TARGET = target;
|
||
const residue = process.env.NODE_ENV;
|
||
if (residue && String(residue).trim().toLowerCase() !== String(target).toLowerCase()) {
|
||
console.warn(`[CERT-SYNC] 检测到终端残留 NODE_ENV=${residue},已由 --env=${args.env} 显式覆盖(WMS_DB_TARGET=${target})`);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 加载 database/config.js 并创建 Sequelize 实例。
|
||
* @param {SyncArgs} args 解析后的参数集合。
|
||
* @return {import('sequelize').Sequelize} Sequelize 实例。
|
||
* @throws {Error} 配置加载失败时抛出异常。
|
||
*/
|
||
function connectDb(args) {
|
||
const dbConfigAll = require(DB_CONFIG_PATH);
|
||
const cfg = dbConfigAll[args.env] || dbConfigAll.production;
|
||
if (!cfg || !cfg.host) {
|
||
throw new Error('数据库配置解析失败,请检查 --env 与 config 文件');
|
||
}
|
||
|
||
const sequelize = new Sequelize(cfg.database, cfg.username, cfg.password, {
|
||
host: cfg.host,
|
||
port: cfg.port,
|
||
dialect: cfg.dialect || 'mysql',
|
||
logging: false,
|
||
define: cfg.define,
|
||
pool: cfg.pool,
|
||
});
|
||
console.log(`[CERT-SYNC] 数据库目标: ${args.env} -> ${cfg.host}:${cfg.port}/${cfg.database}`);
|
||
return sequelize;
|
||
}
|
||
|
||
/**
|
||
* 统一构造 nginx 命令执行描述(系统类型分支集中在唯一入口)。
|
||
*
|
||
* 分支策略:
|
||
* - 自定义命令(reloadCmd / testCmd 覆盖)非空:优先采用覆盖命令字符串,
|
||
* 经 shell 执行(win32 由 cmd.exe 包装、POSIX 由 /bin/sh 包装),
|
||
* 覆盖 systemctl 等非标准 reload 场景;{nginxBin} / {nginxPrefix} 占位符
|
||
* 替换时自动按平台加引号(win32 双引号、POSIX 单引号),
|
||
* 避免路径被 shell 按空白/斜杠二次切分。
|
||
* - win32:nginx 二进制为 .cmd/.bat 批处理时不能被 spawnSync 直接执行,
|
||
* 保持经 shell 包装整条命令(参数做引号转义)的执行方式。
|
||
* - linux / darwin / 其他 POSIX:spawnSync(nginxBin, args, shell=false)
|
||
* 原样执行,避免 shell 注入与引号歧义。
|
||
* @param {string} nginxBin nginx 可执行文件路径。
|
||
* @param {string} nginxPrefix nginx -p 前缀目录(空串表示不启用,供 {nginxPrefix} 占位符使用)。
|
||
* @param {string[]} args nginx 参数数组(默认推导场景)。
|
||
* @param {string} customCommand 自定义覆盖命令(空串表示使用默认推导)。
|
||
* @return {NginxCommandDesc} 命令执行描述。
|
||
* @throws {Error} 自定义命令含 {nginxPrefix} 占位符但未提供 nginxPrefix 时抛出异常。
|
||
*/
|
||
function buildNginxCommand(nginxBin, nginxPrefix, args, customCommand) {
|
||
if (customCommand) {
|
||
if (String(customCommand).includes('{nginxPrefix}') && !nginxPrefix) {
|
||
throw new Error('自定义命令包含 {nginxPrefix} 占位符,但未配置 nginx prefix(--nginx-prefix / CERT_SYNC_NGINX_PREFIX / 配置文件 nginxPrefix)');
|
||
}
|
||
// 占位符替换时自动按平台加引号,防止路径被 shell 按空白/斜杠二次切分
|
||
const quoteShellValue = value => (process.platform === 'win32'
|
||
? `"${String(value).replace(/"/g, '')}"`
|
||
: `'${String(value).replace(/'/g, '')}'`);
|
||
return {
|
||
command: String(customCommand)
|
||
.split('{nginxBin}')
|
||
.join(quoteShellValue(nginxBin))
|
||
.split('{nginxPrefix}')
|
||
.join(quoteShellValue(nginxPrefix)),
|
||
args: [],
|
||
shell: true,
|
||
};
|
||
}
|
||
const isWindowsBatch = process.platform === 'win32' && /\.(cmd|bat)$/i.test(nginxBin);
|
||
if (isWindowsBatch) {
|
||
const quote = value => `"${String(value).replace(/"/g, '')}"`;
|
||
return {
|
||
command: `${quote(nginxBin)} ${args.map(quote).join(' ')}`,
|
||
args: [],
|
||
shell: true,
|
||
};
|
||
}
|
||
return {
|
||
command: nginxBin,
|
||
args,
|
||
shell: false,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 执行 nginx 命令的底层 spawnSync 封装。
|
||
* 仅消费 buildNginxCommand 产出的执行描述,不做任何平台判断。
|
||
* @param {NginxCommandDesc} commandDesc nginx 命令执行描述。
|
||
* @return {import('node:child_process').SpawnSyncReturns<string>} spawnSync 原始结果。
|
||
*/
|
||
function spawnNginxRaw(commandDesc) {
|
||
if (commandDesc.shell) {
|
||
return spawnSync(commandDesc.command, {
|
||
encoding: 'utf8',
|
||
timeout: NGINX_CMD_TIMEOUT_MS,
|
||
shell: true,
|
||
});
|
||
}
|
||
return spawnSync(commandDesc.command, commandDesc.args, {
|
||
encoding: 'utf8',
|
||
timeout: NGINX_CMD_TIMEOUT_MS,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 探测 nginx 二进制是否可用(ENOENT 判定缺失)。
|
||
* @param {string} nginxBin nginx 可执行文件路径。
|
||
* @param {string} nginxPrefix nginx -p 前缀目录(空串表示不启用)。
|
||
* @return {boolean} 可用返回 true,缺失返回 false。
|
||
*/
|
||
function nginxBinExists(nginxBin, nginxPrefix) {
|
||
const result = spawnNginxRaw(buildNginxCommand(nginxBin, nginxPrefix, [ '-v' ], ''));
|
||
return !(result.error && result.error.code === 'ENOENT');
|
||
}
|
||
|
||
/**
|
||
* 扫描待同步记录(sync_status=0,按 id 升序,limit 条)。
|
||
* @param {import('sequelize').Sequelize} sequelize Sequelize 实例。
|
||
* @param {number} limit 单次处理条数上限。
|
||
* @return {Promise<Object[]>} 待同步记录数组。
|
||
*/
|
||
async function scanPendingRows(sequelize, limit) {
|
||
return sequelize.query(
|
||
`SELECT id, store_id, domain, cert_pem, cert_key, cert_expire_at, sync_attempts
|
||
FROM ${TABLE_NAME}
|
||
WHERE sync_status = 0
|
||
ORDER BY id ASC
|
||
LIMIT ?`,
|
||
{ replacements: [ limit ], type: Sequelize.QueryTypes.SELECT }
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 将域名清洗为安全的文件名片段(非法字符替换为下划线)。
|
||
* @param {string} input 原始域名。
|
||
* @return {string} 清洗后的片段,空结果返回 'unknown'。
|
||
*/
|
||
function sanitizeFileNameSegment(input) {
|
||
const cleaned = String(input || '').replace(/[^A-Za-z0-9._-]+/g, '_').replace(/^_+|_+$/g, '');
|
||
return cleaned || 'unknown';
|
||
}
|
||
|
||
/**
|
||
* 计算域名的 8 位短哈希(仅当域名被清洗后与原名不一致时用于防碰撞)。
|
||
* @param {string} domain 原始域名。
|
||
* @return {string} 8 位十六进制哈希。
|
||
*/
|
||
function shortHash(domain) {
|
||
return crypto.createHash('md5')
|
||
.update(String(domain))
|
||
.digest('hex')
|
||
.slice(0, 8);
|
||
}
|
||
|
||
/**
|
||
* 生成一条记录的文件名与落盘路径。
|
||
* 命名规则:{store_id}_{domain};domain 做非法字符清洗,清洗后与原名不一致时
|
||
* 追加 _哈希8 防不同域名清洗后碰撞。
|
||
* @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}`;
|
||
return {
|
||
baseName,
|
||
certPath: path.join(args.certDir, `${baseName}.pem`),
|
||
keyPath: path.join(args.certDir, `${baseName}.key`),
|
||
confPath: path.join(args.confDir, `${domainSafe}${suffix}.conf`),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 校验单条记录的 domain 与证书/私钥内容(不做加密学解析,仅做安全与形态校验)。
|
||
* @param {string} domainRaw 原始域名。
|
||
* @param {*} certPem 证书 PEM 内容。
|
||
* @param {*} keyPem 私钥 PEM 内容。
|
||
* @return {string|null} 校验失败返回错误描述,通过返回 null。
|
||
*/
|
||
function validateRowContent(domainRaw, certPem, keyPem) {
|
||
if (!domainRaw) {
|
||
return 'domain 为空,无法生成文件名与 server 块';
|
||
}
|
||
if (domainRaw.length > DOMAIN_MAX_LENGTH || !DOMAIN_PATTERN.test(domainRaw)) {
|
||
return `domain 含非法字符或超长(${domainRaw.length} 字符)`;
|
||
}
|
||
const cert = String(certPem || '').trim();
|
||
const key = String(keyPem || '').trim();
|
||
if (!cert) {
|
||
return 'cert_pem 内容为空';
|
||
}
|
||
if (!key) {
|
||
return 'cert_key 内容为空';
|
||
}
|
||
if (!cert.includes(PEM_CERT_MARK)) {
|
||
return `cert_pem 缺少 ${PEM_CERT_MARK} 标记,不是合法证书 PEM`;
|
||
}
|
||
if (!key.includes(PEM_KEY_MARK)) {
|
||
return `cert_key 缺少 ${PEM_KEY_MARK} 标记,不是合法私钥 PEM`;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 生成 server 块内容(与项目现有 nginx 风格一致)。
|
||
* @param {string} domain 域名(已通过白名单校验)。
|
||
* @param {string} certPath 证书文件绝对路径。
|
||
* @param {string} keyPath 私钥文件绝对路径。
|
||
* @param {string} upstreamInclude upstream 配置文件 include 名。
|
||
* @return {string} server 块文本。
|
||
*/
|
||
function buildServerConf(domain, certPath, keyPath, upstreamInclude) {
|
||
return `server {
|
||
listen 443 ssl;
|
||
server_name ${domain};
|
||
ssl_certificate ${certPath};
|
||
ssl_certificate_key ${keyPath};
|
||
location / {
|
||
include ${upstreamInclude};
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Warehouse-Domain $host;
|
||
}
|
||
}
|
||
`;
|
||
}
|
||
|
||
/**
|
||
* 原子写文件:先写同目录临时文件(指定权限),rename 覆盖目标;
|
||
* 失败时清理临时文件后抛出异常。
|
||
* @param {string} filePath 目标文件绝对路径。
|
||
* @param {string} content 文件内容。
|
||
* @param {number} mode 文件权限(八进制)。
|
||
* @return {void}
|
||
* @throws {Error} 写文件失败时抛出异常。
|
||
*/
|
||
function writeFileAtomic(filePath, content, mode) {
|
||
const dir = path.dirname(filePath);
|
||
const tmpPath = path.join(dir, `.${path.basename(filePath)}.tmp-${process.pid}-${Date.now()}`);
|
||
try {
|
||
fs.writeFileSync(tmpPath, content, { encoding: 'utf8', mode });
|
||
fs.chmodSync(tmpPath, mode);
|
||
fs.renameSync(tmpPath, filePath);
|
||
} catch (error) {
|
||
try {
|
||
fs.unlinkSync(tmpPath);
|
||
} catch (cleanupError) {
|
||
// 清理临时文件失败可忽略,不影响主错误
|
||
}
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 静默删除文件(不存在时忽略)。
|
||
* @param {string} filePath 文件绝对路径。
|
||
* @return {void}
|
||
*/
|
||
function removeFileQuiet(filePath) {
|
||
try {
|
||
fs.unlinkSync(filePath);
|
||
} catch (error) {
|
||
if (error && error.code !== 'ENOENT') {
|
||
console.warn(`[CERT-SYNC] 删除文件失败: ${filePath} - ${error.message}`);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 执行 nginx 命令(同步,带超时),返回状态与输出。
|
||
* @param {NginxCommandDesc} commandDesc nginx 命令执行描述。
|
||
* @return {{ok: boolean, stdout: string, stderr: string}} 执行结果。
|
||
*/
|
||
function runNginxCommand(commandDesc) {
|
||
const result = spawnNginxRaw(commandDesc);
|
||
const label = commandDesc.shell
|
||
? commandDesc.command
|
||
: `${commandDesc.command} ${commandDesc.args.join(' ')}`;
|
||
if (result.error) {
|
||
return {
|
||
ok: false,
|
||
stdout: '',
|
||
stderr: `${label} 执行失败: ${result.error.message}`,
|
||
};
|
||
}
|
||
return {
|
||
ok: result.status === 0,
|
||
stdout: String(result.stdout || ''),
|
||
stderr: String(result.stderr || ''),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 脱敏并截断错误文本:去掉 PEM 块内容、压缩空白、截断到列上限。
|
||
* @param {string} text 原始错误文本。
|
||
* @return {string} 脱敏截断后的文本(≤500 字符)。
|
||
*/
|
||
function sanitizeErrorText(text) {
|
||
return String(text || '')
|
||
.replace(/-----BEGIN [^-]*-----[\s\S]*?-----END [^-]*-----/g, '[PEM 内容已省略]')
|
||
.replace(/\s+/g, ' ')
|
||
.trim()
|
||
.slice(0, LAST_SYNC_ERROR_MAX_LENGTH);
|
||
}
|
||
|
||
/**
|
||
* 回写单条失败状态:sync_status=2、last_sync_error、sync_attempts+1、
|
||
* cert_path/key_path 置空(内容已换路径未知)。
|
||
* WHERE 带 sync_status=0 与 domain 守卫,避免覆盖并发重新保存的结果。
|
||
* @param {import('sequelize').Sequelize} sequelize Sequelize 实例。
|
||
* @param {Object} row 记录对象(含 id、domain)。
|
||
* @param {string} message 失败原因(原样日志,回写前会脱敏截断)。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async function markRowFailed(sequelize, row, message) {
|
||
await sequelize.query(
|
||
`UPDATE ${TABLE_NAME}
|
||
SET sync_status = 2,
|
||
last_sync_error = :error,
|
||
sync_attempts = sync_attempts + 1,
|
||
cert_path = NULL,
|
||
key_path = NULL,
|
||
updated_at = NOW()
|
||
WHERE id = :id AND sync_status = 0 AND domain = :domain`,
|
||
{
|
||
replacements: {
|
||
error: sanitizeErrorText(message),
|
||
id: row.id,
|
||
domain: row.domain,
|
||
},
|
||
}
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 回写单条成功状态:sync_status=1、回填 cert_path/key_path 实际全路径、
|
||
* last_synced_at=NOW()、last_sync_error 清空。
|
||
* WHERE 带 sync_status=0 与 domain 守卫,避免覆盖并发重新保存的结果。
|
||
* @param {import('sequelize').Sequelize} sequelize Sequelize 实例。
|
||
* @param {Object} row 记录对象(含 id、domain)。
|
||
* @param {string} certPath 证书文件绝对路径。
|
||
* @param {string} keyPath 私钥文件绝对路径。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async function markRowSuccess(sequelize, row, certPath, keyPath) {
|
||
await sequelize.query(
|
||
`UPDATE ${TABLE_NAME}
|
||
SET sync_status = 1,
|
||
cert_path = :certPath,
|
||
key_path = :keyPath,
|
||
last_synced_at = NOW(),
|
||
last_sync_error = NULL,
|
||
updated_at = NOW()
|
||
WHERE id = :id AND sync_status = 0 AND domain = :domain`,
|
||
{
|
||
replacements: {
|
||
certPath,
|
||
keyPath,
|
||
id: row.id,
|
||
domain: row.domain,
|
||
},
|
||
}
|
||
);
|
||
}
|
||
|
||
/**
|
||
* reload 失败后的重试回写:sync_attempts+1;未达重试上限时回退
|
||
* sync_status=0(下分钟自动重试),达到上限转 sync_status=2 终止重试。
|
||
* @param {import('sequelize').Sequelize} sequelize Sequelize 实例。
|
||
* @param {Object} row 记录对象(含 id、domain、sync_attempts)。
|
||
* @param {string} message 失败原因。
|
||
* @return {Promise<number>} 回写后的同步状态(0 重试 / 2 终止)。
|
||
*/
|
||
async function markRowReloadRetry(sequelize, row, message) {
|
||
const attempts = Number(row.sync_attempts || 0) + 1;
|
||
const nextStatus = attempts >= RELOAD_FAILURE_MAX_RETRY ? 2 : 0;
|
||
await sequelize.query(
|
||
`UPDATE ${TABLE_NAME}
|
||
SET sync_status = :status,
|
||
last_sync_error = :error,
|
||
sync_attempts = sync_attempts + 1,
|
||
updated_at = NOW()
|
||
WHERE id = :id AND sync_status = 0 AND domain = :domain`,
|
||
{
|
||
replacements: {
|
||
status: nextStatus,
|
||
error: sanitizeErrorText(message),
|
||
id: row.id,
|
||
domain: row.domain,
|
||
},
|
||
}
|
||
);
|
||
return nextStatus;
|
||
}
|
||
|
||
/**
|
||
* 逐条处理待同步记录:写证书/私钥文件 → 写 server 块 → 单条 nginx -t;
|
||
* nginx -t 失败撤销本条 server 块并重新校验,保证不拖垮整体 nginx。
|
||
* 返回成功条目列表(不立即回写 DB,等 reload 后再统一回写)。
|
||
* @param {SyncContext} ctx 运行上下文。
|
||
* @param {Object[]} rows 待同步记录数组。
|
||
* @param {SyncStats} stats 统计对象(就地更新)。
|
||
* @return {Promise<Array<{row: Object, names: Object}>>} 成功条目数组。
|
||
*/
|
||
async function processRows(ctx, rows, stats) {
|
||
const successRows = [];
|
||
const { args } = ctx;
|
||
|
||
for (const row of rows) {
|
||
const domainRaw = String(row.domain || '').trim();
|
||
const rowLabel = `#${row.id} store=${row.store_id} domain=${domainRaw || '(空)'}`;
|
||
const validateError = validateRowContent(domainRaw, row.cert_pem, row.cert_key);
|
||
const names = buildFileNames(args, row.store_id, domainRaw);
|
||
|
||
if (args.dryRun) {
|
||
console.log(`[CERT-SYNC][PLAN] ${rowLabel}`);
|
||
console.log(`[CERT-SYNC][PLAN] cert_pem ${String(row.cert_pem || '').length} 字节 -> ${names.certPath}`);
|
||
console.log(`[CERT-SYNC][PLAN] cert_key ${String(row.cert_key || '').length} 字节 -> ${names.keyPath}`);
|
||
console.log(`[CERT-SYNC][PLAN] server 块 -> ${names.confPath}`);
|
||
console.log(`[CERT-SYNC][PLAN] nginx -t=${!args.skipNginx && !ctx.nginxMissing} reload=${!args.skipNginx && !ctx.nginxMissing}`);
|
||
console.log(`[CERT-SYNC][PLAN] 预校验: ${validateError ? `将失败(${validateError})` : '通过'}`);
|
||
if (!validateError) {
|
||
stats.success++;
|
||
} else {
|
||
stats.failed++;
|
||
}
|
||
continue;
|
||
}
|
||
|
||
if (validateError) {
|
||
console.error(`[CERT-SYNC][FAIL] ${rowLabel} - ${validateError}`);
|
||
await markRowFailed(ctx.sequelize, row, validateError);
|
||
stats.failed++;
|
||
continue;
|
||
}
|
||
|
||
try {
|
||
writeFileAtomic(names.certPath, String(row.cert_pem), CERT_FILE_MODE);
|
||
writeFileAtomic(names.keyPath, String(row.cert_key), KEY_FILE_MODE);
|
||
writeFileAtomic(names.confPath, buildServerConf(domainRaw, names.certPath, names.keyPath, args.upstreamInclude), CERT_FILE_MODE);
|
||
|
||
if (!args.skipNginx && !ctx.nginxMissing) {
|
||
const testResult = runNginxCommand(ctx.testCommand);
|
||
if (!testResult.ok) {
|
||
// 撤销本条 server 块,避免拖垮整体 nginx,并复验恢复
|
||
removeFileQuiet(names.confPath);
|
||
const restoreResult = runNginxCommand(ctx.testCommand);
|
||
const restoreTip = restoreResult.ok
|
||
? ''
|
||
: `(撤销本条后 nginx -t 仍失败,请检查其他配置: ${sanitizeErrorText(restoreResult.stderr || restoreResult.stdout)})`;
|
||
const errorText = `nginx -t 校验失败: ${sanitizeErrorText(testResult.stderr || testResult.stdout)}${restoreTip}`;
|
||
throw new Error(errorText);
|
||
}
|
||
}
|
||
|
||
console.log(`[CERT-SYNC][OK] ${rowLabel} 文件与 server 块已写入`);
|
||
successRows.push({ row, names });
|
||
stats.success++;
|
||
} catch (error) {
|
||
// 清理本条已落盘的文件(幂等:下次重试重新写入)
|
||
removeFileQuiet(names.certPath);
|
||
removeFileQuiet(names.keyPath);
|
||
removeFileQuiet(names.confPath);
|
||
const message = error && error.message ? error.message : String(error);
|
||
console.error(`[CERT-SYNC][FAIL] ${rowLabel} - ${message}`);
|
||
await markRowFailed(ctx.sequelize, row, message);
|
||
stats.failed++;
|
||
}
|
||
}
|
||
|
||
return successRows;
|
||
}
|
||
|
||
/**
|
||
* 回写本批成功条目(在 reload 成功或 skip-nginx 之后调用)。
|
||
* @param {SyncContext} ctx 运行上下文。
|
||
* @param {Array<{row: Object, names: Object}>} successRows 成功条目数组。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async function writeBackSuccessRows(ctx, successRows) {
|
||
for (const { row, names } of successRows) {
|
||
await markRowSuccess(ctx.sequelize, row, names.certPath, names.keyPath);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* reload 失败处理:本批成功条目按重试策略回写(回退待同步或转失败)。
|
||
* @param {SyncContext} ctx 运行上下文。
|
||
* @param {Array<{row: Object, names: Object}>} successRows 本批成功条目数组。
|
||
* @param {string} reloadError reload 失败原因(脱敏后)。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async function handleReloadFailure(ctx, successRows, reloadError) {
|
||
console.error(`[CERT-SYNC][RELOAD-FAIL] nginx -s reload 失败: ${reloadError}`);
|
||
for (const { row } of successRows) {
|
||
const retriedStatus = await markRowReloadRetry(ctx.sequelize, row, `nginx reload 失败: ${reloadError}`);
|
||
console.error(`[CERT-SYNC][RELOAD-RETRY] #${row.id} 回写 sync_status=${retriedStatus}(${retriedStatus === 0 ? '下分钟自动重试' : '已达重试上限,需人工介入'})`);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 主流程:解析参数 → 连接数据库 → 扫描 → 逐条处理 → reload → 回写状态。
|
||
* @return {Promise<void>} 无返回值,通过 process.exitCode 表达执行结果。
|
||
*/
|
||
async function main() {
|
||
const args = parseArgs(process.argv.slice(2));
|
||
if (args.help) {
|
||
printUsage();
|
||
return;
|
||
}
|
||
|
||
console.log(`[CERT-SYNC] 开始: env=${args.env} dry-run=${args.dryRun} 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}`);
|
||
console.log(`[CERT-SYNC] upstream include: ${args.upstreamInclude}`);
|
||
console.log(`[CERT-SYNC] nginx 二进制: ${args.nginxBin}`);
|
||
if (args.nginxPrefix) {
|
||
console.log(`[CERT-SYNC] nginx -p 前缀: ${args.nginxPrefix}`);
|
||
}
|
||
|
||
applyDbTarget(args);
|
||
|
||
const testCommand = buildNginxCommand(args.nginxBin, args.nginxPrefix, [ '-t' ], args.testCmd);
|
||
const reloadCommand = buildNginxCommand(args.nginxBin, args.nginxPrefix, [ '-s', 'reload' ], args.reloadCmd);
|
||
const commandLabel = desc => (desc.shell ? desc.command : `${desc.command} ${desc.args.join(' ')}`);
|
||
console.log(`[CERT-SYNC] nginx -t 命令: ${commandLabel(testCommand)}(${args.testCmd ? '自定义命令' : '按系统类型推导'})`);
|
||
console.log(`[CERT-SYNC] nginx reload 命令: ${commandLabel(reloadCommand)}(${args.reloadCmd ? '自定义命令' : '按系统类型推导'})`);
|
||
|
||
const sequelize = connectDb(args);
|
||
const ctx = { args, sequelize, nginxMissing: false, testCommand, reloadCommand };
|
||
|
||
try {
|
||
try {
|
||
await sequelize.authenticate();
|
||
} catch (error) {
|
||
throw new Error(`数据库连接失败: ${error.message}`);
|
||
}
|
||
|
||
// 目录准备:不存在则递归创建(创建失败视为致命错误,避免逐条全部失败)
|
||
try {
|
||
fs.mkdirSync(args.certDir, { recursive: true });
|
||
fs.mkdirSync(args.confDir, { recursive: true });
|
||
} catch (error) {
|
||
throw new Error(`目录创建失败: ${error.message}`);
|
||
}
|
||
|
||
// nginx 可用性探测:生产环境缺失视为致命;开发环境缺失自动降级为跳过
|
||
if (!args.skipNginx && !nginxBinExists(args.nginxBin, args.nginxPrefix)) {
|
||
if (args.env === 'development') {
|
||
console.warn(`[CERT-SYNC] 开发环境未检测到 nginx(${args.nginxBin}),自动跳过 nginx -t / reload`);
|
||
ctx.nginxMissing = true;
|
||
} else {
|
||
throw new Error(`nginx 二进制不可用: ${args.nginxBin}(生产环境必须可用,或显式 --skip-nginx)`);
|
||
}
|
||
}
|
||
|
||
const rows = await scanPendingRows(sequelize, args.limit);
|
||
if (!rows || rows.length === 0) {
|
||
console.log('[CERT-SYNC] 无待同步记录,静默退出');
|
||
return;
|
||
}
|
||
|
||
const stats = { total: rows.length, success: 0, failed: 0 };
|
||
const successRows = await processRows(ctx, rows, stats);
|
||
|
||
if (args.dryRun) {
|
||
console.log(`[CERT-SYNC][SUMMARY] dry-run 完成: total=${stats.total} 可成功=${stats.success} 将失败=${stats.failed}`);
|
||
return;
|
||
}
|
||
|
||
if (successRows.length > 0 && !args.skipNginx && !ctx.nginxMissing) {
|
||
const reloadResult = runNginxCommand(ctx.reloadCommand);
|
||
if (reloadResult.ok) {
|
||
console.log('[CERT-SYNC] nginx -s reload 成功');
|
||
await writeBackSuccessRows(ctx, successRows);
|
||
} else {
|
||
await handleReloadFailure(ctx, successRows, sanitizeErrorText(reloadResult.stderr || reloadResult.stdout));
|
||
stats.failed += successRows.length;
|
||
stats.success -= successRows.length;
|
||
}
|
||
} else if (successRows.length > 0) {
|
||
await writeBackSuccessRows(ctx, successRows);
|
||
}
|
||
|
||
console.log(`[CERT-SYNC][SUMMARY] total=${stats.total} success=${stats.success} failed=${stats.failed}`);
|
||
if (stats.failed > 0) {
|
||
process.exitCode = EXIT_BUSINESS_FAILURE;
|
||
} else {
|
||
process.exitCode = EXIT_OK;
|
||
}
|
||
} finally {
|
||
await sequelize.close();
|
||
}
|
||
}
|
||
|
||
// 顶层错误兜底:致命错误退出码 1(cron 可据 1/2 区分告警级别)
|
||
main().catch(error => {
|
||
console.error(`[CERT-SYNC][FATAL] ${error && error.message ? error.message : String(error)}`);
|
||
process.exitCode = EXIT_FATAL;
|
||
});
|