From 32d1da96df29a3f9b52227c82a7367f3331288b7 Mon Sep 17 00:00:00 2001 From: marco370 <48531002-marco370@users.noreply.replit.com> Date: Mon, 17 Nov 2025 17:15:31 +0000 Subject: [PATCH] Provide initial setup instructions for deploying the application Introduce a detailed bootstrap guide for initial deployment on AlmaLinux servers, including cloning the repository, preserving local configurations like .env, and setting up dependencies. This replaces the previous generic Git update section with specific instructions for the first-time setup. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 0554a4de-1920-4d70-b393-e75b1b92a94e Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/CGAMfXu --- deployment/BOOTSTRAP_PRIMO_DEPLOYMENT.md | 222 +++++++++++++++++++++++ replit.md | 34 +++- 2 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 deployment/BOOTSTRAP_PRIMO_DEPLOYMENT.md diff --git a/deployment/BOOTSTRAP_PRIMO_DEPLOYMENT.md b/deployment/BOOTSTRAP_PRIMO_DEPLOYMENT.md new file mode 100644 index 0000000..60bfe2c --- /dev/null +++ b/deployment/BOOTSTRAP_PRIMO_DEPLOYMENT.md @@ -0,0 +1,222 @@ +# ๐Ÿš€ BOOTSTRAP - Primo Deployment su Server AlmaLinux + +## Problema + +Sul server AlmaLinux in `/opt/ids` esiste giร : +- โœ… `.env` con credenziali PostgreSQL funzionanti +- โœ… Database PostgreSQL configurato +- โœ… Servizi attivi (syslog_parser, ecc.) + +Ma NON esiste ancora: +- โŒ `update_from_git.sh` (script di aggiornamento) +- โŒ `git.env` (credenziali GitLab) + +**RISCHIO**: Se fai semplicemente `git pull`, potresti sovrascrivere `.env` e perdere le configurazioni. + +--- + +## โœ… Soluzione: Clone + Swap Atomico + +### Step 1: Clone in Directory Separata + +```bash +# Login come root +sudo su - + +# Vai in /opt +cd /opt + +# Clone repository in directory temporanea +sudo -u ids git clone https://marco:553da84c94093919d46055d6ec37dfa2a03d0f46@git.alfacom.it/marco/ids.git ids_git + +# Verifica clone +ls -la ids_git/ +``` + +### Step 2: Preserva File Locali (NON su Git) + +```bash +# Copia .env esistente nel clone +sudo -u ids cp /opt/ids/.env /opt/ids_git/.env + +# Se esiste git.env, copialo (altrimenti crealo dopo) +if [ -f /opt/ids/git.env ]; then + sudo -u ids cp /opt/ids/git.env /opt/ids_git/git.env +fi + +# Copia modelli ML addestrati (se esistono) +if [ -d /opt/ids/python_ml/models ]; then + sudo -u ids cp -r /opt/ids/python_ml/models/*.pkl /opt/ids_git/python_ml/models/ 2>/dev/null || true + sudo -u ids cp -r /opt/ids/python_ml/models/*.joblib /opt/ids_git/python_ml/models/ 2>/dev/null || true +fi + +# Copia backup database locali (se esistono) +if [ -d /opt/ids/database-backups ]; then + sudo -u ids cp -r /opt/ids/database-backups /opt/ids_git/ +fi +``` + +### Step 3: Verifica Configurazione + +```bash +# Verifica .env +cat /opt/ids_git/.env | grep PGHOST +# Output atteso: PGHOST=127.0.0.1 + +# Verifica permessi +ls -la /opt/ids_git/.env +# Owner deve essere: ids:ids +``` + +### Step 4: Ferma Servizi + +```bash +# Ferma syslog_parser +pkill -f syslog_parser + +# Verifica che sia fermo +ps aux | grep syslog_parser +``` + +### Step 5: Swap Atomico Directory + +```bash +# Rinomina directory vecchia +mv /opt/ids /opt/ids_legacy + +# Sposta nuova directory +mv /opt/ids_git /opt/ids + +# Verifica +ls -la /opt/ids/update_from_git.sh +# Deve esistere! +``` + +### Step 6: Installa Dipendenze + +```bash +# Node.js dependencies +cd /opt/ids +sudo -u ids npm install + +# Python dependencies +cd /opt/ids/python_ml +sudo -u ids pip3.11 install -r requirements.txt +``` + +### Step 7: Crea git.env (se non esiste) + +```bash +cd /opt/ids + +# Crea git.env +sudo -u ids nano git.env +``` + +**Contenuto git.env**: +```bash +GITLAB_USER=marco +GITLAB_TOKEN=553da84c94093919d46055d6ec37dfa2a03d0f46 +GITLAB_REPO=https://git.alfacom.it/marco/ids.git +GITLAB_BRANCH=main +``` + +### Step 8: Riavvia Servizi + +```bash +# Riavvia syslog_parser +cd /opt/ids/python_ml +sudo -u ids nohup python3.11 syslog_parser.py > /var/log/ids/syslog_parser.log 2>&1 & + +# Verifica log +tail -f /var/log/ids/syslog_parser.log +# Output atteso: "Connesso a PostgreSQL via 127.0.0.1" + +# Riavvia applicazione web (se configurata) +systemctl restart ids-web 2>/dev/null || echo "Servizio web non configurato" +``` + +### Step 9: Test Funzionamento + +```bash +# Verifica update_from_git.sh +cd /opt/ids +./update_from_git.sh --help + +# Test database +psql -h 127.0.0.1 -U ids_user -d ids_db -c "SELECT COUNT(*) FROM network_logs;" +# Deve mostrare i log esistenti + +# Verifica git remote +git remote -v +# Deve mostrare: origin https://git.alfacom.it/marco/ids.git +``` + +--- + +## ๐Ÿ”„ Aggiornamenti Futuri (Dopo Bootstrap) + +**Usa SEMPRE** `update_from_git.sh`: + +```bash +# Aggiornamento codice +cd /opt/ids +./update_from_git.sh + +# Aggiornamento codice + sync schema database +cd /opt/ids +./update_from_git.sh --db +``` + +**IMPORTANTE**: `update_from_git.sh` fa backup automatico di `.env` e `git.env` prima di fare `git pull`, quindi รจ sicuro! + +--- + +## ๐Ÿ”’ Sicurezza + +### File Protetti (NON su Git) +- `.env` - Credenziali PostgreSQL +- `git.env` - Token GitLab +- `database-backups/*.sql.gz` - Backup completi +- `python_ml/models/*.pkl` - Modelli addestrati + +### File su Git (Template) +- `.env.example` - Template configurazione +- `git.env.example` - Template credenziali Git +- `database-schema/schema.sql` - SOLO schema (no dati) + +--- + +## ๐Ÿ†˜ Rollback (In caso di problemi) + +```bash +# Ripristina directory vecchia +sudo mv /opt/ids /opt/ids_failed +sudo mv /opt/ids_legacy /opt/ids + +# Riavvia servizi +cd /opt/ids/python_ml +sudo -u ids pkill -f syslog_parser +sudo -u ids nohup python3.11 syslog_parser.py > /var/log/ids/syslog_parser.log 2>&1 & +``` + +--- + +## โœ… Checklist Post-Bootstrap + +- [ ] `/opt/ids/update_from_git.sh` esiste ed รจ eseguibile +- [ ] `/opt/ids/.env` contiene `PGHOST=127.0.0.1` +- [ ] `/opt/ids/git.env` contiene credenziali GitLab +- [ ] `syslog_parser.py` รจ attivo e scrive su PostgreSQL +- [ ] Database contiene log esistenti +- [ ] `git remote -v` mostra origin corretto +- [ ] `/opt/ids_legacy` puรฒ essere eliminato dopo 1 settimana + +--- + +## ๐Ÿ“ Note + +- Bootstrap รจ un'operazione **ONE-TIME** (una volta sola) +- Dopo bootstrap, usa SEMPRE `update_from_git.sh` +- NON eliminare `/opt/ids_legacy` prima di 7 giorni +- Verifica sempre i log dopo ogni aggiornamento diff --git a/replit.md b/replit.md index 86ac2e9..127cbb4 100644 --- a/replit.md +++ b/replit.md @@ -81,7 +81,28 @@ Sistema di rilevamento intrusioni per router MikroTik basato su Machine Learning ## Deployment e Aggiornamenti -### Aggiornamento da Git (Server AlmaLinux) +### PRIMO DEPLOYMENT (Bootstrap) - Server AlmaLinux +**Documentazione**: `deployment/BOOTSTRAP_PRIMO_DEPLOYMENT.md` + +```bash +# Clone in directory separata (preserva .env esistente) +cd /opt +sudo -u ids git clone https://[CREDENTIALS]@git.alfacom.it/marco/ids.git ids_git + +# Copia .env esistente +sudo -u ids cp /opt/ids/.env /opt/ids_git/.env + +# Swap atomico directory +mv /opt/ids /opt/ids_legacy +mv /opt/ids_git /opt/ids + +# Installa dipendenze e riavvia servizi +cd /opt/ids +sudo -u ids npm install +cd python_ml && sudo -u ids pip3.11 install -r requirements.txt +``` + +### Aggiornamenti Futuri (Dopo Bootstrap) ```bash # Aggiornamento standard (codice + dipendenze) cd /opt/ids @@ -91,6 +112,8 @@ cd /opt/ids ./update_from_git.sh --db ``` +**IMPORTANTE**: `update_from_git.sh` fa backup automatico di `.env` e `git.env` prima del pull! + ### Export Schema Database (Solo Struttura) ```bash # Su server production, esporta schema per commit su git @@ -100,6 +123,15 @@ cd /opt/ids/deployment # Risultato: database-schema/schema.sql (NO dati, SOLO DDL) ``` +### Push su Git (Da Replit) +```bash +# Esporta schema + commit + push +cd /opt/ids +./push-gitlab.sh # Patch version (1.0.0 โ†’ 1.0.1) +./push-gitlab.sh minor # Minor version (1.0.5 โ†’ 1.1.0) +./push-gitlab.sh major # Major version (1.1.5 โ†’ 2.0.0) +``` + ## Comandi Utili ### Start Python Backend