1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Component\Ssh; |
9
|
|
|
|
10
|
|
|
use Deployer\Deployer; |
11
|
|
|
use Deployer\Exception\RunException; |
12
|
|
|
use Deployer\Host\Host; |
13
|
|
|
use Deployer\Component\ProcessRunner\Printer; |
14
|
|
|
use Deployer\Logger\Logger; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Process\Exception\ProcessTimedOutException; |
17
|
|
|
use Symfony\Component\Process\Process; |
18
|
|
|
|
19
|
|
|
class Client |
20
|
|
|
{ |
21
|
|
|
private $output; |
22
|
|
|
private $pop; |
23
|
|
|
private $logger; |
24
|
|
|
|
25
|
10 |
|
public function __construct(OutputInterface $output, Printer $pop, Logger $logger) |
26
|
|
|
{ |
27
|
10 |
|
$this->output = $output; |
28
|
10 |
|
$this->pop = $pop; |
29
|
10 |
|
$this->logger = $logger; |
30
|
10 |
|
} |
31
|
|
|
|
32
|
|
|
public function run(Host $host, string $command, array $config = []) |
33
|
|
|
{ |
34
|
|
|
$connectionString = $host->getConnectionString(); |
35
|
|
|
$defaults = [ |
36
|
|
|
'timeout' => $host->get('default_timeout', 300), |
37
|
|
|
'idle_timeout' => null, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
$config = array_merge($defaults, $config); |
41
|
|
|
$sshArguments = $host->getSshArguments(); |
42
|
|
|
if ($host->getSshMultiplexing()) { |
43
|
|
|
$sshArguments = $this->initMultiplexing($host); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$become = ''; |
47
|
|
|
if ($host->has('become')) { |
48
|
|
|
$become = sprintf('sudo -H -u %s', $host->get('become')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$shellCommand = $host->getShell(); |
52
|
|
|
|
53
|
|
|
if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { |
54
|
|
|
$ssh = "ssh $sshArguments $connectionString $become \"$shellCommand; printf '[exit_code:%s]' $?;\""; |
55
|
|
|
} else { |
56
|
|
|
$ssh = "ssh $sshArguments $connectionString $become '$shellCommand; printf \"[exit_code:%s]\" $?;'"; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// -vvv for ssh command |
60
|
|
|
if ($this->output->isDebug()) { |
61
|
|
|
$this->pop->writeln(Process::OUT, $host, "$ssh"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->pop->command($host, $command); |
65
|
|
|
$this->logger->log("[{$host->getAlias()}] run $command"); |
66
|
|
|
|
67
|
|
|
$terminalOutput = $this->pop->callback($host); |
68
|
|
|
$callback = function ($type, $buffer) use ($host, $terminalOutput) { |
69
|
|
|
$this->logger->printBuffer($host, $type, $buffer); |
70
|
|
|
$terminalOutput($type, $buffer); |
71
|
|
|
}; |
72
|
|
|
|
73
|
|
|
$command = str_replace('%secret%', $config['secret'] ?? '', $command); |
74
|
|
|
$command = str_replace('%sudo_pass%', $config['sudo_pass'] ?? '', $command); |
75
|
|
|
|
76
|
|
|
$process = Process::fromShellCommandline($ssh); |
77
|
|
|
$process |
78
|
|
|
->setInput($command) |
79
|
|
|
->setTimeout($config['timeout']) |
80
|
|
|
->setIdleTimeout($config['idle_timeout']); |
81
|
|
|
|
82
|
|
|
$process->run($callback); |
83
|
|
|
|
84
|
|
|
$output = $this->pop->filterOutput($process->getOutput()); |
85
|
|
|
$exitCode = $this->parseExitStatus($process); |
86
|
|
|
|
87
|
|
|
if ($exitCode !== 0) { |
88
|
|
|
throw new RunException( |
89
|
|
|
$host, |
90
|
|
|
$command, |
91
|
|
|
$exitCode, |
92
|
|
|
$output, |
93
|
|
|
$process->getErrorOutput() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $output; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function parseExitStatus(Process $process) |
101
|
|
|
{ |
102
|
|
|
$output = $process->getOutput(); |
103
|
|
|
preg_match('/\[exit_code:(.*?)\]/', $output, $match); |
104
|
|
|
|
105
|
|
|
if (!isset($match[1])) { |
106
|
|
|
return -1; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$exitCode = (int)$match[1]; |
110
|
|
|
return $exitCode; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function connect(Host $host) |
114
|
|
|
{ |
115
|
|
|
if ($host->getSshMultiplexing()) { |
116
|
|
|
$this->initMultiplexing($host); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function initMultiplexing(Host $host) |
121
|
|
|
{ |
122
|
|
|
$sshArguments = $host->getSshArguments()->withMultiplexing($host); |
123
|
|
|
|
124
|
|
|
if (!$this->isMasterRunning($host, $sshArguments)) { |
125
|
|
|
$connectionString = $host->getConnectionString(); |
126
|
|
|
$command = "ssh -N $sshArguments $connectionString"; |
127
|
|
|
|
128
|
|
|
if ($this->output->isDebug()) { |
129
|
|
|
$this->pop->writeln(Process::OUT, $host, '<info>ssh multiplexing initialization</info>'); |
130
|
|
|
$this->pop->writeln(Process::OUT, $host, $command); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$process = Process::fromShellCommandline($command); |
134
|
|
|
$process->setTimeout(30); // Connection timeout (time needed to establish ssh multiplexing) |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$process->mustRun(); |
138
|
|
|
} catch (ProcessTimedOutException $exception) { |
139
|
|
|
// Timeout fired: maybe there is no connection, |
140
|
|
|
// or maybe another process established master connection. |
141
|
|
|
// Let's try proceed anyway. |
142
|
|
|
// TODO: Make sure only one at a time "ssh multiplexing initialization" is running. For example by doing it in master PHP process (ParallelExecutor). |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$output = $process->getOutput(); |
146
|
|
|
|
147
|
|
|
if ($this->output->isDebug()) { |
148
|
|
|
$this->pop->printBuffer(Process::OUT, $host, $output); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $sshArguments; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function isMasterRunning(Host $host, Arguments $sshArguments) |
156
|
|
|
{ |
157
|
|
|
$command = "ssh -O check $sshArguments echo 2>&1"; |
158
|
|
|
if ($this->output->isDebug()) { |
159
|
|
|
$this->pop->printBuffer(Process::OUT, $host, $command); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$process = Process::fromShellCommandline($command); |
163
|
|
|
$process->run(); |
164
|
|
|
$output = $process->getOutput(); |
165
|
|
|
|
166
|
|
|
if ($this->output->isDebug()) { |
167
|
|
|
$this->pop->printBuffer(Process::OUT, $host, $output); |
168
|
|
|
} |
169
|
|
|
return (bool)preg_match('/Master running/', $output); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|