BREAKING CHANGE(core): Migrate filesystem to smartfs (async) and add Elasticsearch service support; refactor format/commit/meta modules

This commit is contained in:
2025-11-27 21:32:34 +00:00
parent 2f3d67f9e3
commit e1d28bc10a
30 changed files with 2217 additions and 995 deletions

View File

@@ -7,12 +7,13 @@ import { logger } from '../gitzone.logging.js';
export class ServiceManager {
private config: ServiceConfiguration;
private docker: DockerContainer;
private enabledServices: string[] | null = null;
constructor() {
this.config = new ServiceConfiguration();
this.docker = new DockerContainer();
}
/**
* Initialize the service manager
*/
@@ -22,15 +23,134 @@ export class ServiceManager {
logger.log('error', 'Error: Docker is not installed. Please install Docker first.');
process.exit(1);
}
// Load or create configuration
await this.config.loadOrCreate();
logger.log('info', `📋 Project: ${this.config.getConfig().PROJECT_NAME}`);
// Load service selection from npmextra.json
await this.loadServiceConfiguration();
// Validate and update ports if needed
await this.config.validateAndUpdatePorts();
}
/**
* Load service configuration from npmextra.json
*/
private async loadServiceConfiguration(): Promise<void> {
const npmextraConfig = new plugins.npmextra.Npmextra(process.cwd());
const gitzoneConfig = npmextraConfig.dataFor<any>('gitzone', {});
// Check if services array exists
if (!gitzoneConfig.services || !Array.isArray(gitzoneConfig.services) || gitzoneConfig.services.length === 0) {
// Prompt user to select services
const smartinteract = new plugins.smartinteract.SmartInteract();
const response = await smartinteract.askQuestion({
name: 'services',
type: 'checkbox',
message: 'Which services do you want to enable for this project?',
choices: [
{ name: 'MongoDB', value: 'mongodb' },
{ name: 'MinIO (S3)', value: 'minio' },
{ name: 'Elasticsearch', value: 'elasticsearch' }
],
default: ['mongodb', 'minio', 'elasticsearch']
});
this.enabledServices = response.value || ['mongodb', 'minio', 'elasticsearch'];
// Save to npmextra.json
await this.saveServiceConfiguration(this.enabledServices);
} else {
this.enabledServices = gitzoneConfig.services;
logger.log('info', `🔧 Enabled services: ${this.enabledServices.join(', ')}`);
}
}
/**
* Save service configuration to npmextra.json
*/
private async saveServiceConfiguration(services: string[]): Promise<void> {
const npmextraPath = plugins.path.join(process.cwd(), 'npmextra.json');
let npmextraData: any = {};
// Read existing npmextra.json if it exists
if (await plugins.smartfs.file(npmextraPath).exists()) {
const content = await plugins.smartfs.file(npmextraPath).encoding('utf8').read();
npmextraData = JSON.parse(content as string);
}
// Update gitzone.services
if (!npmextraData.gitzone) {
npmextraData.gitzone = {};
}
npmextraData.gitzone.services = services;
// Write back to npmextra.json
await plugins.smartfs
.file(npmextraPath)
.encoding('utf8')
.write(JSON.stringify(npmextraData, null, 2));
logger.log('ok', `✅ Saved service configuration to npmextra.json`);
logger.log('info', `🔧 Enabled services: ${services.join(', ')}`);
}
/**
* Check if a service is enabled
*/
private isServiceEnabled(service: string): boolean {
if (!this.enabledServices) {
return true; // If no configuration, enable all
}
return this.enabledServices.includes(service);
}
/**
* Start all enabled services
*/
public async startAll(): Promise<void> {
let first = true;
if (this.isServiceEnabled('mongodb')) {
if (!first) console.log();
await this.startMongoDB();
first = false;
}
if (this.isServiceEnabled('minio')) {
if (!first) console.log();
await this.startMinIO();
first = false;
}
if (this.isServiceEnabled('elasticsearch')) {
if (!first) console.log();
await this.startElasticsearch();
first = false;
}
}
/**
* Stop all enabled services
*/
public async stopAll(): Promise<void> {
let first = true;
if (this.isServiceEnabled('mongodb')) {
if (!first) console.log();
await this.stopMongoDB();
first = false;
}
if (this.isServiceEnabled('minio')) {
if (!first) console.log();
await this.stopMinIO();
first = false;
}
if (this.isServiceEnabled('elasticsearch')) {
if (!first) console.log();
await this.stopElasticsearch();
first = false;
}
}
/**
* Start MongoDB service
*/
@@ -42,7 +162,7 @@ export class ServiceManager {
const directories = this.config.getDataDirectories();
// Ensure data directory exists
await plugins.smartfile.fs.ensureDir(directories.mongo);
await plugins.smartfs.directory(directories.mongo).recursive().create();
const status = await this.docker.getStatus(containers.mongo);
@@ -141,7 +261,7 @@ export class ServiceManager {
const directories = this.config.getDataDirectories();
// Ensure data directory exists
await plugins.smartfile.fs.ensureDir(directories.minio);
await plugins.smartfs.directory(directories.minio).recursive().create();
const status = await this.docker.getStatus(containers.minio);
@@ -259,7 +379,103 @@ export class ServiceManager {
logger.log('info', ` API: http://${config.S3_HOST}:${config.S3_PORT}`);
logger.log('info', ` Console: http://${config.S3_HOST}:${config.S3_CONSOLE_PORT} (login: ${config.S3_ACCESSKEY}/***)`);
}
/**
* Start Elasticsearch service
*/
public async startElasticsearch(): Promise<void> {
logger.log('note', '📦 Elasticsearch:');
const config = this.config.getConfig();
const containers = this.config.getContainerNames();
const directories = this.config.getDataDirectories();
// Ensure data directory exists
await plugins.smartfs.directory(directories.elasticsearch).recursive().create();
const status = await this.docker.getStatus(containers.elasticsearch);
switch (status) {
case 'running':
logger.log('ok', ' Already running ✓');
break;
case 'stopped':
// Check if port mapping matches config
const esPortMappings = await this.docker.getPortMappings(containers.elasticsearch);
if (esPortMappings && esPortMappings['9200'] !== config.ELASTICSEARCH_PORT) {
logger.log('note', ' Port configuration changed, recreating container...');
await this.docker.remove(containers.elasticsearch, true);
// Fall through to create new container
const success = await this.docker.run({
name: containers.elasticsearch,
image: 'elasticsearch:8.11.0',
ports: {
[`0.0.0.0:${config.ELASTICSEARCH_PORT}`]: '9200'
},
volumes: {
[directories.elasticsearch]: '/usr/share/elasticsearch/data'
},
environment: {
'discovery.type': 'single-node',
'xpack.security.enabled': 'true',
'ELASTIC_PASSWORD': config.ELASTICSEARCH_PASS,
'ES_JAVA_OPTS': '-Xms512m -Xmx512m'
},
restart: 'unless-stopped'
});
if (success) {
logger.log('ok', ' Recreated with new port ✓');
} else {
logger.log('error', ' Failed to recreate container');
}
} else {
// Ports match, just start the container
if (await this.docker.start(containers.elasticsearch)) {
logger.log('ok', ' Started ✓');
} else {
logger.log('error', ' Failed to start');
}
}
break;
case 'not_exists':
logger.log('note', ' Creating container...');
const success = await this.docker.run({
name: containers.elasticsearch,
image: 'elasticsearch:8.11.0',
ports: {
[`0.0.0.0:${config.ELASTICSEARCH_PORT}`]: '9200'
},
volumes: {
[directories.elasticsearch]: '/usr/share/elasticsearch/data'
},
environment: {
'discovery.type': 'single-node',
'xpack.security.enabled': 'true',
'ELASTIC_PASSWORD': config.ELASTICSEARCH_PASS,
'ES_JAVA_OPTS': '-Xms512m -Xmx512m'
},
restart: 'unless-stopped'
});
if (success) {
logger.log('ok', ' Created and started ✓');
} else {
logger.log('error', ' Failed to create container');
}
break;
}
logger.log('info', ` Container: ${containers.elasticsearch}`);
logger.log('info', ` Port: ${config.ELASTICSEARCH_PORT}`);
logger.log('info', ` Connection: ${config.ELASTICSEARCH_URL}`);
logger.log('info', ` Username: ${config.ELASTICSEARCH_USER}`);
logger.log('info', ` Password: ${config.ELASTICSEARCH_PASS}`);
}
/**
* Stop MongoDB service
*/
@@ -285,10 +501,10 @@ export class ServiceManager {
*/
public async stopMinIO(): Promise<void> {
logger.log('note', '📦 S3/MinIO:');
const containers = this.config.getContainerNames();
const status = await this.docker.getStatus(containers.minio);
if (status === 'running') {
if (await this.docker.stop(containers.minio)) {
logger.log('ok', ' Stopped ✓');
@@ -299,7 +515,27 @@ export class ServiceManager {
logger.log('note', ' Not running');
}
}
/**
* Stop Elasticsearch service
*/
public async stopElasticsearch(): Promise<void> {
logger.log('note', '📦 Elasticsearch:');
const containers = this.config.getContainerNames();
const status = await this.docker.getStatus(containers.elasticsearch);
if (status === 'running') {
if (await this.docker.stop(containers.elasticsearch)) {
logger.log('ok', ' Stopped ✓');
} else {
logger.log('error', ' Failed to stop');
}
} else {
logger.log('note', ' Not running');
}
}
/**
* Show service status
*/
@@ -385,8 +621,36 @@ export class ServiceManager {
}
break;
}
// Elasticsearch status
const esStatus = await this.docker.getStatus(containers.elasticsearch);
switch (esStatus) {
case 'running':
logger.log('ok', '📦 Elasticsearch: 🟢 Running');
logger.log('info', ` ├─ Container: ${containers.elasticsearch}`);
logger.log('info', ` ├─ Port: ${config.ELASTICSEARCH_PORT}`);
logger.log('info', ` ├─ Connection: ${config.ELASTICSEARCH_URL}`);
logger.log('info', ` └─ Credentials: ${config.ELASTICSEARCH_USER}/${config.ELASTICSEARCH_PASS}`);
break;
case 'stopped':
logger.log('note', '📦 Elasticsearch: 🟡 Stopped');
logger.log('info', ` ├─ Container: ${containers.elasticsearch}`);
logger.log('info', ` └─ Port: ${config.ELASTICSEARCH_PORT}`);
break;
case 'not_exists':
logger.log('info', '📦 Elasticsearch: ⚪ Not installed');
// Check port availability
const esPort = parseInt(config.ELASTICSEARCH_PORT);
const esAvailable = await helpers.isPortAvailable(esPort);
if (!esAvailable) {
logger.log('error', ` └─ ⚠️ Port ${esPort} is in use by another process`);
} else {
logger.log('info', ` └─ Port ${esPort} is available`);
}
break;
}
}
/**
* Show configuration
*/
@@ -420,6 +684,15 @@ export class ServiceManager {
logger.log('info', ` Data: ${this.config.getDataDirectories().minio}`);
logger.log('info', ` Endpoint: ${config.S3_ENDPOINT}`);
logger.log('info', ` Console URL: http://${config.S3_HOST}:${config.S3_CONSOLE_PORT}`);
console.log();
logger.log('note', 'Elasticsearch:');
logger.log('info', ` Host: ${config.ELASTICSEARCH_HOST}:${config.ELASTICSEARCH_PORT}`);
logger.log('info', ` User: ${config.ELASTICSEARCH_USER}`);
logger.log('info', ' Password: ***');
logger.log('info', ` Container: ${this.config.getContainerNames().elasticsearch}`);
logger.log('info', ` Data: ${this.config.getDataDirectories().elasticsearch}`);
logger.log('info', ` Connection: ${config.ELASTICSEARCH_URL}`);
}
/**
@@ -477,16 +750,29 @@ export class ServiceManager {
logger.log('note', 'S3/MinIO container is not running');
}
break;
case 'elasticsearch':
case 'es':
if (await this.docker.isRunning(containers.elasticsearch)) {
helpers.printHeader(`Elasticsearch Logs (last ${lines} lines)`);
const logs = await this.docker.logs(containers.elasticsearch, lines);
console.log(logs);
} else {
logger.log('note', 'Elasticsearch container is not running');
}
break;
case 'all':
case '':
await this.showLogs('mongo', lines);
console.log();
await this.showLogs('minio', lines);
console.log();
await this.showLogs('elasticsearch', lines);
break;
default:
logger.log('note', 'Usage: gitzone services logs [mongo|s3|all] [lines]');
logger.log('note', 'Usage: gitzone services logs [mongo|s3|elasticsearch|all] [lines]');
break;
}
}
@@ -497,21 +783,28 @@ export class ServiceManager {
public async removeContainers(): Promise<void> {
const containers = this.config.getContainerNames();
let removed = false;
if (await this.docker.exists(containers.mongo)) {
if (await this.docker.remove(containers.mongo, true)) {
logger.log('ok', ' MongoDB container removed ✓');
removed = true;
}
}
if (await this.docker.exists(containers.minio)) {
if (await this.docker.remove(containers.minio, true)) {
logger.log('ok', ' S3/MinIO container removed ✓');
removed = true;
}
}
if (await this.docker.exists(containers.elasticsearch)) {
if (await this.docker.remove(containers.elasticsearch, true)) {
logger.log('ok', ' Elasticsearch container removed ✓');
removed = true;
}
}
if (!removed) {
logger.log('note', ' No containers to remove');
}
@@ -523,24 +816,60 @@ export class ServiceManager {
public async cleanData(): Promise<void> {
const directories = this.config.getDataDirectories();
let cleaned = false;
if (await plugins.smartfile.fs.fileExists(directories.mongo)) {
await plugins.smartfile.fs.remove(directories.mongo);
if (await plugins.smartfs.directory(directories.mongo).exists()) {
await plugins.smartfs.directory(directories.mongo).recursive().delete();
logger.log('ok', ' MongoDB data removed ✓');
cleaned = true;
}
if (await plugins.smartfile.fs.fileExists(directories.minio)) {
await plugins.smartfile.fs.remove(directories.minio);
if (await plugins.smartfs.directory(directories.minio).exists()) {
await plugins.smartfs.directory(directories.minio).recursive().delete();
logger.log('ok', ' S3/MinIO data removed ✓');
cleaned = true;
}
if (await plugins.smartfs.directory(directories.elasticsearch).exists()) {
await plugins.smartfs.directory(directories.elasticsearch).recursive().delete();
logger.log('ok', ' Elasticsearch data removed ✓');
cleaned = true;
}
if (!cleaned) {
logger.log('note', ' No data to clean');
}
}
/**
* Configure which services are enabled
*/
public async configureServices(): Promise<void> {
logger.log('note', 'Select which services to enable for this project:');
console.log();
const currentServices = this.enabledServices || ['mongodb', 'minio', 'elasticsearch'];
const smartinteract = new plugins.smartinteract.SmartInteract();
const response = await smartinteract.askQuestion({
name: 'services',
type: 'checkbox',
message: 'Which services do you want to enable?',
choices: [
{ name: 'MongoDB', value: 'mongodb' },
{ name: 'MinIO (S3)', value: 'minio' },
{ name: 'Elasticsearch', value: 'elasticsearch' }
],
default: currentServices
});
this.enabledServices = response.value || ['mongodb', 'minio', 'elasticsearch'];
// Save to npmextra.json
await this.saveServiceConfiguration(this.enabledServices);
logger.log('ok', '✅ Service configuration updated');
}
/**
* Reconfigure services with new ports
*/
@@ -551,20 +880,25 @@ export class ServiceManager {
// Stop existing containers
logger.log('note', '🛑 Stopping existing containers...');
if (await this.docker.exists(containers.mongo)) {
await this.docker.stop(containers.mongo);
logger.log('ok', ' MongoDB stopped ✓');
}
if (await this.docker.exists(containers.minio)) {
await this.docker.stop(containers.minio);
logger.log('ok', ' S3/MinIO stopped ✓');
}
if (await this.docker.exists(containers.elasticsearch)) {
await this.docker.stop(containers.elasticsearch);
logger.log('ok', ' Elasticsearch stopped ✓');
}
// Reconfigure ports
await this.config.reconfigurePorts();
// Ask if user wants to restart services
const smartinteract = new plugins.smartinteract.SmartInteract();
const response = await smartinteract.askQuestion({
@@ -573,11 +907,10 @@ export class ServiceManager {
message: 'Do you want to start services with new ports?',
default: true
});
if (response.value) {
console.log();
await this.startMongoDB();
await this.startMinIO();
await this.startAll();
}
}
}