/usr/share/perl5/Rex/Group.pm is in rex 1.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
package Rex::Group;
use strict;
use warnings;
our $VERSION = '1.4.1'; # VERSION
use Rex::Logger;
use attributes;
use Rex::Group::Entry::Server;
use vars qw(%groups);
use List::MoreUtils qw(uniq);
use Data::Dumper;
sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = {@_};
bless( $self, $proto );
for my $srv ( @{ $self->{servers} } ) {
$srv->append_to_group( $self->{name} );
}
return $self;
}
sub get_servers {
my ($self) = @_;
my @servers = map { ref( $_->to_s ) eq "CODE" ? &{ $_->to_s } : $_ }
@{ $self->{servers} };
return uniq @servers;
}
sub set_auth {
my ( $self, %data ) = @_;
$self->{auth} = \%data;
map { $_->set_auth( %{ $self->get_auth } ) } $self->get_servers;
}
sub get_auth {
my ($self) = @_;
return $self->{auth};
}
################################################################################
# STATIC FUNCTIONS
################################################################################
# Creates a new server group
# Possible calls:
# create_group(name => "g1", "srv1", "srv2");
# create_group(name => "g1", Rex::Group::Entry::Server->new(name => "srv1"), "srv2");
# create_group(name => "g1", "srv1" => { user => "other" }, "srv2");
sub create_group {
my $class = shift;
my $group_name = shift;
my @server = uniq grep { defined } @_;
my @server_obj;
for ( my $i = 0 ; $i <= $#server ; $i++ ) {
next if ref $server[$i] eq 'HASH'; # already processed by previous loop
# if argument is already a Rex::Group::Entry::Server
if ( ref $server[$i] && $server[$i]->isa("Rex::Group::Entry::Server") ) {
push @server_obj, $server[$i];
next;
}
# if next argument is a HashRef, use it as options for the server
my %options =
( $i < $#server and ref $server[ $i + 1 ] eq 'HASH' )
? %{ $server[ $i + 1 ] }
: ();
my $obj = Rex::Group::Entry::Server->new( name => $server[$i], %options );
push @server_obj, $obj;
}
$groups{$group_name} =
Rex::Group->new( servers => \@server_obj, name => $group_name );
}
# returns the servers in the group
sub get_group {
my $class = shift;
my $group_name = shift;
if ( exists $groups{$group_name} ) {
return $groups{$group_name}->get_servers;
}
return ();
}
sub is_group {
my $class = shift;
my $group_name = shift;
if ( defined $groups{$group_name} ) { return 1; }
return 0;
}
sub get_groups {
my $class = shift;
my %ret = ();
for my $key ( keys %groups ) {
$ret{$key} = [ $groups{$key}->get_servers ];
}
return %ret;
}
sub get_group_object {
my $class = shift;
my $name = shift;
return $groups{$name};
}
1;
|