|
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('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
|
3 |
|
public function getSshArguments() |
|
110
|
|
|
{ |
|
111
|
3 |
|
$this->initOptions(); |
|
112
|
3 |
|
return $this->sshArguments; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// TODO: Migrate to configuration. |
|
116
|
2 |
|
public function sshOptions(array $options): self |
|
117
|
|
|
{ |
|
118
|
2 |
|
$this->sshArguments = $this->sshArguments->withOptions($options); |
|
119
|
2 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// TODO: Migrate to configuration. |
|
123
|
1 |
|
public function sshFlags(array $flags): self |
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->sshArguments = $this->sshArguments->withFlags($flags); |
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
3 |
|
private function initOptions() |
|
130
|
|
|
{ |
|
131
|
3 |
|
if ($this->port()) { |
|
132
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-p', $this->port()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
if ($this->configFile()) { |
|
136
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-F', $this->configFile()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
3 |
|
if ($this->identityFile()) { |
|
140
|
2 |
|
$this->sshArguments = $this->sshArguments->withFlag('-i', $this->identityFile()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
3 |
|
if ($this->forwardAgent()) { |
|
144
|
3 |
|
$this->sshArguments = $this->sshArguments->withFlag('-A'); |
|
145
|
|
|
} |
|
146
|
3 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function tag(): string |
|
149
|
|
|
{ |
|
150
|
|
|
if ($this->config->has('tag')) { |
|
151
|
|
|
return $this->config->get('tag'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if (defined('NO_ANSI')) { |
|
155
|
|
|
return $this->alias(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if (getenv('COLORTERM') === 'truecolor') { |
|
159
|
|
|
$hsv = function ($h, $s, $v) { |
|
160
|
|
|
$r = $g = $b = $i = $f = $p = $q = $t = 0; |
|
|
|
|
|
|
161
|
|
|
$i = floor($h * 6); |
|
162
|
|
|
$f = $h * 6 - $i; |
|
163
|
|
|
$p = $v * (1 - $s); |
|
164
|
|
|
$q = $v * (1 - $f * $s); |
|
165
|
|
|
$t = $v * (1 - (1 - $f) * $s); |
|
166
|
|
|
switch ($i % 6) { |
|
167
|
|
|
case 0: |
|
168
|
|
|
$r = $v; |
|
169
|
|
|
$g = $t; |
|
170
|
|
|
$b = $p; |
|
171
|
|
|
break; |
|
172
|
|
|
case 1: |
|
173
|
|
|
$r = $q; |
|
174
|
|
|
$g = $v; |
|
175
|
|
|
$b = $p; |
|
176
|
|
|
break; |
|
177
|
|
|
case 2: |
|
178
|
|
|
$r = $p; |
|
179
|
|
|
$g = $v; |
|
180
|
|
|
$b = $t; |
|
181
|
|
|
break; |
|
182
|
|
|
case 3: |
|
183
|
|
|
$r = $p; |
|
184
|
|
|
$g = $q; |
|
185
|
|
|
$b = $v; |
|
186
|
|
|
break; |
|
187
|
|
|
case 4: |
|
188
|
|
|
$r = $t; |
|
189
|
|
|
$g = $p; |
|
190
|
|
|
$b = $v; |
|
191
|
|
|
break; |
|
192
|
|
|
case 5: |
|
193
|
|
|
$r = $v; |
|
194
|
|
|
$g = $p; |
|
195
|
|
|
$b = $q; |
|
196
|
|
|
break; |
|
197
|
|
|
} |
|
198
|
|
|
$r = round($r * 255); |
|
199
|
|
|
$g = round($g * 255); |
|
200
|
|
|
$b = round($b * 255); |
|
201
|
|
|
return "\x1b[38;2;{$r};{$g};{$b}m"; |
|
202
|
|
|
}; |
|
203
|
|
|
|
|
204
|
|
|
$total = 100; |
|
205
|
|
|
$colors = []; |
|
206
|
|
|
for ($i = 0; $i < $total; $i++) { |
|
207
|
|
|
$colors[] = $hsv($i / $total, 1, .9); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$alias = $this->alias(); |
|
211
|
|
|
$tag = $colors[abs(crc32($alias)) % count($colors)]; |
|
212
|
|
|
|
|
213
|
|
|
return "{$tag}{$alias}\x1b[0m"; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
$colors = [ |
|
218
|
|
|
'fg=cyan;options=bold', |
|
219
|
|
|
'fg=green;options=bold', |
|
220
|
|
|
'fg=yellow;options=bold', |
|
221
|
|
|
'fg=cyan', |
|
222
|
|
|
'fg=blue', |
|
223
|
|
|
'fg=yellow', |
|
224
|
|
|
'fg=magenta', |
|
225
|
|
|
'fg=blue;options=bold', |
|
226
|
|
|
'fg=green', |
|
227
|
|
|
'fg=magenta;options=bold', |
|
228
|
|
|
'fg=red;options=bold', |
|
229
|
|
|
]; |
|
230
|
|
|
$alias = $this->alias(); |
|
231
|
|
|
$tag = $colors[abs(crc32($alias)) % count($colors)]; |
|
232
|
|
|
|
|
233
|
|
|
return "<{$tag}>{$alias}</>"; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
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.