Merge pull request 'fix: allocate PTY for agent install to work with sudo requiretty' (#38) from feat/push-monitoring-agent into master
Reviewed-on: #38
This commit was merged in pull request #38.
This commit is contained in:
@@ -63,7 +63,7 @@ class AgentService
|
|||||||
return ['success' => false, 'error' => 'SSH connection failed'];
|
return ['success' => false, 'error' => 'SSH connection failed'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $ssh->exec($remoteCommand, 120);
|
$result = $ssh->exec($remoteCommand, 120, true);
|
||||||
$ssh->disconnect();
|
$ssh->disconnect();
|
||||||
|
|
||||||
$output = trim($result['output']);
|
$output = trim($result['output']);
|
||||||
@@ -127,7 +127,7 @@ class AgentService
|
|||||||
return ['success' => false, 'error' => 'SSH connection failed'];
|
return ['success' => false, 'error' => 'SSH connection failed'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $ssh->exec($remoteCommand, 60);
|
$result = $ssh->exec($remoteCommand, 60, true);
|
||||||
$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): array
|
public function exec(string $command, int $timeout = 0, bool $pty = false): 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,10 +83,22 @@ class SSHService
|
|||||||
|
|
||||||
$this->connection->setTimeout($timeout);
|
$this->connection->setTimeout($timeout);
|
||||||
|
|
||||||
$output = $this->connection->exec($command);
|
if ($pty) {
|
||||||
|
$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