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\Server\Remote; |
9
|
|
|
|
10
|
|
|
use Deployer\Server\Configuration; |
11
|
|
|
use Deployer\Server\ServerInterface; |
12
|
|
|
use Symfony\Component\Process\Process; |
13
|
|
|
|
14
|
|
|
class NativeSsh implements ServerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Configuration |
18
|
|
|
*/ |
19
|
|
|
private $configuration; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Configuration $configuration |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Configuration $configuration) |
25
|
|
|
{ |
26
|
|
|
$this->configuration = $configuration; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function connect() |
33
|
|
|
{ |
34
|
|
|
/* no persistent connection is used */ |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function run($command) |
41
|
|
|
{ |
42
|
|
|
$serverConfig = $this->getConfiguration(); |
43
|
|
|
$sshOptions = ['-A']; |
44
|
|
|
|
45
|
|
|
$username = $serverConfig->getUser() ? $serverConfig->getUser() : null; |
46
|
|
|
if (!empty($username)) { |
47
|
|
|
$username = $username . '@'; |
48
|
|
|
} |
49
|
|
|
$hostname = $serverConfig->getHost(); |
50
|
|
|
|
51
|
|
|
if ($serverConfig->getPort()) { |
52
|
|
|
$sshOptions[] = '-p ' . escapeshellarg($serverConfig->getPort()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($serverConfig->getPrivateKey()) { |
56
|
|
|
$sshOptions[] = '-i ' . escapeshellarg($serverConfig->getPrivateKey()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$sshCommand = 'ssh ' . implode(' ', $sshOptions) . ' ' . escapeshellarg($username . $hostname) . ' ' . escapeshellarg($command); |
60
|
|
|
|
61
|
|
|
$process = new Process($sshCommand); |
62
|
|
|
$process |
63
|
|
|
->setTimeout(null) |
64
|
|
|
->setIdleTimeout(null) |
65
|
|
|
->mustRun(); |
66
|
|
|
|
67
|
|
|
return $process->getOutput(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function upload($local, $remote) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$serverConfig = $this->getConfiguration(); |
76
|
|
|
|
77
|
|
|
$username = $serverConfig->getUser() ? $serverConfig->getUser() : null; |
78
|
|
|
$hostname = $serverConfig->getHost(); |
79
|
|
|
|
80
|
|
|
return $this->scpCopy($local, (!empty($username) ? $username . '@' : '') . $hostname . ':' . $remote); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function download($local, $remote) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$serverConfig = $this->getConfiguration(); |
89
|
|
|
|
90
|
|
|
$username = $serverConfig->getUser() ? $serverConfig->getUser() : null; |
91
|
|
|
$hostname = $serverConfig->getHost(); |
92
|
|
|
|
93
|
|
|
return $this->scpCopy((!empty($username) ? $username . '@' : '') . $hostname . ':' . $remote, $local); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Copy file from target1 to target 2 via scp |
98
|
|
|
* @param string $target |
99
|
|
|
* @param string $target2 |
100
|
|
|
*/ |
101
|
|
|
public function scpCopy($target, $target2) |
102
|
|
|
{ |
103
|
|
|
$serverConfig = $this->getConfiguration(); |
104
|
|
|
|
105
|
|
|
$scpOptions = []; |
106
|
|
|
|
107
|
|
|
if ($serverConfig->getPort()) { |
108
|
|
|
$scpOptions[] = '-P ' . escapeshellarg($serverConfig->getPort()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($serverConfig->getPrivateKey()) { |
112
|
|
|
$sshOptions[] = '-i ' . escapeshellarg($serverConfig->getPrivateKey()); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$scpCommand = 'scp ' . implode(' ', $scpOptions) . ' ' . escapeshellarg($target) . ' ' . escapeshellarg($target2); |
116
|
|
|
|
117
|
|
|
$process = new Process($scpCommand); |
118
|
|
|
$process |
119
|
|
|
->setTimeout(null) |
120
|
|
|
->setIdleTimeout(null) |
121
|
|
|
->mustRun(); |
122
|
|
|
|
123
|
|
|
return $process->getOutput(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Configuration |
128
|
|
|
*/ |
129
|
|
|
public function getConfiguration() |
130
|
|
|
{ |
131
|
|
|
return $this->configuration; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.