WMS_MAIN/script/deploy-lib.cjs

317 lines
10 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',
];
const DEFAULT_POST_DEPLOY_COMMANDS = [
'npm install --production',
'npm run stop || true',
'npm start',
];
function quoteForBash(value) {
return `'${String(value).replace(/'/g, `'"'"'`)}'`;
}
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' ]);
const includeRules = Array.isArray(config && config.deployIncludePaths) ? config.deployIncludePaths : DEFAULT_EGG_MINIMAL_INCLUDE_PATHS;
const excludeRules = Array.isArray(config && config.deployExcludePaths) ? config.deployExcludePaths : [ 'config/**' ];
return output
.split(/\r?\n/)
.map(item => item.trim())
.filter(Boolean)
.filter(item => !item.startsWith('.git/'))
.filter(item => includeRules.some(rule => pathMatchesRule(item, rule)))
.filter(item => !excludeRules.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,
buildArchive,
buildRemoteCleanupGuard,
buildScopedEggStopScript,
ensureCommandAvailable,
ensureTempDeployDir,
getNowStamp,
loadDeployConfig,
quoteForBash,
runCommand,
runScp,
runSsh,
serializeCommands,
};