fix: dual-mode installer — works with or without sudo #41
@@ -11,28 +11,49 @@ if [ -z "$SERVER_URL" ] || [ -z "$AGENT_KEY" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Self-elevate if not running as root
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
if command -v script >/dev/null 2>&1; then
|
||||
exec script -q -c "sudo bash '$0' '$1' '$2' '$3'" /dev/null
|
||||
fi
|
||||
exec sudo bash "$0" "$@"
|
||||
# Detect if we can run as root (try non-interactive sudo)
|
||||
CAN_ROOT=false
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
CAN_ROOT=true
|
||||
elif command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; then
|
||||
CAN_ROOT=true
|
||||
fi
|
||||
|
||||
AGENT_BIN="/usr/local/bin/servermanager-agent"
|
||||
CONFIG_DIR="/etc/servermanager"
|
||||
CONFIG_FILE="$CONFIG_DIR/agent.conf"
|
||||
SERVICE_FILE="/etc/systemd/system/servermanager-agent.service"
|
||||
if [ "$CAN_ROOT" = true ]; then
|
||||
# ── Root/system mode ──
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
# Re-exec with script PTY if available (handles requiretty)
|
||||
if command -v script >/dev/null 2>&1; then
|
||||
exec script -q -c "sudo bash '$0' '$1' '$2' '$3'" /dev/null
|
||||
fi
|
||||
exec sudo bash "$0" "$@"
|
||||
fi
|
||||
|
||||
echo "[1/5] Creating config directory..."
|
||||
AGENT_BIN="/usr/local/bin/servermanager-agent"
|
||||
CONFIG_DIR="/etc/servermanager"
|
||||
CONFIG_FILE="$CONFIG_DIR/agent.conf"
|
||||
SERVICE_FILE="/etc/systemd/system/servermanager-agent.service"
|
||||
USE_SYSTEMD=true
|
||||
INSTALL_MODE="system"
|
||||
else
|
||||
# ── User/no-root mode ──
|
||||
AGENT_BIN="$HOME/.local/bin/servermanager-agent"
|
||||
CONFIG_DIR="$HOME/.config/servermanager"
|
||||
CONFIG_FILE="$CONFIG_DIR/agent.conf"
|
||||
USE_SYSTEMD=false
|
||||
INSTALL_MODE="user"
|
||||
fi
|
||||
|
||||
echo "[1/4] Creating directories..."
|
||||
mkdir -p "$(dirname "$AGENT_BIN")"
|
||||
mkdir -p "$CONFIG_DIR"
|
||||
|
||||
echo "[2/5] Installing agent binary..."
|
||||
mkdir -p "$(dirname "$AGENT_BIN")"
|
||||
echo "[2/4] Installing agent binary..."
|
||||
cat > "$AGENT_BIN" << 'AGENTSCRIPT'
|
||||
#!/bin/bash
|
||||
set -e
|
||||
CONFIG_FILE="/etc/servermanager/agent.conf"
|
||||
|
||||
CONFIG_FILE="${1:-$HOME/.config/servermanager/agent.conf}"
|
||||
|
||||
load_config() {
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
@@ -75,10 +96,10 @@ EOF
|
||||
-o /dev/null -w "%{http_code}" 2>/dev/null || echo "000"
|
||||
}
|
||||
|
||||
main() {
|
||||
run() {
|
||||
load_config
|
||||
if [ -z "$SERVER_URL" ] || [ -z "$AGENT_KEY" ]; then
|
||||
echo "ERROR: SERVER_URL and AGENT_KEY must be set in $CONFIG_FILE"
|
||||
echo "ERROR: SERVER_URL and AGENT_KEY not configured"
|
||||
exit 1
|
||||
fi
|
||||
echo "ServerManager Agent started (interval: ${INTERVAL}s)"
|
||||
@@ -86,25 +107,22 @@ main() {
|
||||
while true; do
|
||||
collect_metrics
|
||||
http_code=$(push_metrics)
|
||||
if [ "$http_code" = "200" ]; then
|
||||
echo "[$(date)] Metrics pushed successfully"
|
||||
else
|
||||
echo "[$(date)] Failed to push metrics (HTTP $http_code)"
|
||||
fi
|
||||
echo "[$(date)] HTTP $http_code"
|
||||
sleep "$INTERVAL"
|
||||
done
|
||||
}
|
||||
main
|
||||
|
||||
run
|
||||
AGENTSCRIPT
|
||||
chmod +x "$AGENT_BIN"
|
||||
|
||||
if [ ! -f "$AGENT_BIN" ]; then
|
||||
echo "ERROR: Agent binary was not created at $AGENT_BIN"
|
||||
echo "ERROR: Agent binary was not created"
|
||||
exit 1
|
||||
fi
|
||||
echo " Verified: $AGENT_BIN ($(wc -c < "$AGENT_BIN") bytes)"
|
||||
echo " Binary: $AGENT_BIN ($(wc -c < "$AGENT_BIN") bytes)"
|
||||
|
||||
echo "[3/5] Writing config..."
|
||||
echo "[3/4] Writing config..."
|
||||
cat > "$CONFIG_FILE" << EOF
|
||||
SERVER_URL="$SERVER_URL"
|
||||
AGENT_KEY="$AGENT_KEY"
|
||||
@@ -112,38 +130,37 @@ INTERVAL=$INTERVAL
|
||||
EOF
|
||||
chmod 600 "$CONFIG_FILE"
|
||||
|
||||
echo "[4/5] Installing systemd service..."
|
||||
cat > "$SERVICE_FILE" << 'UNIT'
|
||||
echo "[4/4] Enabling auto-start..."
|
||||
|
||||
if [ "$USE_SYSTEMD" = true ]; then
|
||||
echo " Using systemd service..."
|
||||
cat > "$SERVICE_FILE" << 'UNIT'
|
||||
[Unit]
|
||||
Description=ServerManager Monitoring Agent
|
||||
Documentation=https://github.com/servermanager
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/local/bin/servermanager-agent
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
StartLimitIntervalSec=60
|
||||
StartLimitBurst=5
|
||||
NoNewPrivileges=yes
|
||||
ProtectSystem=full
|
||||
ProtectHome=yes
|
||||
PrivateDevices=yes
|
||||
PrivateTmp=yes
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
UNIT
|
||||
|
||||
echo "[5/5] Enabling and starting service..."
|
||||
systemctl daemon-reload
|
||||
systemctl enable servermanager-agent
|
||||
systemctl restart servermanager-agent
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
systemctl enable servermanager-agent 2>/dev/null || true
|
||||
systemctl restart servermanager-agent 2>/dev/null || true
|
||||
else
|
||||
echo " Using crontab (@reboot)..."
|
||||
CRON_JOB="@reboot $AGENT_BIN > $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 &"
|
||||
}
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "ServerManager Agent installed successfully!"
|
||||
echo " Mode: $INSTALL_MODE"
|
||||
echo " Binary: $AGENT_BIN"
|
||||
echo " Config: $CONFIG_FILE"
|
||||
echo " Service: $SERVICE_FILE"
|
||||
|
||||
@@ -1,29 +1,35 @@
|
||||
#!/bin/bash
|
||||
# ServerManager Agent Uninstaller
|
||||
|
||||
# Self-elevate if not running as root
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
if command -v script >/dev/null 2>&1; then
|
||||
exec script -q -c "sudo bash '$0'" /dev/null
|
||||
fi
|
||||
exec sudo bash "$0"
|
||||
# Try system paths first, fall back to user paths
|
||||
if [ -f /usr/local/bin/servermanager-agent ]; then
|
||||
echo "[1/3] Stopping and disabling service..."
|
||||
systemctl stop servermanager-agent 2>/dev/null || true
|
||||
systemctl disable servermanager-agent 2>/dev/null || true
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
|
||||
echo "[2/3] Removing system files..."
|
||||
rm -f /usr/local/bin/servermanager-agent 2>/dev/null || true
|
||||
rm -rf /etc/servermanager 2>/dev/null || true
|
||||
rm -f /etc/systemd/system/servermanager-agent.service 2>/dev/null || true
|
||||
|
||||
echo "[3/3] Reloading systemd..."
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
elif [ -f "$HOME/.local/bin/servermanager-agent" ]; then
|
||||
echo "[1/2] Removing cron job..."
|
||||
crontab -l 2>/dev/null | grep -v 'servermanager-agent' | crontab - 2>/dev/null || true
|
||||
|
||||
echo "[2/2] Removing user files..."
|
||||
rm -f "$HOME/.local/bin/servermanager-agent" 2>/dev/null || true
|
||||
rm -rf "$HOME/.config/servermanager" 2>/dev/null || true
|
||||
else
|
||||
echo "Agent is not installed."
|
||||
# Still try to clean up any leftover system files
|
||||
rm -f /usr/local/bin/servermanager-agent 2>/dev/null || true
|
||||
rm -rf /etc/servermanager 2>/dev/null || true
|
||||
rm -f /etc/systemd/system/servermanager-agent.service 2>/dev/null || true
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
crontab -l 2>/dev/null | grep -v 'servermanager-agent' | crontab - 2>/dev/null || true
|
||||
fi
|
||||
|
||||
AGENT_BIN="/usr/local/bin/servermanager-agent"
|
||||
CONFIG_DIR="/etc/servermanager"
|
||||
SERVICE_FILE="/etc/systemd/system/servermanager-agent.service"
|
||||
|
||||
echo "[1/3] Stopping and disabling service..."
|
||||
systemctl stop servermanager-agent 2>/dev/null || true
|
||||
systemctl disable servermanager-agent 2>/dev/null || true
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
|
||||
echo "[2/3] Removing files..."
|
||||
rm -f "$AGENT_BIN" 2>/dev/null || true
|
||||
rm -rf "$CONFIG_DIR" 2>/dev/null || true
|
||||
rm -f "$SERVICE_FILE" 2>/dev/null || true
|
||||
|
||||
echo "[3/3] Reloading systemd..."
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
|
||||
echo "ServerManager Agent uninstalled successfully."
|
||||
|
||||
@@ -68,7 +68,6 @@ class AgentService
|
||||
|
||||
$output = trim($result['output']);
|
||||
$exitStatus = $result['exit_status'] ?? -1;
|
||||
$path = '/usr/local/bin/servermanager-agent';
|
||||
|
||||
if ($exitStatus !== 0) {
|
||||
$lastLines = implode("\n", array_slice(explode("\n", $output), -20));
|
||||
@@ -79,6 +78,11 @@ class AgentService
|
||||
];
|
||||
}
|
||||
|
||||
$path = '/usr/local/bin/servermanager-agent';
|
||||
if (preg_match('/Binary:\s*(\S+)/', $output, $m)) {
|
||||
$path = $m[1];
|
||||
}
|
||||
|
||||
$serverModel = new \ServerManager\Models\Server();
|
||||
$serverModel->setAgentInstalled($serverId, true, $path);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user