WMS_MAIN/script/deploy-lib.cjs
liufumingren 0918f2d572 feat(deploy): 证书同步改为单片段 include + ver 命名 + 声明式自愈与差集下线
- 站点内容外置为单一片段(upstreamInclude,运维维护):生成的 server 块 = 证书骨架 +
  一行 include <片段>,撤掉此前的 customDir 多片段方案;并强制 include 必须是绝对路径
  且文件已存在(nginx 解析相对 include 的基准是 nginx prefix,不是脚本 cwd,写相对路径
  会让 nginx -t 失败并回滚整轮变更),不满足直接 exit 1(dry-run 下仅告警)
- conf 命名加版本位 <domain>-<ver>.conf:轮转只改配置的 ver,旧命名自动清理并 reload
- 声明式对账自愈:证书 / 私钥 / server 块缺失或被改坏,按库内内容原子重建后统一
  nginx -t + reload(内容一致时零写入、零 reload);失败态(sync_status=2)同样参与
  收敛并在校验通过后回到 1;nginx -t / reload 属全局失败,只落 last_sync_error 下轮
  重试,不再把行写成 2 卡死
- 域名下线改为配置差集驱动:以「已配置 + 已验证 + 证书就绪」的期望集合为准(同一仓库
  只取 id 最小的一条),目录中属于本系统命名规则但配置里已无该域名的 conf / 证书直接
  删除并 reload;不再消费 retired_domain(该列仅作应用侧审计留痕)
- 证书未配置的行不写 conf 也不作为文件保留依据(遗留产物由差集回收,避免 conf 引用
  不存在的证书导致 nginx -t 全局失败),行状态回写 sync_status=0 待同步;证书非空但
  非法、域名形态非法才记行级失败(2)
- cert-sync 部署命令瘦身为 install / uninstall;新增 watchdog 部署命令(precheck /
  install / status / dry-run / run),安装路径统一到 script/;deploy-lib 保护生产运行时
  配置不被发布覆盖;README 补「发布与运维」章节(目录硬约定 / 定时任务 / 命令清单)
2026-09-28 01:02:23 +08:00

371 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const fs = require('node:fs');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const ROOT = path.resolve(__dirname, '..');
const LOCAL_CONFIG_PATH = path.join(ROOT, 'config', 'deploy.config.local.cjs');
const EXAMPLE_CONFIG_PATH = path.join(ROOT, 'config', 'deploy.config.example.cjs');
const DEFAULT_ENV = 'production';
const DEFAULT_EGG_MINIMAL_INCLUDE_PATHS = [
'app.js',
'app/**',
'methods/**',
'native/addons/**',
'package.json',
'package-lock.json',
'printer_data_template/**',
];
const DEFAULT_POST_DEPLOY_COMMANDS = [
'npm install --production',
'npm run stop || true',
'npm start',
];
/**
* 默认发布排除路径:应用配置 + 工程内的证书/nginx 产物目录。
* `.ssl/` 与 `nginx_config/` 同在部署根一级(工程目录内),虽已被 .gitignore 忽略,
* 这里再排一次做防御,避免将来 include 规则放宽时把私钥打进发布包。
*/
const DEFAULT_EXCLUDE_PATHS = [ 'config/**', '.ssl/**', 'nginx_config/**' ];
/**
* 发布包固定排除文件(与 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, BASH_QUOTE_ESCAPE)}'`;
}
function runCommand(command, args, options = {}) {
const result = spawnSync(command, args, {
cwd: options.cwd || ROOT,
encoding: 'utf8',
stdio: options.stdio || 'pipe',
shell: false,
});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
const stderr = (result.stderr || '').trim();
const stdout = (result.stdout || '').trim();
throw new Error(stderr || stdout || `${command} exited with code ${result.status}`);
}
return (result.stdout || '').trim();
}
function ensureCommandAvailable(command, installHint) {
const result = spawnSync(command, [], {
cwd: ROOT,
encoding: 'utf8',
stdio: 'pipe',
shell: false,
});
if (result.error && result.error.code === 'ENOENT') {
throw new Error(`${command} 不可用。${installHint}`);
}
}
function getNowStamp() {
const now = new Date();
const pad = value => String(value).padStart(2, '0');
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
}
function ensureTempDeployDir() {
const tempDir = path.join(ROOT, 'temp', 'deploy');
fs.mkdirSync(tempDir, { recursive: true });
return tempDir;
}
function isPlaceholder(value) {
if (!value) {
return true;
}
return String(value).includes('__FILL_');
}
function normalizeDeployConfig(config, envName) {
const deployRoot = config.deployRoot || '/www/wwwroot/qimen';
const privateKeyPath = config.privateKeyPath ? path.resolve(ROOT, config.privateKeyPath) : '';
const sshOptions = Array.isArray(config.sshOptions) ? [ ...config.sshOptions ] : [];
const preserveDirs = Array.isArray(config.preserveDirs) ? [ ...config.preserveDirs ] : [ 'config' ];
if (!preserveDirs.includes('config')) {
preserveDirs.push('config');
}
if (!sshOptions.some(option => /^IdentitiesOnly=/i.test(option))) {
sshOptions.push('IdentitiesOnly=yes');
}
if (!sshOptions.some(option => /^PreferredAuthentications=/i.test(option))) {
sshOptions.push('PreferredAuthentications=publickey');
}
if (!sshOptions.some(option => /^PubkeyAuthentication=/i.test(option))) {
sshOptions.push('PubkeyAuthentication=yes');
}
if (!sshOptions.some(option => /^PasswordAuthentication=/i.test(option))) {
sshOptions.push('PasswordAuthentication=no');
}
if (!sshOptions.some(option => /^KbdInteractiveAuthentication=/i.test(option))) {
sshOptions.push('KbdInteractiveAuthentication=no');
}
if (!sshOptions.some(option => /^BatchMode=/i.test(option))) {
sshOptions.push('BatchMode=yes');
}
if (!sshOptions.some(option => /^ConnectTimeout=/i.test(option))) {
sshOptions.push('ConnectTimeout=10');
}
const configuredPostDeployCommands = Array.isArray(config.postDeployCommands) && config.postDeployCommands.length > 0
? [ ...config.postDeployCommands ]
: [ ...DEFAULT_POST_DEPLOY_COMMANDS ];
if (!configuredPostDeployCommands.some(cmd => /^npm\s+install\s+--production(?:\s|$)/i.test(String(cmd).trim()))) {
configuredPostDeployCommands.unshift('npm install --production');
}
return {
envName,
host: config.host,
port: Number(config.port || 22),
username: config.username,
privateKeyPath,
deployRoot,
controlDir: `${deployRoot}/.deploy`,
incomingDir: `${deployRoot}/.deploy/incoming`,
backupDir: `${deployRoot}/.deploy/backups`,
stateDir: `${deployRoot}/.deploy/state`,
sharedDirs: Array.isArray(config.sharedDirs) ? config.sharedDirs : [ 'logs', 'run', 'temp' ],
preserveDirs,
keepBackups: Number(config.keepBackups || 5),
preDeployCommands: Array.isArray(config.preDeployCommands) ? config.preDeployCommands : [],
postDeployCommands: configuredPostDeployCommands,
healthCheckUrl: config.healthCheckUrl || '',
deployIncludePaths: Array.isArray(config.deployIncludePaths) && config.deployIncludePaths.length > 0
? config.deployIncludePaths
: DEFAULT_EGG_MINIMAL_INCLUDE_PATHS,
deployExcludePaths: Array.isArray(config.deployExcludePaths) ? config.deployExcludePaths : [ ...DEFAULT_EXCLUDE_PATHS ],
sshOptions,
};
}
function validateDeployConfig(config, configPath, envName) {
if (!config) {
throw new Error(`${configPath} 中未找到 ${envName} 配置。`);
}
const missing = [];
if (isPlaceholder(config.host)) missing.push('host');
if (isPlaceholder(config.username)) missing.push('username');
if (isPlaceholder(config.privateKeyPath)) missing.push('privateKeyPath');
if (missing.length > 0) {
throw new Error(`请先完善 SSH 配置字段: ${missing.join(', ')}。配置文件: ${configPath}`);
}
const resolvedPrivateKeyPath = path.resolve(ROOT, config.privateKeyPath);
if (!fs.existsSync(resolvedPrivateKeyPath)) {
throw new Error(`SSH 私钥不存在: ${resolvedPrivateKeyPath}。请先修正 config/deploy.config.local.cjs 中的 privateKeyPath。`);
}
}
function loadDeployConfig(envName = DEFAULT_ENV) {
if (!fs.existsSync(LOCAL_CONFIG_PATH)) {
throw new Error(`缺少本地部署配置 ${LOCAL_CONFIG_PATH}。请复制 ${EXAMPLE_CONFIG_PATH} 并填入 SSH 信息。`);
}
delete require.cache[LOCAL_CONFIG_PATH];
const loaded = require(LOCAL_CONFIG_PATH);
const config = loaded[envName];
validateDeployConfig(config, LOCAL_CONFIG_PATH, envName);
return normalizeDeployConfig(config, envName);
}
function buildSshBaseArgs(config) {
const args = [ '-p', String(config.port), '-i', config.privateKeyPath ];
for (const option of config.sshOptions) {
args.push('-o', option);
}
return args;
}
function runSsh(config, remoteScript, options = {}) {
const args = [
...buildSshBaseArgs(config),
`${config.username}@${config.host}`,
`bash -lc ${quoteForBash(remoteScript)}`,
];
return runCommand('ssh', args, {
stdio: options.stdio || 'inherit',
});
}
function runScp(config, localPath, remotePath) {
const args = [ '-P', String(config.port), '-i', config.privateKeyPath ];
for (const option of config.sshOptions) {
args.push('-o', option);
}
args.push(localPath, `${config.username}@${config.host}:${remotePath}`);
return runCommand('scp', args, { stdio: 'inherit' });
}
function normalizeRule(rule) {
return String(rule || '').replace(/\\/g, '/').trim();
}
function pathMatchesRule(filePath, rule) {
const normalizedPath = String(filePath || '').replace(/\\/g, '/');
const normalizedRule = normalizeRule(rule);
if (!normalizedRule) {
return false;
}
if (normalizedRule.endsWith('/**')) {
const prefix = normalizedRule.slice(0, -3);
return normalizedPath === prefix || normalizedPath.startsWith(`${prefix}/`);
}
if (normalizedRule.endsWith('/')) {
return normalizedPath.startsWith(normalizedRule);
}
return normalizedPath === normalizedRule;
}
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 : DEFAULT_EXCLUDE_PATHS;
const allExcludeRules = [ ...excludeRules, ...RELEASE_PINNED_EXCLUDE_PATHS ];
return Array.from(new Set(
output
.split(/\r?\n/)
.map(item => item.trim())
.filter(Boolean)
.filter(item => !item.startsWith('.git/'))
.filter(item => fs.existsSync(path.join(ROOT, item)))
.filter(item => includeRules.some(rule => pathMatchesRule(item, rule)))
.filter(item => !allExcludeRules.some(rule => pathMatchesRule(item, rule)))
));
}
function buildArchive(stamp, config) {
const tempDir = ensureTempDeployDir();
const listPath = path.join(tempDir, `deploy-files-${stamp}.txt`);
const archivePath = path.join(tempDir, `release-${stamp}.tar.gz`);
const files = listDeployableFiles(config);
if (files.length === 0) {
throw new Error('git ls-files 未返回可发布文件,无法生成发布包。');
}
fs.writeFileSync(listPath, files.join('\n') + '\n', 'utf8');
runCommand('tar', [ '-czf', archivePath, '-T', listPath ], { stdio: 'inherit' });
return {
archivePath,
listPath,
};
}
function buildRemoteCleanupGuard(sharedDirs, preserveDirs = []) {
const preserved = [ '.deploy', ...sharedDirs, ...preserveDirs ];
return preserved.map(item => `! -name ${quoteForBash(item)}`).join(' ');
}
function buildScopedEggStopScript(deployRoot) {
return [
`DEPLOY_ROOT=${quoteForBash(deployRoot)}`,
'find_deploy_master_pids() {',
" ps -eo pid=,args= | awk -v root=\"$DEPLOY_ROOT\" 'index($0, root) && ($2 == \"node\" || $2 ~ /\\/node$/) && index($0, \"start-cluster\") { print $1 }'",
'}',
'find_deploy_worker_pids() {',
" ps -eo pid=,args= | awk -v root=\"$DEPLOY_ROOT\" 'index($0, root) && ($2 == \"node\" || $2 ~ /\\/node$/) && (index($0, \"egg-cluster/lib/app_worker.js\") || index($0, \"egg-cluster/lib/agent_worker.js\")) { print $1 }'",
'}',
'MASTER_PIDS="$(find_deploy_master_pids | tr "\\n" " " | xargs 2>/dev/null || true)"',
'if [ -n "$MASTER_PIDS" ]; then',
' echo "STOP_CURRENT_DEPLOY_MASTERS $MASTER_PIDS"',
' kill $MASTER_PIDS || true',
' sleep 5',
'fi',
'WORKER_PIDS="$(find_deploy_worker_pids | tr "\\n" " " | xargs 2>/dev/null || true)"',
'if [ -n "$WORKER_PIDS" ]; then',
' echo "STOP_CURRENT_DEPLOY_WORKERS $WORKER_PIDS"',
' kill -9 $WORKER_PIDS || true',
'fi',
].join('\n');
}
function serializeCommands(commands) {
return commands
.filter(Boolean)
.map(command => command.trim())
.filter(Boolean)
.join('\n');
}
module.exports = {
ROOT,
RELEASE_PINNED_EXCLUDE_PATHS,
buildArchive,
buildPinnedConfigGuardLines,
buildRemoteCleanupGuard,
buildScopedEggStopScript,
ensureCommandAvailable,
ensureTempDeployDir,
getNowStamp,
loadDeployConfig,
quoteForBash,
runCommand,
runScp,
runSsh,
serializeCommands,
};