Update install.sh
This commit is contained in:
+103
-28
@@ -1,24 +1,23 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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:
|
# Uso:
|
||||||
# curl -sL https://admin.u-site.app/agent/install.sh | bash -s -- --token=TU_TOKEN --url=https://admin.u-site.app
|
# 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]
|
# bash install.sh --token=TU_TOKEN --url=https://admin.u-site.app [--interval=30] [--dir=/opt/usite-agent]
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
# ── Colores ───────────────────────────────────────────────────────────────────
|
|
||||||
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
|
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} $*"; }
|
info() { echo -e "${BLUE}[INFO]${NC} $*"; }
|
||||||
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
|
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
|
||||||
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
||||||
error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
|
error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
|
||||||
|
|
||||||
# ── Parámetros ────────────────────────────────────────────────────────────────
|
|
||||||
TOKEN=""
|
TOKEN=""
|
||||||
API_URL=""
|
API_URL=""
|
||||||
INTERVAL=30
|
INTERVAL=30
|
||||||
INSTALL_DIR="/opt/usite-agent"
|
INSTALL_DIR=""
|
||||||
|
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
case $arg in
|
case $arg in
|
||||||
@@ -32,7 +31,6 @@ done
|
|||||||
[[ -z "$TOKEN" ]] && error "Falta --token=TU_TOKEN"
|
[[ -z "$TOKEN" ]] && error "Falta --token=TU_TOKEN"
|
||||||
[[ -z "$API_URL" ]] && error "Falta --url=https://admin.u-site.app"
|
[[ -z "$API_URL" ]] && error "Falta --url=https://admin.u-site.app"
|
||||||
|
|
||||||
# ── Detectar arquitectura ─────────────────────────────────────────────────────
|
|
||||||
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
|
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
|
||||||
ARCH="$(uname -m)"
|
ARCH="$(uname -m)"
|
||||||
case $ARCH in
|
case $ARCH in
|
||||||
@@ -45,26 +43,50 @@ esac
|
|||||||
BINARY_NAME="usite-agent-${OS}-${ARCH}"
|
BINARY_NAME="usite-agent-${OS}-${ARCH}"
|
||||||
DOWNLOAD_URL="${API_URL}/agent/download/${BINARY_NAME}"
|
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 "Sistema: ${OS}/${ARCH}"
|
||||||
info "Directorio de instalación: ${INSTALL_DIR}"
|
info "Directorio: ${INSTALL_DIR}"
|
||||||
info "Intervalo de reporte: ${INTERVAL}s"
|
info "Intervalo: ${INTERVAL}s"
|
||||||
|
|
||||||
# ── Crear directorio ──────────────────────────────────────────────────────────
|
# ── Instalar binario ──────────────────────────────────────────────────────────
|
||||||
mkdir -p "$INSTALL_DIR"
|
mkdir -p "$INSTALL_DIR"
|
||||||
|
|
||||||
# ── Descargar binario ─────────────────────────────────────────────────────────
|
|
||||||
info "Descargando agente desde ${DOWNLOAD_URL}..."
|
info "Descargando agente desde ${DOWNLOAD_URL}..."
|
||||||
if command -v curl &>/dev/null; then
|
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
|
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
|
else
|
||||||
error "Se requiere curl o wget para descargar el agente"
|
error "Se requiere curl o wget"
|
||||||
fi
|
fi
|
||||||
chmod +x "${INSTALL_DIR}/usite-agent"
|
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
|
cat > "${INSTALL_DIR}/agent.yml" <<EOF
|
||||||
api_url: ${API_URL}
|
api_url: ${API_URL}
|
||||||
token: ${TOKEN}
|
token: ${TOKEN}
|
||||||
@@ -73,8 +95,11 @@ debug: false
|
|||||||
EOF
|
EOF
|
||||||
ok "Configuración creada en ${INSTALL_DIR}/agent.yml"
|
ok "Configuración creada en ${INSTALL_DIR}/agent.yml"
|
||||||
|
|
||||||
# ── Crear servicio systemd ────────────────────────────────────────────────────
|
# ── Auto-arranque inteligente ─────────────────────────────────────────────────
|
||||||
if command -v systemctl &>/dev/null; then
|
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
|
cat > /etc/systemd/system/usite-agent.service <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=u-site Server Monitoring Agent
|
Description=u-site Server Monitoring Agent
|
||||||
@@ -94,21 +119,71 @@ SyslogIdentifier=usite-agent
|
|||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
systemctl enable usite-agent
|
systemctl enable usite-agent
|
||||||
systemctl start usite-agent
|
systemctl start usite-agent
|
||||||
ok "Servicio systemd 'usite-agent' instalado y activo"
|
ok "Servicio systemd 'usite-agent' instalado y activo"
|
||||||
info "Comandos útiles:"
|
STARTED=1
|
||||||
info " Ver estado: systemctl status usite-agent"
|
|
||||||
info " Ver logs: journalctl -u usite-agent -f"
|
# 2) openrc (Alpine, algunos VPS)
|
||||||
info " Detener: systemctl stop usite-agent"
|
elif [[ "$IS_ROOT" == 1 ]] && command -v rc-update &>/dev/null; then
|
||||||
info " Desinstalar: systemctl disable usite-agent && rm /etc/systemd/system/usite-agent.service"
|
cat > /etc/init.d/usite-agent <<EOF
|
||||||
else
|
#!/sbin/openrc-run
|
||||||
warn "systemd no disponible. Ejecuta manualmente:"
|
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 &"
|
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
|
fi
|
||||||
|
|
||||||
|
# ── Resumen ────────────────────────────────────────────────────────────────────
|
||||||
echo ""
|
echo ""
|
||||||
ok "✅ usite-agent instalado correctamente"
|
if [[ "$STARTED" == 1 ]]; then
|
||||||
info "El agente empezará a reportar métricas a ${API_URL} cada ${INTERVAL} segundos."
|
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