install-agent.sh: rewrite for agent v3.0 config format

Old installer wrote to /opt/jarvis-agent/config.json with server_url/api_key/
heartbeat_interval keys and pre-registered with JARVIS. Agent v3.0 expects:
- Config at /etc/jarvis-agent/config.json with jarvis_url/registration_key/
  hostname/poll_interval/heartbeat_every keys
- api_key stored by agent itself in /var/lib/jarvis-agent/state.json
- Agent self-registers at startup using registration_key

Also adds: imagemagick install (headless screenshot support), apk support
for Alpine/WireGuard, copies to /usr/local/bin/jarvis-agent.py.
This commit is contained in:
2026-06-11 21:33:53 +00:00
parent 950749323c
commit c2c7a2627a
2 changed files with 98 additions and 117 deletions
+51 -51
View File
@@ -7,69 +7,65 @@
set -e
HOSTNAME="${1:-$(hostname -s)}"
HOSTNAME_ARG="${1:-$(hostname -s)}"
AGENT_TYPE="${2:-linux}"
JARVIS_URL="https://165.22.1.228"
JARVIS_HOST="jarvis.orbishosting.com"
INSTALL_DIR="/opt/jarvis-agent"
CONFIG_DIR="/etc/jarvis-agent"
STATE_DIR="/var/lib/jarvis-agent"
REG_KEY="f846a9aaf7ce9a61742c63c87c4186052a71d2a580c65518"
SERVICE_FILE="/etc/systemd/system/jarvis-agent.service"
echo "=== JARVIS Agent Installer ==="
echo "Host: $HOSTNAME | Type: $AGENT_TYPE | Server: $JARVIS_URL"
echo "=== JARVIS Agent Installer v3.0 ==="
echo "Host: $HOSTNAME_ARG | Type: $AGENT_TYPE | Server: $JARVIS_URL"
# ── Dependencies ──────────────────────────────────────────────────────────────
if command -v apt-get &>/dev/null; then
apt-get install -yq python3 python3-pip curl 2>/dev/null
# Prefer apt packages to avoid externally-managed-environment errors
apt-get install -yq python3 curl imagemagick 2>/dev/null || true
apt-get install -yq python3-psutil python3-requests 2>/dev/null || true
elif command -v yum &>/dev/null; then
yum install -yq python3 python3-pip curl 2>/dev/null
yum install -yq python3 curl ImageMagick 2>/dev/null || true
elif command -v apk &>/dev/null; then
apk add --no-cache python3 curl imagemagick 2>/dev/null || true
fi
# Install via pip only if apt didn't get them (TrueNAS, non-Debian, etc.)
python3 -c "import psutil, requests" 2>/dev/null || \
pip3 install -q --break-system-packages requests psutil 2>/dev/null || \
pip3 install -q requests psutil 2>/dev/null || \
pip install -q requests psutil 2>/dev/null || true
# pip fallback if psutil/requests not available via package manager
python3 -c "import psutil, requests" 2>/dev/null || pip3 install -q --break-system-packages requests psutil 2>/dev/null || pip3 install -q requests psutil 2>/dev/null || true
# ── Create directories ─────────────────────────────────────────────────────────
mkdir -p "$INSTALL_DIR" "$CONFIG_DIR" "$STATE_DIR"
# ── Download agent ─────────────────────────────────────────────────────────────
mkdir -p "$INSTALL_DIR"
echo "Downloading agent..."
curl -sk -H "Host: $JARVIS_HOST" "$JARVIS_URL/agent/jarvis-agent.py" -o "$INSTALL_DIR/jarvis-agent.py"
chmod +x "$INSTALL_DIR/jarvis-agent.py"
cp "$INSTALL_DIR/jarvis-agent.py" /usr/local/bin/jarvis-agent.py
chmod +x "$INSTALL_DIR/jarvis-agent.py" /usr/local/bin/jarvis-agent.py
# ── Register with JARVIS to get API key ───────────────────────────────────────
IP=$(hostname -I | awk '{print $1}')
REG=$(curl -sk -H "Host: $JARVIS_HOST" \
-H "Content-Type: application/json" \
-H "X-Registration-Key: f846a9aaf7ce9a61742c63c87c4186052a71d2a580c65518" \
-X POST "$JARVIS_URL/api/agent/register" \
-d "{\"hostname\":\"$HOSTNAME\",\"agent_type\":\"$AGENT_TYPE\",\"ip_address\":\"$IP\",\"capabilities\":[\"metrics\",\"commands\"]}")
AGENT_ID=$(echo "$REG" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d['agent_id'])" 2>/dev/null)
API_KEY=$(echo "$REG" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d['api_key'])" 2>/dev/null)
if [ -z "$API_KEY" ]; then
echo "ERROR: Registration failed — $REG"
exit 1
# ── Write config (skip if already exists) ────────────────────────────────────
if [[ -f "$CONFIG_DIR/config.json" ]]; then
echo "Config already exists at $CONFIG_DIR/config.json — keeping existing settings."
else
cat > "$CONFIG_DIR/config.json" << JSONEOF
{
"jarvis_url": "$JARVIS_URL",
"host_header": "$JARVIS_HOST",
"ssl_verify": false,
"registration_key": "$REG_KEY",
"hostname": "$HOSTNAME_ARG",
"agent_type": "$AGENT_TYPE",
"poll_interval": 30,
"heartbeat_every": 10,
"update_check_hours": 24,
"watch_services": ["ollama", "homeassistant", "mysql", "mariadb", "nginx", "apache2", "docker"]
}
JSONEOF
chmod 600 "$CONFIG_DIR/config.json"
echo "Config written to $CONFIG_DIR/config.json"
fi
echo "Registered: agent_id=$AGENT_ID"
# ── Write config ───────────────────────────────────────────────────────────────
cat > "$INSTALL_DIR/config.json" << EOF
{
"server_url": "$JARVIS_URL",
"host_header": "$JARVIS_HOST",
"agent_id": "$AGENT_ID",
"api_key": "$API_KEY",
"agent_type": "$AGENT_TYPE",
"heartbeat_interval": 10,
"metrics_interval": 30
}
EOF
# ── Systemd service ────────────────────────────────────────────────────────────
cat > "$SERVICE_FILE" << EOF
cat > "$SERVICE_FILE" << SVCEOF
[Unit]
Description=JARVIS Agent
After=network-online.target
@@ -78,21 +74,25 @@ Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 $INSTALL_DIR/jarvis-agent.py
WorkingDirectory=$INSTALL_DIR
Restart=always
RestartSec=10
StartLimitInterval=60
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
EOF
SVCEOF
systemctl daemon-reload
systemctl enable jarvis-agent
systemctl restart jarvis-agent
echo "=== JARVIS Agent installed and running ==="
echo "Config: $INSTALL_DIR/config.json"
echo "Logs: journalctl -u jarvis-agent -f"
systemctl is-active jarvis-agent
sleep 2
if systemctl is-active --quiet jarvis-agent; then
echo ""
echo "=== JARVIS Agent v3.0 installed and running ==="
echo "Config: $CONFIG_DIR/config.json"
echo "State: $STATE_DIR/state.json (created on first run)"
echo "Logs: journalctl -u jarvis-agent -f"
else
echo ""
echo "WARNING: Agent installed but not running. Check: journalctl -u jarvis-agent -n 30"
fi