|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Deployer\Host; |
|
12
|
|
|
|
|
13
|
|
|
use Deployer\Configuration\Configuration; |
|
14
|
|
|
use Deployer\Deployer; |
|
15
|
|
|
use Deployer\Exception\ConfigurationException; |
|
16
|
|
|
use Deployer\Exception\Exception; |
|
17
|
|
|
use Deployer\Task\Context; |
|
18
|
|
|
|
|
19
|
|
|
use function Deployer\Support\colorize_host; |
|
20
|
25 |
|
use function Deployer\Support\parse_home_dir; |
|
21
|
|
|
|
|
22
|
25 |
|
class Host |
|
23
|
25 |
|
{ |
|
24
|
23 |
|
/** |
|
25
|
|
|
* @var Configuration $config |
|
26
|
25 |
|
*/ |
|
27
|
25 |
|
private $config; |
|
28
|
25 |
|
|
|
29
|
25 |
|
public function __construct(string $hostname) |
|
30
|
25 |
|
{ |
|
31
|
|
|
$parent = null; |
|
32
|
12 |
|
if (Deployer::get()) { |
|
33
|
|
|
$parent = Deployer::get()->config; |
|
34
|
12 |
|
} |
|
35
|
|
|
$this->config = new Configuration($parent); |
|
36
|
|
|
$this->set('#alias', $hostname); |
|
37
|
25 |
|
$this->set('hostname', preg_replace('/\/.+$/', '', $hostname)); |
|
38
|
|
|
} |
|
39
|
25 |
|
|
|
40
|
25 |
|
public function __toString(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->getTag(); |
|
|
|
|
|
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function config(): Configuration |
|
46
|
1 |
|
{ |
|
47
|
|
|
return $this->config; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param mixed $value |
|
52
|
|
|
*/ |
|
53
|
|
|
public function set(string $name, $value): self |
|
54
|
19 |
|
{ |
|
55
|
|
|
if ($name === 'alias') { |
|
56
|
19 |
|
throw new ConfigurationException("Can not update alias of the host.\nThis will change only host own alias,\nbut not the key it is stored in HostCollection."); |
|
57
|
|
|
} |
|
58
|
|
|
if ($name === '#alias') { |
|
59
|
19 |
|
$name = 'alias'; |
|
60
|
|
|
} |
|
61
|
19 |
|
$this->config->set($name, $value); |
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function add(string $name, array $value): self |
|
66
|
|
|
{ |
|
67
|
|
|
$this->config->add($name, $value); |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
11 |
|
|
|
71
|
|
|
public function has(string $name): bool |
|
72
|
11 |
|
{ |
|
73
|
|
|
return $this->config->has($name); |
|
74
|
|
|
} |
|
75
|
1 |
|
|
|
76
|
|
|
public function hasOwn(string $name): bool |
|
77
|
1 |
|
{ |
|
78
|
1 |
|
return $this->config->hasOwn($name); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
/** |
|
82
|
|
|
* @param mixed|null $default |
|
83
|
3 |
|
* @return mixed|null |
|
84
|
|
|
*/ |
|
85
|
|
|
public function get(string $name, $default = null) |
|
86
|
1 |
|
{ |
|
87
|
|
|
return $this->config->get($name, $default); |
|
88
|
1 |
|
} |
|
89
|
1 |
|
|
|
90
|
|
|
public function getAlias(): ?string |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->config->get('alias', null); |
|
93
|
|
|
} |
|
94
|
2 |
|
|
|
95
|
|
|
public function setTag(string $tag): self |
|
96
|
|
|
{ |
|
97
|
1 |
|
$this->config->set('tag', $tag); |
|
98
|
|
|
return $this; |
|
99
|
1 |
|
} |
|
100
|
1 |
|
|
|
101
|
|
|
public function getTag(): ?string |
|
102
|
|
|
{ |
|
103
|
3 |
|
return $this->config->get('tag', colorize_host($this->getAlias())); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
3 |
|
|
|
106
|
|
|
public function setHostname(string $hostname): self |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->config->set('hostname', $hostname); |
|
109
|
|
|
return $this; |
|
110
|
1 |
|
} |
|
111
|
1 |
|
|
|
112
|
|
|
public function getHostname(): ?string |
|
113
|
|
|
{ |
|
114
|
3 |
|
return $this->config->get('hostname', null); |
|
115
|
|
|
} |
|
116
|
3 |
|
|
|
117
|
|
|
public function setRemoteUser(string $user): self |
|
118
|
|
|
{ |
|
119
|
1 |
|
$this->config->set('remote_user', $user); |
|
120
|
|
|
return $this; |
|
121
|
1 |
|
} |
|
122
|
1 |
|
|
|
123
|
|
|
public function getRemoteUser(): ?string |
|
124
|
|
|
{ |
|
125
|
4 |
|
return $this->config->get('remote_user', null); |
|
126
|
|
|
} |
|
127
|
4 |
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string|int|null $port |
|
130
|
1 |
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function setPort($port): self |
|
133
|
1 |
|
{ |
|
134
|
|
|
$this->config->set('port', $port); |
|
135
|
|
|
return $this; |
|
136
|
3 |
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
/** |
|
139
|
|
|
* @return string|int|null |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function getPort() |
|
142
|
|
|
{ |
|
143
|
1 |
|
return $this->config->get('port', null); |
|
144
|
1 |
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function setConfigFile(string $file): self |
|
147
|
2 |
|
{ |
|
148
|
|
|
$this->config->set('config_file', $file); |
|
149
|
2 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getConfigFile(): ?string |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->config->get('config_file', null); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function setIdentityFile(string $file): self |
|
158
|
|
|
{ |
|
159
|
|
|
$this->config->set('identity_file', $file); |
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function getIdentityFile(): ?string |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->config->get('identity_file', null); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function setForwardAgent(bool $on): self |
|
169
|
|
|
{ |
|
170
|
|
|
$this->config->set('forward_agent', $on); |
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
2 |
|
public function getForwardAgent(): ?bool |
|
175
|
|
|
{ |
|
176
|
2 |
|
return $this->config->get('forward_agent', null); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
2 |
|
public function setSshMultiplexing(bool $on): self |
|
180
|
|
|
{ |
|
181
|
|
|
$this->config->set('ssh_multiplexing', $on); |
|
182
|
3 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
3 |
|
|
|
185
|
3 |
|
public function getSshMultiplexing(): ?bool |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->config->get('ssh_multiplexing', null); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
2 |
|
public function setShell(string $command): self |
|
191
|
|
|
{ |
|
192
|
2 |
|
$this->config->set('shell', $command); |
|
193
|
2 |
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function getShell(): ?string |
|
197
|
|
|
{ |
|
198
|
2 |
|
return $this->config->get('shell', null); |
|
199
|
|
|
} |
|
200
|
2 |
|
|
|
201
|
2 |
|
public function setDeployPath(string $path): self |
|
202
|
|
|
{ |
|
203
|
|
|
$this->config->set('deploy_path', $path); |
|
204
|
3 |
|
return $this; |
|
205
|
|
|
} |
|
206
|
3 |
|
|
|
207
|
3 |
|
public function getDeployPath(): ?string |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->config->get('deploy_path', null); |
|
210
|
3 |
|
} |
|
211
|
2 |
|
|
|
212
|
|
|
public function setLabels(array $labels): self |
|
213
|
|
|
{ |
|
214
|
3 |
|
$this->config->set('labels', $labels); |
|
215
|
2 |
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
3 |
|
public function addLabels(array $labels): self |
|
219
|
3 |
|
{ |
|
220
|
|
|
$existingLabels = $this->getLabels() ?? []; |
|
221
|
3 |
|
$this->setLabels(array_replace_recursive($existingLabels, $labels)); |
|
222
|
|
|
return $this; |
|
223
|
11 |
|
} |
|
224
|
|
|
|
|
225
|
11 |
|
public function getLabels(): ?array |
|
226
|
10 |
|
{ |
|
227
|
|
|
return $this->config->get('labels', null); |
|
228
|
|
|
} |
|
229
|
1 |
|
|
|
230
|
|
|
public function setSshArguments(array $args): self |
|
231
|
|
|
{ |
|
232
|
|
|
$this->config->set('ssh_arguments', $args); |
|
233
|
1 |
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function getSshArguments(): ?array |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->config->get('ssh_arguments', null); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function setSshControlPath(string $path): self |
|
242
|
|
|
{ |
|
243
|
|
|
$this->config->set('ssh_control_path', $path); |
|
244
|
|
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function getSshControlPath(): string |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->config->get('ssh_control_path', $this->generateControlPath()); |
|
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
private function generateControlPath(): string |
|
253
|
|
|
{ |
|
254
|
|
|
$C = $this->getHostname(); |
|
255
|
|
|
if ($this->has('remote_user')) { |
|
256
|
|
|
$C = $this->getRemoteUser() . '@' . $C; |
|
257
|
|
|
} |
|
258
|
|
|
if ($this->has('port')) { |
|
259
|
|
|
$C .= ':' . $this->getPort(); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
// In case of CI environment, lets use shared memory. |
|
263
|
|
|
if (getenv('CI') && is_writable('/dev/shm')) { |
|
264
|
|
|
return "/dev/shm/$C"; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return "~/.ssh/$C"; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function connectionString(): string |
|
271
|
|
|
{ |
|
272
|
|
|
if ($this->get('remote_user', '') !== '') { |
|
273
|
|
|
return $this->get('remote_user') . '@' . $this->get('hostname'); |
|
274
|
|
|
} |
|
275
|
|
|
return $this->get('hostname'); |
|
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function connectionOptionsString(): string |
|
279
|
|
|
{ |
|
280
|
|
|
return implode(' ', array_map('escapeshellarg', $this->connectionOptionsArray())); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @return string[] |
|
285
|
|
|
*/ |
|
286
|
|
|
public function connectionOptionsArray(): array |
|
287
|
|
|
{ |
|
288
|
|
|
$options = []; |
|
289
|
|
|
if ($this->has('ssh_arguments')) { |
|
290
|
|
|
foreach ($this->getSshArguments() as $arg) { |
|
291
|
|
|
$options = array_merge($options, explode(' ', $arg)); |
|
292
|
|
|
} |
|
293
|
1 |
|
} |
|
294
|
|
|
if ($this->has('port')) { |
|
295
|
|
|
$options = array_merge($options, ['-p', $this->getPort()]); |
|
296
|
|
|
} |
|
297
|
|
|
if ($this->has('config_file')) { |
|
298
|
|
|
$options = array_merge($options, ['-F', parse_home_dir($this->getConfigFile())]); |
|
|
|
|
|
|
299
|
|
|
} |
|
300
|
|
|
if ($this->has('identity_file')) { |
|
301
|
|
|
$options = array_merge($options, ['-i', parse_home_dir($this->getIdentityFile())]); |
|
302
|
|
|
} |
|
303
|
|
|
if ($this->has('forward_agent') && $this->getForwardAgent()) { |
|
304
|
|
|
$options = array_merge($options, ['-A']); |
|
305
|
1 |
|
} |
|
306
|
1 |
|
if ($this->has('ssh_multiplexing') && $this->getSshMultiplexing()) { |
|
307
|
|
|
$options = array_merge($options, [ |
|
308
|
1 |
|
'-o', 'ControlMaster=auto', |
|
309
|
|
|
'-o', 'ControlPersist=60', |
|
310
|
|
|
'-o', 'ControlPath=' . $this->getSshControlPath(), |
|
311
|
|
|
]); |
|
312
|
|
|
} |
|
313
|
|
|
return $options; |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|