mirror of
https://gitee.com/qqtt-ah/ai-crm.git
synced 2026-11-23 23:49:19 +08:00
279 lines
9.0 KiB
TypeScript
279 lines
9.0 KiB
TypeScript
import { readFile, writeFile } from 'fs/promises';
|
||
import { homedir } from 'os';
|
||
import { dirname, join } from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
|
||
import type { DeployConfig, Environment, Service, ServiceEnvConfig, ServerConfig } from './types.js';
|
||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||
const CONFIG_PATH = process.env.ECOM_DEPLOY_CONFIG || join(__dirname, '..', 'config.json');
|
||
const ARTIFACTS_ROOT = join(__dirname, '..', 'artifacts');
|
||
const SSH_KEY_PATH = join(homedir(), '.ssh', 'ecom-deploy');
|
||
|
||
export function getSshKeyPath(): string {
|
||
return SSH_KEY_PATH;
|
||
}
|
||
|
||
/** 远程统一根目录名:{deployPath}/ecom-dmb/{service}/{env} */
|
||
export const REMOTE_APP_ROOT = 'ecom-dmb';
|
||
|
||
function defaultServiceConfig(host: string, port: number, options?: { staticDeploy?: boolean }): ServiceEnvConfig {
|
||
return {
|
||
server: { host, port: 22, username: 'root', deployPath: '/opt', authType: 'password' },
|
||
port,
|
||
...(options?.staticDeploy ? { staticDeploy: true } : {}),
|
||
};
|
||
}
|
||
|
||
function defaultConfig(): DeployConfig {
|
||
return {
|
||
currentEnv: 'test',
|
||
services: {
|
||
frontend: {
|
||
test: defaultServiceConfig('192.168.1.100', 80, { staticDeploy: true }),
|
||
pre: defaultServiceConfig('192.168.1.101', 80, { staticDeploy: true }),
|
||
prod: defaultServiceConfig('frontend.prod.com', 80, { staticDeploy: true }),
|
||
},
|
||
backend: {
|
||
test: { ...defaultServiceConfig('192.168.1.100', 8101), springProfile: 'test', dbUrl: '', redisUrl: '' },
|
||
pre: { ...defaultServiceConfig('192.168.1.101', 8102), springProfile: 'pre', dbUrl: '', redisUrl: '' },
|
||
prod: { ...defaultServiceConfig('backend.prod.com', 8103), springProfile: 'prod', dbUrl: '', redisUrl: '' },
|
||
},
|
||
license: {
|
||
test: { ...defaultServiceConfig('192.168.1.100', 3100), nodeEnv: 'test', licenseDbUrl: '' },
|
||
pre: { ...defaultServiceConfig('192.168.1.101', 3100), nodeEnv: 'pre', licenseDbUrl: '' },
|
||
prod: { ...defaultServiceConfig('license.prod.com', 3100), nodeEnv: 'prod', licenseDbUrl: '' },
|
||
},
|
||
agent: {
|
||
test: defaultServiceConfig('192.168.3.101', 8201),
|
||
pre: defaultServiceConfig('192.168.3.101', 8202),
|
||
prod: defaultServiceConfig('agent.prod.com', 8203),
|
||
},
|
||
},
|
||
};
|
||
}
|
||
|
||
/** 旧 config.json 缺服务时补默认项并写回 */
|
||
function mergeMissingServices(cfg: DeployConfig): { config: DeployConfig; dirty: boolean } {
|
||
const defaults = defaultConfig();
|
||
let dirty = false;
|
||
for (const service of Object.keys(defaults.services) as Service[]) {
|
||
if (!cfg.services[service]) {
|
||
cfg.services[service] = defaults.services[service];
|
||
dirty = true;
|
||
}
|
||
}
|
||
return { config: cfg, dirty };
|
||
}
|
||
|
||
export async function loadConfig(): Promise<DeployConfig> {
|
||
try {
|
||
const raw = await readFile(CONFIG_PATH, 'utf8');
|
||
const parsed = JSON.parse(raw) as DeployConfig;
|
||
const { config, dirty } = mergeMissingServices(parsed);
|
||
if (dirty) await saveConfig(config);
|
||
return config;
|
||
} catch {
|
||
const cfg = defaultConfig();
|
||
await saveConfig(cfg);
|
||
return cfg;
|
||
}
|
||
}
|
||
|
||
export async function saveConfig(config: DeployConfig): Promise<void> {
|
||
await writeFile(CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
|
||
}
|
||
|
||
export function getServiceConfig(config: DeployConfig, service: Service, env?: Environment): ServiceEnvConfig {
|
||
return config.services[service][env || config.currentEnv];
|
||
}
|
||
|
||
export async function updateServiceServerConfig(
|
||
service: Service,
|
||
env: Environment,
|
||
updates: Partial<ServerConfig>,
|
||
): Promise<DeployConfig> {
|
||
const config = await loadConfig();
|
||
config.services[service][env].server = { ...config.services[service][env].server, ...updates };
|
||
await saveConfig(config);
|
||
return config;
|
||
}
|
||
|
||
export async function updateServiceEnvConfig(
|
||
service: Service,
|
||
env: Environment,
|
||
updates: Partial<ServiceEnvConfig>,
|
||
): Promise<DeployConfig> {
|
||
const config = await loadConfig();
|
||
config.services[service][env] = { ...config.services[service][env], ...updates };
|
||
await saveConfig(config);
|
||
return config;
|
||
}
|
||
|
||
export function getConfigPath(): string {
|
||
return CONFIG_PATH;
|
||
}
|
||
|
||
/** 本地编译产物根目录 deploy/artifacts */
|
||
export function getArtifactsRoot(): string {
|
||
return ARTIFACTS_ROOT;
|
||
}
|
||
|
||
export function getArtifactDir(service: Service): string {
|
||
return join(ARTIFACTS_ROOT, service);
|
||
}
|
||
|
||
/** 规范化部署根目录(去掉末尾斜杠) */
|
||
export function normalizeDeployRoot(deployPath: string): string {
|
||
return deployPath.replace(/\/+$/, '') || '/';
|
||
}
|
||
|
||
/**
|
||
* 远程环境部署目录(后端 / 授权):
|
||
* {deployPath}/ecom-dmb/{service}/{env}
|
||
* 例:/opt/ecom-dmb/backend/test
|
||
*/
|
||
export function resolveEnvDeployDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||
return `${normalizeDeployRoot(cfg.server.deployPath)}/${REMOTE_APP_ROOT}/${service}/${env}`;
|
||
}
|
||
|
||
/**
|
||
* 前端静态部署目录(按环境后缀区分,不走 service 子路径):
|
||
* {deployPath}/ecom-dmb-{env}/index
|
||
* 例:/opt/ecom-dmb-test/index
|
||
*/
|
||
export function resolveFrontendDeployDir(cfg: ServiceEnvConfig, env: Environment): string {
|
||
return `${normalizeDeployRoot(cfg.server.deployPath)}/${REMOTE_APP_ROOT}-${env}`;
|
||
}
|
||
|
||
export function resolveFrontendIndexDir(cfg: ServiceEnvConfig, env: Environment): string {
|
||
return `${resolveFrontendDeployDir(cfg, env)}/index`;
|
||
}
|
||
|
||
/** 展示用:解析服务实际部署路径 */
|
||
export function resolveServiceDeployPath(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||
if (service === 'frontend') {
|
||
return resolveFrontendIndexDir(cfg, env);
|
||
}
|
||
return resolveEnvDeployDir(cfg, service, env);
|
||
}
|
||
|
||
/** 远程服务目录(含 Dockerfile、compose、data),等价于环境部署目录 */
|
||
export function getRemoteServiceDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||
return resolveEnvDeployDir(cfg, service, env);
|
||
}
|
||
|
||
/** 远程业务产物目录(宿主 app/,挂载到容器 /app) */
|
||
export function getRemoteAppDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||
return `${resolveEnvDeployDir(cfg, service, env)}/app`;
|
||
}
|
||
|
||
/** @deprecated 使用 getRemoteAppDir */
|
||
export function getRemoteDataDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||
return getRemoteAppDir(cfg, service, env);
|
||
}
|
||
|
||
/** 是否仅静态部署(不走 Docker),兼容旧字段 extractDir */
|
||
export function isStaticDeploy(cfg: ServiceEnvConfig): boolean {
|
||
return cfg.staticDeploy === true || Boolean(cfg.extractDir);
|
||
}
|
||
|
||
/** 与 application-{profile}.yml 中 server.port 保持一致 */
|
||
export function resolveBackendPort(env: Environment): number {
|
||
switch (env) {
|
||
case 'test':
|
||
return 8101;
|
||
case 'pre':
|
||
return 8102;
|
||
case 'prod':
|
||
return 8103;
|
||
}
|
||
}
|
||
|
||
/** Agent 各环境监听端口(开发本机仍用 8200) */
|
||
export function resolveAgentPort(env: Environment): number {
|
||
switch (env) {
|
||
case 'test':
|
||
return 8201;
|
||
case 'pre':
|
||
return 8202;
|
||
case 'prod':
|
||
return 8203;
|
||
}
|
||
}
|
||
|
||
/** 部署环境 pre 对应 Spring profile pre(application-pre.yml) */
|
||
export function resolveSpringProfile(profile?: string): string {
|
||
return profile || 'prod';
|
||
}
|
||
|
||
/** 部署环境后缀:test / pre / prod */
|
||
export function resolveEnvSuffix(env: Environment): string {
|
||
return env;
|
||
}
|
||
|
||
/** Docker 镜像、容器、Compose 服务/项目名(含环境后缀) */
|
||
export function getDockerResourceNames(service: Service, env: Environment) {
|
||
const suffix = resolveEnvSuffix(env);
|
||
const base = `ecom-${service}-${suffix}`;
|
||
return {
|
||
composeService: base,
|
||
containerName: base,
|
||
imageTag: `${base}:latest`,
|
||
composeProject: base,
|
||
};
|
||
}
|
||
|
||
/** 同一 host+username 的服务共享 SSH 密钥配置 */
|
||
export async function syncServerKeyAuth(
|
||
host: string,
|
||
username: string,
|
||
updates: Partial<ServerConfig>,
|
||
): Promise<DeployConfig> {
|
||
const config = await loadConfig();
|
||
for (const service of Object.keys(config.services) as Service[]) {
|
||
for (const env of Object.keys(config.services[service]) as Environment[]) {
|
||
const srv = config.services[service][env].server;
|
||
if (srv.host === host && srv.username === username) {
|
||
config.services[service][env].server = { ...srv, ...updates };
|
||
}
|
||
}
|
||
}
|
||
await saveConfig(config);
|
||
return config;
|
||
}
|
||
|
||
const PROJECT_ROOT = join(__dirname, '..', '..');
|
||
|
||
export function getFrontendDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'web');
|
||
}
|
||
|
||
export function getBackendDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'server');
|
||
}
|
||
|
||
export function getLicenseDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'license-server');
|
||
}
|
||
|
||
export function getAgentDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'agent');
|
||
}
|
||
|
||
export function getFrontendDeployDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'web', 'deploy');
|
||
}
|
||
|
||
export function getBackendDeployDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'server', 'deploy');
|
||
}
|
||
|
||
export function getLicenseDeployDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'license-server', 'deploy');
|
||
}
|
||
|
||
export function getAgentDeployDir(): string {
|
||
return join(PROJECT_ROOT, 'apps', 'agent', 'deploy');
|
||
}
|