| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Rex::Group; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 58 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 58 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 58 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 58 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 58 | ▶ | use attributes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 58 | ▶ | use Rex::Group::Entry::Server; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 58 | ▶ | use vars qw(%groups); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 58 | ▶ | use List::Util 1.45 qw(uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 58 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 18 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 18 | my $that = shift; | |||||||||||||||||||||||||||
| 23 | 18 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||
| 24 | 18 | my $self = {@_}; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 18 | bless( $self, $proto ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 18 | for my $srv ( @{ $self->{servers} } ) { | |||||||||||||||||||||||||||
| 29 | 40 | $srv->append_to_group( $self->{name} ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 40 | for my $group ( keys %groups ) { | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 100 | for my $host ( $groups{$group}->get_servers ) { | |||||||||||||||||||||||||||
| 34 | 283 | ▶ | if ( $host eq $srv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 12 | $host->append_to_group( $self->{name} ); | |||||||||||||||||||||||||||
| 36 | 12 | $srv->append_to_group($group); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 18 | return $self; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 125 | ▶ | sub get_servers { | ||||||||||||||||||||||||||
|
Subroutine
get_servers: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 125 | my ($self) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my @servers = map { ref( $_->to_s ) eq "CODE" ? &{ $_->to_s } : $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 125 | @{ $self->{servers} }; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 125 | return uniq @servers; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | sub set_auth { | ||||||||||||||||||||||||||
|
Subroutine
set_auth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 3 | my ( $self, %data ) = @_; | |||||||||||||||||||||||||||
| 56 | 3 | $self->{auth} = \%data; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | map { $_->set_auth( %{ $self->get_auth } ) } $self->get_servers; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | ▶ | sub get_auth { | ||||||||||||||||||||||||||
|
Subroutine
get_auth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | 3 | return $self->{auth}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ################################################################################ | ||||||||||||||||||||||||||||
| 67 | # STATIC FUNCTIONS | ||||||||||||||||||||||||||||
| 68 | ################################################################################ | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Creates a new server group | ||||||||||||||||||||||||||||
| 71 | # Possible calls: | ||||||||||||||||||||||||||||
| 72 | # create_group(name => "g1", "srv1", "srv2"); | ||||||||||||||||||||||||||||
| 73 | # create_group(name => "g1", Rex::Group::Entry::Server->new(name => "srv1"), "srv2"); | ||||||||||||||||||||||||||||
| 74 | # create_group(name => "g1", "srv1" => { user => "other" }, "srv2"); | ||||||||||||||||||||||||||||
| 75 | 18 | ▶ | sub create_group { | ||||||||||||||||||||||||||
|
Subroutine
create_group: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 18 | my $class = shift; | |||||||||||||||||||||||||||
| 77 | 18 | my $group_name = shift; | |||||||||||||||||||||||||||
| 78 | 18 | my @server = uniq grep { defined } @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 18 | my @server_obj; | |||||||||||||||||||||||||||
| 81 | 18 | ▶ | for ( my $i = 0 ; $i <= $#server ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 40 | ▶ | next if ref $server[$i] eq 'HASH'; # already processed by previous loop | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # if argument is already a Rex::Group::Entry::Server | ||||||||||||||||||||||||||||
| 85 | 40 | ▶ | if ( ref $server[$i] && $server[$i]->isa("Rex::Group::Entry::Server") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $server[$i] and $server[$i]->isa("Rex::Group::Entry::Server")
MC/DC50%1/2
ref $server[$i] and $server[$i]->isa("Rex::Group::Entry::Server")
ref $server[$i]
$server[$i]->isa("Rex::Group::Entry::Server")
Truth table66%2/3
A and B
A= ref $server[$i]B= $server[$i]->isa("Rex::Group::Entry::Server")
| |||||||||||||||||||||||||||||
| 86 | 25 | push @server_obj, $server[$i]; | |||||||||||||||||||||||||||
| 87 | 25 | next; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # if next argument is a HashRef, use it as options for the server | ||||||||||||||||||||||||||||
| 91 | my %options = | ||||||||||||||||||||||||||||
| 92 | ( $i < $#server and ref $server[ $i + 1 ] eq 'HASH' ) | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | ? %{ $server[ $i + 1 ] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i < $#server and ref $server[$i + 1] eq "HASH"
MC/DC0%0/2
$i < $#server and ref $server[$i + 1] eq "HASH"
$i < $#server
ref $server[$i + 1] eq "HASH"
Truth table66%2/3
A and B
A= $i < $#serverB= ref $server[$i + 1] eq "HASH"
| |||||||||||||||||||||||||||||
| 94 | : (); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 15 | my $obj = Rex::Group::Entry::Server->new( name => $server[$i], %options ); | |||||||||||||||||||||||||||
| 97 | 15 | push @server_obj, $obj; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 18 | $groups{$group_name} = | |||||||||||||||||||||||||||
| 101 | Rex::Group->new( servers => \@server_obj, name => $group_name ); | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # returns the servers in the group | ||||||||||||||||||||||||||||
| 105 | 13 | ▶ | sub get_group { | ||||||||||||||||||||||||||
|
Subroutine
get_group: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 13 | my $class = shift; | |||||||||||||||||||||||||||
| 107 | 13 | my $group_name = shift; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 13 | ▶ | if ( exists $groups{$group_name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 13 | return $groups{$group_name}->get_servers; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | return (); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 8 | ▶ | sub is_group { | ||||||||||||||||||||||||||
|
Subroutine
is_group: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 8 | my $class = shift; | |||||||||||||||||||||||||||
| 118 | 8 | my $group_name = shift; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 8 | ▶ | if ( defined $groups{$group_name} ) { return 1; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | return 0; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | sub get_groups { | ||||||||||||||||||||||||||
|
Subroutine
get_groups: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 126 | 1 | my %ret = (); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | for my $key ( keys %groups ) { | |||||||||||||||||||||||||||
| 129 | 9 | $ret{$key} = [ $groups{$key}->get_servers ]; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 1 | return %ret; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 6 | ▶ | sub get_group_object { | ||||||||||||||||||||||||||
|
Subroutine
get_group_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 137 | 6 | my $name = shift; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 6 | return $groups{$name}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 58 | 1; | |||||||||||||||||||||||||||