Update install.sh
This commit is contained in:
+103
-28
@@ -1,24 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
# install.sh — Instalador del agente usite-agent
|
||||
# install.sh — Instalador inteligente del agente usite-agent
|
||||
# Se adapta automáticamente al entorno: VPS root, contenedor, usuario sin root, etc.
|
||||
#
|
||||
# Uso:
|
||||
# curl -sL https://admin.u-site.app/agent/install.sh | bash -s -- --token=TU_TOKEN --url=https://admin.u-site.app
|
||||
# o descargando primero:
|
||||
# bash install.sh --token=TU_TOKEN --url=https://admin.u-site.app [--interval=30] [--dir=/opt/usite-agent]
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# ── Colores ───────────────────────────────────────────────────────────────────
|
||||
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
|
||||
info() { echo -e "${BLUE}[INFO]${NC} $*"; }
|
||||
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
|
||||
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
||||
error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
|
||||
|
||||
# ── Parámetros ────────────────────────────────────────────────────────────────
|
||||
TOKEN=""
|
||||
API_URL=""
|
||||
INTERVAL=30
|
||||
INSTALL_DIR="/opt/usite-agent"
|
||||
INSTALL_DIR=""
|
||||
|
||||
for arg in "$@"; do
|
||||
case $arg in
|
||||
@@ -32,7 +31,6 @@ done
|
||||
[[ -z "$TOKEN" ]] && error "Falta --token=TU_TOKEN"
|
||||
[[ -z "$API_URL" ]] && error "Falta --url=https://admin.u-site.app"
|
||||
|
||||
# ── Detectar arquitectura ─────────────────────────────────────────────────────
|
||||
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
|
||||
ARCH="$(uname -m)"
|
||||
case $ARCH in
|
||||
@@ -45,26 +43,50 @@ esac
|
||||
BINARY_NAME="usite-agent-${OS}-${ARCH}"
|
||||
DOWNLOAD_URL="${API_URL}/agent/download/${BINARY_NAME}"
|
||||
|
||||
IS_ROOT=0
|
||||
[[ "$(id -u)" == "0" ]] && IS_ROOT=1
|
||||
|
||||
# ── Detectar entorno ──────────────────────────────────────────────────────────
|
||||
ENV_TYPE="vps"
|
||||
IS_CONTAINER=0
|
||||
if [[ -f "/run/.containerenv" ]] || [[ -f "/.dockerenv" ]]; then
|
||||
IS_CONTAINER=1; ENV_TYPE="contenedor"
|
||||
fi
|
||||
if [[ -n "${DOCKER_HOST:-}" ]] || [[ -n "${KUBERNETES_SERVICE_HOST:-}" ]]; then
|
||||
IS_CONTAINER=1; ENV_TYPE="contenedor"
|
||||
fi
|
||||
if [[ "$IS_ROOT" == 0 ]]; then
|
||||
ENV_TYPE="usuario"
|
||||
fi
|
||||
|
||||
# ── Directorio ─────────────────────────────────────────────────────────────────
|
||||
if [[ -z "$INSTALL_DIR" ]]; then
|
||||
if [[ "$IS_ROOT" == 1 ]]; then
|
||||
INSTALL_DIR="/opt/usite-agent"
|
||||
else
|
||||
INSTALL_DIR="${HOME}/.usite-agent"
|
||||
fi
|
||||
fi
|
||||
|
||||
info "Entorno: ${ENV_TYPE}${IS_CONTAINER:+(contenedor)}"
|
||||
info "Sistema: ${OS}/${ARCH}"
|
||||
info "Directorio de instalación: ${INSTALL_DIR}"
|
||||
info "Intervalo de reporte: ${INTERVAL}s"
|
||||
info "Directorio: ${INSTALL_DIR}"
|
||||
info "Intervalo: ${INTERVAL}s"
|
||||
|
||||
# ── Crear directorio ──────────────────────────────────────────────────────────
|
||||
# ── Instalar binario ──────────────────────────────────────────────────────────
|
||||
mkdir -p "$INSTALL_DIR"
|
||||
|
||||
# ── Descargar binario ─────────────────────────────────────────────────────────
|
||||
info "Descargando agente desde ${DOWNLOAD_URL}..."
|
||||
if command -v curl &>/dev/null; then
|
||||
curl -fsSL "$DOWNLOAD_URL" -o "${INSTALL_DIR}/usite-agent" || error "No se pudo descargar el binario"
|
||||
curl -fsSL "$DOWNLOAD_URL" -o "${INSTALL_DIR}/usite-agent" || error "No se pudo descargar"
|
||||
elif command -v wget &>/dev/null; then
|
||||
wget -q "$DOWNLOAD_URL" -O "${INSTALL_DIR}/usite-agent" || error "No se pudo descargar el binario"
|
||||
wget -q "$DOWNLOAD_URL" -O "${INSTALL_DIR}/usite-agent" || error "No se pudo descargar"
|
||||
else
|
||||
error "Se requiere curl o wget para descargar el agente"
|
||||
error "Se requiere curl o wget"
|
||||
fi
|
||||
chmod +x "${INSTALL_DIR}/usite-agent"
|
||||
ok "Binario descargado y listo"
|
||||
ok "Binario descargado"
|
||||
|
||||
# ── Crear configuración ───────────────────────────────────────────────────────
|
||||
# ── Configuración ─────────────────────────────────────────────────────────────
|
||||
cat > "${INSTALL_DIR}/agent.yml" <<EOF
|
||||
api_url: ${API_URL}
|
||||
token: ${TOKEN}
|
||||
@@ -73,8 +95,11 @@ debug: false
|
||||
EOF
|
||||
ok "Configuración creada en ${INSTALL_DIR}/agent.yml"
|
||||
|
||||
# ── Crear servicio systemd ────────────────────────────────────────────────────
|
||||
if command -v systemctl &>/dev/null; then
|
||||
# ── Auto-arranque inteligente ─────────────────────────────────────────────────
|
||||
STARTED=0
|
||||
|
||||
# 1) systemd (VPS con root)
|
||||
if [[ "$IS_ROOT" == 1 ]] && command -v systemctl &>/dev/null; then
|
||||
cat > /etc/systemd/system/usite-agent.service <<EOF
|
||||
[Unit]
|
||||
Description=u-site Server Monitoring Agent
|
||||
@@ -94,21 +119,71 @@ SyslogIdentifier=usite-agent
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable usite-agent
|
||||
systemctl start usite-agent
|
||||
ok "Servicio systemd 'usite-agent' instalado y activo"
|
||||
info "Comandos útiles:"
|
||||
info " Ver estado: systemctl status usite-agent"
|
||||
info " Ver logs: journalctl -u usite-agent -f"
|
||||
info " Detener: systemctl stop usite-agent"
|
||||
info " Desinstalar: systemctl disable usite-agent && rm /etc/systemd/system/usite-agent.service"
|
||||
else
|
||||
warn "systemd no disponible. Ejecuta manualmente:"
|
||||
STARTED=1
|
||||
|
||||
# 2) openrc (Alpine, algunos VPS)
|
||||
elif [[ "$IS_ROOT" == 1 ]] && command -v rc-update &>/dev/null; then
|
||||
cat > /etc/init.d/usite-agent <<EOF
|
||||
#!/sbin/openrc-run
|
||||
description="u-site Server Monitoring Agent"
|
||||
command="${INSTALL_DIR}/usite-agent"
|
||||
command_args="--config=${INSTALL_DIR}/agent.yml"
|
||||
command_background=true
|
||||
pidfile="/run/usite-agent.pid"
|
||||
EOF
|
||||
chmod +x /etc/init.d/usite-agent
|
||||
rc-update add usite-agent default
|
||||
rc-service usite-agent start
|
||||
ok "Servicio openrc 'usite-agent' instalado y activo"
|
||||
STARTED=1
|
||||
|
||||
# 3) Docker / contenedor — ejecución en foreground con restart automático
|
||||
elif [[ "$IS_CONTAINER" == 1 ]]; then
|
||||
warn "Entorno contenedor detectado. El agente se ejecutará en primer plano."
|
||||
warn "Agrega al entrypoint de tu contenedor:"
|
||||
warn " ${INSTALL_DIR}/usite-agent --config=${INSTALL_DIR}/agent.yml &"
|
||||
STARTED=1
|
||||
|
||||
# 4) Usuario sin root — crontab para auto-arranque
|
||||
else
|
||||
CRON_JOB="@reboot ${INSTALL_DIR}/usite-agent --config=${INSTALL_DIR}/agent.yml >/dev/null 2>&1 &"
|
||||
if crontab -l 2>/dev/null | grep -q "usite-agent"; then
|
||||
warn "Crontab ya tiene una entrada para usite-agent, se omite"
|
||||
else
|
||||
(crontab -l 2>/dev/null || true; echo "$CRON_JOB") | crontab -
|
||||
ok "Entrada @reboot agregada al crontab"
|
||||
fi
|
||||
nohup "${INSTALL_DIR}/usite-agent" --config="${INSTALL_DIR}/agent.yml" >/dev/null 2>&1 &
|
||||
ok "Agente iniciado en background (PID $!)"
|
||||
STARTED=1
|
||||
fi
|
||||
|
||||
# ── Resumen ────────────────────────────────────────────────────────────────────
|
||||
echo ""
|
||||
ok "✅ usite-agent instalado correctamente"
|
||||
info "El agente empezará a reportar métricas a ${API_URL} cada ${INTERVAL} segundos."
|
||||
if [[ "$STARTED" == 1 ]]; then
|
||||
ok "usite-agent instalado correctamente en ${INSTALL_DIR}"
|
||||
info "El agente reportará métricas a ${API_URL} cada ${INTERVAL}s"
|
||||
info ""
|
||||
info "Comandos útiles:"
|
||||
if [[ "$IS_ROOT" == 1 ]] && command -v systemctl &>/dev/null; then
|
||||
info " systemctl status usite-agent"
|
||||
info " journalctl -u usite-agent -f"
|
||||
fi
|
||||
info " ${INSTALL_DIR}/usite-agent --config=${INSTALL_DIR}/agent.yml (ejecutar manual)"
|
||||
info ""
|
||||
info "Para desinstalar:"
|
||||
if [[ "$IS_ROOT" == 1 ]] && command -v systemctl &>/dev/null; then
|
||||
info " systemctl disable usite-agent && rm /etc/systemd/system/usite-agent.service && rm -rf ${INSTALL_DIR}"
|
||||
elif [[ "$IS_ROOT" == 1 ]] && command -v rc-update &>/dev/null; then
|
||||
info " rc-update del usite-agent && rm /etc/init.d/usite-agent && rm -rf ${INSTALL_DIR}"
|
||||
else
|
||||
info " crontab -l | grep -v usite-agent | crontab - && rm -rf ${INSTALL_DIR}"
|
||||
fi
|
||||
else
|
||||
warn "No se pudo configurar auto-arranque. Ejecuta manualmente:"
|
||||
warn " ${INSTALL_DIR}/usite-agent --config=${INSTALL_DIR}/agent.yml &"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user