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   1212 use v5.12.5;
  85         349  
8 85     85   616 use warnings;
  85         218  
  85         3689  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 85     85   544 use Rex::Logger;
  85         320  
  85         662  
13 85     85   4151 use Rex::Helper::System;
  85         288  
  85         1002  
14 85     85   3568 use Rex::Config;
  85         182  
  85         591  
15 85     85   2125 use Rex::Interface::Exec;
  85         277  
  85         935  
16 85     85   2378 use Data::Dumper;
  85         231  
  85         4273  
17 85     85   2113 use Sort::Naturally;
  85         208355  
  85         5135  
18 85     85   705 use Symbol;
  85         361  
  85         6083  
19              
20 85     85   585 use List::Util 1.45 qw(uniq);
  85         2178  
  85         15109  
21              
22             use overload
23 157     157   17173 'eq' => sub { shift->is_eq(@_); },
24 246     246   1608 'ne' => sub { shift->is_ne(@_); },
25 742     742   3960 '""' => sub { shift->to_s(@_); },
26 85     85   716 'cmp' => sub { shift->compare(@_); };
  85     0   192  
  85         1465  
  0         0  
27              
28 85     85   9904 use attributes;
  85         2699  
  85         594  
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         13 *{$ref_to_function} = $code;
  1         9  
35             }
36              
37             sub new {
38 592     592 0 1660 my $that = shift;
39 592   33     3590 my $proto = ref($that) || $that;
40 592         2724 my $self = {@_};
41              
42 592         1904 bless( $self, $proto );
43              
44             # be save check if name is already a server ref
45 592 50       4737 if ( ref $self->{name} eq __PACKAGE__ ) {
46 0         0 return $self->{name};
47             }
48              
49             # rewrite auth info
50 592 100       1681 if ( $self->{user} ) {
51 1         7 $self->{auth}->{user} = $self->{user};
52 1         2 delete $self->{user};
53             }
54              
55 592 100       1734 if ( $self->{password} ) {
56 1         3 $self->{auth}->{password} = $self->{password};
57 1         1 delete $self->{password};
58             }
59              
60 592 50       1638 if ( $self->{port} ) {
61 0         0 $self->{auth}->{port} = $self->{port};
62 0         0 delete $self->{port};
63             }
64              
65 592 50       1522 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       1441 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       1480 if ( $self->{sudo} ) {
76 1         3 $self->{auth}->{sudo} = $self->{sudo};
77 1         13 delete $self->{sudo};
78             }
79              
80 592 50       1670 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       1530 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         1522 $self->{__group__} = [];
92             }
93              
94 592         1916 return $self;
95             }
96              
97             sub get_servers {
98 31     31 0 73 my ($self) = @_;
99             return uniq map {
100 31 50 33     81 if ( ref $_ && $_->isa("Rex::Group::Entry::Server") ) {
  83         304  
101 83         249 $_;
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 2825 my ($self) = @_;
111 1394         11143 return $self->{name};
112             }
113              
114             sub is_eq {
115 157     157 0 314 my ( $self, $comp ) = @_;
116 157 100       270 if ( $comp eq $self->to_s ) {
117 38         213 return 1;
118             }
119             }
120              
121             sub is_ne {
122 246     246 0 1312 my ( $self, $comp ) = @_;
123 246 100       981 if ( $comp ne $self->to_s ) {
124 78         875 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 20 my ( $self, %auth ) = @_;
140 3         21 $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 222 my ($self) = @_;
150              
151 88 100       497 if ( exists $self->{auth}->{user} ) {
152 18         68 return $self->{auth}->{user};
153             }
154              
155 70 50 66     793 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         461 return Rex::Config->get_user;
163             }
164              
165             sub get_password {
166 88     88 0 259 my ($self) = @_;
167              
168 88 100       361 if ( exists $self->{auth}->{password} ) {
169 18         48 return $self->{auth}->{password};
170             }
171              
172 70         625 return Rex::Config->get_password;
173             }
174              
175             sub get_port {
176 88     88 0 253 my ($self) = @_;
177              
178 88 50       388 if ( exists $self->{auth}->{port} ) {
179 0         0 return $self->{auth}->{port};
180             }
181              
182 88         504 return Rex::Config->get_port;
183             }
184              
185             sub get_public_key {
186 88     88 0 320 my ($self) = @_;
187              
188 88 50 66     863 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     524 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     342 Rex::Logger::debug( "Rex::Group::Entry::Server (public_key): returning "
206             . ( Rex::Config->get_public_key || "" ) );
207 88         264 return Rex::Config->get_public_key;
208             }
209              
210             sub get_private_key {
211 88     88 0 252 my ($self) = @_;
212              
213 88 50 66     526 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     624 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     714 Rex::Logger::debug( "Rex::Group::Entry::Server (private_key): returning "
231             . ( Rex::Config->get_private_key || "" ) );
232 88         321 return Rex::Config->get_private_key;
233             }
234              
235             sub get_auth_type {
236 88     88 0 223 my ($self) = @_;
237              
238 88 0 33     387 if ( exists $self->{auth}->{auth_type} && $self->{auth}->{auth_type} ) {
239 0         0 return $self->{auth}->{auth_type};
240             }
241              
242 88 50 66     1044 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         53 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         17 return "pass";
261             }
262             elsif ( Rex::Config->get_key_auth ) {
263 1         4 return "key";
264             }
265              
266 62         617 return "try";
267             }
268              
269             sub get_sudo {
270 88     88 0 231 my ($self) = @_;
271 88 100       382 if ( exists $self->{auth}->{sudo} ) {
272 3         8 return $self->{auth}->{sudo};
273             }
274              
275 85         554 return 0;
276             }
277              
278             sub get_sudo_password {
279 88     88 0 275 my ($self) = @_;
280 88 50       305 if ( exists $self->{auth}->{sudo_password} ) {
281 0         0 return $self->{auth}->{sudo_password};
282             }
283              
284 88         567 Rex::Config->get_sudo_password;
285             }
286              
287             sub merge_auth {
288 88     88 0 345 my ( $self, $other_auth ) = @_;
289              
290 88         189 my %new_auth;
291 88         1673 my @keys =
292             qw/user password port private_key public_key auth_type sudo sudo_password/;
293              
294 88         361 for my $key (@keys) {
295 704         1584 my $call = "get_$key";
296 704 50       5110 if ( ref($self)->can($call) ) {
297 704         2255 $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     4461 if ( exists $other_auth->{$key}
      100        
305             && defined $other_auth->{$key}
306             && Rex::Config->get_use_server_auth() == 0 )
307             {
308 101         438 $new_auth{$key} = $other_auth->{$key};
309             }
310             }
311              
312 88         999 return %new_auth;
313             }
314              
315             sub append_to_group {
316 36     36 0 79 my ( $self, $group ) = @_;
317 36         47 push @{ $self->{__group__} }, $group;
  36         101  
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 1847 my ( $self, $option ) = @_;
332 2 50       22 if ( exists $self->{$option} ) {
333 2         38 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   169753 use vars qw($AUTOLOAD);
  85         249  
  85         31853  
345 371     371   5191 my $self = shift;
346              
347 371 50       19752 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 54 my ($self) = @_;
368              
369 31         65 my @servers = Rex::Commands::evaluate_hostname( $self->to_s );
370 31         52 my @multiple_me;
371              
372 31         63 for (@servers) {
373 83         136 push @multiple_me, ref($self)->new( %{$self} );
  83         280  
374 83         184 $multiple_me[-1]->{name} = $_;
375             }
376              
377 31         81 return @multiple_me;
378             }
379              
380             sub test_perl {
381 36     36 0 181 my ($self) = @_;
382 36         808 my $exec = Rex::Interface::Exec->create;
383 36         467 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;