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\Component\Ssh\Arguments; |
12
|
|
|
use Deployer\Deployer; |
13
|
|
|
|
14
|
|
|
class Host |
15
|
|
|
{ |
16
|
|
|
private $config; |
17
|
|
|
private $sshArguments; |
18
|
|
|
|
19
|
21 |
|
public function __construct(string $hostname) |
20
|
|
|
{ |
21
|
21 |
|
$parent = null; |
22
|
21 |
|
if (Deployer::get()) { |
23
|
19 |
|
$parent = Deployer::get()->config; |
24
|
|
|
} |
25
|
21 |
|
$this->config = new Configuration($parent); |
|
|
|
|
26
|
21 |
|
$this->set('alias', $hostname); |
27
|
21 |
|
$this->set('hostname', preg_replace('/\/.+$/', '', $hostname)); |
28
|
21 |
|
$this->set('remote_user', ''); |
29
|
21 |
|
$this->set('port', ''); |
30
|
21 |
|
$this->set('config_file', ''); |
31
|
21 |
|
$this->set('identity_file', ''); |
32
|
21 |
|
$this->set('forward_agent', true); |
33
|
21 |
|
$this->set('shell', 'bash -s'); |
34
|
21 |
|
$this->sshArguments = new Arguments(); |
35
|
21 |
|
} |
36
|
|
|
|
37
|
9 |
|
public function getConfig() |
38
|
|
|
{ |
39
|
9 |
|
return $this->config; |
40
|
|
|
} |
41
|
|
|
|
42
|
21 |
|
public function set(string $name, $value) |
43
|
|
|
{ |
44
|
21 |
|
$this->config->set($name, $value); |
45
|
21 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function add(string $name, array $value) |
49
|
|
|
{ |
50
|
1 |
|
$this->config->add($name, $value); |
51
|
1 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function has(string $name): bool |
55
|
|
|
{ |
56
|
|
|
return $this->config->has($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
14 |
|
public function get(string $name, $default = null) |
60
|
|
|
{ |
61
|
14 |
|
return $this->config->get($name, $default); |
62
|
|
|
} |
63
|
|
|
|
64
|
17 |
|
public function getAlias() |
65
|
|
|
{ |
66
|
17 |
|
return $this->config->get('alias'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setTag(string $tag) |
70
|
|
|
{ |
71
|
|
|
$this->config->set('tag', $tag); |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
public function getTag(): string |
76
|
|
|
{ |
77
|
9 |
|
return $this->config->get('tag', $this->generateTag()); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function setHostname(string $hostname) |
81
|
|
|
{ |
82
|
1 |
|
$this->config->set('hostname', $hostname); |
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
public function getHostname() |
87
|
|
|
{ |
88
|
3 |
|
return $this->config->get('hostname'); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function setRemoteUser($user) |
92
|
|
|
{ |
93
|
1 |
|
$this->config->set('remote_user', $user); |
94
|
1 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function getRemoteUser() |
98
|
|
|
{ |
99
|
2 |
|
return $this->config->get('remote_user'); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function setPort(int $port) |
103
|
|
|
{ |
104
|
1 |
|
$this->config->set('port', $port); |
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
public function getPort() |
109
|
|
|
{ |
110
|
5 |
|
return $this->config->get('port'); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
public function setConfigFile(string $file) |
114
|
|
|
{ |
115
|
1 |
|
$this->config->set('config_file', $file); |
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
public function getConfigFile() |
120
|
|
|
{ |
121
|
3 |
|
return $this->config->get('config_file'); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
public function setIdentityFile($file) |
125
|
|
|
{ |
126
|
1 |
|
$this->config->set('identity_file', $file); |
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
public function getIdentityFile() |
131
|
|
|
{ |
132
|
4 |
|
return $this->config->get('identity_file'); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
public function setForwardAgent(bool $on) |
136
|
|
|
{ |
137
|
1 |
|
$this->config->set('forward_agent', $on); |
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
public function getForwardAgent() |
142
|
|
|
{ |
143
|
3 |
|
return $this->config->get('forward_agent'); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function setSshMultiplexing(bool $on) |
147
|
|
|
{ |
148
|
1 |
|
$this->config->set('ssh_multiplexing', $on); |
149
|
1 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
public function getSshMultiplexing() |
153
|
|
|
{ |
154
|
2 |
|
return $this->config->get('ssh_multiplexing'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function setShell(string $command) |
158
|
|
|
{ |
159
|
|
|
$this->config->set('shell', $command); |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getShell(): string |
164
|
|
|
{ |
165
|
|
|
return $this->config->get('shell'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getConnectionString(): string |
169
|
|
|
{ |
170
|
|
|
if ($this->get('remote_user') !== '') { |
171
|
|
|
return $this->get('remote_user') . '@' . $this->get('hostname'); |
172
|
|
|
} |
173
|
|
|
return $this->get('hostname'); |
174
|
|
|
} |
175
|
|
|
|
176
|
3 |
|
public function getSshArguments() |
177
|
|
|
{ |
178
|
3 |
|
$this->initOptions(); |
179
|
3 |
|
return $this->sshArguments; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// TODO: Migrate to configuration. |
183
|
|
|
|
184
|
2 |
|
public function setSshOptions(array $options) |
185
|
|
|
{ |
186
|
2 |
|
$this->sshArguments = $this->sshArguments->withOptions($options); |
187
|
2 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// TODO: Migrate to configuration. |
191
|
|
|
|
192
|
2 |
|
public function setSshFlags(array $flags) |
193
|
|
|
{ |
194
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlags($flags); |
195
|
2 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
3 |
|
private function initOptions() |
199
|
|
|
{ |
200
|
3 |
|
if ($this->getPort()) { |
201
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-p', $this->getPort()); |
202
|
|
|
} |
203
|
|
|
|
204
|
3 |
|
if ($this->getConfigFile()) { |
205
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-F', $this->getConfigFile()); |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
if ($this->getIdentityFile()) { |
209
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-i', $this->getIdentityFile()); |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
if ($this->getForwardAgent()) { |
213
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-A'); |
214
|
|
|
} |
215
|
3 |
|
} |
216
|
|
|
|
217
|
9 |
|
private function generateTag() |
218
|
|
|
{ |
219
|
9 |
|
if (defined('NO_ANSI')) { |
220
|
8 |
|
return $this->getAlias(); |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
if ($this->getAlias() === 'localhost') { |
224
|
|
|
return $this->getAlias(); |
225
|
|
|
} |
226
|
|
|
|
227
|
1 |
|
if (getenv('COLORTERM') === 'truecolor') { |
228
|
|
|
$hsv = function ($h, $s, $v) { |
229
|
|
|
$r = $g = $b = $i = $f = $p = $q = $t = 0; |
|
|
|
|
230
|
|
|
$i = floor($h * 6); |
231
|
|
|
$f = $h * 6 - $i; |
232
|
|
|
$p = $v * (1 - $s); |
233
|
|
|
$q = $v * (1 - $f * $s); |
234
|
|
|
$t = $v * (1 - (1 - $f) * $s); |
235
|
|
|
switch ($i % 6) { |
236
|
|
|
case 0: |
237
|
|
|
$r = $v; |
238
|
|
|
$g = $t; |
239
|
|
|
$b = $p; |
240
|
|
|
break; |
241
|
|
|
case 1: |
242
|
|
|
$r = $q; |
243
|
|
|
$g = $v; |
244
|
|
|
$b = $p; |
245
|
|
|
break; |
246
|
|
|
case 2: |
247
|
|
|
$r = $p; |
248
|
|
|
$g = $v; |
249
|
|
|
$b = $t; |
250
|
|
|
break; |
251
|
|
|
case 3: |
252
|
|
|
$r = $p; |
253
|
|
|
$g = $q; |
254
|
|
|
$b = $v; |
255
|
|
|
break; |
256
|
|
|
case 4: |
257
|
|
|
$r = $t; |
258
|
|
|
$g = $p; |
259
|
|
|
$b = $v; |
260
|
|
|
break; |
261
|
|
|
case 5: |
262
|
|
|
$r = $v; |
263
|
|
|
$g = $p; |
264
|
|
|
$b = $q; |
265
|
|
|
break; |
266
|
|
|
} |
267
|
|
|
$r = round($r * 255); |
268
|
|
|
$g = round($g * 255); |
269
|
|
|
$b = round($b * 255); |
270
|
|
|
return "\x1b[38;2;{$r};{$g};{$b}m"; |
271
|
|
|
}; |
272
|
|
|
|
273
|
|
|
$total = 100; |
274
|
|
|
$colors = []; |
275
|
|
|
for ($i = 0; $i < $total; $i++) { |
276
|
|
|
$colors[] = $hsv($i / $total, 1, .9); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$alias = $this->getAlias(); |
280
|
|
|
$tag = $colors[abs(crc32($alias)) % count($colors)]; |
281
|
|
|
|
282
|
|
|
return "{$tag}{$alias}\x1b[0m"; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
$colors = [ |
287
|
1 |
|
'fg=cyan;options=bold', |
288
|
|
|
'fg=green;options=bold', |
289
|
|
|
'fg=yellow;options=bold', |
290
|
|
|
'fg=cyan', |
291
|
|
|
'fg=blue', |
292
|
|
|
'fg=yellow', |
293
|
|
|
'fg=magenta', |
294
|
|
|
'fg=blue;options=bold', |
295
|
|
|
'fg=green', |
296
|
|
|
'fg=magenta;options=bold', |
297
|
|
|
'fg=red;options=bold', |
298
|
|
|
]; |
299
|
1 |
|
$alias = $this->getAlias(); |
300
|
1 |
|
$tag = $colors[abs(crc32($alias)) % count($colors)]; |
301
|
|
|
|
302
|
1 |
|
return "<{$tag}>{$alias}</>"; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.