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\Host; |
9
|
|
|
|
10
|
|
|
use Deployer\Configuration\Configuration; |
11
|
|
|
use Deployer\Configuration\ConfigurationAccessor; |
12
|
|
|
use Deployer\Ssh\Arguments; |
13
|
|
|
use function Deployer\Support\array_flatten; |
14
|
|
|
|
15
|
|
|
class Host |
16
|
|
|
{ |
17
|
|
|
use ConfigurationAccessor; |
18
|
|
|
|
19
|
|
|
private $hostname; |
20
|
|
|
private $realHostname; |
21
|
|
|
private $user; |
22
|
|
|
private $port; |
23
|
|
|
private $configFile; |
24
|
|
|
private $identityFile; |
25
|
|
|
private $forwardAgent = true; |
26
|
|
|
private $multiplexing = null; |
27
|
|
|
private $sshArguments; |
28
|
|
|
private $shellCommand = 'bash -s'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $hostname |
32
|
|
|
*/ |
33
|
34 |
|
public function __construct(string $hostname) |
34
|
|
|
{ |
35
|
34 |
|
$this->hostname = $hostname; |
36
|
34 |
|
$this->setRealHostname($hostname); |
37
|
34 |
|
$this->config = new Configuration(); |
38
|
34 |
|
$this->sshArguments = new Arguments(); |
39
|
34 |
|
} |
40
|
|
|
|
41
|
3 |
|
private function initOptions() |
42
|
|
|
{ |
43
|
3 |
|
if ($this->port) { |
44
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-p', $this->port); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
if ($this->configFile) { |
48
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-F', $this->configFile); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
if ($this->identityFile) { |
52
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-i', $this->getIdentityFile()); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
if ($this->forwardAgent) { |
56
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-A'); |
57
|
|
|
} |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns pair user/hostname |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
6 |
|
public function __toString() |
66
|
|
|
{ |
67
|
6 |
|
$user = empty($this->user) ? '' : "{$this->user}@"; |
68
|
6 |
|
return "$user{$this->realHostname}"; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
28 |
|
public function getHostname() |
75
|
|
|
{ |
76
|
28 |
|
return $this->config->parse($this->hostname); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function getRealHostname() |
83
|
|
|
{ |
84
|
|
|
return $this->config->parse($this->realHostname); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
public function hostname(string $hostname): self |
88
|
|
|
{ |
89
|
4 |
|
$this->setRealHostname($hostname); |
90
|
4 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $hostname |
95
|
|
|
*/ |
96
|
34 |
|
private function setRealHostname(string $hostname) |
97
|
|
|
{ |
98
|
34 |
|
$this->realHostname = preg_replace('/\/.+$/', '', $hostname); |
99
|
34 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
2 |
|
public function getUser() |
105
|
|
|
{ |
106
|
2 |
|
return $this->config->parse($this->user); |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
public function user(string $user): self |
110
|
|
|
{ |
111
|
4 |
|
$this->user = $user; |
112
|
4 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
4 |
|
public function getPort() |
119
|
|
|
{ |
120
|
4 |
|
return $this->port; |
121
|
|
|
} |
122
|
|
|
|
123
|
4 |
|
public function port(int $port): self |
124
|
|
|
{ |
125
|
4 |
|
$this->port = $port; |
126
|
4 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
2 |
|
public function getConfigFile() |
133
|
|
|
{ |
134
|
2 |
|
return $this->configFile; |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
public function configFile(string $configFile): self |
138
|
|
|
{ |
139
|
3 |
|
$this->configFile = $configFile; |
140
|
3 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
3 |
|
public function getIdentityFile() |
147
|
|
|
{ |
148
|
3 |
|
return $this->config->parse($this->identityFile); |
149
|
|
|
} |
150
|
|
|
|
151
|
4 |
|
public function identityFile(string $identityFile): self |
152
|
|
|
{ |
153
|
4 |
|
$this->identityFile = $identityFile; |
154
|
4 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
2 |
|
public function isForwardAgent() |
161
|
|
|
{ |
162
|
2 |
|
return $this->forwardAgent; |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
public function forwardAgent(bool $forwardAgent = true): self |
166
|
|
|
{ |
167
|
3 |
|
$this->forwardAgent = $forwardAgent; |
168
|
3 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
2 |
|
public function isMultiplexing() |
175
|
|
|
{ |
176
|
2 |
|
return $this->multiplexing; |
177
|
|
|
} |
178
|
|
|
|
179
|
3 |
|
public function multiplexing(bool $multiplexing = true): self |
180
|
|
|
{ |
181
|
3 |
|
$this->multiplexing = $multiplexing; |
182
|
3 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
public function getSshArguments() |
186
|
|
|
{ |
187
|
3 |
|
$this->initOptions(); |
188
|
3 |
|
return $this->sshArguments; |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
public function sshOptions(array $options): self |
192
|
|
|
{ |
193
|
3 |
|
$this->sshArguments = $this->sshArguments->withOptions($options); |
194
|
3 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
public function sshFlags(array $flags): self |
198
|
|
|
{ |
199
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlags($flags); |
200
|
2 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
1 |
|
public function addSshOption(string $option, $value): self |
204
|
|
|
{ |
205
|
1 |
|
$this->sshArguments = $this->sshArguments->withOption($option, $value); |
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function addSshFlag(string $flag, string $value = null): self |
210
|
|
|
{ |
211
|
|
|
$this->sshArguments = $this->sshArguments->withFlag($flag, $value); |
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function getShellCommand() : string |
216
|
|
|
{ |
217
|
|
|
return $this->shellCommand; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function shellCommand(string $shellCommand): self |
221
|
|
|
{ |
222
|
|
|
$this->shellCommand = $shellCommand; |
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
5 |
|
public function stage(string $stage): self |
227
|
|
|
{ |
228
|
5 |
|
$this->config->set('stage', $stage); |
229
|
5 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
5 |
|
public function binary(string $binary, string $path) |
233
|
|
|
{ |
234
|
5 |
|
$binaries = $this->config->get('binaries', []); |
235
|
|
|
$binaries[$binary] = $path; |
236
|
5 |
|
$this->config->add('binaries', $binaries); |
237
|
5 |
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
5 |
|
|
241
|
|
|
public function getBinary(string $binary): ?string |
242
|
|
|
{ |
243
|
|
|
$binaries = $this->config->get('binaries'); |
244
|
|
|
|
245
|
|
|
return $binaries[$binary] ?? null; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function roles(...$roles): self |
249
|
|
|
{ |
250
|
|
|
$this->config->set('roles', []); |
251
|
|
|
|
252
|
|
|
foreach (array_flatten($roles) as $role) { |
253
|
|
|
$this->config->add('roles', [$role]); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function become(string $user): self |
260
|
|
|
{ |
261
|
|
|
$this->config->set('become', $user); |
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|