'use strict'; const { loadDeployConfig, quoteForBash, runSsh } = require('./deploy-lib.cjs'); const COMMANDS = new Set([ 'inspect', 'smoke', 'verify', 'cleanup-backups' ]); const DEFAULT_SMOKE_PATHS = [ '/', '/health', '/download/version.json' ]; function parseArgs(argv) { const options = { envName: 'production', command: '', args: [] }; const tokens = [ ...argv ]; if (tokens.length === 0) { throw new Error('缺少命令: inspect | smoke | verify | cleanup-backups'); } if (!COMMANDS.has(tokens[0])) { options.envName = tokens[0]; tokens.shift(); } if (tokens.length === 0) { throw new Error('缺少命令: inspect | smoke | verify | cleanup-backups'); } options.command = tokens[0]; options.args = tokens.slice(1); if (!COMMANDS.has(options.command)) { throw new Error(`不支持的命令: ${options.command}`); } return options; } function getActivePortInfo(config) { const currentMdPath = `${config.deployRoot}/current.md`; const output = runSsh(config, `cat ${quoteForBash(currentMdPath)} 2>/dev/null || true`, { stdio: 'pipe' }); const rawContent = String(output || '').trim(); if (!rawContent) { throw new Error(`无法读取远端 ${currentMdPath},内容为空或文件不存在。`); } const version = rawContent.replace(/\r/g, '').trim(); if (version !== 'v1' && version !== 'v2') { throw new Error(`远端 current.md 内容无效: "${rawContent}",期望值为 v1 或 v2。`); } return { port: config.ports[version], version, rawContent }; } function inspectState(config) { const latestBackupFile = `${config.stateDir}/latest-backup.txt`; const remoteScript = [ 'set -euo pipefail', `STATE_DIR=${quoteForBash(config.stateDir)}`, `BACKUP_DIR=${quoteForBash(config.backupDir)}`, `LATEST_FILE=${quoteForBash(latestBackupFile)}`, 'printf "[STATE]\\n"', 'if [ -d "$STATE_DIR" ]; then ls -la "$STATE_DIR"; else echo MISSING; fi', 'printf "\\n[LATEST_BACKUP]\\n"', 'if [ -f "$LATEST_FILE" ]; then cat "$LATEST_FILE"; else echo MISSING; fi', 'printf "\\n[BACKUPS]\\n"', 'if [ -d "$BACKUP_DIR" ]; then ls -1t "$BACKUP_DIR"; else echo MISSING; fi', ].join('\n'); return runSsh(config, remoteScript, { stdio: 'pipe' }).trim(); } function smokeRoutes(config) { const { port } = getActivePortInfo(config); const paths = DEFAULT_SMOKE_PATHS.map(quoteForBash).join(' '); const useNginx = Boolean(config.serverName); const baseUrl = useNginx ? 'http://127.0.0.1' : `http://127.0.0.1:${port}`; const remoteScript = [ 'set -euo pipefail', `BASE_URL=${quoteForBash(baseUrl)}`, `SERVER_NAME=${quoteForBash(config.serverName || '')}`, `for path in ${paths}; do`, ' if [ -n "$SERVER_NAME" ]; then', ' code=$(curl -sS -o /dev/null -w "%{http_code}" -H "Host: $SERVER_NAME" "$BASE_URL$path" || true)', ' else', ' code=$(curl -sS -o /dev/null -w "%{http_code}" "$BASE_URL$path" || true)', ' fi', ' printf "%s %s\\n" "$path" "$code"', 'done', ].join('\n'); return runSsh(config, remoteScript, { stdio: 'pipe' }).trim(); } function cleanupBackups(config, names) { if (!names.length) { throw new Error('cleanup-backups 需要至少一个备份文件名'); } const remoteScript = [ 'set -euo pipefail', `BACKUP_DIR=${quoteForBash(config.backupDir)}`, ...names.map((name, index) => `FILE_${index}=${quoteForBash(name)}`), ...names.map((_, index) => `if [ -f "$BACKUP_DIR/$FILE_${index}" ]; then rm -f "$BACKUP_DIR/$FILE_${index}"; echo "REMOVED $FILE_${index}"; else echo "MISS $FILE_${index}"; fi`), ].join('\n'); return runSsh(config, remoteScript, { stdio: 'pipe' }).trim(); } function showActiveInfo(config) { let activeInfo; try { activeInfo = getActivePortInfo(config); } catch (err) { console.log(`[CURRENT_MD] 读取失败: ${err.message}`); console.log(''); return; } console.log(`[CURRENT_MD] 内容: ${activeInfo.rawContent}`); console.log(`[ACTIVE] 端口: ${activeInfo.port} (${activeInfo.version})`); console.log(''); } function main() { const options = parseArgs(process.argv.slice(2)); const config = loadDeployConfig(options.envName); if (options.command === 'inspect') { showActiveInfo(config); console.log(inspectState(config)); return; } if (options.command === 'smoke') { showActiveInfo(config); console.log('[SMOKE]'); console.log(smokeRoutes(config)); return; } if (options.command === 'verify') { showActiveInfo(config); console.log(inspectState(config)); console.log(''); console.log('[SMOKE]'); console.log(smokeRoutes(config)); return; } if (options.command === 'cleanup-backups') { console.log(cleanupBackups(config, options.args)); return; } throw new Error(`不支持的命令: ${options.command}`); } try { main(); } catch (err) { console.error(`[ERROR] ${err && err.message ? err.message : err}`); process.exit(1); }