fix: installer self-elevates with script PTY instead of external sudo chain

- agent-install.sh and agent-uninstall.sh now detect if running as root
  at startup and re-execute via 'script -qc sudo bash' if needed
- AgentService commands simplified: just write to /tmp and run bash,
  the scripts handle privilege escalation internally
- Fixes exit code propagation: the script's exit code now correctly
  flows back through exec -> script -> sudo -> bash -> SSH
This commit is contained in:
2026-06-11 19:07:14 -04:00
parent 0ce24c4b8f
commit 0edc882f2e
3 changed files with 18 additions and 6 deletions

View File

@@ -1,7 +1,6 @@
#!/bin/bash
# ServerManager Agent Installer
# Usage: bash agent-install.sh <SERVER_URL> <AGENT_KEY> [INTERVAL]
set -e
SERVER_URL="${1:-}"
AGENT_KEY="${2:-}"
@@ -12,6 +11,14 @@ 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" "$@"
fi
AGENT_BIN="/usr/local/bin/servermanager-agent"
CONFIG_DIR="/etc/servermanager"
CONFIG_FILE="$CONFIG_DIR/agent.conf"

View File

@@ -1,6 +1,13 @@
#!/bin/bash
# ServerManager Agent Uninstaller
set -e
# 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"
fi
AGENT_BIN="/usr/local/bin/servermanager-agent"
CONFIG_DIR="/etc/servermanager"

View File

@@ -51,11 +51,9 @@ class AgentService
$b64 = base64_encode($script);
$remoteCommand = sprintf(
'echo %s | base64 -d > /tmp/_sm_install.sh && chmod +x /tmp/_sm_install.sh && (script -q -c "sudo bash /tmp/_sm_install.sh %s %s" /dev/null < /dev/null 2>/dev/null || sudo bash /tmp/_sm_install.sh %s %s) 2>&1; rc=$?; rm -f /tmp/_sm_install.sh; exit $rc',
'echo %s | base64 -d > /tmp/_sm_install.sh 2>/dev/null && chmod +x /tmp/_sm_install.sh && bash /tmp/_sm_install.sh %s %s 2>&1; rc=$?; rm -f /tmp/_sm_install.sh 2>/dev/null; exit $rc',
escapeshellarg($b64),
escapeshellarg($serverUrl),
escapeshellarg($agentKey),
escapeshellarg($serverUrl),
escapeshellarg($agentKey)
);
@@ -119,7 +117,7 @@ class AgentService
$script = file_get_contents($scriptPath);
$remoteCommand = sprintf(
'echo %s | base64 -d > /tmp/_sm_uninstall.sh && chmod +x /tmp/_sm_uninstall.sh && (script -q -c "sudo bash /tmp/_sm_uninstall.sh" /dev/null < /dev/null 2>/dev/null || sudo bash /tmp/_sm_uninstall.sh) 2>&1; rc=$?; rm -f /tmp/_sm_uninstall.sh; exit $rc',
'echo %s | base64 -d > /tmp/_sm_uninstall.sh 2>/dev/null && chmod +x /tmp/_sm_uninstall.sh && bash /tmp/_sm_uninstall.sh 2>&1; rc=$?; rm -f /tmp/_sm_uninstall.sh 2>/dev/null; exit $rc',
escapeshellarg(base64_encode($script))
);