#!/usr/bin/env bash set -euo pipefail if [ "$(id -u)" -ne 0 ]; then echo "Run this installer with sudo or as root." >&2 exit 1 fi MONITOR_URL="${MONITOR_URL:-${1:-}}" SERVER_ID="${SERVER_ID:-$(hostname -s)}" SERVER_NAME="${SERVER_NAME:-$(hostname -f 2>/dev/null || hostname)}" SERVER_ENV="${SERVER_ENV:-production}" AGENT_TOKEN="${AGENT_TOKEN:-}" INTERVAL="${INTERVAL:-60}" INSTALL_BIN="${INSTALL_BIN:-/usr/local/bin/fleet-monitor-agent}" ENV_FILE="${ENV_FILE:-/etc/fleet-monitor-agent.env}" SERVICE_FILE="${SERVICE_FILE:-/etc/systemd/system/fleet-monitor-agent.service}" if [ -z "$MONITOR_URL" ]; then echo "Usage: MONITOR_URL=https://watchtower.1exch.app AGENT_TOKEN=your-token sudo -E bash install-agent.sh" >&2 echo " or: sudo AGENT_TOKEN=your-token bash install-agent.sh https://watchtower.1exch.app" >&2 exit 1 fi require_command() { if ! command -v "$1" >/dev/null 2>&1; then echo "Missing required command: $1" >&2 exit 1 fi } escape_env() { printf "%s" "$1" | sed "s/'/'\\\\''/g" } require_command curl require_command systemctl echo "Installing WatchTower agent from $MONITOR_URL" curl -fsS "$MONITOR_URL/downloads/linux-agent.sh" -o "$INSTALL_BIN" chmod 0755 "$INSTALL_BIN" curl -fsS "$MONITOR_URL/downloads/linux-agent.service" -o "$SERVICE_FILE" chmod 0644 "$SERVICE_FILE" cat > "$ENV_FILE" <