fix: use script command for PTY instead of modifying SSHService #39
@@ -21,6 +21,7 @@ echo "[1/5] Creating config directory..."
|
|||||||
mkdir -p "$CONFIG_DIR"
|
mkdir -p "$CONFIG_DIR"
|
||||||
|
|
||||||
echo "[2/5] Installing agent binary..."
|
echo "[2/5] Installing agent binary..."
|
||||||
|
mkdir -p "$(dirname "$AGENT_BIN")"
|
||||||
cat > "$AGENT_BIN" << 'AGENTSCRIPT'
|
cat > "$AGENT_BIN" << 'AGENTSCRIPT'
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
@@ -49,7 +50,6 @@ collect_metrics() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
push_metrics() {
|
push_metrics() {
|
||||||
local payload
|
|
||||||
payload=$(cat <<EOF
|
payload=$(cat <<EOF
|
||||||
{
|
{
|
||||||
"agent_key": "$AGENT_KEY",
|
"agent_key": "$AGENT_KEY",
|
||||||
@@ -75,7 +75,7 @@ main() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "ServerManager Agent started (interval: ${INTERVAL}s)"
|
echo "ServerManager Agent started (interval: ${INTERVAL}s)"
|
||||||
sleep 5
|
sleep 2
|
||||||
while true; do
|
while true; do
|
||||||
collect_metrics
|
collect_metrics
|
||||||
http_code=$(push_metrics)
|
http_code=$(push_metrics)
|
||||||
@@ -91,6 +91,12 @@ main
|
|||||||
AGENTSCRIPT
|
AGENTSCRIPT
|
||||||
chmod +x "$AGENT_BIN"
|
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..."
|
echo "[3/5] Writing config..."
|
||||||
cat > "$CONFIG_FILE" << EOF
|
cat > "$CONFIG_FILE" << EOF
|
||||||
SERVER_URL="$SERVER_URL"
|
SERVER_URL="$SERVER_URL"
|
||||||
|
|||||||
@@ -51,9 +51,11 @@ class AgentService
|
|||||||
|
|
||||||
$b64 = base64_encode($script);
|
$b64 = base64_encode($script);
|
||||||
$remoteCommand = sprintf(
|
$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($b64),
|
||||||
escapeshellarg($serverUrl),
|
escapeshellarg($serverUrl),
|
||||||
|
escapeshellarg($agentKey),
|
||||||
|
escapeshellarg($serverUrl),
|
||||||
escapeshellarg($agentKey)
|
escapeshellarg($agentKey)
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -63,7 +65,7 @@ class AgentService
|
|||||||
return ['success' => false, 'error' => 'SSH connection failed'];
|
return ['success' => false, 'error' => 'SSH connection failed'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $ssh->exec($remoteCommand, 120, true);
|
$result = $ssh->exec($remoteCommand, 120);
|
||||||
$ssh->disconnect();
|
$ssh->disconnect();
|
||||||
|
|
||||||
$output = trim($result['output']);
|
$output = trim($result['output']);
|
||||||
@@ -117,7 +119,7 @@ class AgentService
|
|||||||
$script = file_get_contents($scriptPath);
|
$script = file_get_contents($scriptPath);
|
||||||
|
|
||||||
$remoteCommand = sprintf(
|
$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))
|
escapeshellarg(base64_encode($script))
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -127,7 +129,7 @@ class AgentService
|
|||||||
return ['success' => false, 'error' => 'SSH connection failed'];
|
return ['success' => false, 'error' => 'SSH connection failed'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $ssh->exec($remoteCommand, 60, true);
|
$result = $ssh->exec($remoteCommand, 60);
|
||||||
$ssh->disconnect();
|
$ssh->disconnect();
|
||||||
|
|
||||||
$output = trim($result['output']);
|
$output = trim($result['output']);
|
||||||
|
|||||||
@@ -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()) {
|
if ($this->connection === null || !$this->connection->isConnected()) {
|
||||||
throw new \RuntimeException('No active SSH connection');
|
throw new \RuntimeException('No active SSH connection');
|
||||||
@@ -83,22 +83,10 @@ class SSHService
|
|||||||
|
|
||||||
$this->connection->setTimeout($timeout);
|
$this->connection->setTimeout($timeout);
|
||||||
|
|
||||||
if ($pty) {
|
$output = $this->connection->exec($command);
|
||||||
$command .= '; echo "___EXIT___:$?"';
|
|
||||||
}
|
|
||||||
|
|
||||||
$output = $this->connection->exec($command, null, $pty);
|
|
||||||
$exitStatus = $this->connection->getExitStatus();
|
$exitStatus = $this->connection->getExitStatus();
|
||||||
$stderr = $this->connection->getStdError() ?? '';
|
$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", [
|
$this->logger->info("SSH command executed", [
|
||||||
'command' => $command,
|
'command' => $command,
|
||||||
'exit_status' => $exitStatus,
|
'exit_status' => $exitStatus,
|
||||||
|
|||||||
Reference in New Issue
Block a user