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
This commit is contained in:
marco370 2025-11-17 17:15:31 +00:00
parent c2dcb7bb79
commit 32d1da96df
2 changed files with 255 additions and 1 deletions

View File

@ -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

View File

@ -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