Files
fleet_monitoring/hub/deploy_agent.sh
T

72 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Despliega metrics_agent en un nodo remoto como servicio systemd.
#
# Uso: ./deploy_agent.sh <node_name> <ssh_host> [arch]
# node_name : valor de la label "instance" en Grafana (ej. homer)
# ssh_host : alias SSH del nodo (debe existir en ~/.ssh/config)
# arch : amd64 (default) | arm64
#
# Requisitos:
# - Binario compilado en apps/metrics_agent/dist/metrics_agent_<arch>
# (compila con: cd apps/metrics_agent && CGO_ENABLED=0 GOOS=linux GOARCH=<arch> \
# go build -ldflags="-s -w" -o dist/metrics_agent_<arch> .)
# - `pass fleet/ingest-pass` con el password de ingesta.
# - sudo -n (sin password) disponible en el nodo remoto.
set -euo pipefail
NODE="${1:?uso: deploy_agent.sh <node> <ssh_host> [arch]}"
HOST="${2:?uso: deploy_agent.sh <node> <ssh_host> [arch]}"
ARCH="${3:-amd64}"
HUB="https://metrics-dxaqj3ina6eqd5pjt85wkrrj.organic-machine.com/api/v1/import/prometheus"
LOKI="https://logs-wmaxecsjcfnocz81d5luca92.organic-machine.com/loki/api/v1/push"
PW="$(pass show fleet/ingest-pass | head -1)"
BIN="$(cd "$(dirname "$0")/.." && pwd)/apps/metrics_agent/dist/metrics_agent_${ARCH}"
[ -f "$BIN" ] || { echo "ERROR: falta el binario $BIN (compílalo primero)"; exit 1; }
echo ">> copiando binario a $HOST"
scp -q -o BatchMode=yes "$BIN" "$HOST:/tmp/metrics_agent"
echo ">> instalando servicio en $NODE ($HOST)"
ssh -o BatchMode=yes "$HOST" "NODE='$NODE' PW='$PW' HUB='$HUB' LOKI='$LOKI' bash -s" <<'OUTER'
set -e
sudo -n mkdir -p /opt/fleet-agent /etc/fleet-agent
sudo -n mv /tmp/metrics_agent /opt/fleet-agent/metrics_agent
sudo -n chmod 755 /opt/fleet-agent/metrics_agent
sudo -n tee /etc/fleet-agent/agent.json >/dev/null <<JSON
{
"node": "${NODE}",
"hub_url": "${HUB}",
"loki_url": "${LOKI}",
"user": "fleet",
"pass": "${PW}",
"interval_sec": 15
}
JSON
sudo -n chmod 600 /etc/fleet-agent/agent.json
sudo -n tee /etc/systemd/system/fleet-agent.service >/dev/null <<'UNIT'
[Unit]
Description=Fleet metrics agent (fleet_monitoring)
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/opt/fleet-agent/metrics_agent -config /etc/fleet-agent/agent.json
Restart=always
RestartSec=10
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
UNIT
sudo -n systemctl daemon-reload
sudo -n systemctl enable fleet-agent >/dev/null 2>&1
sudo -n systemctl restart fleet-agent
sleep 3
echo -n "status: "; systemctl is-active fleet-agent
OUTER
echo ">> $NODE desplegado"