mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 10:29:19 +08:00
feat: 新增部署脚本
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
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 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'),
|
||||
production: defaultServiceConfig('frontend.prod.com', 80, '/var/www/ecom-frontend'),
|
||||
},
|
||||
backend: {
|
||||
test: { ...defaultServiceConfig('192.168.1.100', 8080), springProfile: 'test', dbUrl: '', redisUrl: '' },
|
||||
pre: { ...defaultServiceConfig('192.168.1.101', 8080), springProfile: 'pre', dbUrl: '', redisUrl: '' },
|
||||
production: { ...defaultServiceConfig('backend.prod.com', 8080), 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: '' },
|
||||
production: { ...defaultServiceConfig('license.prod.com', 3100), nodeEnv: 'production', 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;
|
||||
}
|
||||
|
||||
/** 部署环境 pre 对应 Spring profile staging */
|
||||
export function resolveSpringProfile(profile?: string): string {
|
||||
if (profile === 'pre') return 'staging';
|
||||
return profile || 'prod';
|
||||
}
|
||||
|
||||
/** 同一 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');
|
||||
}
|
||||
Reference in New Issue
Block a user