feat(services): Add Docker port mapping sync and reconfigure workflow for local services

This commit is contained in:
2025-08-16 08:47:39 +00:00
parent ff57f8a322
commit dbc1a1ba18
6 changed files with 368 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
import * as plugins from './mod.plugins.js';
import * as helpers from './helpers.js';
import { logger } from '../gitzone.logging.js';
import { DockerContainer } from './classes.dockercontainer.js';
export interface IServiceConfig {
PROJECT_NAME: string;
@@ -23,9 +24,11 @@ export interface IServiceConfig {
export class ServiceConfiguration {
private configPath: string;
private config: IServiceConfig;
private docker: DockerContainer;
constructor() {
this.configPath = plugins.path.join(process.cwd(), '.nogit', 'env.json');
this.docker = new DockerContainer();
}
/**
@@ -41,6 +44,9 @@ export class ServiceConfiguration {
await this.createDefaultConfig();
}
// Sync ports from existing Docker containers if they exist
await this.syncPortsFromDocker();
return this.config;
}
@@ -280,4 +286,147 @@ export class ServiceConfiguration {
minio: plugins.path.join(process.cwd(), '.nogit', 'miniodata')
};
}
/**
* Sync port configuration from existing Docker containers
*/
private async syncPortsFromDocker(): Promise<void> {
const containers = this.getContainerNames();
let updated = false;
// Check MongoDB container
const mongoStatus = await this.docker.getStatus(containers.mongo);
if (mongoStatus !== 'not_exists') {
const portMappings = await this.docker.getPortMappings(containers.mongo);
if (portMappings && portMappings['27017']) {
const dockerPort = portMappings['27017'];
if (this.config.MONGODB_PORT !== dockerPort) {
this.config.MONGODB_PORT = dockerPort;
updated = true;
}
}
}
// Check MinIO container
const minioStatus = await this.docker.getStatus(containers.minio);
if (minioStatus !== 'not_exists') {
const portMappings = await this.docker.getPortMappings(containers.minio);
if (portMappings) {
if (portMappings['9000']) {
const dockerPort = portMappings['9000'];
if (this.config.S3_PORT !== dockerPort) {
this.config.S3_PORT = dockerPort;
updated = true;
}
}
if (portMappings['9001']) {
const dockerPort = portMappings['9001'];
if (this.config.S3_CONSOLE_PORT !== dockerPort) {
this.config.S3_CONSOLE_PORT = dockerPort;
updated = true;
}
}
}
}
if (updated) {
// Update derived fields
this.config.MONGODB_URL = `mongodb://${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@${this.config.MONGODB_HOST}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}?authSource=admin`;
const protocol = this.config.S3_USESSL ? 'https' : 'http';
this.config.S3_ENDPOINT = `${protocol}://${this.config.S3_HOST}:${this.config.S3_PORT}`;
await this.saveConfig();
}
}
/**
* Validate and update ports if they're not available
*/
public async validateAndUpdatePorts(): Promise<boolean> {
let updated = false;
const containers = this.getContainerNames();
// Check if containers exist - if they do, ports are fine
const mongoExists = await this.docker.exists(containers.mongo);
const minioExists = await this.docker.exists(containers.minio);
// Only check port availability if containers don't exist
if (!mongoExists) {
const mongoPort = parseInt(this.config.MONGODB_PORT);
if (!(await helpers.isPortAvailable(mongoPort))) {
logger.log('note', `⚠️ MongoDB port ${mongoPort} is in use, finding new port...`);
const newPort = await helpers.getRandomAvailablePort();
this.config.MONGODB_PORT = newPort.toString();
logger.log('ok', `✅ New MongoDB port: ${newPort}`);
updated = true;
}
}
if (!minioExists) {
const s3Port = parseInt(this.config.S3_PORT);
const s3ConsolePort = parseInt(this.config.S3_CONSOLE_PORT);
if (!(await helpers.isPortAvailable(s3Port))) {
logger.log('note', `⚠️ S3 API port ${s3Port} is in use, finding new port...`);
const newPort = await helpers.getRandomAvailablePort();
this.config.S3_PORT = newPort.toString();
logger.log('ok', `✅ New S3 API port: ${newPort}`);
updated = true;
}
if (!(await helpers.isPortAvailable(s3ConsolePort))) {
logger.log('note', `⚠️ S3 Console port ${s3ConsolePort} is in use, finding new port...`);
let newPort = parseInt(this.config.S3_PORT) + 1;
while (!(await helpers.isPortAvailable(newPort))) {
newPort++;
}
this.config.S3_CONSOLE_PORT = newPort.toString();
logger.log('ok', `✅ New S3 Console port: ${newPort}`);
updated = true;
}
}
if (updated) {
// Update derived fields
this.config.MONGODB_URL = `mongodb://${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@${this.config.MONGODB_HOST}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}?authSource=admin`;
const protocol = this.config.S3_USESSL ? 'https' : 'http';
this.config.S3_ENDPOINT = `${protocol}://${this.config.S3_HOST}:${this.config.S3_PORT}`;
await this.saveConfig();
}
return updated;
}
/**
* Force reconfigure all ports with new available ones
*/
public async reconfigurePorts(): Promise<void> {
logger.log('note', '🔄 Finding new available ports...');
const mongoPort = await helpers.getRandomAvailablePort();
const s3Port = await helpers.getRandomAvailablePort();
let s3ConsolePort = s3Port + 1;
// Ensure console port is also available
while (!(await helpers.isPortAvailable(s3ConsolePort))) {
s3ConsolePort++;
}
this.config.MONGODB_PORT = mongoPort.toString();
this.config.S3_PORT = s3Port.toString();
this.config.S3_CONSOLE_PORT = s3ConsolePort.toString();
// Update derived fields
this.config.MONGODB_URL = `mongodb://${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@${this.config.MONGODB_HOST}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}?authSource=admin`;
const protocol = this.config.S3_USESSL ? 'https' : 'http';
this.config.S3_ENDPOINT = `${protocol}://${this.config.S3_HOST}:${this.config.S3_PORT}`;
await this.saveConfig();
logger.log('ok', '✅ New port configuration:');
logger.log('info', ` 📍 MongoDB: ${mongoPort}`);
logger.log('info', ` 📍 S3 API: ${s3Port}`);
logger.log('info', ` 📍 S3 Console: ${s3ConsolePort}`);
}
}