From cbeb5e666ee85ade0d0374830f89bbc90bcded9a Mon Sep 17 00:00:00 2001 From: marco370 <48531002-marco370@users.noreply.replit.com> Date: Mon, 17 Nov 2025 16:03:32 +0000 Subject: [PATCH] Improve system update script with ownership fixes and better output Update deployment script to automatically fix Git ownership issues, add backup for git.env, implement stash for local changes, and enhance output formatting for a clearer user experience. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: abc30983-39f6-45cf-b89b-5b3507bb3c27 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/c9ITWqD --- .replit | 4 ++ deployment/fix_git_ownership.sh | 34 ++++++++++ deployment/update_from_git.sh | 108 ++++++++++++++++++++++++++------ 3 files changed, 128 insertions(+), 18 deletions(-) create mode 100755 deployment/fix_git_ownership.sh mode change 100644 => 100755 deployment/update_from_git.sh diff --git a/.replit b/.replit index af7007c..4a1db68 100644 --- a/.replit +++ b/.replit @@ -14,6 +14,10 @@ run = ["npm", "run", "start"] localPort = 5000 externalPort = 80 +[[ports]] +localPort = 34465 +externalPort = 3000 + [env] PORT = "5000" diff --git a/deployment/fix_git_ownership.sh b/deployment/fix_git_ownership.sh new file mode 100755 index 0000000..50c042e --- /dev/null +++ b/deployment/fix_git_ownership.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# ============================================================================= +# IDS - Fix Git Ownership Error +# ============================================================================= +# Risolve: "fatal: detected dubious ownership in repository" +# ============================================================================= + +set -e + +GREEN='\033[0;32m' +BLUE='\033[0;34m' +NC='\033[0m' + +echo -e "${BLUE}" +echo "╔═══════════════════════════════════════════════╗" +echo "║ Git Ownership Fix ║" +echo "╚═══════════════════════════════════════════════╝" +echo -e "${NC}" + +# Aggiungi /opt/ids come safe directory per root +echo -e "${BLUE}🔧 Configuro /opt/ids come safe directory...${NC}" +git config --global --add safe.directory /opt/ids + +# Configura anche per utente ids +sudo -u ids git config --global --add safe.directory /opt/ids + +echo -e "${GREEN}✅ Configurazione completata${NC}" +echo "" +echo "Repository configurati come safe:" +git config --global --get-all safe.directory +echo "" + +exit 0 diff --git a/deployment/update_from_git.sh b/deployment/update_from_git.sh old mode 100644 new mode 100755 index 7894d25..abde83f --- a/deployment/update_from_git.sh +++ b/deployment/update_from_git.sh @@ -1,7 +1,13 @@ #!/bin/bash -# Script per aggiornare il sistema IDS da git.alfacom.it -# Eseguire con: sudo -u ids ./update_from_git.sh +# ============================================================================= +# 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' @@ -10,12 +16,32 @@ YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' -echo -e "${BLUE}🔄 AGGIORNAMENTO SISTEMA IDS DA GIT${NC}" -echo "====================================" +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 @@ -23,9 +49,14 @@ if [ -f ".env" ]; then 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 ! git diff-index --quiet HEAD --; then +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 @@ -33,12 +64,15 @@ if ! git diff-index --quiet HEAD --; then 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}" -git fetch origin -git pull origin main +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}" @@ -48,33 +82,71 @@ else fi # Ripristina configurazione locale +echo -e "\n${BLUE}🔄 Ripristino configurazione locale...${NC}" if [ -f ".env.backup" ]; then cp .env.backup .env - echo -e "${GREEN}✅ Configurazione locale ripristinata${NC}" + 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}" -npm install +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 -/usr/bin/python3.11 -m pip install -r requirements.txt +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}" -npm run db:push +sudo -u $IDS_USER npm run db:push + +if [ $? -eq 0 ]; then + echo -e "${GREEN}✅ Schema database sincronizzato${NC}" +fi # Restart servizi echo -e "\n${BLUE}🔄 Restart servizi...${NC}" -./deployment/restart_all.sh +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 -e "\n${GREEN}✅ AGGIORNAMENTO COMPLETATO!${NC}" echo "" -echo -e "${YELLOW}📋 VERIFICA:${NC}" -echo -e " • Controlla log backend: ${BLUE}tail -f /var/log/ids/backend.log${NC}" -echo -e " • Controlla log frontend: ${BLUE}tail -f /var/log/ids/frontend.log${NC}" -echo -e " • Testa API backend: ${BLUE}curl http://localhost:8000/health${NC}" -echo -e " • Testa frontend: ${BLUE}curl http://localhost:5000${NC}" +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