import { connectWithRetry, installKeyAfterPasswordAuth, execRemote } from '../ssh.js'; import { composeRestartAfterUpload, initServiceRuntime, remoteImageExists, uploadComposeFile, composeRestart, composeLogs, composeStatus, composeDown, } from '../docker.js'; import { assertArtifactsExist, ensureArtifactsDirs } from '../artifacts.js'; import { getArtifactDir, loadConfig, getServiceConfig, syncServerKeyAuth, isStaticDeploy } 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 Promise; deploy: (conn: any, cfg: ServiceEnvConfig, buildDir: string, env: Environment) => Promise; }> = { frontend: frontendDeploy as any, backend: backendDeploy as any, license: licenseDeploy as any, }; export async function buildService(service: Service, cfg: ServiceEnvConfig): Promise { ensureArtifactsDirs(); const deployer = deployers[service]; if (!deployer) throw new Error(`未知服务: ${service}`); printInfo(`构建 ${service}...`); const buildDir = await deployer.build(cfg); printSuccess(`${service} 构建完成 → ${getArtifactDir(service)}`); return buildDir; } export async function deployService( conn: any, service: Service, cfg: ServiceEnvConfig, buildDir: string, env: Environment, ): Promise { const deployer = deployers[service]; if (!deployer) throw new Error(`未知服务: ${service}`); printInfo(`部署 ${service} 到 ${cfg.server.host}...`); await deployer.deploy(conn, cfg, buildDir, env); } /** 连接 SSH;密码登录成功后自动安装公钥 */ async function connectForService( cfg: ServiceEnvConfig, onPassword?: () => Promise, ): Promise { 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; } /** Docker 服务:上传产物后,首次 init 镜像,后续仅重启 */ async function finishDockerDeploy(conn: any, service: Service, cfg: ServiceEnvConfig, env: Environment): Promise { const exists = await remoteImageExists(conn, service, env); if (!exists) { await initServiceRuntime(conn, service, cfg, env); } else { await uploadComposeFile(conn, service, cfg, env); await composeRestartAfterUpload(conn, cfg, service, env); } } /** 仅编译到 artifacts/,不上传 */ export async function buildServicesOnly(services: Service[]): Promise { ensureArtifactsDirs(); for (const service of services) { const config = await loadConfig(); const cfg = getServiceConfig(config, service); console.clear(); await buildService(service, cfg); } } /** 从 artifacts/ 上传并重启(不编译) */ export async function deployArtifactsOnly( services: Service[], onPassword?: (service: Service) => Promise, ): Promise { ensureArtifactsDirs(); for (const service of services) { assertArtifactsExist(service); const config = await loadConfig(); const env = config.currentEnv; const cfg = getServiceConfig(config, service, env); console.clear(); const conn = await connectForService( cfg, onPassword ? () => onPassword(service) : undefined, ); try { if (isStaticDeploy(cfg)) { await deployService(conn, service, cfg, getArtifactDir(service), env); } else { await deployService(conn, service, cfg, getArtifactDir(service), env); await finishDockerDeploy(conn, service, cfg, env); } } finally { conn.end(); } printSuccess(`${service} 产物已上传并重启`); } } /** 自动部署:编译 → 上传 → 重启(不重建镜像) */ export async function buildAndDeployServices( services: Service[], onPassword?: (service: Service) => Promise, ): Promise { ensureArtifactsDirs(); for (const service of services) { const config = await loadConfig(); const env = config.currentEnv; const cfg = getServiceConfig(config, service, env); console.clear(); await buildService(service, cfg); const conn = await connectForService( cfg, onPassword ? () => onPassword(service) : undefined, ); try { if (isStaticDeploy(cfg)) { await deployService(conn, service, cfg, getArtifactDir(service), env); } else { await deployService(conn, service, cfg, getArtifactDir(service), env); await finishDockerDeploy(conn, service, cfg, env); } } finally { conn.end(); } printSuccess(`${service} 部署完成`); } } export async function restartService(service: Service, onPassword?: () => Promise): Promise { const config = await loadConfig(); const env = config.currentEnv; const cfg = getServiceConfig(config, service, env); const conn = await connectForService(cfg, onPassword); try { await composeRestart(conn, cfg, service, env); } finally { conn.end(); } } export async function stopService(service: Service, onPassword?: () => Promise): Promise { const config = await loadConfig(); const env = config.currentEnv; const cfg = getServiceConfig(config, service, env); const conn = await connectForService(cfg, onPassword); try { await composeDown(conn, cfg, service, env); } finally { conn.end(); } } export async function serviceLogs(service: Service, tail = 50, onPassword?: () => Promise): Promise { const config = await loadConfig(); const env = config.currentEnv; const cfg = getServiceConfig(config, service, env); const conn = await connectForService(cfg, onPassword); try { return await composeLogs(conn, cfg, service, env, tail); } finally { conn.end(); } } export async function serviceStatus(service: Service, onPassword?: () => Promise): Promise { const config = await loadConfig(); const env = config.currentEnv; const cfg = getServiceConfig(config, service, env); const conn = await connectForService(cfg, onPassword); try { return await composeStatus(conn, cfg, service, env); } finally { conn.end(); } } export async function checkRemoteDocker(service: Service, onPassword?: () => Promise): Promise { const config = await loadConfig(); const env = config.currentEnv; const cfg = getServiceConfig(config, service, env); 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; } }