feat: 新增部署脚本

This commit is contained in:
小蟑螂
2026-07-07 23:15:10 +08:00
parent dd8cba9777
commit 8af2e9194b
15 changed files with 2323 additions and 0 deletions
+129
View File
@@ -0,0 +1,129 @@
import { connectWithRetry, installKeyAfterPasswordAuth, execRemote } from '../ssh.js';
import { composeUp, composeRestart, composeLogs, composeStatus, composeDown, uploadComposeFile } from '../docker.js';
import { loadConfig, getServiceConfig, syncServerKeyAuth } from '../config.js';
import { printError, printInfo, printSuccess } from '../ui/render.js';
import type { Environment, Service, ServiceEnvConfig } from '../types.js';
import * as frontendDeploy from './frontend.js';
import * as backendDeploy from './backend.js';
import * as licenseDeploy from './license.js';
const deployers: Record<Service, { build: (cfg?: ServiceEnvConfig) => Promise<string>; deploy: (conn: any, cfg: ServiceEnvConfig, buildDir: string) => Promise<void> }> = {
frontend: frontendDeploy as any,
backend: backendDeploy as any,
license: licenseDeploy as any,
};
export async function buildService(service: Service, cfg: ServiceEnvConfig): Promise<string> {
const deployer = deployers[service];
if (!deployer) throw new Error(`未知服务: ${service}`);
printInfo(`构建 ${service}...`);
const buildDir = await deployer.build(cfg);
printSuccess(`${service} 构建完成`);
return buildDir;
}
export async function deployService(conn: any, service: Service, cfg: ServiceEnvConfig, buildDir: string): Promise<void> {
const deployer = deployers[service];
if (!deployer) throw new Error(`未知服务: ${service}`);
printInfo(`部署 ${service} 到 ${cfg.server.host}...`);
await deployer.deploy(conn, cfg, buildDir);
}
/** 连接 SSH;密码登录成功后自动安装公钥并持久化到 config.json */
async function connectForService(
cfg: ServiceEnvConfig,
onPassword?: () => Promise<string>,
): Promise<any> {
const { conn, authMethod } = await connectWithRetry(cfg.server, undefined, onPassword);
if (authMethod === 'password') {
printInfo('首次密码登录成功,正在配置 SSH 密钥免密...');
const keyPath = await installKeyAfterPasswordAuth(conn, cfg.server);
await syncServerKeyAuth(cfg.server.host, cfg.server.username, {
authType: 'key',
keyPath,
lastPassword: undefined,
});
cfg.server.authType = 'key';
cfg.server.keyPath = keyPath;
printSuccess(`已配置免密登录 (${cfg.server.host}),后续部署不再询问密码`);
}
return conn;
}
export async function buildAndDeployServices(
services: Service[],
onPassword?: (service: Service) => Promise<string>,
): Promise<void> {
for (const service of services) {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
console.clear();
const buildDir = await buildService(service, cfg);
// 若同 host 已配置密钥,connectWithRetry 会直接免密,不再触发 onPassword
const conn = await connectForService(
cfg,
onPassword ? () => onPassword(service) : undefined,
);
try {
if (cfg.extractDir) {
await deployService(conn, service, cfg, buildDir);
} else {
await uploadComposeFile(conn, service, cfg);
await deployService(conn, service, cfg, buildDir);
await composeUp(conn, cfg, service);
}
} finally {
conn.end();
}
printSuccess(`${service} 部署完成`);
}
}
export async function restartService(service: Service, onPassword?: () => Promise<string>): Promise<void> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const conn = await connectForService(cfg, onPassword);
try { await composeRestart(conn, cfg, service); } finally { conn.end(); }
}
export async function stopService(service: Service, onPassword?: () => Promise<string>): Promise<void> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const conn = await connectForService(cfg, onPassword);
try { await composeDown(conn, cfg, service); } finally { conn.end(); }
}
export async function serviceLogs(service: Service, tail = 50, onPassword?: () => Promise<string>): Promise<string> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const conn = await connectForService(cfg, onPassword);
try { return await composeLogs(conn, cfg, service, tail); } finally { conn.end(); }
}
export async function serviceStatus(service: Service, onPassword?: () => Promise<string>): Promise<string> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const conn = await connectForService(cfg, onPassword);
try { return await composeStatus(conn, cfg); } finally { conn.end(); }
}
export async function checkRemoteDocker(service: Service, onPassword?: () => Promise<string>): Promise<boolean> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
try {
const conn = await connectForService(cfg, onPassword);
try {
const { stdout } = await execRemote(conn, 'docker --version && docker compose version');
printInfo(stdout.trim());
return true;
} finally { conn.end(); }
} catch (e: any) {
printError(`Docker 检查失败: ${e.message}`);
return false;
}
}