WMS_MAIN/script/deploy-ops.cjs

142 lines
4.1 KiB
JavaScript

'use strict';
const { loadDeployConfig, quoteForBash, runSsh } = require('./deploy-lib.cjs');
const COMMANDS = new Set([ 'inspect', 'smoke', 'verify', 'cleanup-backups', 'gateway-probe' ]);
function parseArgs(argv) {
const options = {
envName: 'production',
command: '',
args: [],
};
const tokens = [ ...argv ];
if (tokens.length === 0) {
throw new Error('缺少命令: inspect | smoke | verify | cleanup-backups | gateway-probe');
}
const first = tokens[0];
if (!COMMANDS.has(first)) {
options.envName = first;
tokens.shift();
}
if (tokens.length === 0) {
throw new Error('缺少命令: inspect | smoke | verify | cleanup-backups | gateway-probe');
}
options.command = tokens[0];
options.args = tokens.slice(1);
if (!COMMANDS.has(options.command)) {
throw new Error(`不支持的命令: ${options.command}`);
}
return options;
}
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"',
'ls -la "$STATE_DIR"',
'printf "\\n[LATEST_BACKUP]\\n"',
'if [ -f "$LATEST_FILE" ]; then cat "$LATEST_FILE"; else echo MISSING; fi',
'printf "\\n[BACKUPS]\\n"',
'ls -1 "$BACKUP_DIR"',
].join('\n');
return runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
}
function smokeRoutes(config) {
const remoteScript = [
'set -euo pipefail',
'for path in / /api/docs /internal/v1/gateway /system; do',
' if [ "$path" = "/internal/v1/gateway" ] || [ "$path" = "/system" ]; then',
' code=$(curl -sS -o /dev/null -w "%{http_code}" -X POST "http://127.0.0.1:6060$path" || true)',
' else',
' code=$(curl -sS -o /dev/null -w "%{http_code}" "http://127.0.0.1:6060$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 gatewayProbe(config, methodName, payloadText) {
if (!methodName) {
throw new Error('gateway-probe 需要 method 名称');
}
const payload = payloadText && payloadText.trim() ? payloadText : '{}';
const remoteScript = [
'set -euo pipefail',
`METHOD_NAME=${quoteForBash(methodName)}`,
`PAYLOAD=${quoteForBash(payload)}`,
'timestamp=$(date +%s)',
'curl -sS -X POST "http://127.0.0.1:6060/internal/v1/gateway?method=$METHOD_NAME&v=1&timestamp=$timestamp" -H "Content-Type: text/plain" --data "$PAYLOAD"',
].join('\n');
return runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
}
function main() {
const options = parseArgs(process.argv.slice(2));
const config = loadDeployConfig(options.envName);
if (options.command === 'inspect') {
console.log(inspectState(config));
return;
}
if (options.command === 'smoke') {
console.log(smokeRoutes(config));
return;
}
if (options.command === 'verify') {
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;
}
if (options.command === 'gateway-probe') {
const methodName = options.args[0];
const payloadText = options.args[1] || '{}';
console.log(gatewayProbe(config, methodName, payloadText));
return;
}
throw new Error(`不支持的命令: ${options.command}`);
}
main();