Skip to content

Commit

Permalink
Refactor SshClient
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Oct 27, 2024
1 parent 38eb101 commit d581fdc
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 47 deletions.
1 change: 0 additions & 1 deletion contrib/rsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@

namespace Deployer;

use Deployer\Component\Ssh\Client;
use Deployer\Host\Localhost;
use Deployer\Task\Context;

Expand Down
7 changes: 0 additions & 7 deletions src/Component/ProcessRunner/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,11 @@ 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;
}

$this->output->writeln("[$host] $line");
}

public static function filterOutput(string $output): string
{
return preg_replace('/\[exit_code:(.*?)]/', '', $output);
}
}
6 changes: 3 additions & 3 deletions src/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']);
Expand Down
5 changes: 1 addition & 4 deletions src/Executor/Master.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 9 additions & 26 deletions src/Component/Ssh/Client.php → src/Ssh/SshClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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 = [
Expand All @@ -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;
Expand All @@ -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']);

Expand All @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions src/Utility/Rsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Deployer\Component\Ssh;
namespace Deployer\Ssh;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArgvInput;
Expand Down

0 comments on commit d581fdc

Please sign in to comment.