183 lines
5.4 KiB
JavaScript
183 lines
5.4 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' ? 7072 : 7071;
|
||
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_NOT_FOUND',
|
||
'fi',
|
||
].join('\n');
|
||
|
||
const output = runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
|
||
if (!output || output === 'ONLINE_VERSION_HISTORY_NOT_FOUND') {
|
||
return null;
|
||
}
|
||
|
||
const records = [];
|
||
for (const line of output.split(/\r?\n/)) {
|
||
const trimmed = line.trim();
|
||
if (!trimmed) continue;
|
||
try {
|
||
records.push(JSON.parse(trimmed));
|
||
} catch (error) {
|
||
records.push({ raw: trimmed });
|
||
}
|
||
}
|
||
return records;
|
||
}
|
||
|
||
function main() {
|
||
const envName = process.argv[2] || 'production';
|
||
const options = parseArgs(process.argv.slice(3));
|
||
const config = loadDeployConfig(envName);
|
||
|
||
ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。');
|
||
|
||
const active = queryActiveInstance(config);
|
||
const currentRelease = queryCurrentRelease(config);
|
||
|
||
console.log('========================================');
|
||
console.log(`部署环境: ${envName}`);
|
||
console.log(`远端主机: ${config.username}@${config.host}:${config.port}`);
|
||
console.log(`部署根目录: ${config.deployRoot}`);
|
||
console.log('========================================');
|
||
|
||
if (active) {
|
||
console.log(`活跃版本: ${active.version} (端口 ${active.port})`);
|
||
} else {
|
||
console.log('活跃版本: 未检测到(current.md 不存在或无效)');
|
||
}
|
||
|
||
console.log('----------------------------------------');
|
||
|
||
if (currentRelease && typeof currentRelease === 'object') {
|
||
console.log('当前线上版本:');
|
||
console.log(` 发布版本: ${currentRelease.releaseVersion || 'N/A'}`);
|
||
console.log(` package版本: ${currentRelease.packageVersion || 'N/A'}`);
|
||
console.log(` Git commit: ${currentRelease.gitCommitShort || 'N/A'}`);
|
||
console.log(` 目标版本: ${currentRelease.targetVersion || 'N/A'} (端口 ${currentRelease.targetPort || 'N/A'})`);
|
||
console.log(` 发布时间: ${currentRelease.deployedAt || 'N/A'}`);
|
||
} else if (currentRelease) {
|
||
console.log(`当前线上版本 (原始): ${String(currentRelease)}`);
|
||
} else {
|
||
console.log('当前线上版本: 未记录');
|
||
}
|
||
|
||
// 历史记录
|
||
if (options.history > 0) {
|
||
console.log('----------------------------------------');
|
||
console.log(`最近 ${options.history} 条发布记录:`);
|
||
const history = queryReleaseHistory(config, options.history);
|
||
if (history && history.length > 0) {
|
||
for (const record of history) {
|
||
if (record.releaseVersion) {
|
||
console.log(` ${record.releaseVersion} | ${record.targetVersion || '?'} | ${record.deployedAt || '?'}`);
|
||
} else {
|
||
console.log(` ${JSON.stringify(record)}`);
|
||
}
|
||
}
|
||
} else {
|
||
console.log(' (无历史记录)');
|
||
}
|
||
}
|
||
}
|
||
|
||
main();
|