From b1000fd0e87326132ca94bf6e67ef9b05c951a25 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 8 Sep 2026 04:26:39 +0800 Subject: [PATCH] =?UTF-8?q?chore(deploy):=20=E8=AF=81=E4=B9=A6=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=8F=8C=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E7=BA=A6=E5=AE=9A=E8=90=BD=E5=9C=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 发布/回退链路新增固定文件排除与保护: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 --- package.json | 6 + script/deploy-cert-sync.cjs | 431 +++++++++ script/deploy-lib.cjs | 57 +- script/deploy-remote.cjs | 18 +- script/rollback-remote.cjs | 9 +- script/warehouse-cert-sync.cjs | 963 ++++++++++++++++++++ script/warehouse-cert-sync.config.dev.json | 10 + script/warehouse-cert-sync.config.prod.json | 10 + 8 files changed, 1492 insertions(+), 12 deletions(-) create mode 100644 script/deploy-cert-sync.cjs create mode 100644 script/warehouse-cert-sync.cjs create mode 100644 script/warehouse-cert-sync.config.dev.json create mode 100644 script/warehouse-cert-sync.config.prod.json diff --git a/package.json b/package.json index eb49980..933df6d 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,12 @@ "deploy:smoke": "node ./script/deploy-ops.cjs production smoke", "deploy:verify": "node ./script/deploy-ops.cjs production verify", "deploy:cleanup-backups": "node ./script/deploy-ops.cjs production cleanup-backups", + "deploy:cert-sync:precheck": "node ./script/deploy-cert-sync.cjs production precheck", + "deploy:cert-sync:install": "node ./script/deploy-cert-sync.cjs production install", + "deploy:cert-sync:dry-run": "node ./script/deploy-cert-sync.cjs production dry-run", + "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: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 new file mode 100644 index 0000000..be2d855 --- /dev/null +++ b/script/deploy-cert-sync.cjs @@ -0,0 +1,431 @@ +'use strict'; + +const fs = require('node:fs'); +const path = require('node:path'); +const { + ROOT, + getNowStamp, + loadDeployConfig, + quoteForBash, + runScp, + runSsh, +} = require('./deploy-lib.cjs'); + +const COMMANDS = new Set([ 'precheck', 'install', 'dry-run', 'run', 'status', 'config-check' ]); +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'); + +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'); + } + 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'); + } + options.command = tokens[0]; + return options; +} + +/** + * Build the thin shell wrapper content for the server. + * 薄包装职责收敛为: + * 1. cd 部署根目录(脚本以部署根为 cwd 运行,certDir/confDir 按 prod 配置解析); + * 2. 运行时依赖引导:从活跃实例 node_modules 借用 sequelize(部署根无 node_modules); + * 3. 运行时 DB 引导:从活跃实例 egg 配置解析 DB_* 环境变量(不落盘、不打印凭据); + * 4. 执行 node script/warehouse-cert-sync.cjs --env production "$@"。 + * nginx 二进制 / upstream include / reload 命令已全部收敛到 + * script/warehouse-cert-sync.config.prod.json,本包装不再注入任何 nginx 相关参数。 + */ +function buildWrapperContent() { + return `#!/usr/bin/env bash +# ============================================================== +# warehouse-cert-sync thin shell wrapper (installed by wms-deploy-agent) +# 1. cd into deploy root; cert/conf default to ../.ssl and ../nginx_config +# 2. nginx binary / upstream include / reload 命令全部来自 prod 配置文件 +# 3. resolve prod DB connection at runtime from the ACTIVE instance egg +# config (exported as DB_* env vars only; not persisted, never printed) +# 4. dependency resolution: borrow sequelize from active instance node_modules +# 5. all output appended to /var/log/warehouse-cert-sync.log (tee to tty) +# ============================================================== +set -uo pipefail + +DEPLOY_ROOT='/www/wwwroot/qimen' +LOG_FILE='/var/log/warehouse-cert-sync.log' + +exec > >(tee -a "$LOG_FILE") 2>&1 + +echo "===== $(date '+%F %T') warehouse-cert-sync.sh args=[$*] =====" + +cd "$DEPLOY_ROOT" || { echo "[CERT-WRAPPER] FATAL cannot enter $DEPLOY_ROOT"; exit 1; } + +# ---------- nginx 参数来源 ---------- +# nginx 二进制 / upstream include / reload 命令全部由 +# script/warehouse-cert-sync.config.prod.json 提供(--env production 自动加载), +# 本包装不再注入 --nginx-bin / --upstream-include / --reload-cmd 参数。 + +# ---------- runtime dependency resolution ---------- +# borrow sequelize etc. from the active instance node_modules via NODE_PATH +if [ ! -d "$DEPLOY_ROOT/node_modules" ]; then + ACTIVE_VERSION='' + if [ -f "$DEPLOY_ROOT/current.md" ]; then + ACTIVE_VERSION="$(tr -d '[:space:]' < "$DEPLOY_ROOT/current.md")" + fi + for CAND in "$ACTIVE_VERSION" v1 v2; do + if [ -n "$CAND" ] && [ -d "$DEPLOY_ROOT/$CAND/node_modules" ]; then + export NODE_PATH="$DEPLOY_ROOT/$CAND/node_modules\${NODE_PATH:+:$NODE_PATH}" + echo "[CERT-WRAPPER] NODE_PATH -> $DEPLOY_ROOT/$CAND/node_modules" + break + fi + done +fi + +# ---------- prod DB connection resolution ---------- +# deploy root has no config dir (release mechanism ships app code only), so the +# DB connection is resolved from the active instance egg config at runtime. +ACTIVE_APP_DIR='' +if [ -n "\${ACTIVE_VERSION:-}" ] && [ -d "$DEPLOY_ROOT/$ACTIVE_VERSION/config" ]; then + ACTIVE_APP_DIR="$DEPLOY_ROOT/$ACTIVE_VERSION" +fi +if [ -z "$ACTIVE_APP_DIR" ]; then + for CAND in v1 v2; do + if [ -d "$DEPLOY_ROOT/$CAND/config" ]; then ACTIVE_APP_DIR="$DEPLOY_ROOT/$CAND"; break; fi + done +fi +if [ -z "$ACTIVE_APP_DIR" ]; then + echo '[CERT-WRAPPER] FATAL no active instance dir found (current.md / v1 / v2)' + exit 1 +fi + +eval "$(node -e ' +const path = require("node:path"); +const appDir = process.argv[1]; +function load(p) { + try { + const f = require(p); + const c = typeof f === "function" ? f({ name: "cert-sync-wrapper", baseDir: appDir }) : f; + return c && typeof c === "object" ? c : {}; + } catch (e) { return {}; } +} +let src = "none"; let client = null; +const def = load(path.join(appDir, "config", "config.default.js")); +if (def.sequelize && def.sequelize.host) { src = "config.default.js#sequelize"; client = def.sequelize; } +else if (def.mysql && def.mysql.clients && def.mysql.clients.wms && def.mysql.clients.wms.host) { src = "config.default.js#mysql.clients.wms"; client = def.mysql.clients.wms; } +else if (def.mysql && def.mysql.host) { src = "config.default.js#mysql"; client = def.mysql; } +if (!client) { + const loc = load(path.join(appDir, "config", "config.local.js")); + if (loc.sequelize && loc.sequelize.host) { src = "config.local.js#sequelize"; client = loc.sequelize; } + else if (loc.mysql && loc.mysql.host) { src = "config.local.js#mysql"; client = loc.mysql; } +} +if (!client) { console.error("[CERT-WRAPPER] cannot resolve db config from: " + appDir); process.exit(1); } +console.log("export DB_HOST=" + JSON.stringify(String(client.host || ""))); +console.log("export DB_PORT=" + JSON.stringify(String(client.port || 3306))); +console.log("export DB_USER=" + JSON.stringify(String(client.username || client.user || ""))); +console.log("export DB_PASSWORD=" + JSON.stringify(String(client.password || ""))); +console.log("export DB_NAME=" + JSON.stringify(String(client.database || "wms"))); +console.error("[CERT-WRAPPER] db config source: " + src + " (database=" + String(client.database || "") + ")"); +' "$ACTIVE_APP_DIR")" || { echo '[CERT-WRAPPER] FATAL db config resolution failed'; exit 1; } + +# ---------- run main script ---------- +# nginx 二进制 / upstream include / reload 命令全部由 prod 配置文件提供 +node "$DEPLOY_ROOT/script/warehouse-cert-sync.cjs" --env production "$@" +STATUS=$? +echo "===== $(date '+%F %T') warehouse-cert-sync.sh exit=$STATUS =====" +exit $STATUS +`; +} + +/** Read-only DB probe JS (never prints credentials). */ +const DB_PROBE_JS = ` +const path = require("node:path"); +const Sequelize = require("sequelize"); +const appDir = process.argv[1]; +function load(p) { + try { + const f = require(p); + const c = typeof f === "function" ? f({ name: "cert-sync-precheck", baseDir: appDir }) : f; + return c && typeof c === "object" ? c : {}; + } catch (e) { return {}; } +} +let src = "none"; let client = null; +const def = load(path.join(appDir, "config", "config.default.js")); +if (def.sequelize && def.sequelize.host) { src = "config.default.js#sequelize"; client = def.sequelize; } +else if (def.mysql && def.mysql.clients && def.mysql.clients.wms && def.mysql.clients.wms.host) { src = "config.default.js#mysql.clients.wms"; client = def.mysql.clients.wms; } +else if (def.mysql && def.mysql.host) { src = "config.default.js#mysql"; client = def.mysql; } +if (!client) { + const loc = load(path.join(appDir, "config", "config.local.js")); + if (loc.sequelize && loc.sequelize.host) { src = "config.local.js#sequelize"; client = loc.sequelize; } + else if (loc.mysql && loc.mysql.host) { src = "config.local.js#mysql"; client = loc.mysql; } +} +if (!client) { console.log("[DB_PROBE] cannot resolve db config from active instance"); process.exit(0); } +console.log("[DB_PROBE] config source: " + src + " database=" + String(client.database || "")); +const seq = new Sequelize(client.database, client.username || client.user, client.password, { + host: client.host, port: client.port || 3306, dialect: "mysql", logging: false, +}); +(async () => { + try { + await seq.authenticate(); + console.log("[DB_PROBE] prod db connect OK"); + const [t] = await seq.query("SELECT COUNT(*) AS c FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'wms_warehouse_front_config'"); + const exists = Number(t[0].c) > 0; + console.log("[DB_PROBE] wms_warehouse_front_config exists: " + (exists ? "YES" : "NO")); + if (exists) { + const [cols] = await seq.query("SELECT column_name AS cn FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'wms_warehouse_front_config'"); + console.log("[DB_PROBE] columns: " + cols.map(r => r.cn).join(",")); + const [total] = await seq.query("SELECT COUNT(*) AS c FROM wms_warehouse_front_config"); + console.log("[DB_PROBE] total rows: " + total[0].c); + const [dist] = await seq.query("SELECT sync_status AS s, COUNT(*) AS c FROM wms_warehouse_front_config GROUP BY sync_status ORDER BY sync_status"); + console.log("[DB_PROBE] sync_status distribution: " + (dist.length ? dist.map(r => r.s + "=" + r.c).join(" ") : "(empty)")); + } + } catch (e) { + console.log("[DB_PROBE] failed: " + e.message); + } + await seq.close(); +})(); +`; + +/** Read-only remote precheck. */ +function runPrecheck(config) { + const deployRoot = config.deployRoot; + const remoteScript = [ + 'set -uo pipefail', + `DEPLOY_ROOT=${quoteForBash(deployRoot)}`, + 'echo "[WHOAMI] $(id -un)"', + 'echo "[UNAME] $(uname -a)"', + `echo "[CURRENT_MD] $(cat ${quoteForBash(`${deployRoot}/current.md`)} 2>/dev/null || echo MISSING)"`, + `echo "[DEPLOY_ROOT_LS]"; ls -la ${quoteForBash(deployRoot)} | awk '{print $1, $3, $4, $9}'`, + 'echo "[NGINX_BIN]"; command -v nginx || echo "nginx not in PATH"', + 'echo "[NGINX_V]"; nginx -v 2>&1 || true', + 'echo "[NGINX_V_PREFIX]"; nginx -V 2>&1 | grep -E "configure arguments|--prefix" | head -2 || true', + 'echo "[NGINX_T_NOW]"; nginx -t 2>&1 || true', + 'echo "[NGINX_PROCESS]"; ps -eo pid=,args= | grep "[n]ginx" | head -6 || true', + 'echo "[SYSTEMD_NGINX]"; systemctl is-active nginx 2>&1 || true; systemctl is-enabled nginx 2>&1 || true', + 'echo "[FLOCK]"; command -v flock || echo "flock missing"', + 'echo "[MAIN_CONF_INCLUDES]"; for f in /etc/nginx/nginx.conf /www/server/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf; do if [ -f "$f" ]; then echo "-- $f"; grep -n "include" "$f" | grep -v "^[[:space:]]*#" | head -20; fi; done', + 'echo "[CONF_DIR_PROBE]"; for d in /etc/nginx/conf.d /www/server/nginx/conf/conf.d; do if [ -d "$d" ]; then echo "-- $d EXISTS"; ls -1 "$d" | head -20; fi; done', + 'echo "[SSL_DIR] /www/wwwroot/.ssl"; if [ -d /www/wwwroot/.ssl ]; then echo EXISTS; ls -la /www/wwwroot/.ssl | head -20; else echo MISSING; fi', + 'echo "[CERT_CONF_DIR] /www/wwwroot/nginx_config"; if [ -d /www/wwwroot/nginx_config ]; then echo EXISTS; ls -la /www/wwwroot/nginx_config | head -30; else echo MISSING; fi', + 'echo "[PARENT_WRITABLE]"; if [ -w /www/wwwroot ]; then echo YES; else echo NO; fi', + 'echo "[DEPLOY_ROOT_META]"; for p in script database config node_modules; do if [ -e "$DEPLOY_ROOT/$p" ]; then echo "$p EXISTS"; else echo "$p MISSING"; fi; done', + 'echo "[V1_CONFIG_LS]"; ls -la "$DEPLOY_ROOT/v1/config" 2>/dev/null | head -20 || echo "v1/config missing"', + 'echo "[V2_CONFIG_LS]"; ls -la "$DEPLOY_ROOT/v2/config" 2>/dev/null | head -20 || echo "v2/config missing"', + 'echo "[V1_SEQUELIZE]"; if [ -d "$DEPLOY_ROOT/v1/node_modules/sequelize" ]; then echo YES; else echo NO; fi', + 'echo "[V2_SEQUELIZE]"; if [ -d "$DEPLOY_ROOT/v2/node_modules/sequelize" ]; then echo YES; else echo NO; fi', + 'echo "[EXISTING_CRON_CERT]"; crontab -l 2>/dev/null | grep -i "cert" || echo NONE', + 'echo "[CRON_LINE_COUNT]"; crontab -l 2>/dev/null | grep -cv "^#" || echo 0', + 'echo "[CERT_LOG_TAIL]"; tail -20 /var/log/warehouse-cert-sync.log 2>/dev/null || echo "log not exists"', + ].join('\n'); + + console.log('[PRECHECK] remote env check (read-only)'); + console.log(runSsh(config, remoteScript, { stdio: 'pipe' }).trim()); + + const dbProbeScript = [ + 'set -uo pipefail', + `DEPLOY_ROOT=${quoteForBash(deployRoot)}`, + 'ACTIVE=$(cat "$DEPLOY_ROOT/current.md" 2>/dev/null | tr -d "[:space:]" || true)', + 'APP_DIR=""', + 'if [ -n "$ACTIVE" ] && [ -d "$DEPLOY_ROOT/$ACTIVE/config" ]; then APP_DIR="$DEPLOY_ROOT/$ACTIVE"; fi', + 'if [ -z "$APP_DIR" ]; then for CAND in v1 v2; do if [ -d "$DEPLOY_ROOT/$CAND/config" ]; then APP_DIR="$DEPLOY_ROOT/$CAND"; break; fi; done; fi', + 'if [ -z "$APP_DIR" ]; then echo "[DB_PROBE] no active instance dir found"; exit 0; fi', + 'export NODE_PATH="$APP_DIR/node_modules"', + `node -e ${quoteForBash(DB_PROBE_JS)} "$APP_DIR"`, + ].join('\n'); + + console.log(''); + console.log('[PRECHECK] prod db probe (read-only, no credentials printed)'); + console.log(runSsh(config, dbProbeScript, { stdio: 'pipe' }).trim()); +} + +/** Install: upload files, create thin shell wrapper, register root crontab. */ +function runInstall(config) { + const deployRoot = config.deployRoot; + for (const [ file, label ] of [ + [ LOCAL_SYNC_SCRIPT, 'script/warehouse-cert-sync.cjs' ], + [ LOCAL_DB_CONFIG, 'database/config.js' ], + ]) { + if (!fs.existsSync(file)) { + throw new Error(`local file missing: ${file} (${label})`); + } + } + + const remoteDirScript = [ + 'set -euo pipefail', + `DEPLOY_ROOT=${quoteForBash(deployRoot)}`, + `STAMP=${quoteForBash(getNowStamp())}`, + 'mkdir -p "$DEPLOY_ROOT/script" "$DEPLOY_ROOT/database"', + 'for p in "$DEPLOY_ROOT/warehouse-cert-sync.sh" "$DEPLOY_ROOT/script/warehouse-cert-sync.cjs" "$DEPLOY_ROOT/database/config.js"; do', + ' if [ -f "$p" ]; then cp -a "$p" "$p.prev-$STAMP" && echo "BACKED_UP $p -> $p.prev-$STAMP"; fi', + 'done', + ].join('\n'); + console.log('[INSTALL] remote dir prep + backup of existing files'); + console.log(runSsh(config, remoteDirScript, { stdio: 'pipe' }).trim()); + + const remoteScriptPath = `${deployRoot}/script/warehouse-cert-sync.cjs`; + const remoteDbConfigPath = `${deployRoot}/database/config.js`; + runScp(config, LOCAL_SYNC_SCRIPT, remoteScriptPath); + console.log(`[INSTALL] uploaded -> ${remoteScriptPath}`); + runScp(config, LOCAL_DB_CONFIG, remoteDbConfigPath); + console.log(`[INSTALL] uploaded -> ${remoteDbConfigPath}`); + + // prod 配置文件:服务器「首次放置一次、之后永不覆盖」的发布约定。 + // dev 配置只属于开发机,永不上传生产服务器。 + if (!fs.existsSync(LOCAL_PROD_CONFIG)) { + throw new Error(`local file missing: ${LOCAL_PROD_CONFIG}`); + } + JSON.parse(fs.readFileSync(LOCAL_PROD_CONFIG, 'utf8')); + + const prodConfigRemotePath = `${deployRoot}/script/warehouse-cert-sync.config.prod.json`; + const prodConfigProbeScript = [ + 'set -euo pipefail', + `PROD_CONFIG=${quoteForBash(prodConfigRemotePath)}`, + 'if [ -f "$PROD_CONFIG" ]; then', + ' echo "PROD_CONFIG_EXISTS"', + ' if node -e \'JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"))\' "$PROD_CONFIG" >/dev/null 2>&1; then', + ' echo "PROD_CONFIG_JSON_VALID"', + ' else', + ' echo "PROD_CONFIG_JSON_INVALID"', + ' fi', + 'else', + ' echo "PROD_CONFIG_MISSING"', + 'fi', + ].join('\n'); + const prodConfigProbe = String(runSsh(config, prodConfigProbeScript, { stdio: 'pipe' }) || '').trim(); + console.log(prodConfigProbe); + + if (prodConfigProbe.includes('PROD_CONFIG_EXISTS')) { + console.log('[INSTALL] prod config already on server -> skip placement (发布约定:永不覆盖)'); + } else { + runScp(config, LOCAL_PROD_CONFIG, prodConfigRemotePath); + console.log(`[INSTALL] placed once -> ${prodConfigRemotePath}`); + const verifyScript = [ + 'set -euo pipefail', + `node -e ${quoteForBash('JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8")); console.log("PROD_CONFIG_JSON_VALID")')} ${quoteForBash(prodConfigRemotePath)}`, + ].join('\n'); + console.log(String(runSsh(config, verifyScript, { stdio: 'pipe' }) || '').trim()); + } + console.log('[INSTALL] dev config NOT uploaded (development-only)'); + + const wrapperPath = `${deployRoot}/warehouse-cert-sync.sh`; + const installScript = [ + 'set -euo pipefail', + `DEPLOY_ROOT=${quoteForBash(deployRoot)}`, + `WRAPPER=${quoteForBash(wrapperPath)}`, + 'LOG_FILE="/var/log/warehouse-cert-sync.log"', + 'cat > "$WRAPPER" <<\'WMS_CERT_WRAPPER_EOF\'', + buildWrapperContent(), + 'WMS_CERT_WRAPPER_EOF', + 'chmod 755 "$WRAPPER"', + 'bash -n "$WRAPPER" && echo "WRAPPER_SYNTAX_OK"', + 'touch "$LOG_FILE" && chmod 644 "$LOG_FILE" && echo "LOG_FILE_OK"', + 'FLOCK_BIN=$(command -v flock || true)', + 'if [ -z "$FLOCK_BIN" ]; then echo "FATAL: flock not found, cannot register crontab"; exit 1; fi', + 'echo "FLOCK_BIN=$FLOCK_BIN"', + 'CRON_LINE="* * * * * $FLOCK_BIN -n /var/lock/warehouse-cert-sync.lock $WRAPPER >/dev/null 2>&1"', + '( crontab -l 2>/dev/null | grep -v "warehouse-cert-sync" || true; printf "%s\\n" "$CRON_LINE" ) | crontab -', + 'echo "CRONTAB_INSTALLED:"', + 'crontab -l | grep -n "warehouse-cert-sync" || true', + ].join('\n'); + + console.log('[INSTALL] write thin shell wrapper + register crontab'); + console.log(runSsh(config, installScript, { stdio: 'pipe' }).trim()); +} + +/** Run the remote wrapper with optional extra args; returns output + exit code. */ +function runRemoteWrapper(config, extraArgs = []) { + const wrapperPath = `${config.deployRoot}/warehouse-cert-sync.sh`; + 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', + 'echo "CERT_SYNC_EXIT=$?"', + ].join('\n'); + return runSsh(config, remoteScript, { stdio: 'pipe' }).trim(); +} + +/** Status: crontab line, remote file list, log tail. */ +function runStatus(config) { + const deployRoot = config.deployRoot; + const remoteScript = [ + 'set -uo pipefail', + 'echo "[CRONTAB_CERT_LINES]"', + 'crontab -l 2>/dev/null | grep -n "warehouse-cert-sync" || echo NONE', + 'echo "[REMOTE_FILES]"', + `ls -la ${quoteForBash(`${deployRoot}/warehouse-cert-sync.sh`)} ${quoteForBash(`${deployRoot}/script/warehouse-cert-sync.cjs`)} ${quoteForBash(`${deployRoot}/script/warehouse-cert-sync.config.prod.json`)} ${quoteForBash(`${deployRoot}/script/warehouse-cert-sync.config.dev.json`)} ${quoteForBash(`${deployRoot}/database/config.js`)} 2>&1`, + 'echo "[WRAPPER_HEAD]"', + `head -12 ${quoteForBash(`${deployRoot}/warehouse-cert-sync.sh`)} 2>/dev/null`, + 'echo "[CERT_LOG_TAIL]"', + 'tail -30 /var/log/warehouse-cert-sync.log 2>/dev/null || echo "log not exists"', + ].join('\n'); + console.log(runSsh(config, remoteScript, { stdio: 'pipe' }).trim()); +} + +/** 只读核对服务器 prod 配置文件:存在性 + JSON 合法性 + 内容展示(无凭据,可安全输出)。 */ +function runConfigCheck(config) { + const deployRoot = config.deployRoot; + const remoteScript = [ + 'set -uo pipefail', + `DEPLOY_ROOT=${quoteForBash(deployRoot)}`, + 'PROD="$DEPLOY_ROOT/script/warehouse-cert-sync.config.prod.json"', + 'DEV="$DEPLOY_ROOT/script/warehouse-cert-sync.config.dev.json"', + 'for f in "$PROD" "$DEV"; do', + ' if [ -f "$f" ]; then', + ' echo "[CONFIG_EXISTS] $f"', + ' if node -e \'JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"))\' "$f" >/dev/null 2>&1; then', + ' echo "[CONFIG_JSON] $f VALID"', + ' else', + ' echo "[CONFIG_JSON] $f INVALID"', + ' fi', + ' else', + ' echo "[CONFIG_MISSING] $f"', + ' fi', + 'done', + 'echo "[PROD_CONFIG_CONTENT]"', + 'cat "$PROD" 2>/dev/null || echo "(missing)"', + 'echo "[DEV_CONFIG_ON_SERVER]"', + 'if [ -f "$DEV" ]; then echo YES; else echo NO; fi', + ].join('\n'); + console.log(runSsh(config, remoteScript, { stdio: 'pipe' }).trim()); +} + +function main() { + const options = parseArgs(process.argv.slice(2)); + const config = loadDeployConfig(options.envName); + + if (options.command === 'precheck') { + runPrecheck(config); + return; + } + if (options.command === 'install') { + runInstall(config); + return; + } + if (options.command === 'dry-run') { + console.log('[DRY-RUN] remote dry-run (read-only, no file writes, no db updates)'); + console.log(runRemoteWrapper(config, [ '--dry-run' ])); + return; + } + if (options.command === 'run') { + console.log('[RUN] remote real execution'); + console.log(runRemoteWrapper(config, [])); + return; + } + if (options.command === 'status') { + runStatus(config); + return; + } + if (options.command === 'config-check') { + runConfigCheck(config); + return; + } + throw new Error(`unsupported command: ${options.command}`); +} + +main(); diff --git a/script/deploy-lib.cjs b/script/deploy-lib.cjs index 88d1f15..e32a12e 100644 --- a/script/deploy-lib.cjs +++ b/script/deploy-lib.cjs @@ -23,8 +23,51 @@ const DEFAULT_POST_DEPLOY_COMMANDS = [ 'npm start', ]; +/** + * 发布包固定排除文件(与 config.deployExcludePaths 叠加,防御性兜底)。 + * warehouse-cert-sync 双环境配置文件由部署 agent 在服务器上「首次放置一次、 + * 之后永不覆盖/删除」独立管理(见 script/deploy-cert-sync.cjs install 的 + * place-if-missing 逻辑)。因此无论 deployIncludePaths 是否覆盖 script/**, + * 这两份文件都不允许进入发布包,避免发布/回退覆盖或删除服务器既有副本。 + * 修改生产配置请走 deploy:cert-sync:install(首次放置)或运维人工修改服务器文件。 + */ +const RELEASE_PINNED_EXCLUDE_PATHS = [ + 'script/warehouse-cert-sync.config.dev.json', + 'script/warehouse-cert-sync.config.prod.json', +]; + +/** + * 生成「固定文件保护」远端脚本行(供发布与回退共用): + * stash 在清理前暂存目标目录中的固定文件副本;restore 在清理+解压后 + * 将副本放回(发布包/备份包内已固定排除,正常情况下不会冲突)。 + * @param {string} dirVar 目标目录的远端 shell 变量名(如 TARGET_DIR / CURRENT_DIR)。 + * @param {string} stashDirVar 暂存目录的远端 shell 变量名(如 PINNED_DIR)。 + * @return {{ stash: string[], restore: string[] }} 远端脚本行数组。 + */ +function buildPinnedConfigGuardLines(dirVar, stashDirVar) { + const stash = [ + `mkdir -p "$${stashDirVar}"`, + `for REL in ${RELEASE_PINNED_EXCLUDE_PATHS.map(quoteForBash).join(' ')}; do`, + ` if [ -f "$${dirVar}/$REL" ]; then`, + ` cp -a "$${dirVar}/$REL" "$${stashDirVar}/" && echo "PINNED_SAVED $REL"`, + ' fi', + 'done', + ]; + const restore = [ + `for REL in ${RELEASE_PINNED_EXCLUDE_PATHS.map(quoteForBash).join(' ')}; do`, + ` if [ -f "$${stashDirVar}/$(basename "$REL")" ] && [ ! -f "$${dirVar}/$REL" ]; then`, + ` cp -a "$${stashDirVar}/$(basename "$REL")" "$${dirVar}/$REL" && echo "PINNED_RESTORED $REL"`, + ' fi', + 'done', + `rm -rf "$${stashDirVar}"`, + ]; + return { stash, restore }; +} + +const BASH_QUOTE_ESCAPE = [ '\'', '"', '\'', '"', '\'' ].join(''); + function quoteForBash(value) { - return `'${String(value).replace(/'/g, `'"'"'`)}'`; + return `'${String(value).replace(/'/g, BASH_QUOTE_ESCAPE)}'`; } function runCommand(command, args, options = {}) { @@ -203,10 +246,7 @@ function runScp(config, localPath, remotePath) { args.push('-o', option); } - args.push( - localPath, - `${config.username}@${config.host}:${remotePath}`, - ); + args.push(localPath, `${config.username}@${config.host}:${remotePath}`); return runCommand('scp', args, { stdio: 'inherit' }); } @@ -236,6 +276,7 @@ function listDeployableFiles(config) { const output = runCommand('git', [ 'ls-files', '--cached', '--others', '--exclude-standard' ]); const includeRules = Array.isArray(config && config.deployIncludePaths) ? config.deployIncludePaths : DEFAULT_EGG_MINIMAL_INCLUDE_PATHS; const excludeRules = Array.isArray(config && config.deployExcludePaths) ? config.deployExcludePaths : [ 'config/**' ]; + const allExcludeRules = [ ...excludeRules, ...RELEASE_PINNED_EXCLUDE_PATHS ]; return Array.from(new Set( output @@ -245,7 +286,7 @@ function listDeployableFiles(config) { .filter(item => !item.startsWith('.git/')) .filter(item => fs.existsSync(path.join(ROOT, item))) .filter(item => includeRules.some(rule => pathMatchesRule(item, rule))) - .filter(item => !excludeRules.some(rule => pathMatchesRule(item, rule))) + .filter(item => !allExcludeRules.some(rule => pathMatchesRule(item, rule))) )); } @@ -306,7 +347,9 @@ function serializeCommands(commands) { module.exports = { ROOT, + RELEASE_PINNED_EXCLUDE_PATHS, buildArchive, + buildPinnedConfigGuardLines, buildRemoteCleanupGuard, buildScopedEggStopScript, ensureCommandAvailable, @@ -318,4 +361,4 @@ module.exports = { runScp, runSsh, serializeCommands, -}; \ No newline at end of file +}; diff --git a/script/deploy-remote.cjs b/script/deploy-remote.cjs index 31b84c4..6137c1f 100644 --- a/script/deploy-remote.cjs +++ b/script/deploy-remote.cjs @@ -5,6 +5,7 @@ const path = require('node:path'); const { ROOT, buildArchive, + buildPinnedConfigGuardLines, buildRemoteCleanupGuard, ensureCommandAvailable, getNowStamp, @@ -122,29 +123,38 @@ function main() { runScp(config, archivePath, remoteArchive); console.log(`发布包已上传到远端: ${remoteArchive}`); + // 4.5 推导目标版本标识与端口(供固定文件保护暂存目录命名与后续切换使用) + const targetVersion = resolveTargetVersion(currentVersion); + const targetPort = getTargetPort(targetVersion); + // 5. 在目标目录清理旧代码、解压、安装依赖 + // 固定文件保护:warehouse-cert-sync 双环境配置由服务器独立管理(发布包内已 + // 固定排除),清理前暂存目标目录既有副本、解压后放回,防止被覆盖/删除。 + const pinnedStashDir = `/tmp/wms-pinned-${targetVersion}`; + const pinnedGuard = buildPinnedConfigGuardLines('TARGET_DIR', 'PINNED_DIR'); const cleanupGuard = buildRemoteCleanupGuard(config.sharedDirs, config.preserveDirs); const remoteScript = [ 'set -euo pipefail', `TARGET_DIR=${quoteForBash(targetDir)}`, `REMOTE_ARCHIVE=${quoteForBash(remoteArchive)}`, + `PINNED_DIR=${quoteForBash(pinnedStashDir)}`, + ...pinnedGuard.stash, // 进入目标目录,清理旧代码文件(保留 node_modules、logs、run、temp、.deploy 等运行时/状态目录) 'cd "$TARGET_DIR"', `find . -mindepth 1 -maxdepth 1 ${cleanupGuard} -exec rm -rf {} +`, // 解压发布包 'tar -xzf "$REMOTE_ARCHIVE" -C "$TARGET_DIR"', 'rm -f "$REMOTE_ARCHIVE"', + // 解压后放回固定保护的配置文件(发布包内已固定排除,不会冲突) + ...pinnedGuard.restore, // 在目标目录安装生产依赖 'npm install --production', ].join('\n'); - runSsh(config, remoteScript); console.log(`远端目标目录已清理、解压并安装依赖: ${targetDir}`); - // 推导目标版本标识与启动日志路径 - const targetVersion = resolveTargetVersion(currentVersion); - const targetPort = getTargetPort(targetVersion); + // 启动日志路径 const startLogPath = `/tmp/wms-${targetVersion}-start.log`; const currentMdPath = `${config.deployRoot}/current.md`; diff --git a/script/rollback-remote.cjs b/script/rollback-remote.cjs index 7841415..1a14311 100644 --- a/script/rollback-remote.cjs +++ b/script/rollback-remote.cjs @@ -3,6 +3,7 @@ const path = require('node:path'); const { buildRemoteCleanupGuard, + buildPinnedConfigGuardLines, ensureCommandAvailable, loadDeployConfig, quoteForBash, @@ -140,6 +141,10 @@ function main() { } // 4. 在活跃目录清理旧代码、解压备份包、安装依赖 + // 固定文件保护:与发布链路一致,清理前暂存、解压后放回 + // warehouse-cert-sync 双环境配置文件,避免回退覆盖/删除服务器既有副本。 + const pinnedStashDir = `/tmp/wms-pinned-rollback-${currentVersion}`; + const pinnedGuard = buildPinnedConfigGuardLines('CURRENT_DIR', 'PINNED_DIR'); const cleanupGuard = buildRemoteCleanupGuard(config.sharedDirs, config.preserveDirs); const restoreScript = [ @@ -147,15 +152,17 @@ function main() { `CURRENT_DIR=${quoteForBash(currentDir)}`, `BACKUP_DIR=${quoteForBash(config.backupDir)}`, `TARGET_BACKUP=${quoteForBash(targetBackup)}`, + `PINNED_DIR=${quoteForBash(pinnedStashDir)}`, + ...pinnedGuard.stash, 'cd "$CURRENT_DIR"', `find . -mindepth 1 -maxdepth 1 ${cleanupGuard} -exec rm -rf {} +`, 'echo "旧代码已清理完成"', 'tar -xzf "$BACKUP_DIR/$TARGET_BACKUP" -C "$CURRENT_DIR"', 'echo "备份包已解压到 $CURRENT_DIR"', + ...pinnedGuard.restore, 'npm install --production', 'echo "生产依赖安装完成"', ].join('\n'); - runSsh(config, restoreScript); console.log(`当前活跃目录已恢复为备份包内容: ${currentDir}`); diff --git a/script/warehouse-cert-sync.cjs b/script/warehouse-cert-sync.cjs new file mode 100644 index 0000000..91c48de --- /dev/null +++ b/script/warehouse-cert-sync.cjs @@ -0,0 +1,963 @@ +'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 --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 [选项]', + '', + '环境配置文件(按 --env 自动加载,随代码提交,发布时不得覆盖删除):', + ' script/warehouse-cert-sync.config.dev.json 开发环境(本机宝塔 Windows nginx)', + ' script/warehouse-cert-sync.config.prod.json 生产环境(服务器宝塔构建 nginx,systemd)', + ' 所选配置缺失或 JSON 非法将致命退出(exit 1),生产环境宁可不跑也不猜。', + '', + '参数优先级: CLI 参数 > 环境变量 > 外部配置文件 > 内置默认', + '', + '选项:', + ' --env 运行环境(必填,显式指定,不受终端 NODE_ENV 残留影响,同时决定数据库与配置文件)', + ' --dry-run 仅扫描并输出计划,不写文件、不改库', + ' --cert-dir 证书输出目录(默认取配置文件或 ../.ssl,相对路径按 cwd 解析)', + ' --conf-dir nginx server 块目录(默认取配置文件或 ../nginx_config,同样按 cwd 解析)', + ' --upstream-include server 块 include 的 upstream 配置名(默认取配置文件或 nginx-upstream.conf)', + ' --skip-nginx 跳过 nginx -t 校验与 nginx reload(开发/测试用)', + ' --nginx-bin nginx 可执行文件路径(默认取配置文件或 PATH 中的 nginx)', + ' --nginx-prefix nginx -p 前缀目录(win32 宝塔 nginx 防 cwd 劫持,默认取配置文件)', + ' --reload-cmd reload 命令覆盖(默认按系统类型推导 "{nginxBin} -s reload";', + ' 支持 {nginxBin} / {nginxPrefix} 占位符且自动按平台加引号;', + ' linux 可传 "systemctl reload nginx")', + ' --test-cmd nginx -t 校验命令覆盖(默认按系统类型推导 "{nginxBin} -t",语义同上)', + ' --limit 单次处理条数上限(默认 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} 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} 待同步记录数组。 + */ +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} 无返回值。 + */ +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} 无返回值。 + */ +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} 回写后的同步状态(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>} 成功条目数组。 + */ +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} 无返回值。 + */ +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} 无返回值。 + */ +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} 无返回值,通过 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; +}); diff --git a/script/warehouse-cert-sync.config.dev.json b/script/warehouse-cert-sync.config.dev.json new file mode 100644 index 0000000..6be1605 --- /dev/null +++ b/script/warehouse-cert-sync.config.dev.json @@ -0,0 +1,10 @@ +{ + "description": "开发环境配置(Windows 本机 / 宝塔 Windows 版 nginx,nginx.exe)。nginx.exe 从任意 cwd 启动时会把启动目录当作前缀,导致读不到 conf/nginx.conf,因此 test/reload 命令必须带 -p 前缀指向 D:/BtSoft/nginx/;-p 的值使用正斜杠形式,经 win 版 nginx -t 实测可用。", + "nginxBin": "D:/BtSoft/nginx/nginx.exe", + "nginxPrefix": "D:/BtSoft/nginx/", + "certDir": "D:/wms/.ssl", + "confDir": "D:/wms/nginx_config", + "upstreamInclude": "D:/wms/nginx-upstream-local.conf", + "testCmd": "{nginxBin} -p {nginxPrefix} -t", + "reloadCmd": "{nginxBin} -p {nginxPrefix} -s reload" +} diff --git a/script/warehouse-cert-sync.config.prod.json b/script/warehouse-cert-sync.config.prod.json new file mode 100644 index 0000000..1ac73d2 --- /dev/null +++ b/script/warehouse-cert-sync.config.prod.json @@ -0,0 +1,10 @@ +{ + "description": "生产环境配置(Linux 服务器 / 宝塔构建 nginx,systemd 管理)。脚本以部署根 /www/wwwroot/qimen 为 cwd 运行,certDir/confDir 相对 cwd 解析到上一级;reload 走 systemctl reload nginx,test 直接调 /usr/bin/nginx -t。", + "nginxBin": "/usr/bin/nginx", + "nginxPrefix": "", + "certDir": "../.ssl", + "confDir": "../nginx_config", + "upstreamInclude": "/www/wwwroot/qimen/nginx-upstream.conf", + "testCmd": "{nginxBin} -t", + "reloadCmd": "systemctl reload nginx" +}