fix: agent config search order — 1st arg > /etc/ > ~/.config/

The systemd service runs as root so /root is /root, but config is
written to /etc/servermanager/agent.conf. The agent now tries:
1. Path from command-line argument
2. /etc/servermanager/agent.conf (system mode)
3. $HOME/.config/servermanager/agent.conf (user mode)

Service ExecStart and cron job both pass the config path explicitly.
This commit is contained in:
2026-06-11 19:38:10 -04:00
parent aae01b0a1e
commit 8697f37862

View File

@@ -53,7 +53,14 @@ cat > "$AGENT_BIN" << 'AGENTSCRIPT'
#!/bin/bash
set -e
CONFIG_FILE="${1:-$HOME/.config/servermanager/agent.conf}"
# Config search order: 1st arg > /etc/servermanager/agent.conf > $HOME/.config/servermanager/agent.conf
if [ -n "$1" ] && [ -f "$1" ]; then
CONFIG_FILE="$1"
elif [ -f "/etc/servermanager/agent.conf" ]; then
CONFIG_FILE="/etc/servermanager/agent.conf"
else
CONFIG_FILE="$HOME/.config/servermanager/agent.conf"
fi
load_config() {
if [ -f "$CONFIG_FILE" ]; then
@@ -141,7 +148,7 @@ After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/servermanager-agent
ExecStart=/usr/local/bin/servermanager-agent /etc/servermanager/agent.conf
Restart=always
RestartSec=10
[Install]
@@ -152,7 +159,7 @@ UNIT
systemctl restart servermanager-agent 2>/dev/null || true
else
echo " Using crontab (@reboot)..."
CRON_JOB="@reboot $AGENT_BIN > $CONFIG_DIR/agent.log 2>&1"
CRON_JOB="@reboot $AGENT_BIN $CONFIG_FILE > $CONFIG_DIR/agent.log 2>&1"
(crontab -l 2>/dev/null | grep -v '_sm_agent\|servermanager-agent'; echo "$CRON_JOB") | crontab - 2>/dev/null || {
echo "WARNING: Could not install crontab. Agent must be started manually."
echo " Run: $AGENT_BIN &"