| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Rex::Group::Lookup::INI - read host names and groups from an INI style file | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | With this module you can define host groups in an INI style file. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Rex::Group::Lookup::INI; | ||||||||||||||||||||||||||||
| 16 | groups_file 'file.ini'; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | package Rex::Group::Lookup::INI; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Rex -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 32 | 1 | ▶ | use base qw(Rex::Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Rex::Helper::INI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | @EXPORT = qw(groups_file); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head2 groups_file($file) | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | With this function you can read groups from INI style files. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | File example: | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # servers.ini | ||||||||||||||||||||||||||||
| 46 | [webservers] | ||||||||||||||||||||||||||||
| 47 | fe01 | ||||||||||||||||||||||||||||
| 48 | fe02 | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | [backends] | ||||||||||||||||||||||||||||
| 51 | be[01..03] | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | It supports the same expressions as the L<group|Rex::Commands/group> command. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Since 0.42, it also supports custom host properties if the L<use_server_auth|Rex/use_server_auth> feature flag is enabled: | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # servers.ini | ||||||||||||||||||||||||||||
| 58 | [webservers] | ||||||||||||||||||||||||||||
| 59 | server01 user=root password=foob4r sudo=true services=apache,memcache | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Rexfile | ||||||||||||||||||||||||||||
| 62 | use Rex -feature => ['use_server_auth']; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | task 'list_services', group => 'webservers', sub { | ||||||||||||||||||||||||||||
| 65 | say connection->server->option('services'); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub groups_file { | ||||||||||||||||||||||||||
|
Subroutine
groups_file: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 1 | my ($file) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | my $section; | |||||||||||||||||||||||||||
| 74 | my %hash; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | open( my $INI, "<", "$file" ) || die "Can't open $file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | my @lines = <$INI>; | |||||||||||||||||||||||||||
| 78 | 1 | chomp @lines; | |||||||||||||||||||||||||||
| 79 | 1 | close($INI); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | my $hash = Rex::Helper::INI::parse(@lines); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | for my $k ( keys %{$hash} ) { | |||||||||||||||||||||||||||
| 84 | 9 | my @servers; | |||||||||||||||||||||||||||
| 85 | 9 | for my $servername ( keys %{ $hash->{$k} } ) { | |||||||||||||||||||||||||||
| 86 | 25 | my $add = {}; | |||||||||||||||||||||||||||
| 87 | 25 | ▶ | if ( exists $hash->{$k}->{$servername} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $hash->{$k}{$servername} and ref $hash->{$k}{$servername} eq "HASH"
MC/DC50%1/2
exists $hash->{$k}{$servername} and ref $hash->{$k}{$servername} eq "HASH"
exists $hash->{$k}{$servername}
ref $hash->{$k}{$servername} eq "HASH"
Truth table66%2/3
A and B
A= exists $hash->{$k}{$servername}B= ref $hash->{$k}{$servername} eq "HASH"
| |||||||||||||||||||||||||||||
| 88 | && ref $hash->{$k}->{$servername} eq "HASH" ) | ||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 1 | $add = $hash->{$k}->{$servername}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 25 | my $obj = Rex::Group::Entry::Server->new( name => $servername, %{$add} ); | |||||||||||||||||||||||||||
| 94 | 25 | push @servers, $obj; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 9 | group( "$k" => @servers ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | 1; | |||||||||||||||||||||||||||