diff --git a/install/agent-install.sh b/install/agent-install.sh index 6706f89..8d44552 100644 --- a/install/agent-install.sh +++ b/install/agent-install.sh @@ -1,7 +1,6 @@ #!/bin/bash # ServerManager Agent Installer # Usage: bash agent-install.sh [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" diff --git a/install/agent-uninstall.sh b/install/agent-uninstall.sh index e260733..b054494 100644 --- a/install/agent-uninstall.sh +++ b/install/agent-uninstall.sh @@ -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" diff --git a/src/Services/AgentService.php b/src/Services/AgentService.php index c8f4469..69095f3 100644 --- a/src/Services/AgentService.php +++ b/src/Services/AgentService.php @@ -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)) );