Files
ecom-dmb/ecom-deploy/src/config.ts
T

195 lines
6.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
function defaultServiceConfig(host: string, port: number, extractDir?: string): ServiceEnvConfig {
const deployPath = extractDir || '/opt/ecom-dmb';
return {
server: { host, port: 22, username: 'root', deployPath, authType: 'password' },
port,
...(extractDir ? { extractDir } : {}),
};
}
function defaultConfig(): DeployConfig {
return {
currentEnv: 'test',
services: {
frontend: {
test: defaultServiceConfig('192.168.1.100', 80, '/var/www/ecom-frontend'),
pre: defaultServiceConfig('192.168.1.101', 80, '/var/www/ecom-frontend'),
prod: defaultServiceConfig('frontend.prod.com', 80, '/var/www/ecom-frontend'),
},
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: '' },
},
},
};
}
export async function loadConfig(): Promise<DeployConfig> {
try {
const raw = await readFile(CONFIG_PATH, 'utf8');
return JSON.parse(raw) as DeployConfig;
} 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;
}
/** 本地编译产物根目录 ecom-deploy/artifacts */
export function getArtifactsRoot(): string {
return ARTIFACTS_ROOT;
}
export function getArtifactDir(service: Service): string {
return join(ARTIFACTS_ROOT, service);
}
/** 远程服务目录(含 Dockerfile、data 挂载点) */
export function getRemoteServiceDir(cfg: ServiceEnvConfig, service: Service): string {
return `${cfg.server.deployPath}/${service}`;
}
/** 远程挂载目录,容器内映射为 /data */
export function getRemoteDataDir(cfg: ServiceEnvConfig, service: Service): string {
return `${getRemoteServiceDir(cfg, service)}/data`;
}
/** 与 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;
}
}
/** 部署环境 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, 'ecom-dmb-frontend');
}
export function getBackendDir(): string {
return join(PROJECT_ROOT, 'ecom-dmb-backend');
}
export function getLicenseDir(): string {
return join(PROJECT_ROOT, 'ecom-license');
}
function getProjectDeployDir(project: string): string {
return join(PROJECT_ROOT, project, 'deploy');
}
export function getFrontendDeployDir(): string {
return getProjectDeployDir('ecom-dmb-frontend');
}
export function getBackendDeployDir(): string {
return getProjectDeployDir('ecom-dmb-backend');
}
export function getLicenseDeployDir(): string {
return getProjectDeployDir('ecom-license');
}