|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the GitCommandBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paul Schweppe <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace VersionControl\GitCommandBundle\Service; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
16
|
|
|
use VersionControl\GitCommandBundle\GitCommands\Exception\RunGitCommandException; |
|
17
|
|
|
use phpseclib\Net\SSH2; |
|
18
|
|
|
use phpseclib\Crypt\RSA; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Use PhpSecLib to make SSH2 requests. |
|
22
|
|
|
* |
|
23
|
|
|
* @link https://github.com/phpseclib/phpseclib |
|
24
|
|
|
* |
|
25
|
|
|
* @author Paul Schweppe <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class SecLibSshProcess implements SshProcessInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var EventDispatcherInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $dispatcher; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $config; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var SSH2|null |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $shell; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $stdout; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $stderr; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* //EventDispatcherInterface $eventDispatcher,. |
|
56
|
|
|
* |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->shell = null; |
|
61
|
|
|
$this->stdout = array(); |
|
62
|
|
|
$this->stdin = array(); |
|
|
|
|
|
|
63
|
|
|
$this->stderr = array(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $glue |
|
68
|
|
|
* |
|
69
|
|
|
* @return array|string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getStdout($glue = "\n") |
|
72
|
|
|
{ |
|
73
|
|
|
if (!$glue) { |
|
74
|
|
|
$output = $this->stdout; |
|
75
|
|
|
} else { |
|
76
|
|
|
$output = implode($glue, $this->stdout); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $output; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $glue |
|
84
|
|
|
* |
|
85
|
|
|
* @return array|string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getStderr($glue = "\n") |
|
88
|
|
|
{ |
|
89
|
|
|
if (!$glue) { |
|
90
|
|
|
return $this->stderr; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return implode($glue, $this->stderr); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $commands |
|
98
|
|
|
* @param string $host |
|
99
|
|
|
* @param string $username |
|
100
|
|
|
* @param int $port |
|
101
|
|
|
* @param string $password |
|
102
|
|
|
* @param string|null $publicKeyFile |
|
103
|
|
|
* @param string|null $privateKeyFile |
|
104
|
|
|
* @param string $passphrase |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
* @throws RunGitCommandException |
|
108
|
|
|
*/ |
|
109
|
|
|
public function run( |
|
110
|
|
|
array $commands, |
|
111
|
|
|
string $host, |
|
112
|
|
|
string $username, |
|
113
|
|
|
int $port = 22, |
|
114
|
|
|
?string $password = null, |
|
115
|
|
|
?string $publicKeyFile = null, |
|
116
|
|
|
?string $privateKeyFile = null, |
|
117
|
|
|
?string $passphrase = null |
|
118
|
|
|
): array { |
|
119
|
|
|
$this->reset(); |
|
120
|
|
|
|
|
121
|
|
|
if ($this->shell === null) { |
|
122
|
|
|
$this->connect($host, $username, $port, $password, $pubkeyFile, $privkeyFile, $passphrase); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
foreach ($commands as $command) { |
|
126
|
|
|
$this->execute($command); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
//$this->disconnect(); |
|
130
|
|
|
|
|
131
|
|
|
return $this->stdout; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Resets out puts for next command. |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function reset() |
|
138
|
|
|
{ |
|
139
|
|
|
$this->stdout = array(); |
|
140
|
|
|
$this->stdin = array(); |
|
|
|
|
|
|
141
|
|
|
$this->stderr = array(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param $host |
|
146
|
|
|
* @param $username |
|
147
|
|
|
* @param int $port |
|
148
|
|
|
* @param null $password |
|
|
|
|
|
|
149
|
|
|
* @param null $pubkeyFile |
|
|
|
|
|
|
150
|
|
|
* @param null $privateKey |
|
|
|
|
|
|
151
|
|
|
* @param null $privateKeyPassword |
|
|
|
|
|
|
152
|
|
|
* |
|
153
|
|
|
* @throws InvalidArgumentException |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function connect( |
|
156
|
|
|
$host, |
|
157
|
|
|
$username, |
|
158
|
|
|
$port = 22, |
|
159
|
|
|
$password = null, |
|
160
|
|
|
$pubkeyFile = null, |
|
161
|
|
|
$privateKey = null, |
|
162
|
|
|
$privateKeyPassword = null |
|
163
|
|
|
) { |
|
164
|
|
|
$this->shell = new SSH2($host, $port); |
|
165
|
|
|
|
|
166
|
|
|
if (!$this->shell) { |
|
167
|
|
|
throw new InvalidArgumentException(sprintf('SSH connection failed on "%s:%s"', $host, $port)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if (isset($username) && trim($privateKey)) { |
|
171
|
|
|
$key = new RSA(); |
|
172
|
|
|
if ($privateKeyPassword) { |
|
|
|
|
|
|
173
|
|
|
$key->setPassword($privateKeyPassword); |
|
174
|
|
|
} |
|
175
|
|
|
$key->loadKey($privateKey); |
|
176
|
|
|
if (!$this->shell->login($username, $key)) { |
|
177
|
|
|
throw new InvalidArgumentException(sprintf('SSH authentication failed for user "%s" using private key', |
|
178
|
|
|
$username, $pubkeyFile)); |
|
179
|
|
|
} |
|
180
|
|
|
} elseif ($username && $password) { |
|
|
|
|
|
|
181
|
|
|
if (!$this->shell->login($username, $password)) { |
|
182
|
|
|
throw new InvalidArgumentException(sprintf('SSH authentication failed for user "%s"', $username)); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
$this->shell->getServerPublicHostKey(); |
|
186
|
|
|
|
|
187
|
|
|
$this->stdout = array(); |
|
188
|
|
|
$this->stdin = array(); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function disconnect() |
|
192
|
|
|
{ |
|
193
|
|
|
if ($this->shell) { |
|
194
|
|
|
//$this->shell->disconnect(); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param array $command |
|
200
|
|
|
* |
|
201
|
|
|
* @throws RunGitCommandException |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function execute($command) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->shell->enableQuietMode(); |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
$stdOutput = $this->shell->exec($command); |
|
|
|
|
|
|
208
|
|
|
$stdError = $this->shell->getStdError(); |
|
209
|
|
|
$exitStatus = $this->shell->getExitStatus(); |
|
210
|
|
|
|
|
211
|
|
|
$stdout = explode("\n", $stdOutput); |
|
212
|
|
|
$stderr = array_filter(explode("\n", $stdError)); |
|
213
|
|
|
|
|
214
|
|
|
if ($exitStatus != 0) { |
|
215
|
|
|
//print_r($stderr); |
|
216
|
|
|
throw new RunGitCommandException( |
|
217
|
|
|
sprintf( |
|
218
|
|
|
"Error in command shell:%s \n Error Response:%s%s", |
|
219
|
|
|
$command, |
|
|
|
|
|
|
220
|
|
|
implode("\n", $stderr), |
|
221
|
|
|
$stdOutput |
|
222
|
|
|
) |
|
223
|
|
|
); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
$this->stdout = array_merge($this->stdout, $stdout); |
|
227
|
|
|
|
|
228
|
|
|
if (is_array($stderr)) { |
|
|
|
|
|
|
229
|
|
|
$this->stderr = array_merge($this->stderr, $stderr); |
|
230
|
|
|
|
|
231
|
|
|
if ($exitStatus === 0) { |
|
232
|
|
|
$this->stdout = array_merge($this->stdout, $stderr); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get exit status |
|
239
|
|
|
* @return false|int |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getExitStatus() |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->shell->getExitStatus(); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function __destruct() |
|
247
|
|
|
{ |
|
248
|
|
|
$this->disconnect(); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|