ids.alfacom.it/deployment/update_from_git.sh
marco370 c9b2a8a9a9 Set up system to receive and store MikroTik logs
Add rsyslog configuration for receiving MikroTik logs via UDP, store them in a dedicated file, and prevent duplicates in system messages.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: b452008c-bd98-4e68-81a9-f20d3f714372
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/DR50xVM
2025-11-21 17:26:52 +00:00

188 lines
6.5 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# IDS - Aggiornamento Sistema da git.alfacom.it
# =============================================================================
# Eseguire come ROOT: ./update_from_git.sh
# Lo script esegue i comandi git come utente 'ids' automaticamente
# =============================================================================
set -e
# Colori per output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}"
echo "╔═══════════════════════════════════════════════╗"
echo "║ 🔄 AGGIORNAMENTO SISTEMA IDS DA GIT ║"
echo "╚═══════════════════════════════════════════════╝"
echo -e "${NC}"
IDS_DIR="/opt/ids"
IDS_USER="ids"
# 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 ./update_from_git.sh${NC}"
exit 1
fi
cd "$IDS_DIR" || exit 1
# Fix git ownership se necessario
echo -e "${BLUE}🔧 Verifica configurazione git...${NC}"
if ! sudo -u $IDS_USER git config --global --get-all safe.directory | grep -q "^/opt/ids$"; then
echo -e "${YELLOW} Configuro safe directory...${NC}"
sudo -u $IDS_USER git config --global --add safe.directory /opt/ids
echo -e "${GREEN} ✅ Configurato${NC}"
fi
# Backup configurazione locale
echo -e "\n${BLUE}💾 Backup configurazione locale...${NC}"
if [ -f ".env" ]; then
cp .env .env.backup
echo -e "${GREEN}✅ .env salvato in .env.backup${NC}"
fi
if [ -f "git.env" ]; then
cp git.env git.env.backup
echo -e "${GREEN}✅ git.env salvato in git.env.backup${NC}"
fi
# Verifica modifiche locali
echo -e "\n${BLUE}🔍 Verifica modifiche locali...${NC}"
if ! sudo -u $IDS_USER git diff-index --quiet HEAD -- 2>/dev/null; then
echo -e "${YELLOW}⚠️ Ci sono modifiche locali non committate${NC}"
echo -e "${YELLOW} Esegui 'git status' per vedere i dettagli${NC}"
read -p "Vuoi procedere comunque? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
# Stash modifiche locali
echo -e "${BLUE} Salvo modifiche locali temporaneamente...${NC}"
sudo -u $IDS_USER git stash
fi
# Pull da git
echo -e "\n${BLUE}📥 Download aggiornamenti da git.alfacom.it...${NC}"
sudo -u $IDS_USER git fetch origin
sudo -u $IDS_USER git pull origin main
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Aggiornamenti scaricati con successo${NC}"
else
echo -e "${RED}❌ Errore durante il download${NC}"
exit 1
fi
# Ripristina configurazione locale
echo -e "\n${BLUE}🔄 Ripristino configurazione locale...${NC}"
if [ -f ".env.backup" ]; then
cp .env.backup .env
chown $IDS_USER:$IDS_USER .env
echo -e "${GREEN}✅ .env ripristinato${NC}"
fi
if [ -f "git.env.backup" ]; then
cp git.env.backup git.env
chown $IDS_USER:$IDS_USER git.env
echo -e "${GREEN}✅ git.env ripristinato${NC}"
fi
# Aggiorna dipendenze Node.js
echo -e "\n${BLUE}📦 Aggiornamento dipendenze Node.js...${NC}"
sudo -u $IDS_USER npm install
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Dipendenze Node.js aggiornate${NC}"
else
echo -e "${YELLOW}⚠️ Possibili warning npm (spesso ignorabili)${NC}"
fi
# Aggiorna dipendenze Python
echo -e "\n${BLUE}📦 Aggiornamento dipendenze Python...${NC}"
cd python_ml
sudo -u $IDS_USER /usr/bin/python3.11 -m pip install --upgrade -r requirements.txt
cd ..
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Dipendenze Python aggiornate${NC}"
fi
# Aggiorna schema database
echo -e "\n${BLUE}🗄️ Aggiornamento schema database...${NC}"
# 1. Applica migrazioni SQL manuali (ALTER TABLE, ecc.)
if [ -f "./database-schema/apply_migrations.sh" ]; then
echo -e "${BLUE} Applicando migrazioni SQL...${NC}"
sudo -u $IDS_USER bash ./database-schema/apply_migrations.sh
if [ $? -eq 0 ]; then
echo -e "${GREEN} ✅ Migrazioni SQL applicate${NC}"
else
echo -e "${YELLOW} ⚠️ Alcune migrazioni potrebbero essere già applicate${NC}"
fi
fi
# 2. Sincronizza schema Drizzle
echo -e "${BLUE} Sincronizzando schema Drizzle...${NC}"
sudo -u $IDS_USER npm run db:push
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Schema database completamente sincronizzato${NC}"
else
echo -e "${YELLOW}⚠️ Schema Drizzle potrebbe richiedere --force${NC}"
fi
# Setup rsyslog (solo prima volta o se modificato)
if [ -f "./deployment/setup_rsyslog.sh" ]; then
echo -e "\n${BLUE}📡 Configurazione RSyslog (log MikroTik)...${NC}"
chmod +x ./deployment/setup_rsyslog.sh
# Esegui setup rsyslog se:
# - File config non esiste
# - Config è più vecchia di quella nel repo
RSYSLOG_CONF="/etc/rsyslog.d/99-mikrotik.conf"
RSYSLOG_SOURCE="./deployment/rsyslog/99-mikrotik.conf"
if [ ! -f "$RSYSLOG_CONF" ] || [ "$RSYSLOG_SOURCE" -nt "$RSYSLOG_CONF" ]; then
echo -e "${BLUE} Setup/aggiornamento rsyslog necessario...${NC}"
./deployment/setup_rsyslog.sh
else
echo -e "${GREEN} ✅ RSyslog già configurato${NC}"
fi
fi
# Restart servizi
echo -e "\n${BLUE}🔄 Restart servizi...${NC}"
if [ -f "./deployment/restart_all.sh" ]; then
chmod +x ./deployment/restart_all.sh
./deployment/restart_all.sh
echo -e "${GREEN}✅ Servizi riavviati${NC}"
else
echo -e "${YELLOW}⚠️ Script restart_all.sh non trovato${NC}"
echo -e "${YELLOW} Riavvia manualmente i servizi se necessario${NC}"
fi
echo ""
echo -e "${GREEN}╔═══════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ ✅ AGGIORNAMENTO COMPLETATO ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${BLUE}📋 VERIFICA SISTEMA:${NC}"
echo -e " • Log backend: ${YELLOW}tail -f /var/log/ids/backend.log${NC}"
echo -e " • Log frontend: ${YELLOW}tail -f /var/log/ids/frontend.log${NC}"
echo -e " • API backend: ${YELLOW}curl http://localhost:8000/health${NC}"
echo -e " • Frontend: ${YELLOW}curl http://localhost:5000${NC}"
echo ""
echo -e "${BLUE}📊 STATO SERVIZI:${NC}"
ps aux | grep -E 'python.*main|npm.*dev|syslog_parser' | grep -v grep
echo ""
exit 0