Merge pull request 'fix: use script command for PTY instead of modifying SSHService' (#39) from feat/push-monitoring-agent into master

Reviewed-on: #39
This commit was merged in pull request #39.
This commit is contained in:
2026-06-11 19:02:34 -04:00
3 changed files with 16 additions and 20 deletions

View File

@@ -21,6 +21,7 @@ echo "[1/5] Creating config directory..."
mkdir -p "$CONFIG_DIR"
echo "[2/5] Installing agent binary..."
mkdir -p "$(dirname "$AGENT_BIN")"
cat > "$AGENT_BIN" << 'AGENTSCRIPT'
#!/bin/bash
set -e
@@ -49,7 +50,6 @@ collect_metrics() {
}
push_metrics() {
local payload
payload=$(cat <<EOF
{
"agent_key": "$AGENT_KEY",
@@ -75,7 +75,7 @@ main() {
exit 1
fi
echo "ServerManager Agent started (interval: ${INTERVAL}s)"
sleep 5
sleep 2
while true; do
collect_metrics
http_code=$(push_metrics)
@@ -91,6 +91,12 @@ main
AGENTSCRIPT
chmod +x "$AGENT_BIN"
if [ ! -f "$AGENT_BIN" ]; then
echo "ERROR: Agent binary was not created at $AGENT_BIN"
exit 1
fi
echo " Verified: $AGENT_BIN ($(wc -c < "$AGENT_BIN") bytes)"
echo "[3/5] Writing config..."
cat > "$CONFIG_FILE" << EOF
SERVER_URL="$SERVER_URL"

View File

@@ -51,9 +51,11 @@ class AgentService
$b64 = base64_encode($script);
$remoteCommand = sprintf(
'echo %s | base64 -d | sudo bash -s -- %s %s 2>&1',
'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',
escapeshellarg($b64),
escapeshellarg($serverUrl),
escapeshellarg($agentKey),
escapeshellarg($serverUrl),
escapeshellarg($agentKey)
);
@@ -63,7 +65,7 @@ class AgentService
return ['success' => false, 'error' => 'SSH connection failed'];
}
$result = $ssh->exec($remoteCommand, 120, true);
$result = $ssh->exec($remoteCommand, 120);
$ssh->disconnect();
$output = trim($result['output']);
@@ -117,7 +119,7 @@ class AgentService
$script = file_get_contents($scriptPath);
$remoteCommand = sprintf(
'echo %s | base64 -d | sudo bash 2>&1',
'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',
escapeshellarg(base64_encode($script))
);
@@ -127,7 +129,7 @@ class AgentService
return ['success' => false, 'error' => 'SSH connection failed'];
}
$result = $ssh->exec($remoteCommand, 60, true);
$result = $ssh->exec($remoteCommand, 60);
$ssh->disconnect();
$output = trim($result['output']);

View File

@@ -73,7 +73,7 @@ class SSHService
}
}
public function exec(string $command, int $timeout = 0, bool $pty = false): array
public function exec(string $command, int $timeout = 0): array
{
if ($this->connection === null || !$this->connection->isConnected()) {
throw new \RuntimeException('No active SSH connection');
@@ -83,22 +83,10 @@ class SSHService
$this->connection->setTimeout($timeout);
if ($pty) {
$command .= '; echo "___EXIT___:$?"';
}
$output = $this->connection->exec($command, null, $pty);
$output = $this->connection->exec($command);
$exitStatus = $this->connection->getExitStatus();
$stderr = $this->connection->getStdError() ?? '';
if ($pty && ($exitStatus === false || $exitStatus === null)) {
if (preg_match('/___EXIT___:(\d+)/', $output, $m)) {
$exitStatus = (int) $m[1];
$output = str_replace($m[0] . "\n", '', $output);
$output = str_replace($m[0], '', $output);
}
}
$this->logger->info("SSH command executed", [
'command' => $command,
'exit_status' => $exitStatus,