146 lines
3.7 KiB
JavaScript
146 lines
3.7 KiB
JavaScript
'use strict';
|
|
|
|
const path = require('node:path');
|
|
const {
|
|
ensureCommandAvailable,
|
|
loadDeployConfig,
|
|
quoteForBash,
|
|
runSsh,
|
|
} = require('./deploy-lib.cjs');
|
|
|
|
function parseArgs(argv) {
|
|
const options = {
|
|
history: 0,
|
|
};
|
|
|
|
for (const item of argv) {
|
|
if (!item) continue;
|
|
if (item.startsWith('--history=')) {
|
|
const value = Number(item.slice('--history='.length));
|
|
if (Number.isFinite(value) && value > 0) {
|
|
options.history = Math.floor(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return options;
|
|
}
|
|
|
|
/**
|
|
* 查询远端 v1/v2 活跃实例状态
|
|
* @param {object} config - 部署配置
|
|
* @return {{ version: string, port: number, currentMd: string } | null} 活跃实例信息,读不到返回 null
|
|
*/
|
|
function queryActiveInstance(config) {
|
|
const currentMdFile = path.posix.join(config.deployRoot, 'current.md');
|
|
const remoteScript = [
|
|
'set -euo pipefail',
|
|
`FILE=${quoteForBash(currentMdFile)}`,
|
|
'if [ -f "$FILE" ]; then',
|
|
' cat "$FILE"',
|
|
'else',
|
|
' echo CURRENT_MD_NOT_FOUND',
|
|
'fi',
|
|
].join('\n');
|
|
|
|
const raw = runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
|
|
if (!raw || raw === 'CURRENT_MD_NOT_FOUND') {
|
|
return null;
|
|
}
|
|
|
|
const version = raw.replace(/[\r\n\s]/g, '');
|
|
if (version !== 'v1' && version !== 'v2') {
|
|
return null;
|
|
}
|
|
|
|
const port = version === 'v2' ? 6062 : 6061;
|
|
return { version, port, currentMd: raw.split('\n')[0].trim() };
|
|
}
|
|
|
|
/**
|
|
* 查询远端当前 release 信息
|
|
* @param {object} config - 部署配置
|
|
* @return {object | string | null} 解析后的 JSON 或原始字符串,查不到返回 null
|
|
*/
|
|
function queryCurrentRelease(config) {
|
|
const releaseFile = path.posix.join(config.stateDir, 'current-release.json');
|
|
const remoteScript = [
|
|
'set -euo pipefail',
|
|
`FILE=${quoteForBash(releaseFile)}`,
|
|
'if [ -f "$FILE" ]; then',
|
|
' cat "$FILE"',
|
|
'else',
|
|
' echo ONLINE_VERSION_NOT_FOUND',
|
|
'fi',
|
|
].join('\n');
|
|
|
|
const output = runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
|
|
if (!output || output === 'ONLINE_VERSION_NOT_FOUND') {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return JSON.parse(output);
|
|
} catch (error) {
|
|
return output;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 查询远端 release 历史记录
|
|
* @param {object} config - 部署配置
|
|
* @param {number} limit - 返回最近 N 条记录
|
|
* @return {Array<object> | null} 历史记录数组,查不到返回 null
|
|
*/
|
|
function queryReleaseHistory(config, limit) {
|
|
const historyFile = path.posix.join(config.stateDir, 'release-history.jsonl');
|
|
const remoteScript = [
|
|
'set -euo pipefail',
|
|
`FILE=${quoteForBash(historyFile)}`,
|
|
`LIMIT=${Number(limit)}`,
|
|
'if [ -f "$FILE" ]; then',
|
|
' tail -n "$LIMIT" "$FILE"',
|
|
'else',
|
|
' echo ONLINE_VERSION_HISTORY_EMPTY',
|
|
'fi',
|
|
].join('\n');
|
|
|
|
const output = runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
|
|
if (!output || output === 'ONLINE_VERSION_HISTORY_EMPTY') {
|
|
return null;
|
|
}
|
|
|
|
const lines = output.split(/\r?\n/).map(item => item.trim()).filter(Boolean);
|
|
const records = [];
|
|
for (const line of lines) {
|
|
try {
|
|
records.push(JSON.parse(line));
|
|
} catch (error) {
|
|
records.push({ raw: line });
|
|
}
|
|
}
|
|
return records;
|
|
}
|
|
|
|
function main() {
|
|
const envName = process.argv[2] || 'production';
|
|
const options = parseArgs(process.argv.slice(3));
|
|
|
|
ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。');
|
|
|
|
const config = loadDeployConfig(envName);
|
|
const active = queryActiveInstance(config);
|
|
|
|
if (options.history > 0) {
|
|
const history = queryReleaseHistory(config, options.history);
|
|
console.log(JSON.stringify({ active, history }, null, 2));
|
|
return;
|
|
}
|
|
|
|
const release = queryCurrentRelease(config);
|
|
console.log(JSON.stringify({ active, release }, null, 2));
|
|
}
|
|
|
|
|
|
main();
|