190 lines
7.0 KiB
Bash
190 lines
7.0 KiB
Bash
#!/usr/bin/env bash
|
|
# 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
|
|
# bash install.sh --token=TU_TOKEN --url=https://admin.u-site.app [--interval=30] [--dir=/opt/usite-agent]
|
|
|
|
set -euo pipefail
|
|
|
|
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; }
|
|
|
|
TOKEN=""
|
|
API_URL=""
|
|
INTERVAL=30
|
|
INSTALL_DIR=""
|
|
|
|
for arg in "$@"; do
|
|
case $arg in
|
|
--token=*) TOKEN="${arg#*=}" ;;
|
|
--url=*) API_URL="${arg#*=}" ;;
|
|
--interval=*) INTERVAL="${arg#*=}" ;;
|
|
--dir=*) INSTALL_DIR="${arg#*=}" ;;
|
|
esac
|
|
done
|
|
|
|
[[ -z "$TOKEN" ]] && error "Falta --token=TU_TOKEN"
|
|
[[ -z "$API_URL" ]] && error "Falta --url=https://admin.u-site.app"
|
|
|
|
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
|
|
ARCH="$(uname -m)"
|
|
case $ARCH in
|
|
x86_64) ARCH="amd64" ;;
|
|
aarch64) ARCH="arm64" ;;
|
|
armv7l) ARCH="arm" ;;
|
|
*) error "Arquitectura no soportada: $ARCH" ;;
|
|
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: ${INSTALL_DIR}"
|
|
info "Intervalo: ${INTERVAL}s"
|
|
|
|
# ── Instalar binario ──────────────────────────────────────────────────────────
|
|
mkdir -p "$INSTALL_DIR"
|
|
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"
|
|
elif command -v wget &>/dev/null; then
|
|
wget -q "$DOWNLOAD_URL" -O "${INSTALL_DIR}/usite-agent" || error "No se pudo descargar"
|
|
else
|
|
error "Se requiere curl o wget"
|
|
fi
|
|
chmod +x "${INSTALL_DIR}/usite-agent"
|
|
ok "Binario descargado"
|
|
|
|
# ── Configuración ─────────────────────────────────────────────────────────────
|
|
cat > "${INSTALL_DIR}/agent.yml" <<EOF
|
|
api_url: ${API_URL}
|
|
token: ${TOKEN}
|
|
interval: ${INTERVAL}
|
|
debug: false
|
|
EOF
|
|
ok "Configuración creada en ${INSTALL_DIR}/agent.yml"
|
|
|
|
# ── 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
|
|
After=network.target
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
ExecStart=${INSTALL_DIR}/usite-agent --config=${INSTALL_DIR}/agent.yml
|
|
WorkingDirectory=${INSTALL_DIR}
|
|
Restart=always
|
|
RestartSec=10
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
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"
|
|
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 ""
|
|
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
|