argos-setup/first-setup.sh

179 lines
10 KiB
Bash
Raw Normal View History

#!/bin/bash
# ══════════════════════════════════════════════════════════════════════════════
# ARGOS SOC — Installer ambiente
# Tecnotel Servizi SRL — Ubuntu 24.04 LTS
# Uso: sudo bash first-setup.sh
# ══════════════════════════════════════════════════════════════════════════════
set -euo pipefail
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
BLUE='\033[0;34m'; CYAN='\033[0;36m'; NC='\033[0m'
info() { echo -e "${CYAN}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
section() { echo -e "\n${BLUE}══════════════════════════════════════${NC}"; echo -e "${BLUE} $1${NC}"; echo -e "${BLUE}══════════════════════════════════════${NC}"; }
[[ $EUID -ne 0 ]] && error "Eseguire come root: sudo bash first-setup.sh"
. /etc/os-release
[[ "$ID" != "ubuntu" || "$VERSION_ID" != "24.04" ]] && error "Richiesto Ubuntu 24.04 LTS"
echo ""
echo -e "${BLUE}╔══════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ ARGOS SOC — Setup ambiente base ║${NC}"
echo -e "${BLUE}║ Tecnotel Servizi SRL ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════╝${NC}"
echo ""
# ══════════════════════════════════════════════════════════════════════════════
section "1. Sistema base"
# ══════════════════════════════════════════════════════════════════════════════
timedatectl set-timezone Europe/Rome
apt-get update -qq
apt-get upgrade -y -qq
apt-get install -y -qq \
curl wget git vim htop unzip jq \
python3 python3-pip python3-venv python3-cryptography \
nginx certbot python3-certbot-nginx \
ufw fail2ban \
build-essential libssl-dev libffi-dev python3-dev \
sqlite3 net-tools dnsutils lsof tcpdump nmap \
ca-certificates gnupg apt-transport-https
success "Pacchetti sistema installati"
# ══════════════════════════════════════════════════════════════════════════════
section "2. Node.js 20 LTS"
# ══════════════════════════════════════════════════════════════════════════════
if ! node --version 2>/dev/null | grep -q "v2[02]"; then
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - >/dev/null 2>&1
apt-get install -y nodejs -qq
fi
success "Node.js $(node --version) installato"
# ══════════════════════════════════════════════════════════════════════════════
section "3. Utente applicazione"
# ══════════════════════════════════════════════════════════════════════════════
if ! id "argos" &>/dev/null; then
useradd -r -s /bin/bash -m -d /home/argos argos
success "Utente argos creato"
else
warn "Utente argos già esistente"
fi
# Aggiungi argos al gruppo systemd-journal per lettura log via journalctl
# (necessario per la pagina 'Log Servizi' in UI)
usermod -aG systemd-journal argos
success "Utente argos aggiunto al gruppo systemd-journal"
# Permetti a user 'argos' di restart dei servizi via UI (Backup & Restore)
# Scope ristretto: solo restart dei 4 demoni ARGOS, nessun altro comando.
cat > /etc/sudoers.d/argos-systemctl <<'SUDOEOF'
argos ALL=(ALL) NOPASSWD: /bin/systemctl restart argos-backend
argos ALL=(ALL) NOPASSWD: /bin/systemctl restart argos-sync
argos ALL=(ALL) NOPASSWD: /bin/systemctl restart argos-ops
argos ALL=(ALL) NOPASSWD: /bin/systemctl restart argos-analytics
SUDOEOF
chmod 440 /etc/sudoers.d/argos-systemctl
visudo -cf /etc/sudoers.d/argos-systemctl > /dev/null
success "Sudoers per restart servizi configurato"
# ══════════════════════════════════════════════════════════════════════════════
section "4. Struttura cartelle"
# ══════════════════════════════════════════════════════════════════════════════
mkdir -p /opt/argos/{app,config,data,feeds,logs,certs,backups,setup}
mkdir -p /opt/argos/config/assets
mkdir -p /opt/argos/data/{reports,models}
chown -R argos:argos /opt/argos
chmod -R 750 /opt/argos
# /opt/argos/feeds: pubblicamente leggibili (FortiGate ETF via nginx/www-data)
chmod 755 /opt/argos/feeds
success "Struttura /opt/argos/ creata"
# ══════════════════════════════════════════════════════════════════════════════
section "5. Firewall UFW"
# ══════════════════════════════════════════════════════════════════════════════
ufw --force reset >/dev/null
ufw default deny incoming >/dev/null
ufw default allow outgoing >/dev/null
ufw allow 22/tcp comment 'SSH' >/dev/null
ufw allow 80/tcp comment 'HTTP' >/dev/null
ufw allow 443/tcp comment 'HTTPS' >/dev/null
ufw allow 8888/tcp comment 'ARGOS Web Installer (temporaneo)' >/dev/null
ufw --force enable >/dev/null
success "Firewall UFW configurato"
# ══════════════════════════════════════════════════════════════════════════════
section "6. Fail2ban"
# ══════════════════════════════════════════════════════════════════════════════
systemctl enable --now fail2ban >/dev/null 2>&1
success "Fail2ban attivo"
# ══════════════════════════════════════════════════════════════════════════════
section "7. Nginx temporaneo"
# ══════════════════════════════════════════════════════════════════════════════
rm -f /etc/nginx/sites-enabled/default
cat > /etc/nginx/sites-available/argos-setup << 'NGINX'
server {
listen 80 default_server;
server_name _;
return 200 'ARGOS SOC Setup in corso — vai a http://IP:8888';
add_header Content-Type text/plain;
}
NGINX
ln -sf /etc/nginx/sites-available/argos-setup /etc/nginx/sites-enabled/
nginx -t && systemctl restart nginx
success "Nginx temporaneo configurato"
# ══════════════════════════════════════════════════════════════════════════════
section "8. Web Installer"
# ══════════════════════════════════════════════════════════════════════════════
# Copia file setup dalla directory dello script (argos-setup tarball)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
for f in setup_server.py setup.html gen_config.py; do
if [[ -f "$SCRIPT_DIR/$f" ]]; then
cp "$SCRIPT_DIR/$f" /opt/argos/setup/
else
error "File $f non trovato in $SCRIPT_DIR — installare argos-setup completo"
fi
done
chown -R root:root /opt/argos/setup
# Systemd service web installer
cat > /etc/systemd/system/argos-setup.service << 'EOF'
[Unit]
Description=ARGOS SOC Web Installer
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/argos/setup
ExecStart=/usr/bin/python3 /opt/argos/setup/setup_server.py
Restart=on-failure
RestartSec=3
StandardOutput=journal
StandardError=journal
SyslogIdentifier=argos-setup
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now argos-setup
success "Web installer avviato"
SERVER_IP=$(hostname -I | awk '{print $1}')
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Ambiente pronto! ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e " ${CYAN}Completa la configurazione aprendo nel browser:${NC}"
echo -e " ${YELLOW}→ http://${SERVER_IP}:8888${NC}"
echo ""
echo -e " ${YELLOW}NOTA:${NC} La porta 8888 verrà chiusa automaticamente"
echo -e " al termine dell'installazione."
echo ""