File Coverage

lib/Rex/Group/Entry/Server.pm
Criterion Covered Total %
statement 146 202 72.2
branch 45 72 62.5
condition 30 52 57.6
subroutine 37 44 84.0
pod 0 26 0.0
total 258 396 65.1


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Group::Entry::Server;
6              
7 85     85   1104 use v5.12.5;
  85         369  
8 85     85   493 use warnings;
  85         190  
  85         3654  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 85     85   509 use Rex::Logger;
  85         217  
  85         639  
13 85     85   4217 use Rex::Helper::System;
  85         234  
  85         1042  
14 85     85   3418 use Rex::Config;
  85         192  
  85         678  
15 85     85   2205 use Rex::Interface::Exec;
  85         206  
  85         801  
16 85     85   2238 use Data::Dumper;
  85         174  
  85         4129  
17 85     85   1958 use Sort::Naturally;
  85         199222  
  85         4714  
18 85     85   681 use Symbol;
  85         311  
  85         5404  
19              
20 85     85   584 use List::Util 1.45 qw(uniq);
  85         2118  
  85         14715  
21              
22             use overload
23 157     157   17435 'eq' => sub { shift->is_eq(@_); },
24 246     246   1519 'ne' => sub { shift->is_ne(@_); },
25 742     742   3892 '""' => sub { shift->to_s(@_); },
26 85     85   717 'cmp' => sub { shift->compare(@_); };
  85     0   208  
  85         1397  
  0         0  
27              
28 85     85   8986 use attributes;
  85         4029  
  85         578  
29              
30             sub function {
31 1     1 0 15 my ( $class, $name, $code ) = @_;
32              
33 1         6 my $ref_to_function = qualify_to_ref( $name, $class );
34 1         11 *{$ref_to_function} = $code;
  1         10  
35             }
36              
37             sub new {
38 592     592 0 1684 my $that = shift;
39 592   33     3301 my $proto = ref($that) || $that;
40 592         2366 my $self = {@_};
41              
42 592         1655 bless( $self, $proto );
43              
44             # be save check if name is already a server ref
45 592 50       4449 if ( ref $self->{name} eq __PACKAGE__ ) {
46 0         0 return $self->{name};
47             }
48              
49             # rewrite auth info
50 592 100       1594 if ( $self->{user} ) {
51 1         10 $self->{auth}->{user} = $self->{user};
52 1         3 delete $self->{user};
53             }
54              
55 592 100       1623 if ( $self->{password} ) {
56 1         3 $self->{auth}->{password} = $self->{password};
57 1         2 delete $self->{password};
58             }
59              
60 592 50       1695 if ( $self->{port} ) {
61 0         0 $self->{auth}->{port} = $self->{port};
62 0         0 delete $self->{port};
63             }
64              
65 592 50       1512 if ( $self->{public_key} ) {
66 0         0 $self->{auth}->{public_key} = $self->{public_key};
67 0         0 delete $self->{public_key};
68             }
69              
70 592 50       1631 if ( $self->{private_key} ) {
71 0         0 $self->{auth}->{private_key} = $self->{private_key};
72 0         0 delete $self->{private_key};
73             }
74              
75 592 100       1559 if ( $self->{sudo} ) {
76 1         2 $self->{auth}->{sudo} = $self->{sudo};
77 1         2 delete $self->{sudo};
78             }
79              
80 592 50       1544 if ( $self->{sudo_password} ) {
81 0         0 $self->{auth}->{sudo_password} = $self->{sudo_password};
82 0         0 delete $self->{sudo_password};
83             }
84              
85 592 50       1419 if ( $self->{auth_type} ) {
86 0         0 $self->{auth}->{auth_type} = $self->{auth_type};
87 0         0 delete $self->{auth_type};
88             }
89              
90 592 100       1572 if ( !ref $self->{__group__} ) {
91 509         1460 $self->{__group__} = [];
92             }
93              
94 592         1977 return $self;
95             }
96              
97             sub get_servers {
98 31     31 0 69 my ($self) = @_;
99             return uniq map {
100 31 50 33     89 if ( ref $_ && $_->isa("Rex::Group::Entry::Server") ) {
  83         318  
101 83         275 $_;
102             }
103             else {
104 0         0 Rex::Group::Entry::Server->new( name => $_, auth => $self->{auth} );
105             }
106             } $self->evaluate_hostname;
107             }
108              
109             sub to_s {
110 1394     1394 0 2931 my ($self) = @_;
111 1394         11241 return $self->{name};
112             }
113              
114             sub is_eq {
115 157     157 0 307 my ( $self, $comp ) = @_;
116 157 100       277 if ( $comp eq $self->to_s ) {
117 38         217 return 1;
118             }
119             }
120              
121             sub is_ne {
122 246     246 0 1464 my ( $self, $comp ) = @_;
123 246 100       897 if ( $comp ne $self->to_s ) {
124 78         905 return 1;
125             }
126             }
127              
128             sub compare {
129 0     0 0 0 my ( $self, $comp ) = @_;
130 0         0 return ncmp( $self->to_s, $comp->to_s );
131             }
132              
133             sub has_auth {
134 0     0 0 0 my ($self) = @_;
135 0         0 return exists $self->{auth};
136             }
137              
138             sub set_auth {
139 3     3 0 11 my ( $self, %auth ) = @_;
140 3         18 $self->{auth} = \%auth;
141             }
142              
143             sub get_auth {
144 0     0 0 0 my ($self) = @_;
145 0         0 return $self->{auth};
146             }
147              
148             sub get_user {
149 88     88 0 242 my ($self) = @_;
150              
151 88 100       485 if ( exists $self->{auth}->{user} ) {
152 18         78 return $self->{auth}->{user};
153             }
154              
155 70 50 66     787 if (!Rex::Config->has_user
156             && Rex::Config->get_ssh_config_username( server => $self->to_s ) )
157             {
158 0         0 Rex::Logger::debug("Checking for a user in .ssh/config");
159 0         0 return Rex::Config->get_ssh_config_username( server => $self->to_s );
160             }
161              
162 70         620 return Rex::Config->get_user;
163             }
164              
165             sub get_password {
166 88     88 0 236 my ($self) = @_;
167              
168 88 100       371 if ( exists $self->{auth}->{password} ) {
169 18         47 return $self->{auth}->{password};
170             }
171              
172 70         531 return Rex::Config->get_password;
173             }
174              
175             sub get_port {
176 88     88 0 246 my ($self) = @_;
177              
178 88 50       323 if ( exists $self->{auth}->{port} ) {
179 0         0 return $self->{auth}->{port};
180             }
181              
182 88         602 return Rex::Config->get_port;
183             }
184              
185             sub get_public_key {
186 88     88 0 298 my ($self) = @_;
187              
188 88 50 66     917 if ( exists $self->{auth}->{public_key} && -f $self->{auth}->{public_key} ) {
189 0         0 Rex::Logger::debug(
190             "Rex::Group::Entry::Server (public_key): returning $self->{auth}->{public_key}"
191             );
192 0         0 return $self->{auth}->{public_key};
193             }
194              
195 88 50 66     392 if (!Rex::Config->has_public_key
196             && Rex::Config->get_ssh_config_public_key( server => $self->to_s ) )
197             {
198 0         0 Rex::Logger::debug("Checking for a public key in .ssh/config");
199 0         0 my $key = Rex::Config->get_ssh_config_public_key( server => $self->to_s );
200 0         0 Rex::Logger::debug(
201             "Rex::Group::Entry::Server (public_key): returning $key");
202 0         0 return $key;
203             }
204              
205 88   100     413 Rex::Logger::debug( "Rex::Group::Entry::Server (public_key): returning "
206             . ( Rex::Config->get_public_key || "" ) );
207 88         278 return Rex::Config->get_public_key;
208             }
209              
210             sub get_private_key {
211 88     88 0 209 my ($self) = @_;
212              
213 88 50 66     461 if ( exists $self->{auth}->{private_key} && -f $self->{auth}->{private_key} )
214             {
215             Rex::Logger::debug( "Rex::Group::Entry::Server (private_key): returning "
216 0         0 . $self->{auth}->{private_key} );
217 0         0 return $self->{auth}->{private_key};
218             }
219              
220 88 50 66     652 if (!Rex::Config->has_private_key
221             && Rex::Config->get_ssh_config_private_key( server => $self->to_s ) )
222             {
223 0         0 Rex::Logger::debug("Checking for a private key in .ssh/config");
224 0         0 my $key = Rex::Config->get_ssh_config_private_key( server => $self->to_s );
225 0         0 Rex::Logger::debug(
226             "Rex::Group::Entry::Server (private_key): returning " . $key );
227 0         0 return $key;
228             }
229              
230 88   100     324 Rex::Logger::debug( "Rex::Group::Entry::Server (private_key): returning "
231             . ( Rex::Config->get_private_key || "" ) );
232 88         434 return Rex::Config->get_private_key;
233             }
234              
235             sub get_auth_type {
236 88     88 0 222 my ($self) = @_;
237              
238 88 0 33     372 if ( exists $self->{auth}->{auth_type} && $self->{auth}->{auth_type} ) {
239 0         0 return $self->{auth}->{auth_type};
240             }
241              
242 88 50 66     1133 if ( exists $self->{auth}->{public_key}
    100 33        
    50 0        
    100 66        
    100 33        
      33        
243             && -f $self->{auth}->{public_key}
244             && exists $self->{auth}->{private_key}
245             && -f $self->{auth}->{private_key} )
246             {
247 0         0 return "try";
248             }
249             elsif ( exists $self->{auth}->{user}
250             && $self->{auth}->{user}
251             && exists $self->{auth}->{password}
252             && $self->{auth}->{password} ne "" )
253             {
254 18         45 return "try";
255             }
256             elsif ( Rex::Config->get_krb5_auth ) {
257 0         0 return "krb5";
258             }
259             elsif ( Rex::Config->get_password_auth ) {
260 7         16 return "pass";
261             }
262             elsif ( Rex::Config->get_key_auth ) {
263 1         6 return "key";
264             }
265              
266 62         644 return "try";
267             }
268              
269             sub get_sudo {
270 88     88 0 248 my ($self) = @_;
271 88 100       301 if ( exists $self->{auth}->{sudo} ) {
272 3         10 return $self->{auth}->{sudo};
273             }
274              
275 85         588 return 0;
276             }
277              
278             sub get_sudo_password {
279 88     88 0 248 my ($self) = @_;
280 88 50       268 if ( exists $self->{auth}->{sudo_password} ) {
281 0         0 return $self->{auth}->{sudo_password};
282             }
283              
284 88         365 Rex::Config->get_sudo_password;
285             }
286              
287             sub merge_auth {
288 88     88 0 394 my ( $self, $other_auth ) = @_;
289              
290 88         205 my %new_auth;
291 88         1614 my @keys =
292             qw/user password port private_key public_key auth_type sudo sudo_password/;
293              
294 88         377 for my $key (@keys) {
295 704         1460 my $call = "get_$key";
296 704 50       4955 if ( ref($self)->can($call) ) {
297 704         2354 $new_auth{$key} = $self->$call();
298             }
299             else {
300 0         0 $new_auth{$key} = $other_auth->{$key};
301             }
302              
303             # other_auth has presedence
304 704 100 100     4256 if ( exists $other_auth->{$key}
      100        
305             && defined $other_auth->{$key}
306             && Rex::Config->get_use_server_auth() == 0 )
307             {
308 101         406 $new_auth{$key} = $other_auth->{$key};
309             }
310             }
311              
312 88         1116 return %new_auth;
313             }
314              
315             sub append_to_group {
316 36     36 0 81 my ( $self, $group ) = @_;
317 36         48 push @{ $self->{__group__} }, $group;
  36         112  
318             }
319              
320             sub group {
321 0     0 0 0 my ($self) = @_;
322 0         0 return $self->groups;
323             }
324              
325             sub groups {
326 0     0 0 0 my ($self) = @_;
327 0         0 return @{ $self->{__group__} };
  0         0  
328             }
329              
330             sub option {
331 2     2 0 1783 my ( $self, $option ) = @_;
332 2 50       34 if ( exists $self->{$option} ) {
333 2         34 return $self->{$option};
334             }
335             }
336              
337             sub gather_information {
338 0     0 0 0 my ($self) = @_;
339 0         0 my %info = Rex::Helper::System::info();
340 0         0 $self->{__hardware_info__} = \%info;
341             }
342              
343             sub AUTOLOAD {
344 85     85   161059 use vars qw($AUTOLOAD);
  85         211  
  85         29613  
345 371     371   5251 my $self = shift;
346              
347 371 50       17893 return $self if ( $AUTOLOAD =~ m/DESTROY/ );
348              
349 0         0 my ($wanted_data) = ( $AUTOLOAD =~ m/::([a-z0-9A-Z_]+)$/ );
350              
351 0 0       0 if ( scalar( keys %{ $self->{__hardware_info__} } ) == 0 ) {
  0         0  
352 0         0 $self->gather_information;
353             }
354              
355 0 0       0 if ( exists $self->{__hardware_info__}->{$wanted_data} ) {
356 0         0 return $self->{__hardware_info__}->{$wanted_data};
357             }
358              
359 0 0       0 if ( exists $self->{$wanted_data} ) {
360 0         0 return $self->{$wanted_data};
361             }
362              
363 0         0 return;
364             }
365              
366             sub evaluate_hostname {
367 31     31 0 50 my ($self) = @_;
368              
369 31         66 my @servers = Rex::Commands::evaluate_hostname( $self->to_s );
370 31         57 my @multiple_me;
371              
372 31         62 for (@servers) {
373 83         145 push @multiple_me, ref($self)->new( %{$self} );
  83         245  
374 83         167 $multiple_me[-1]->{name} = $_;
375             }
376              
377 31         92 return @multiple_me;
378             }
379              
380             sub test_perl {
381 36     36 0 208 my ($self) = @_;
382 36         889 my $exec = Rex::Interface::Exec->create;
383 36         414 return $exec->can_run( ["perl"] ); # use a new anon ref, so that we don't have drawbacks if some lower layers will manipulate things.
384             }
385              
386             1;