WMS_MAIN/script/deploy-ops.cjs
liufumingren 1fbc24c904 chore(deploy): 运维命令收敛为 install/uninstall,移除失效的 deploy:gateway-probe
- watchdog 部署工具只保留 install / uninstall:uninstall 先备份到
  /tmp/qimen-watchdog-uninstall-<时间戳>/(cp -a 保留权限/时间),再**注销调度**
  —— 删除 /etc/cron.d/qimen-watchdog 并清理 root crontab 中残留的 qimen-watchdog 行
  (历史手工安装可能留过,双跑会并发 kill/start 同一实例),随后删除服务器副本
  <部署根>/script/qimen-watchdog.sh 及其 .prev-* / .legacy-* 与历史根目录副本;
  不碰日志、锁文件与接口服务进程;支持 --dry-run 只打印计划。
  原 precheck 的只读环境快照改为 install 开头自动打印;status / dry-run / run
  三个命令删除(需要时直接 ssh 执行 <部署根>/script/qimen-watchdog.sh --dry-run)。
- 移除失效的 deploy:gateway-probe:npm 脚本未传必需的 method 位置参数,而
  gatewayProbe() 在缺参时直接抛错,命令必然 exit 1(CLI 契约与 npm 接线不匹配,
  该命令从未能成功)。删除 npm 命令与 script/deploy-ops.cjs 中的 gatewayProbe 实现;
  需要网关真实探活时直连活跃端口(current.md:v1→6061 / v2→6062)。
- 两份发布 agent 文档中的 "npm run deploy:gateway-probe -- releaseProbe.ping {}"
  已删除 —— 其中的 releaseProbe.ping 这个 method 在应用内并不存在(双重失效),
  改为「直连活跃端口」的说明;README 同步运维命令清单与 watchdog 卸载说明。
- 顺手修掉 script/deploy-ops.cjs 两处历史 lint 问题(模板字符串改单引号、
  文件末尾补换行),该文件 lint 现已干净。
2026-09-28 01:21:55 +08:00

206 lines
5.8 KiB
JavaScript
Raw Permalink 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 { loadDeployConfig, quoteForBash, runSsh } = require('./deploy-lib.cjs');
const COMMANDS = new Set([ 'inspect', 'smoke', 'verify', 'cleanup-backups' ]);
/**
* 解析命令行参数,返回环境名、命令及额外参数
*
* @param {string[]} argv - 命令行参数数组
* @return {{ envName: string, command: string, args: string[] }} 解析后的选项对象
*/
function parseArgs(argv) {
const options = {
envName: 'production',
command: '',
args: [],
};
const tokens = [ ...argv ];
if (tokens.length === 0) {
throw new Error('缺少命令: inspect | smoke | verify | cleanup-backups');
}
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');
}
options.command = tokens[0];
options.args = tokens.slice(1);
if (!COMMANDS.has(options.command)) {
throw new Error(`不支持的命令: ${options.command}`);
}
return options;
}
/**
* 通过 SSH 读取远端 current.md 文件,获取当前活跃端口
* v1 → 6061,v2 → 6062
*
* @param {object} config - 部署配置对象
* @return {{ port: number, version: string, rawContent: string }} 活跃端口、版本标识及 current.md 原始内容
* @throws {Error} 如果读取失败或内容不是 v1/v2
*/
function getActivePortInfo(config) {
const currentMdPath = `${config.deployRoot}/current.md`;
const remoteScript = [
'set -euo pipefail',
`cat ${quoteForBash(currentMdPath)} 2>/dev/null || true`,
].join('\n');
const output = runSsh(config, remoteScript, { 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。文件路径: ${currentMdPath}`
);
}
const port = version === 'v1' ? 6061 : 6062;
return { port, version, rawContent };
}
/**
* 通过 SSH 检查远端部署状态(state 目录、备份等)
*
* @param {object} config - 部署配置对象
* @return {string} SSH 远程执行输出
*/
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();
}
/**
* 对当前活跃端口的关键路由执行冒烟探测
*
* @param {object} config - 部署配置对象
* @return {string} 各路由的 HTTP 状态码输出
*/
function smokeRoutes(config) {
const { port } = getActivePortInfo(config);
const remoteScript = [
'set -euo pipefail',
`PORT=${port}`,
'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:$PORT$path" || true)',
' else',
' code=$(curl -sS -o /dev/null -w "%{http_code}" "http://127.0.0.1:$PORT$path" || true)',
' fi',
' printf "%s %s\\n" "$path" "$code"',
'done',
].join('\n');
return runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
}
/**
* 清理远端备份目录中指定的备份文件
*
* @param {object} config - 部署配置对象
* @param {string[]} names - 要清理的备份文件名列表
* @return {string} 清理结果输出
*/
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();
}
/**
* 显示远端 current.md 内容及当前活跃端口信息
*
* @param {object} config - 部署配置对象
*/
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}`);
}
main();