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