diff --git a/database-schema/apply_migrations.sh b/database-schema/apply_migrations.sh index 1c63358..36e9c51 100755 --- a/database-schema/apply_migrations.sh +++ b/database-schema/apply_migrations.sh @@ -13,6 +13,7 @@ set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" MIGRATIONS_DIR="$SCRIPT_DIR/migrations" IDS_DIR="$(dirname "$SCRIPT_DIR")" +DEPLOYMENT_MIGRATIONS_DIR="$IDS_DIR/deployment/migrations" # Carica variabili ambiente ed esportale if [ -f "$IDS_DIR/.env" ]; then @@ -79,9 +80,25 @@ echo -e "${CYAN}๐Ÿ“Š Versione database corrente: ${YELLOW}${CURRENT_VERSION}${NC # STEP 3: Trova migrazioni da applicare # ============================================================================= # Formato migrazioni: 001_description.sql, 002_another.sql, etc. +# Cerca in ENTRAMBE le cartelle: database-schema/migrations E deployment/migrations MIGRATIONS_TO_APPLY=() -for migration_file in $(find "$MIGRATIONS_DIR" -name "[0-9][0-9][0-9]_*.sql" | sort); do +# Combina migrations da entrambe le cartelle e ordina per numero +ALL_MIGRATIONS="" +if [ -d "$MIGRATIONS_DIR" ]; then + ALL_MIGRATIONS+=$(find "$MIGRATIONS_DIR" -name "[0-9][0-9][0-9]_*.sql" 2>/dev/null || true) +fi +if [ -d "$DEPLOYMENT_MIGRATIONS_DIR" ]; then + if [ -n "$ALL_MIGRATIONS" ]; then + ALL_MIGRATIONS+=$'\n' + fi + ALL_MIGRATIONS+=$(find "$DEPLOYMENT_MIGRATIONS_DIR" -name "[0-9][0-9][0-9]_*.sql" 2>/dev/null || true) +fi + +# Ordina le migrations per nome file (NNN_*.sql) estraendo il basename +SORTED_MIGRATIONS=$(echo "$ALL_MIGRATIONS" | grep -v '^$' | while read f; do echo "$(basename "$f"):$f"; done | sort | cut -d':' -f2) + +for migration_file in $SORTED_MIGRATIONS; do MIGRATION_NAME=$(basename "$migration_file") # Estrai numero versione dal nome file (001, 002, etc.) diff --git a/deployment/install_list_fetcher.sh b/deployment/install_list_fetcher.sh new file mode 100644 index 0000000..5d52945 --- /dev/null +++ b/deployment/install_list_fetcher.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# ============================================================================= +# IDS - Installazione Servizio List Fetcher +# ============================================================================= +# Installa e configura il servizio systemd per il fetcher delle liste pubbliche +# Eseguire come ROOT: ./install_list_fetcher.sh +# ============================================================================= + +set -e + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +echo -e "${BLUE}" +echo "โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—" +echo "โ•‘ ๐Ÿ“‹ INSTALLAZIONE IDS LIST FETCHER โ•‘" +echo "โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•" +echo -e "${NC}" + +IDS_DIR="/opt/ids" +SYSTEMD_DIR="/etc/systemd/system" + +# Verifica di essere root +if [ "$EUID" -ne 0 ]; then + echo -e "${RED}โŒ Questo script deve essere eseguito come root${NC}" + echo -e "${YELLOW} Esegui: sudo ./install_list_fetcher.sh${NC}" + exit 1 +fi + +# Verifica che i file sorgente esistano +SERVICE_SRC="$IDS_DIR/deployment/systemd/ids-list-fetcher.service" +TIMER_SRC="$IDS_DIR/deployment/systemd/ids-list-fetcher.timer" + +if [ ! -f "$SERVICE_SRC" ]; then + echo -e "${RED}โŒ File service non trovato: $SERVICE_SRC${NC}" + exit 1 +fi + +if [ ! -f "$TIMER_SRC" ]; then + echo -e "${RED}โŒ File timer non trovato: $TIMER_SRC${NC}" + exit 1 +fi + +# Verifica che il virtual environment Python esista +VENV_PYTHON="$IDS_DIR/python_ml/venv/bin/python3" +if [ ! -f "$VENV_PYTHON" ]; then + echo -e "${YELLOW}โš ๏ธ Virtual environment non trovato, creazione...${NC}" + cd "$IDS_DIR/python_ml" + python3.11 -m venv venv + ./venv/bin/pip install --upgrade pip + ./venv/bin/pip install -r requirements.txt + echo -e "${GREEN}โœ… Virtual environment creato${NC}" +fi + +# Verifica che run_fetcher.py esista +FETCHER_SCRIPT="$IDS_DIR/python_ml/list_fetcher/run_fetcher.py" +if [ ! -f "$FETCHER_SCRIPT" ]; then + echo -e "${RED}โŒ Script fetcher non trovato: $FETCHER_SCRIPT${NC}" + exit 1 +fi + +# Copia file systemd +echo -e "${BLUE}๐Ÿ“ฆ Installazione file systemd...${NC}" + +cp "$SERVICE_SRC" "$SYSTEMD_DIR/ids-list-fetcher.service" +cp "$TIMER_SRC" "$SYSTEMD_DIR/ids-list-fetcher.timer" + +echo -e "${GREEN} โœ… ids-list-fetcher.service installato${NC}" +echo -e "${GREEN} โœ… ids-list-fetcher.timer installato${NC}" + +# Ricarica systemd +echo -e "${BLUE}๐Ÿ”„ Ricarica configurazione systemd...${NC}" +systemctl daemon-reload +echo -e "${GREEN}โœ… Daemon ricaricato${NC}" + +# Abilita e avvia timer +echo -e "${BLUE}โฑ๏ธ Abilitazione timer (ogni 10 minuti)...${NC}" +systemctl enable ids-list-fetcher.timer +systemctl start ids-list-fetcher.timer +echo -e "${GREEN}โœ… Timer abilitato e avviato${NC}" + +# Test esecuzione manuale +echo -e "${BLUE}๐Ÿงช Test esecuzione fetcher...${NC}" +if systemctl start ids-list-fetcher.service; then + echo -e "${GREEN}โœ… Fetcher eseguito con successo${NC}" +else + echo -e "${YELLOW}โš ๏ธ Prima esecuzione potrebbe fallire se liste non configurate${NC}" +fi + +# Mostra stato +echo "" +echo -e "${GREEN}โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—${NC}" +echo -e "${GREEN}โ•‘ โœ… INSTALLAZIONE COMPLETATA โ•‘${NC}" +echo -e "${GREEN}โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•${NC}" +echo "" +echo -e "${BLUE}๐Ÿ“‹ COMANDI UTILI:${NC}" +echo -e " โ€ข Stato timer: ${YELLOW}systemctl status ids-list-fetcher.timer${NC}" +echo -e " โ€ข Stato service: ${YELLOW}systemctl status ids-list-fetcher.service${NC}" +echo -e " โ€ข Esegui manuale: ${YELLOW}systemctl start ids-list-fetcher.service${NC}" +echo -e " โ€ข Visualizza logs: ${YELLOW}journalctl -u ids-list-fetcher -n 50${NC}" +echo -e " โ€ข Timer attivi: ${YELLOW}systemctl list-timers | grep ids${NC}" +echo "" diff --git a/deployment/update_from_git.sh b/deployment/update_from_git.sh index 504ffe9..35de891 100755 --- a/deployment/update_from_git.sh +++ b/deployment/update_from_git.sh @@ -158,6 +158,20 @@ if [ -f "./deployment/setup_rsyslog.sh" ]; then fi fi +# Verifica e installa servizio list-fetcher se mancante +echo -e "\n${BLUE}๐Ÿ“‹ Verifica servizio list-fetcher...${NC}" +if ! systemctl list-unit-files | grep -q "ids-list-fetcher"; then + echo -e "${YELLOW} Servizio ids-list-fetcher non installato, installazione...${NC}" + if [ -f "./deployment/install_list_fetcher.sh" ]; then + chmod +x ./deployment/install_list_fetcher.sh + ./deployment/install_list_fetcher.sh + else + echo -e "${RED} โŒ Script install_list_fetcher.sh non trovato${NC}" + fi +else + echo -e "${GREEN} โœ… Servizio ids-list-fetcher giร  installato${NC}" +fi + # Restart servizi echo -e "\n${BLUE}๐Ÿ”„ Restart servizi...${NC}" if [ -f "./deployment/restart_all.sh" ]; then