#!/usr/bin/env bash # Installe l'agent Nanometrics depuis la dernière release Gitea. # Usage : # curl -fsSL https://git.maison43gil.com/gilles/nano_metrics/raw/branch/main/deploy/install.sh | bash # SERVER_IP=10.0.0.50 SERVER_PORT=9999 curl -fsSL ... | bash set -euo pipefail REPO_API="https://git.maison43gil.com/api/v1/repos/gilles/nano_metrics" SERVICE_URL="https://git.maison43gil.com/gilles/nano_metrics/raw/branch/main/deploy/nanometrics-agent.service" INSTALL_BIN="/usr/local/bin/nanometrics-agent" CONFIG_DIR="/etc/nanometrics" CONFIG_FILE="$CONFIG_DIR/config.toml" SERVICE_FILE="/etc/systemd/system/nanometrics-agent.service" # ── Couleurs ─────────────────────────────────────────────────────────────────── RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m' ok() { echo -e "${GREEN}✓${NC} $*"; } warn() { echo -e "${YELLOW}!${NC} $*"; } err() { echo -e "${RED}✗${NC} $*" >&2; } # ── Root check ───────────────────────────────────────────────────────────────── if [ "$(id -u)" -ne 0 ]; then err "Ce script doit être lancé en root (sudo bash ou sudo curl | bash)" exit 1 fi echo "======================================" echo " Nanometrics Agent — Installation" echo "======================================" echo "" # ── 1. Détection de l'architecture ──────────────────────────────────────────── ARCH="$(uname -m)" case "$ARCH" in x86_64) LABEL="linux-amd64" ;; aarch64) LABEL="linux-arm64" ;; *) err "Architecture non supportée : $ARCH" err "Seules x86_64 et aarch64 sont supportées." exit 1 ;; esac ok "Architecture détectée : $ARCH → $LABEL" # ── 2. Récupérer l'URL du binaire depuis la dernière release ────────────────── echo "→ Récupération de la dernière release..." ASSETS_JSON=$(curl -sf "$REPO_API/releases?limit=1&page=1") ASSET_URL=$(echo "$ASSETS_JSON" | python3 -c " import sys, json releases = json.load(sys.stdin) if not releases: raise SystemExit('Aucune release trouvée sur le dépôt.') assets = releases[0].get('assets', []) name = 'nanometrics-agent-$LABEL' for a in assets: if a['name'] == name: print(a['browser_download_url']) break else: raise SystemExit(f'Asset {name!r} introuvable dans la release.') ") TAG=$(echo "$ASSETS_JSON" | python3 -c " import sys, json releases = json.load(sys.stdin) print(releases[0]['tag_name']) ") ok "Release : $TAG — URL : $ASSET_URL" # ── 3. Télécharger le binaire ───────────────────────────────────────────────── TMP_BIN="$(mktemp)" trap 'rm -f "$TMP_BIN"' EXIT echo "→ Téléchargement du binaire..." curl -fsSL -o "$TMP_BIN" "$ASSET_URL" chmod 755 "$TMP_BIN" ok "Binaire téléchargé ($(du -sh "$TMP_BIN" | cut -f1))" # ── 4. Paramètres de configuration ──────────────────────────────────────────── echo "" echo "--- Configuration du serveur ---" SERVER_IP="${SERVER_IP:-10.0.0.50}" SERVER_PORT="${SERVER_PORT:-9999}" MQTT_HOST="${MQTT_HOST:-10.0.0.3}" MQTT_ENABLED="${MQTT_ENABLED:-false}" ok "Serveur : $SERVER_IP:$SERVER_PORT | MQTT broker : $MQTT_HOST" # ── 5. Installer le binaire ──────────────────────────────────────────────────── echo "" echo "[1/5] Installation du binaire dans /usr/local/bin/" # Arrêter le service si en cours (le binaire ne peut pas être écrasé à chaud) if systemctl is-active --quiet nanometrics-agent 2>/dev/null; then warn "Service en cours — arrêt temporaire..." systemctl stop nanometrics-agent fi cp "$TMP_BIN" "$INSTALL_BIN" chmod 755 "$INSTALL_BIN" ok "Binaire installé" # ── 6. Créer le répertoire de configuration ─────────────────────────────────── echo "[2/5] Création de $CONFIG_DIR" mkdir -p "$CONFIG_DIR" chmod 755 "$CONFIG_DIR" ok "Répertoire créé" # ── 7. Écrire config.toml ───────────────────────────────────────────────────── echo "[3/5] Écriture de $CONFIG_FILE" OVERWRITE_CONFIG="${OVERWRITE_CONFIG:-}" WRITE_CONFIG=true if [ -f "$CONFIG_FILE" ]; then if [ "${OVERWRITE_CONFIG}" = "true" ]; then warn "OVERWRITE_CONFIG=true — config.toml sera écrasé" WRITE_CONFIG=true elif [ -t 0 ]; then # Mode interactif (bash local, pas curl | bash) echo "" warn "Un config.toml existe déjà :" echo " $CONFIG_FILE" printf " Écraser la configuration existante ? [o/N] : " read -r _ANS if [[ "$_ANS" =~ ^[Oo]$ ]]; then WRITE_CONFIG=true else ok "config.toml conservé" WRITE_CONFIG=false fi else warn "config.toml déjà présent — conservé (relancez avec OVERWRITE_CONFIG=true pour écraser)" WRITE_CONFIG=false fi fi if [ "$WRITE_CONFIG" = "true" ]; then cat > "$CONFIG_FILE" << TOML [server] ip = "$SERVER_IP" port = $SERVER_PORT [protocols.udp] enabled = true [protocols.mqtt] enabled = $MQTT_ENABLED host = "$MQTT_HOST" port = 1883 topic_base = "nanometrics/agents" auto_discovery = true birth_message = true last_will = true [metrics.cpu] udp = true mqtt = false [metrics.memory] udp = true mqtt = false [metrics.disk] udp = true mqtt = false [metrics.network] udp = false mqtt = false [metrics.uptime] udp = true mqtt = false [metrics.temperature] udp = true mqtt = false [metrics.smart] udp = true mqtt = false TOML chmod 644 "$CONFIG_FILE" ok "config.toml créé" fi # S'assurer que le fichier est toujours lisible (cas d'un config existant en 640) chmod 644 "$CONFIG_FILE" 2>/dev/null || true chmod 755 "$CONFIG_DIR" # ── 8. Installer le fichier service ────────────────────────────────────────── echo "[4/5] Installation du service systemd" curl -fsSL -o "$SERVICE_FILE" "$SERVICE_URL" chmod 644 "$SERVICE_FILE" systemctl daemon-reload systemctl enable nanometrics-agent ok "Service installé et activé" # ── 9. Démarrer le service ──────────────────────────────────────────────────── echo "[5/5] Démarrage du service" systemctl restart nanometrics-agent sleep 2 echo "" echo "=== Statut ===" systemctl status nanometrics-agent --no-pager || true echo "" ok "Installation terminée — agent $TAG opérationnel" echo " Config : $CONFIG_FILE" echo " Logs : journalctl -u nanometrics-agent -f"