- 发布/回退链路新增固定文件排除与保护: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
365 lines
13 KiB
JavaScript
365 lines
13 KiB
JavaScript
'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',
|
||
];
|
||
|
||
/**
|
||
* 发布包固定排除文件(与 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 : [ 'config/**' ],
|
||
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 : [ 'config/**' ];
|
||
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,
|
||
};
|