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
|
18 |
|
public function __construct(string $hostname) |
20
|
|
|
{ |
21
|
18 |
|
$parent = null; |
22
|
18 |
|
if (Deployer::get()) { |
23
|
16 |
|
$parent = Deployer::get()->config; |
24
|
|
|
} |
25
|
18 |
|
$this->config = new Configuration($parent); |
26
|
18 |
|
$this->set('alias', $hostname); |
27
|
18 |
|
$this->set('hostname', preg_replace('/\/.+$/', '', $hostname)); |
28
|
18 |
|
$this->set('remote_user', ''); |
29
|
18 |
|
$this->set('port', ''); |
30
|
18 |
|
$this->set('config_file', ''); |
31
|
18 |
|
$this->set('identity_file', ''); |
32
|
18 |
|
$this->set('forward_agent', true); |
33
|
18 |
|
$this->set('shell', 'bash -s'); |
34
|
18 |
|
$this->sshArguments = new Arguments(); |
35
|
18 |
|
} |
36
|
|
|
|
37
|
|
|
public function getConfig() |
38
|
|
|
{ |
39
|
|
|
return $this->config; |
40
|
|
|
} |
41
|
|
|
|
42
|
18 |
|
public function set(string $name, $value) |
43
|
|
|
{ |
44
|
18 |
|
$this->config->set($name, $value); |
45
|
18 |
|
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
|
9 |
|
public function get(string $name, $default = null) |
60
|
|
|
{ |
61
|
9 |
|
return $this->config->get($name, $default); |
62
|
|
|
} |
63
|
|
|
|
64
|
10 |
|
public function alias() |
65
|
|
|
{ |
66
|
10 |
|
return $this->config->get('alias'); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function hostname() |
70
|
|
|
{ |
71
|
3 |
|
return $this->config->get('hostname'); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function user() |
75
|
|
|
{ |
76
|
2 |
|
return $this->config->get('user'); |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
public function port() |
80
|
|
|
{ |
81
|
5 |
|
return $this->config->get('port'); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function configFile() |
85
|
|
|
{ |
86
|
3 |
|
return $this->config->get('config_file'); |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
public function identityFile() |
90
|
|
|
{ |
91
|
4 |
|
return $this->config->get('identity_file'); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public function forwardAgent() |
95
|
|
|
{ |
96
|
3 |
|
return $this->config->get('forward_agent'); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function sshMultiplexing() |
100
|
|
|
{ |
101
|
2 |
|
return $this->config->get('ssh_multiplexing'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function shell(): string |
105
|
|
|
{ |
106
|
|
|
return $this->config->get('shell'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getConnectionString(): string |
110
|
|
|
{ |
111
|
|
|
if ($this->get('remote_user') !== '') { |
112
|
|
|
return $this->get('remote_user') . '@' . $this->get('hostname'); |
113
|
|
|
} |
114
|
|
|
return $this->get('hostname'); |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
public function getSshArguments() |
118
|
|
|
{ |
119
|
3 |
|
$this->initOptions(); |
120
|
3 |
|
return $this->sshArguments; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// TODO: Migrate to configuration. |
124
|
2 |
|
public function sshOptions(array $options): self |
125
|
|
|
{ |
126
|
2 |
|
$this->sshArguments = $this->sshArguments->withOptions($options); |
127
|
2 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// TODO: Migrate to configuration. |
131
|
1 |
|
public function sshFlags(array $flags): self |
132
|
|
|
{ |
133
|
1 |
|
$this->sshArguments = $this->sshArguments->withFlags($flags); |
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
private function initOptions() |
138
|
|
|
{ |
139
|
3 |
|
if ($this->port()) { |
140
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-p', $this->port()); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
if ($this->configFile()) { |
144
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-F', $this->configFile()); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
if ($this->identityFile()) { |
148
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-i', $this->identityFile()); |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
if ($this->forwardAgent()) { |
152
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-A'); |
153
|
|
|
} |
154
|
3 |
|
} |
155
|
|
|
|
156
|
|
|
public function tag(): string |
157
|
|
|
{ |
158
|
|
|
if ($this->config->has('tag')) { |
159
|
|
|
return $this->config->get('tag'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (defined('NO_ANSI')) { |
163
|
|
|
return $this->alias(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($this->alias() === 'localhost') { |
167
|
|
|
return $this->alias(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (getenv('COLORTERM') === 'truecolor') { |
171
|
|
|
$hsv = function ($h, $s, $v) { |
172
|
|
|
$r = $g = $b = $i = $f = $p = $q = $t = 0; |
|
|
|
|
173
|
|
|
$i = floor($h * 6); |
174
|
|
|
$f = $h * 6 - $i; |
175
|
|
|
$p = $v * (1 - $s); |
176
|
|
|
$q = $v * (1 - $f * $s); |
177
|
|
|
$t = $v * (1 - (1 - $f) * $s); |
178
|
|
|
switch ($i % 6) { |
179
|
|
|
case 0: |
180
|
|
|
$r = $v; |
181
|
|
|
$g = $t; |
182
|
|
|
$b = $p; |
183
|
|
|
break; |
184
|
|
|
case 1: |
185
|
|
|
$r = $q; |
186
|
|
|
$g = $v; |
187
|
|
|
$b = $p; |
188
|
|
|
break; |
189
|
|
|
case 2: |
190
|
|
|
$r = $p; |
191
|
|
|
$g = $v; |
192
|
|
|
$b = $t; |
193
|
|
|
break; |
194
|
|
|
case 3: |
195
|
|
|
$r = $p; |
196
|
|
|
$g = $q; |
197
|
|
|
$b = $v; |
198
|
|
|
break; |
199
|
|
|
case 4: |
200
|
|
|
$r = $t; |
201
|
|
|
$g = $p; |
202
|
|
|
$b = $v; |
203
|
|
|
break; |
204
|
|
|
case 5: |
205
|
|
|
$r = $v; |
206
|
|
|
$g = $p; |
207
|
|
|
$b = $q; |
208
|
|
|
break; |
209
|
|
|
} |
210
|
|
|
$r = round($r * 255); |
211
|
|
|
$g = round($g * 255); |
212
|
|
|
$b = round($b * 255); |
213
|
|
|
return "\x1b[38;2;{$r};{$g};{$b}m"; |
214
|
|
|
}; |
215
|
|
|
|
216
|
|
|
$total = 100; |
217
|
|
|
$colors = []; |
218
|
|
|
for ($i = 0; $i < $total; $i++) { |
219
|
|
|
$colors[] = $hsv($i / $total, 1, .9); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$alias = $this->alias(); |
223
|
|
|
$tag = $colors[abs(crc32($alias)) % count($colors)]; |
224
|
|
|
|
225
|
|
|
return "{$tag}{$alias}\x1b[0m"; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
$colors = [ |
230
|
|
|
'fg=cyan;options=bold', |
231
|
|
|
'fg=green;options=bold', |
232
|
|
|
'fg=yellow;options=bold', |
233
|
|
|
'fg=cyan', |
234
|
|
|
'fg=blue', |
235
|
|
|
'fg=yellow', |
236
|
|
|
'fg=magenta', |
237
|
|
|
'fg=blue;options=bold', |
238
|
|
|
'fg=green', |
239
|
|
|
'fg=magenta;options=bold', |
240
|
|
|
'fg=red;options=bold', |
241
|
|
|
]; |
242
|
|
|
$alias = $this->alias(); |
243
|
|
|
$tag = $colors[abs(crc32($alias)) % count($colors)]; |
244
|
|
|
|
245
|
|
|
return "<{$tag}>{$alias}</>"; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.