1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer\Ssh; |
12
|
|
|
|
13
|
|
|
use Deployer\Component\ProcessRunner\Printer; |
14
|
|
|
use Deployer\Exception\RunException; |
15
|
|
|
use Deployer\Exception\TimeoutException; |
16
|
|
|
use Deployer\Host\Host; |
17
|
|
|
use Deployer\Logger\Logger; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Process\Exception\ProcessTimedOutException; |
20
|
|
|
use Symfony\Component\Process\Process; |
21
|
|
|
|
22
|
|
|
class SshClient |
23
|
|
|
{ |
24
|
|
|
private OutputInterface $output; |
25
|
|
|
private Printer $pop; |
26
|
|
|
private Logger $logger; |
27
|
|
|
|
28
|
|
|
public function __construct(OutputInterface $output, Printer $pop, Logger $logger) |
29
|
|
|
{ |
30
|
|
|
$this->output = $output; |
31
|
|
|
$this->pop = $pop; |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function run(Host $host, string $command, array $config = []): string |
36
|
|
|
{ |
37
|
|
|
$defaults = [ |
38
|
|
|
'timeout' => $host->get('default_timeout', 300), |
39
|
|
|
'idle_timeout' => null, |
40
|
|
|
'real_time_output' => false, |
41
|
|
|
'no_throw' => false, |
42
|
|
|
]; |
43
|
|
|
$config = array_merge($defaults, $config); |
44
|
|
|
|
45
|
|
|
$shellId = 'id$' . bin2hex(random_bytes(10)); |
46
|
|
|
$shellCommand = $host->getShell(); |
47
|
|
|
if ($host->has('become') && !empty($host->get('become'))) { |
48
|
|
|
$shellCommand = "sudo -H -u {$host->get('become')} " . $shellCommand; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$ssh = array_merge(['ssh'], $host->connectionOptionsArray(), [$host->connectionString(), ": $shellId; $shellCommand"]); |
52
|
|
|
|
53
|
|
|
// -vvv for ssh command |
54
|
|
|
if ($this->output->isDebug()) { |
55
|
|
|
$sshString = $ssh[0]; |
56
|
|
|
for ($i = 1; $i < count($ssh); $i++) { |
|
|
|
|
57
|
|
|
$sshString .= ' ' . escapeshellarg((string) $ssh[$i]); |
58
|
|
|
} |
59
|
|
|
$this->output->writeln("[$host] $sshString"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->pop->command($host, 'run', $command); |
63
|
|
|
$this->logger->log("[{$host->getAlias()}] run $command"); |
64
|
|
|
|
65
|
|
|
$command = str_replace('%secret%', strval($config['secret'] ?? ''), $command); |
66
|
|
|
$command = str_replace('%sudo_pass%', strval($config['sudo_pass'] ?? ''), $command); |
67
|
|
|
|
68
|
|
|
$process = new Process($ssh); |
69
|
|
|
$process |
70
|
|
|
->setInput($command) |
71
|
|
|
->setTimeout((null === $config['timeout']) ? null : (float) $config['timeout']) |
72
|
|
|
->setIdleTimeout((null === $config['idle_timeout']) ? null : (float) $config['idle_timeout']); |
73
|
|
|
|
74
|
|
|
$callback = function ($type, $buffer) use ($config, $host) { |
75
|
|
|
$this->logger->printBuffer($host, $type, $buffer); |
76
|
|
|
$this->pop->callback($host, boolval($config['real_time_output']))($type, $buffer); |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$process->run($callback); |
81
|
|
|
} catch (ProcessTimedOutException $exception) { |
82
|
|
|
// Let's try to kill all processes started by this command. |
83
|
|
|
$pid = $this->run($host, "ps x | grep $shellId | grep -v grep | awk '{print \$1}'"); |
84
|
|
|
// Minus before pid means all processes in this group. |
85
|
|
|
$this->run($host, "kill -9 -$pid"); |
86
|
|
|
throw new TimeoutException( |
87
|
|
|
$command, |
88
|
|
|
$exception->getExceededTimeout(), |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$output = $process->getOutput(); |
93
|
|
|
$exitCode = $process->getExitCode(); |
94
|
|
|
|
95
|
|
|
if ($exitCode !== 0 && !$config['no_throw']) { |
96
|
|
|
throw new RunException( |
97
|
|
|
$host, |
98
|
|
|
$command, |
99
|
|
|
$exitCode, |
|
|
|
|
100
|
|
|
$output, |
101
|
|
|
$process->getErrorOutput(), |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $output; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function parseExitStatus(Process $process): int |
109
|
|
|
{ |
110
|
|
|
preg_match('/\[exit_code:(\d*)]/', $process->getOutput(), $match); |
111
|
|
|
return (int) ($match[1] ?? -1); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: