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\Process; |
17
|
|
|
|
18
|
|
|
class Client |
19
|
|
|
{ |
20
|
|
|
private $output; |
21
|
|
|
private $pop; |
22
|
|
|
private $logger; |
23
|
|
|
|
24
|
8 |
|
public function __construct(OutputInterface $output, Printer $pop, Logger $logger) |
25
|
|
|
{ |
26
|
8 |
|
$this->output = $output; |
27
|
8 |
|
$this->pop = $pop; |
28
|
8 |
|
$this->logger = $logger; |
29
|
8 |
|
} |
30
|
|
|
|
31
|
|
|
public function run(Host $host, string $command, array $config = []) |
32
|
|
|
{ |
33
|
|
|
$hostname = $host->hostname(); |
|
|
|
|
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->sshMultiplexing()) { |
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->shell(); |
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->alias()}] 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
|
|
|
$process = $this->createProcess($ssh); |
74
|
|
|
$process |
75
|
|
|
->setInput(str_replace('%secret%', $config['secret'] ?? '', $command)) |
76
|
|
|
->setTimeout($config['timeout']) |
77
|
|
|
->setIdleTimeout($config['idle_timeout']); |
78
|
|
|
|
79
|
|
|
$process->run($callback); |
80
|
|
|
|
81
|
|
|
$output = $this->pop->filterOutput($process->getOutput()); |
82
|
|
|
$exitCode = $this->parseExitStatus($process); |
83
|
|
|
|
84
|
|
|
if ($exitCode !== 0) { |
85
|
|
|
throw new RunException( |
86
|
|
|
$host, |
87
|
|
|
$command, |
88
|
|
|
$exitCode, |
89
|
|
|
$output, |
90
|
|
|
$process->getErrorOutput() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $output; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function parseExitStatus(Process $process) |
98
|
|
|
{ |
99
|
|
|
$output = $process->getOutput(); |
100
|
|
|
preg_match('/\[exit_code:(.*?)\]/', $output, $match); |
101
|
|
|
|
102
|
|
|
if (!isset($match[1])) { |
103
|
|
|
return -1; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$exitCode = (int)$match[1]; |
107
|
|
|
return $exitCode; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function connect(Host $host) |
111
|
|
|
{ |
112
|
|
|
if ($host->sshMultiplexing()) { |
113
|
|
|
$this->initMultiplexing($host); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function initMultiplexing(Host $host) |
118
|
|
|
{ |
119
|
|
|
$sshArguments = $host->getSshArguments()->withMultiplexing($host); |
120
|
|
|
|
121
|
|
|
if (!$this->isMultiplexingInitialized($host, $sshArguments)) { |
122
|
|
|
$connectionString = $host->getConnectionString(); |
123
|
|
|
$command = "ssh -N $sshArguments $connectionString"; |
124
|
|
|
|
125
|
|
|
if ($this->output->isDebug()) { |
126
|
|
|
$this->pop->writeln(Process::OUT, $host, '<info>ssh multiplexing initialization</info>'); |
127
|
|
|
$this->pop->writeln(Process::OUT, $host, $command); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$output = $this->exec($command); |
131
|
|
|
|
132
|
|
|
if ($this->output->isDebug()) { |
133
|
|
|
$this->pop->printBuffer(Process::OUT, $host, $output); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $sshArguments; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function isMultiplexingInitialized(Host $host, Arguments $sshArguments) |
141
|
|
|
{ |
142
|
|
|
$command = "ssh -O check $sshArguments echo 2>&1"; |
143
|
|
|
if ($this->output->isDebug()) { |
144
|
|
|
$this->pop->printBuffer(Process::OUT, $host, $command); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$process = $this->createProcess($command); |
148
|
|
|
$process->run(); |
149
|
|
|
$output = $process->getOutput(); |
150
|
|
|
|
151
|
|
|
if ($this->output->isDebug()) { |
152
|
|
|
$this->pop->printBuffer(Process::OUT, $host, $output); |
153
|
|
|
} |
154
|
|
|
return (bool)preg_match('/Master running/', $output); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function exec($command, &$exitCode = null) |
158
|
|
|
{ |
159
|
|
|
$descriptors = [ |
160
|
|
|
['pipe', 'r'], |
161
|
|
|
['pipe', 'w'], |
162
|
|
|
['pipe', 'w'], |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
// Don't read from stderr, there is a bug in OpenSSH_7.2p2 (stderr doesn't closed with ControlMaster) |
166
|
|
|
|
167
|
|
|
$process = proc_open($command, $descriptors, $pipes); |
168
|
|
|
if (is_resource($process)) { |
169
|
|
|
fclose($pipes[0]); |
170
|
|
|
$output = stream_get_contents($pipes[1]); |
171
|
|
|
fclose($pipes[1]); |
172
|
|
|
fclose($pipes[2]); |
173
|
|
|
$exitCode = proc_close($process); |
174
|
|
|
} else { |
175
|
|
|
$output = 'proc_open failure'; |
176
|
|
|
$exitCode = 1; |
177
|
|
|
} |
178
|
|
|
return $output; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function createProcess($command) |
182
|
|
|
{ |
183
|
|
|
if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) { |
184
|
|
|
return Process::fromShellCommandline($command); |
185
|
|
|
} else { |
186
|
|
|
return new Process($command); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.