diff --git a/app/controller/gateway.js b/app/controller/gateway.js new file mode 100644 index 0000000..47f15eb --- /dev/null +++ b/app/controller/gateway.js @@ -0,0 +1,118 @@ +'use strict'; + +const { Controller } = require('egg'); +const { toSuccess, toFailure } = require('../lib/api_response'); + +class GatewayController extends Controller { + validateParams(params) { + const required = [ 'method', 'v', 'timestamp' ]; + + for (const field of required) { + if (!params[field]) { + return false; + } + } + + if (typeof params.method !== 'string' || !params.method.includes('.')) { + return false; + } + + return true; + } + + normalizeResult(result) { + if (result && typeof result === 'object' && Object.prototype.hasOwnProperty.call(result, 'success') && Object.prototype.hasOwnProperty.call(result, 'code')) { + if (result.success === false || Number(result.code) <= 0) { + return { + httpStatus: 200, + body: toFailure({ + message: result.message || '请求失败', + httpStatus: 200, + code: typeof result.code === 'number' ? result.code : 0, + data: result.data || null, + }), + }; + } + + return { + httpStatus: 200, + body: toSuccess({ + flag: 'success', + code: result.code, + message: result.message || 'ok', + data: result.data || null, + }), + }; + } + + return { + httpStatus: 200, + body: toSuccess(result), + }; + } + + async internal() { + const { ctx, service } = this; + + try { + const getParams = ctx.query; + if (!this.validateParams(getParams)) { + ctx.status = 400; + ctx.body = toFailure({ + message: '参数缺失或错误: method/v/timestamp', + httpStatus: 400, + code: 400, + }); + ctx.type = 'application/json'; + return; + } + + const methods = String(getParams.method).split('.'); + const actionName = methods.pop(); + const className = methods.join('.'); + + let jsonData = ''; + if (ctx.request.rawBody) { + jsonData = ctx.request.rawBody; + } else { + jsonData = await new Promise((resolve, reject) => { + let data = ''; + ctx.req.on('data', chunk => { + data += chunk; + }); + ctx.req.on('end', () => { + resolve(data); + }); + ctx.req.on('error', reject); + }); + } + + jsonData = jsonData ? String(jsonData) : ''; + if (!jsonData || jsonData.trim() === '') { + throw new Error('json数据为空'); + } + + try { + jsonData = JSON.parse(jsonData); + } catch (error) { + throw new Error('json数据解析错误'); + } + + const result = await service.api.callMethod(className, actionName, jsonData); + const normalized = this.normalizeResult(result); + ctx.status = normalized.httpStatus; + ctx.body = normalized.body; + ctx.type = 'application/json'; + } catch (error) { + ctx.status = 500; + ctx.body = toFailure({ + message: '接口错误: ' + error.message, + httpStatus: 500, + code: 500, + }); + ctx.type = 'application/json'; + } + } +} + +module.exports = GatewayController; \ No newline at end of file diff --git a/script/deploy-remote.cjs b/script/deploy-remote.cjs new file mode 100644 index 0000000..b3457f5 --- /dev/null +++ b/script/deploy-remote.cjs @@ -0,0 +1,191 @@ +'use strict'; + +const fs = require('node:fs'); +const path = require('node:path'); +const { URL } = require('node:url'); +const { + ROOT, + buildArchive, + buildRemoteCleanupGuard, + buildScopedEggStopScript, + ensureCommandAvailable, + getNowStamp, + loadDeployConfig, + quoteForBash, + runCommand, + runScp, + runSsh, + serializeCommands, +} = require('./deploy-lib.cjs'); +const { recordVersionUpdate } = require('./version-registry.cjs'); + +function splitPostDeployCommands(commands) { + const normalized = Array.isArray(commands) ? commands.filter(Boolean).map(item => String(item).trim()).filter(Boolean) : []; + const stopCommandPattern = /^(?:npm(?:\.cmd)?\s+(?:run\s+)?stop|egg-scripts\s+stop)(?:\s|$)/i; + const startCommandIndex = normalized.findIndex(item => /^npm\s+start(?:\s|$)/i.test(item)); + const stopCommandIndexes = normalized + .map((item, index) => (stopCommandPattern.test(item) ? index : -1)) + .filter(index => index >= 0); + + if (startCommandIndex === -1) { + return { + preStartCommands: normalized.filter((_, index) => !stopCommandIndexes.includes(index)), + startCommand: '', + }; + } + + return { + preStartCommands: normalized.filter((_, index) => index !== startCommandIndex && !stopCommandIndexes.includes(index)), + startCommand: normalized[startCommandIndex], + }; +} + +function buildHealthCheckScript(healthCheckUrl) { + if (!healthCheckUrl) { + return ''; + } + + let fallbackUrl = ''; + try { + const parsed = new URL(healthCheckUrl); + const hostname = String(parsed.hostname || '').toLowerCase(); + const isLocalHost = hostname === '127.0.0.1' || hostname === 'localhost'; + if (!isLocalHost) { + const port = parsed.port || '6060'; + fallbackUrl = `http://127.0.0.1:${port}${parsed.pathname || '/'}${parsed.search || ''}`; + } + } catch (error) { + fallbackUrl = 'http://127.0.0.1:6060/'; + } + + return [ + `HEALTHCHECK_URL=${quoteForBash(healthCheckUrl)}`, + ...(fallbackUrl ? [ `HEALTHCHECK_FALLBACK_URL=${quoteForBash(fallbackUrl)}` ] : []), + 'HEALTHCHECK_CURL_OPTS="--connect-timeout 2 --max-time 4"', + 'for attempt in $(seq 1 20); do', + ' if curl -fsS $HEALTHCHECK_CURL_OPTS "$HEALTHCHECK_URL" >/dev/null; then', + ' break', + ' fi', + ...(fallbackUrl ? [ ' if curl -fsS $HEALTHCHECK_CURL_OPTS "$HEALTHCHECK_FALLBACK_URL" >/dev/null; then', ' break', ' fi' ] : []), + ' if [ "$attempt" -eq 20 ]; then', + ...(fallbackUrl + ? [ ' echo "健康检查失败: $HEALTHCHECK_URL (fallback: $HEALTHCHECK_FALLBACK_URL)" >&2' ] + : [ ' echo "健康检查失败: $HEALTHCHECK_URL" >&2' ]), + ' exit 1', + ' fi', + ' sleep 1', + 'done', + ].join('\n'); +} + +function main() { + const envName = process.argv[2] || 'production'; + const config = loadDeployConfig(envName); + + ensureCommandAvailable('git', '请先安装 Git 并确保 git 在 PATH 中。'); + ensureCommandAvailable('tar', '请先安装 tar,Windows 10/11 通常已内置 bsdtar。'); + ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。'); + ensureCommandAvailable('scp', '请先安装 OpenSSH Client 并确保 scp 在 PATH 中。'); + + const stamp = getNowStamp(); + const packageJson = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')); + const packageVersion = String(packageJson.version || '0.0.0'); + const gitCommit = runCommand('git', [ 'rev-parse', 'HEAD' ]); + const gitCommitShort = runCommand('git', [ 'rev-parse', '--short', 'HEAD' ]); + const gitBranch = runCommand('git', [ 'rev-parse', '--abbrev-ref', 'HEAD' ]); + const releaseVersion = `v${packageVersion}-${stamp}-${gitCommitShort}`; + + const { archivePath } = buildArchive(stamp, config); + const remoteArchive = `${config.incomingDir}/release-${stamp}.tar.gz`; + const remoteArchiveName = path.posix.basename(remoteArchive); + const latestBackupFile = `latest-backup.txt`; + const releaseStateFile = path.posix.join(config.stateDir, 'current-release.json'); + const releaseHistoryFile = path.posix.join(config.stateDir, 'release-history.jsonl'); + const backupName = `backup-${stamp}.tar.gz`; + const cleanupGuard = buildRemoteCleanupGuard(config.sharedDirs, config.preserveDirs); + const preDeployCommands = serializeCommands(config.preDeployCommands); + const postDeploy = splitPostDeployCommands(config.postDeployCommands); + const postDeployCommands = serializeCommands(postDeploy.preStartCommands); + const startCommand = postDeploy.startCommand; + const scopedStopCommand = buildScopedEggStopScript(config.deployRoot); + const detachedStartCommand = startCommand + ? `nohup bash -lc ${quoteForBash(startCommand)} >/tmp/wms-deploy-start.log 2>&1 &` + : ''; + const healthCheckCommand = buildHealthCheckScript(config.healthCheckUrl); + const releaseMeta = { + releaseVersion, + packageVersion, + gitCommit, + gitCommitShort, + gitBranch, + stamp, + deployedAt: new Date().toISOString(), + envName, + deployRoot: config.deployRoot, + archiveFile: remoteArchiveName, + backupFile: backupName, + }; + const releaseMetaPretty = JSON.stringify(releaseMeta, null, 2); + const releaseMetaLine = JSON.stringify(releaseMeta); + + const setupScript = [ + 'set -euo pipefail', + `mkdir -p ${quoteForBash(config.deployRoot)} ${quoteForBash(config.controlDir)} ${quoteForBash(config.incomingDir)} ${quoteForBash(config.backupDir)} ${quoteForBash(config.stateDir)}`, + ].join('\n'); + runSsh(config, setupScript); + runScp(config, archivePath, remoteArchive); + + const remoteScript = [ + 'set -euo pipefail', + `DEPLOY_ROOT=${quoteForBash(config.deployRoot)}`, + `BACKUP_DIR=${quoteForBash(config.backupDir)}`, + `STATE_DIR=${quoteForBash(config.stateDir)}`, + `REMOTE_ARCHIVE=${quoteForBash(remoteArchive)}`, + `KEEP_BACKUPS=${Number(config.keepBackups)}`, + `BACKUP_NAME=${quoteForBash(backupName)}`, + `CURRENT_RELEASE_FILE=${quoteForBash(releaseStateFile)}`, + `RELEASE_HISTORY_FILE=${quoteForBash(releaseHistoryFile)}`, + `RELEASE_META_JSON=${quoteForBash(releaseMetaPretty)}`, + `RELEASE_META_LINE=${quoteForBash(releaseMetaLine)}`, + 'cd "$DEPLOY_ROOT"', + 'mkdir -p "$BACKUP_DIR" "$STATE_DIR"', + 'tar --exclude=.deploy -czf "$BACKUP_DIR/$BACKUP_NAME" .', + preDeployCommands, + `find . -mindepth 1 -maxdepth 1 ${cleanupGuard} -exec rm -rf {} +`, + 'tar -xzf "$REMOTE_ARCHIVE" -C "$DEPLOY_ROOT"', + 'rm -f "$REMOTE_ARCHIVE"', + `printf '%s\\n' "$BACKUP_NAME" > ${quoteForBash(path.posix.join(config.stateDir, latestBackupFile))}`, + 'if [ ! -d "$DEPLOY_ROOT/config" ]; then echo "配置目录缺失: $DEPLOY_ROOT/config(首次发布请先创建并放置生产配置)" >&2; exit 1; fi', + postDeployCommands, + detachedStartCommand, + healthCheckCommand, + 'printf "%s\\n" "$RELEASE_META_JSON" > "$CURRENT_RELEASE_FILE"', + 'printf "%s\\n" "$RELEASE_META_LINE" >> "$RELEASE_HISTORY_FILE"', + 'ls -1t "$BACKUP_DIR"/*.tar.gz 2>/dev/null | tail -n +$((KEEP_BACKUPS + 1)) | xargs -r rm -f', + ].filter(Boolean).join('\n'); + + try { + runSsh(config, scopedStopCommand, { stdio: 'inherit' }); + } catch (error) { + console.warn(`定点停进程失败,继续发布流程: ${error.message}`); + } + + runSsh(config, remoteScript); + try { + recordVersionUpdate({ + source: 'deploy:prod', + note: `env=${envName}`, + releaseVersion, + deployRoot: config.deployRoot, + remoteBackupFile: backupName, + remoteStateFile: releaseStateFile, + remoteHistoryFile: releaseHistoryFile, + }); + } catch (error) { + console.warn(`本地版本记录写入失败: ${error.message}`); + } + console.log(`发布完成: ${envName} -> ${config.username}@${config.host}:${config.deployRoot}`); + console.log(`线上版本号: ${releaseVersion}`); +} + +main(); \ No newline at end of file diff --git a/script/rollback-remote.cjs b/script/rollback-remote.cjs new file mode 100644 index 0000000..0bc8dd9 --- /dev/null +++ b/script/rollback-remote.cjs @@ -0,0 +1,140 @@ +'use strict'; + +const path = require('node:path'); +const { URL } = require('node:url'); +const { + buildRemoteCleanupGuard, + buildScopedEggStopScript, + ensureCommandAvailable, + getNowStamp, + loadDeployConfig, + quoteForBash, + runSsh, + serializeCommands, +} = require('./deploy-lib.cjs'); + +function splitPostDeployCommands(commands) { + const normalized = Array.isArray(commands) ? commands.filter(Boolean).map(item => String(item).trim()).filter(Boolean) : []; + const stopCommandPattern = /^(?:npm(?:\.cmd)?\s+(?:run\s+)?stop|egg-scripts\s+stop)(?:\s|$)/i; + const startCommandIndex = normalized.findIndex(item => /^npm\s+start(?:\s|$)/i.test(item)); + const stopCommandIndexes = normalized + .map((item, index) => (stopCommandPattern.test(item) ? index : -1)) + .filter(index => index >= 0); + + if (startCommandIndex === -1) { + return { + preStartCommands: normalized.filter((_, index) => !stopCommandIndexes.includes(index)), + startCommand: '', + }; + } + + return { + preStartCommands: normalized.filter((_, index) => index !== startCommandIndex && !stopCommandIndexes.includes(index)), + startCommand: normalized[startCommandIndex], + }; +} + +function buildHealthCheckScript(healthCheckUrl) { + if (!healthCheckUrl) { + return ''; + } + + let fallbackUrl = ''; + try { + const parsed = new URL(healthCheckUrl); + const hostname = String(parsed.hostname || '').toLowerCase(); + const isLocalHost = hostname === '127.0.0.1' || hostname === 'localhost'; + if (!isLocalHost) { + const port = parsed.port || '6060'; + fallbackUrl = `http://127.0.0.1:${port}${parsed.pathname || '/'}${parsed.search || ''}`; + } + } catch (error) { + fallbackUrl = 'http://127.0.0.1:6060/'; + } + + return [ + `HEALTHCHECK_URL=${quoteForBash(healthCheckUrl)}`, + ...(fallbackUrl ? [ `HEALTHCHECK_FALLBACK_URL=${quoteForBash(fallbackUrl)}` ] : []), + 'HEALTHCHECK_CURL_OPTS="--connect-timeout 2 --max-time 4"', + 'for attempt in $(seq 1 20); do', + ' if curl -fsS $HEALTHCHECK_CURL_OPTS "$HEALTHCHECK_URL" >/dev/null; then', + ' break', + ' fi', + ...(fallbackUrl ? [ ' if curl -fsS $HEALTHCHECK_CURL_OPTS "$HEALTHCHECK_FALLBACK_URL" >/dev/null; then', ' break', ' fi' ] : []), + ' if [ "$attempt" -eq 20 ]; then', + ...(fallbackUrl + ? [ ' echo "健康检查失败: $HEALTHCHECK_URL (fallback: $HEALTHCHECK_FALLBACK_URL)" >&2' ] + : [ ' echo "健康检查失败: $HEALTHCHECK_URL" >&2' ]), + ' exit 1', + ' fi', + ' sleep 1', + 'done', + ].join('\n'); +} + +function parseBackupArg(argv) { + const backupArg = argv.find(item => item.startsWith('--backup=')); + if (!backupArg) { + return ''; + } + return backupArg.slice('--backup='.length).trim(); +} + +function main() { + const envName = process.argv[2] || 'production'; + const config = loadDeployConfig(envName); + const backupName = parseBackupArg(process.argv.slice(3)); + + ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。'); + + const stamp = getNowStamp(); + const cleanupGuard = buildRemoteCleanupGuard(config.sharedDirs, config.preserveDirs); + const postDeploy = splitPostDeployCommands(config.postDeployCommands); + const postDeployCommands = serializeCommands(postDeploy.preStartCommands); + const startCommand = postDeploy.startCommand; + const scopedStopCommand = buildScopedEggStopScript(config.deployRoot); + const detachedStartCommand = startCommand + ? `nohup bash -lc ${quoteForBash(startCommand)} >/tmp/wms-rollback-start.log 2>&1 &` + : ''; + const latestBackupFile = path.posix.join(config.stateDir, 'latest-backup.txt'); + const healthCheckCommand = buildHealthCheckScript(config.healthCheckUrl); + + const remoteScript = [ + 'set -euo pipefail', + `DEPLOY_ROOT=${quoteForBash(config.deployRoot)}`, + `BACKUP_DIR=${quoteForBash(config.backupDir)}`, + `LATEST_BACKUP_FILE=${quoteForBash(latestBackupFile)}`, + `REQUESTED_BACKUP=${quoteForBash(backupName)}`, + `CURRENT_SAFETY_BACKUP=${quoteForBash(`rollback-safety-${stamp}.tar.gz`)}`, + 'if [ -n "$REQUESTED_BACKUP" ]; then', + ' TARGET_BACKUP="$REQUESTED_BACKUP"', + 'elif [ -f "$LATEST_BACKUP_FILE" ]; then', + ' TARGET_BACKUP="$(cat "$LATEST_BACKUP_FILE")"', + 'else', + ' echo "未找到最近一次发布备份,请通过 --backup=backup-时间戳.tar.gz 指定回退包。" >&2', + ' exit 1', + 'fi', + 'if [ ! -f "$BACKUP_DIR/$TARGET_BACKUP" ]; then', + ' echo "回退包不存在: $BACKUP_DIR/$TARGET_BACKUP" >&2', + ' exit 1', + 'fi', + 'cd "$DEPLOY_ROOT"', + 'tar --exclude=.deploy -czf "$BACKUP_DIR/$CURRENT_SAFETY_BACKUP" .', + `find . -mindepth 1 -maxdepth 1 ${cleanupGuard} -exec rm -rf {} +`, + 'tar -xzf "$BACKUP_DIR/$TARGET_BACKUP" -C "$DEPLOY_ROOT"', + postDeployCommands, + detachedStartCommand, + healthCheckCommand, + ].filter(Boolean).join('\n'); + + try { + runSsh(config, scopedStopCommand, { stdio: 'inherit' }); + } catch (error) { + console.warn(`定点停进程失败,继续回退流程: ${error.message}`); + } + + runSsh(config, remoteScript); + console.log(`回退完成: ${envName} -> ${config.username}@${config.host}:${config.deployRoot}`); +} + +main(); \ No newline at end of file