ids.alfacom.it/deployment/BOOTSTRAP_PRIMO_DEPLOYMENT.md
marco370 32d1da96df 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
2025-11-17 17:15:31 +00:00

223 lines
4.9 KiB
Markdown

# 🚀 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