From d581fdcdc5712f1bfe69128deb2534249d920de8 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Sun, 27 Oct 2024 11:53:08 +0100 Subject: [PATCH] Refactor SshClient --- contrib/rsync.php | 1 - src/Component/ProcessRunner/Printer.php | 7 ---- src/Deployer.php | 6 ++-- src/Executor/Master.php | 5 +-- src/Logger/Logger.php | 2 -- src/{Component => }/Ssh/IOArguments.php | 2 +- .../Ssh/Client.php => Ssh/SshClient.php} | 35 +++++-------------- src/Utility/Rsync.php | 2 -- .../{Component => }/Ssh/IOArgumentsTest.php | 2 +- 9 files changed, 15 insertions(+), 47 deletions(-) rename src/{Component => }/Ssh/IOArguments.php (98%) rename src/{Component/Ssh/Client.php => Ssh/SshClient.php} (85%) rename tests/src/{Component => }/Ssh/IOArgumentsTest.php (97%) diff --git a/contrib/rsync.php b/contrib/rsync.php index cbdc03628..c76c4cd02 100644 --- a/contrib/rsync.php +++ b/contrib/rsync.php @@ -113,7 +113,6 @@ namespace Deployer; -use Deployer\Component\Ssh\Client; use Deployer\Host\Localhost; use Deployer\Task\Context; diff --git a/src/Component/ProcessRunner/Printer.php b/src/Component/ProcessRunner/Printer.php index cc7967f9b..d256b51dc 100644 --- a/src/Component/ProcessRunner/Printer.php +++ b/src/Component/ProcessRunner/Printer.php @@ -59,8 +59,6 @@ public function printBuffer(string $type, Host $host, string $buffer): void public function writeln(string $type, Host $host, string $line): void { - $line = self::filterOutput($line); - // Omit empty lines if (empty($line)) { return; @@ -68,9 +66,4 @@ public function writeln(string $type, Host $host, string $line): void $this->output->writeln("[$host] $line"); } - - public static function filterOutput(string $output): string - { - return preg_replace('/\[exit_code:(.*?)]/', '', $output); - } } diff --git a/src/Deployer.php b/src/Deployer.php index fe3f4799c..d477d5735 100755 --- a/src/Deployer.php +++ b/src/Deployer.php @@ -24,7 +24,7 @@ use Deployer\Component\Pimple\Container; use Deployer\Component\ProcessRunner\Printer; use Deployer\Component\ProcessRunner\ProcessRunner; -use Deployer\Component\Ssh\Client; +use Deployer\Ssh\SshClient; use Deployer\Configuration\Configuration; use Deployer\Executor\Master; use Deployer\Executor\Messenger; @@ -58,7 +58,7 @@ * @property HostCollection|Host[] $hosts * @property Configuration $config * @property Rsync $rsync - * @property Client $sshClient + * @property SshClient $sshClient * @property ProcessRunner $processRunner * @property Task\ScriptManager $scriptManager * @property Selector $selector @@ -128,7 +128,7 @@ public function __construct(Application $console) return new Printer($c['output']); }; $this['sshClient'] = function ($c) { - return new Client($c['output'], $c['pop'], $c['logger']); + return new SshClient($c['output'], $c['pop'], $c['logger']); }; $this['rsync'] = function ($c) { return new Rsync($c['pop'], $c['output']); diff --git a/src/Executor/Master.php b/src/Executor/Master.php index a2e4b9a52..ff7b789cc 100644 --- a/src/Executor/Master.php +++ b/src/Executor/Master.php @@ -10,14 +10,11 @@ namespace Deployer\Executor; -use Deployer\Component\Ssh\Client; -use Deployer\Component\Ssh\IOArguments; use Deployer\Deployer; -use Deployer\Exception\Exception; use Deployer\Host\Host; use Deployer\Host\HostCollection; -use Deployer\Host\Localhost; use Deployer\Selector\Selector; +use Deployer\Ssh\IOArguments; use Deployer\Task\Context; use Deployer\Task\Task; use Symfony\Component\Console\Input\InputInterface; diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php index 86567e9e0..8fd064a7e 100644 --- a/src/Logger/Logger.php +++ b/src/Logger/Logger.php @@ -47,8 +47,6 @@ public function printBuffer(Host $host, string $type, string $buffer): void public function writeln(Host $host, string $type, string $line): void { - $line = Printer::filterOutput($line); - // Omit empty lines if (empty($line)) { return; diff --git a/src/Component/Ssh/IOArguments.php b/src/Ssh/IOArguments.php similarity index 98% rename from src/Component/Ssh/IOArguments.php rename to src/Ssh/IOArguments.php index d492caa63..252d53bb4 100644 --- a/src/Component/Ssh/IOArguments.php +++ b/src/Ssh/IOArguments.php @@ -8,7 +8,7 @@ * file that was distributed with this source code. */ -namespace Deployer\Component\Ssh; +namespace Deployer\Ssh; use Deployer\Exception\Exception; use Symfony\Component\Console\Input\InputInterface; diff --git a/src/Component/Ssh/Client.php b/src/Ssh/SshClient.php similarity index 85% rename from src/Component/Ssh/Client.php rename to src/Ssh/SshClient.php index 358686fae..eb3747d5f 100644 --- a/src/Component/Ssh/Client.php +++ b/src/Ssh/SshClient.php @@ -8,10 +8,9 @@ * file that was distributed with this source code. */ -namespace Deployer\Component\Ssh; +namespace Deployer\Ssh; use Deployer\Component\ProcessRunner\Printer; -use Deployer\Exception\Exception; use Deployer\Exception\RunException; use Deployer\Exception\TimeoutException; use Deployer\Host\Host; @@ -20,24 +19,11 @@ use Symfony\Component\Process\Exception\ProcessTimedOutException; use Symfony\Component\Process\Process; -use function Deployer\Support\parse_home_dir; - -class Client +class SshClient { - /** - * @var OutputInterface - */ - private $output; - - /** - * @var Printer - */ - private $pop; - - /** - * @var Logger - */ - private $logger; + private OutputInterface $output; + private Printer $pop; + private Logger $logger; public function __construct(OutputInterface $output, Printer $pop, Logger $logger) { @@ -46,9 +32,6 @@ public function __construct(OutputInterface $output, Printer $pop, Logger $logge $this->logger = $logger; } - /** - * @throws RunException|TimeoutException|Exception - */ public function run(Host $host, string $command, array $config = []): string { $defaults = [ @@ -59,7 +42,7 @@ public function run(Host $host, string $command, array $config = []): string ]; $config = array_merge($defaults, $config); - $shellId = bin2hex(random_bytes(10)); + $shellId = 'id$' . bin2hex(random_bytes(10)); $shellCommand = $host->getShell(); if ($host->has('become') && !empty($host->get('become'))) { $shellCommand = "sudo -H -u {$host->get('become')} " . $shellCommand; @@ -84,7 +67,7 @@ public function run(Host $host, string $command, array $config = []): string $process = new Process($ssh); $process - ->setInput("( $command ); printf '[exit_code:%s]' $?;") + ->setInput($command) ->setTimeout((null === $config['timeout']) ? null : (float) $config['timeout']) ->setIdleTimeout((null === $config['idle_timeout']) ? null : (float) $config['idle_timeout']); @@ -106,8 +89,8 @@ public function run(Host $host, string $command, array $config = []): string ); } - $output = $this->pop->filterOutput($process->getOutput()); - $exitCode = $this->parseExitStatus($process); + $output = $process->getOutput(); + $exitCode = $process->getExitCode(); if ($exitCode !== 0 && !$config['no_throw']) { throw new RunException( diff --git a/src/Utility/Rsync.php b/src/Utility/Rsync.php index e24538d38..e8f048c66 100644 --- a/src/Utility/Rsync.php +++ b/src/Utility/Rsync.php @@ -11,14 +11,12 @@ namespace Deployer\Utility; use Deployer\Component\ProcessRunner\Printer; -use Deployer\Component\Ssh\Client; use Deployer\Exception\RunException; use Deployer\Host\Host; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; - use function Deployer\writeln; class Rsync diff --git a/tests/src/Component/Ssh/IOArgumentsTest.php b/tests/src/Ssh/IOArgumentsTest.php similarity index 97% rename from tests/src/Component/Ssh/IOArgumentsTest.php rename to tests/src/Ssh/IOArgumentsTest.php index 61d039e61..45b220e34 100644 --- a/tests/src/Component/Ssh/IOArgumentsTest.php +++ b/tests/src/Ssh/IOArgumentsTest.php @@ -1,6 +1,6 @@