lib/Rex/Commands/Host.pm

filter: Statementstmt 47.8% 55 / 115 Branchbran 29.4% 10 / 34 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 78.5% 11 / 14 Podpod 100% 4 / 4 totaltotal 41.7% 81 / 194 CC 27 SCAR 34.7
CC
27
Cov
40%
host_entry
72.0
create_host
72.0
delete_host
12.0
CRAP 32.1
« lib/Rex/Virtualization/LibVirt/list.pm ↑ Summary blib/lib/Rex/Interface/Connection/SSH.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Host - Edit /etc/hosts
8  
9 =head1 DESCRIPTION
10  
11 With this module you can manage the host entries in /etc/hosts.
12  
13 =head1 SYNOPSIS
14  
15 task "create-host", "remoteserver", sub {
16 create_host "rexify.org" => {
17 ip => "88.198.93.110",
18 aliases => ["www.rexify.org"],
19 };
20 };
21  
22 =head1 EXPORTED FUNCTIONS
23  
24 =cut
25  
26 package Rex::Commands::Host;
27  
28 33 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 33 our $VERSION = '1.16.1'; # VERSION
32  
33 33 require Rex::Exporter;
34 33 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 33 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 33 ▶use Rex::Commands::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 33 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 33 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 33 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 33 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 33 @EXPORT = qw(create_host get_host delete_host host_entry);
44  
45 =head2 host_entry($name, %option)
46  
47 Manages the entries in /etc/hosts.
48  
49 host_entry "rexify.org",
50 ensure => "present",
51 ip => "88.198.93.110",
52 aliases => ["www.rexify.org"],
53 on_change => sub { say "added host entry"; };
54  
55 host_entry "rexify.org",
56 ensure => "absent",
57 on_change => sub { say "removed host entry"; };
58  
59 =cut
60  
61 0 ▶sub host_entry {
Subroutine host_entry: not called CC=8 SCAR=42.8
Pod documented
62 0 my ( $res_name, %option ) = @_;
63  
64 0 ▶ $option{ensure} ||= "present";
Condition0%0/2
$option{"ensure"} ||= "present"
l!l
00
MC/DC0%0/1
$option{"ensure"} ||= "present"
$option{"ensure"}
Truth table0%0/2
A
A= $option{"ensure"}
Aresult
11
00
65  
66 0 my $name = $res_name;
67 0 ▶ if ( exists $option{host} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"host"}) 0 0 0
68 0 $name = $option{host};
69 }
70  
71 0 my $file = "/etc/hosts";
72  
73 0 ▶ if ( exists $option{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"file"}) 0 0 0
74 0 $file = $option{file};
75 }
76  
77 Rex::get_current_connection()->{reporter}
78 0 ->report_resource_start( type => "host_entry", name => $res_name );
79  
80 0 my $old_md5 = md5($file);
81 0 ▶ if ( $option{ensure} eq "present" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{'ensure'} eq 'present') { } 0 0 0
82 0 &create_host( $name, \%option );
83 }
84 else {
85 0 &delete_host($name);
86 }
87 0 my $new_md5 = md5($file);
88  
89 0 ▶ if ( $new_md5 ne $old_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_md5 ne $old_md5) 0 0 0
90 0 ▶ if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"on_change"} and ref $option{"on_change"} eq "CODE") 0 0 0
Condition0%0/3
exists $option{"on_change"} and ref $option{"on_change"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option{"on_change"} and ref $option{"on_change"} eq "CODE"
exists $option{"on_change"} ref $option{"on_change"} eq "CODE"
Truth table0%0/3
A and B
A= exists $option{"on_change"}B= ref $option{"on_change"} eq "CODE"
ABresult
0X0
100
111
91 0 $option{on_change}->( $name, %option );
92 }
93  
94 Rex::get_current_connection()->{reporter}->report(
95 0 changed => 1,
96 message => "Resource host_entry changed to $option{ensure}"
97 );
98 }
99  
100 Rex::get_current_connection()->{reporter}
101 0 ->report_resource_end( type => "host_entry", name => $res_name );
102 }
103  
104 =head2 create_host($)
105  
106 Update or create a /etc/hosts entry.
107  
108 create_host "rexify.org", {
109 ip => "88.198.93.110",
110 aliases => ["www.rexify.org", ...]
111 };
112  
113 =cut
114  
115 0 ▶sub create_host {
Subroutine create_host: not called CC=8 SCAR=42.8
Pod documented
116 0 my ( $host, $data ) = @_;
117  
118 0 ▶ if ( !defined $data->{"ip"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->{"ip"}) 0 0 0
119 0 Rex::Logger::info("You need to set an ip for $host");
120 0 die("You need to set an ip for $host");
121 }
122  
123 0 ▶ $data->{file} ||= "/etc/hosts";
Condition0%0/2
$data->{"file"} ||= "/etc/hosts"
l!l
00
MC/DC0%0/1
$data->{"file"} ||= "/etc/hosts"
$data->{"file"}
Truth table0%0/2
A
A= $data->{"file"}
Aresult
11
00
124  
125 0 Rex::Logger::debug("Creating host $host");
126  
127 0 my @cur_host = get_host( $host, { file => $data->{file} } );
128 0 ▶ if ( !@cur_host ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @cur_host) { } 0 0 0
129 0 my $fh = file_append $data->{file};
130 0 $fh->write( $data->{"ip"} . "\t" . $host );
131 0 ▶ if ( exists $data->{"aliases"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"aliases"}) 0 0 0
132 0 $fh->write( " " . join( " ", @{ $data->{"aliases"} } ) );
133 }
134 0 $fh->write("\n");
135 0 $fh->close;
136 }
137 else {
138 0 ▶ if ( $data->{"ip"} eq $cur_host[0]->{"ip"}
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"ip"} eq $cur_host[0]{"ip"} and join(" ", @{$data->{"aliases"} or [];}) eq join(" ", @{$cur_host[0]{"aliases"};})) 0 0 0
Condition0%0/3
$data->{"ip"} eq $cur_host[0]{"ip"} and join(" ", @{$data->{"aliases"} or [];}) eq join(" ", @{$cur_host[0]{"aliases"};})
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"ip"} eq $cur_host[0]{"ip"} and join(" ", @{$data->{"aliases"} or [];}) eq join(" ", @{$cur_host[0]{"aliases"};})
$data->{"ip"} eq $cur_host[0]{"ip"} join(" ", @{$data->{"aliases"} or [];}) eq join(" ", @{$cur_host[0]{"aliases"};})
Truth table0%0/3
A and B
A= $data->{"ip"} eq $cur_host[0]{"ip"}B= join(" ", @{$data->{"aliases"} or [];}) eq join(" ", @{$cur_host[0]{"aliases"};})
ABresult
0X0
100
111
139 0 ▶ && join( " ", @{ $data->{"aliases"} || [] } ) eq
Condition0%0/2
$data->{'aliases'} || []
l!l
00
MC/DC0%0/1
$data->{'aliases'} || []
$data->{'aliases'}
Truth table0%0/2
A
A= $data->{'aliases'}
Aresult
11
00
140 0 join( " ", @{ $cur_host[0]->{"aliases"} } ) )
141 {
142  
143 0 Rex::Logger::debug("Nothing to update for host $host");
144 0 return;
145  
146 }
147 0 Rex::Logger::debug("Host already exists. Updating...");
148  
149 0 delete_host( $host, $data->{file} );
150 0 return create_host(@_);
151 }
152 }
153  
154 =head2 delete_host($host)
155  
156 Delete a host from /etc/hosts.
157  
158 delete_host "www.rexify.org";
159  
160 =cut
161  
162 0 ▶sub delete_host {
Subroutine delete_host: not called CC=3 SCAR=24.8
Pod documented
163 0 my ( $host, $file ) = @_;
164  
165 0 Rex::Logger::debug("Deleting host $host");
166 0 ▶ $file ||= "/etc/hosts";
Condition0%0/2
$file ||= "/etc/hosts"
l!l
00
MC/DC0%0/1
$file ||= "/etc/hosts"
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
167  
168 0 ▶ if ( get_host( $host, { file => $file } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_host($host, {'file', $file})) { } 0 0 0
169 0 my $fh = file_read $file;
170 0 my @content = $fh->read_all;
171 0 $fh->close;
172  
173 0 my @new_content = grep { !/\s\Q$host\E\b/ } @content;
174  
175 0 $fh = file_write $file;
176 0 $fh->write(@new_content);
177 0 $fh->close;
178 }
179 else {
180 0 Rex::Logger::debug("Host does not exists.");
181 }
182 }
183  
184 =head2 get_host($host)
185  
186 Returns the information of $host in /etc/hosts.
187  
188 my @host_info = get_host "localhost";
189 say "Host-IP: " . $host_info[0]->{"ip"};
190  
191 =cut
192  
193 2 ▶sub get_host {
Subroutine get_host: called CC=7 SCAR=21.6
Pod documented
194 2 my ( $hostname, @lines ) = @_;
195  
196 2 Rex::Logger::debug("Getting host ($hostname) information");
197  
198 2 my $file = "/etc/hosts";
199  
200 2 my @content;
201 2 ▶ if ( @lines && !ref $lines[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and not ref $lines[0]) { } 2 0 2
Condition33%1/3
@lines and not ref $lines[0]
!ll&&!rl&&r
002
MC/DC0%0/2
@lines and not ref $lines[0]
@lines not ref $lines[0]
Truth table33%1/3
A and not(B)
A= @linesB= not ref $lines[0]
ABresult
0X0
100
111
202 2 @content = @lines;
203 }
204 else {
205 0 ▶ if ( ref $lines[0] eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lines[0] eq "HASH") 0 0 0
206 0 $file = $lines[0]->{file};
207 }
208 0 my $fh = file_read $file;
209 0 @content = $fh->read_all;
210 0 $fh->close;
211 }
212  
213 2 my @hosts = _parse_hosts(@content);
214 2 my @ret;
215 2 for my $item (@hosts) {
216 8 ▶ if ( $item->{host} eq $hostname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{'host'} eq $hostname) { } 1 7 8
217 1 push @ret, $item;
218 }
219 else {
220 7 ▶ push @ret, $item if ( grep { $_ eq $hostname } @{ $item->{aliases} } );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $hostname;} @{$$item{"aliases"};} 1 6 7
221 }
222 }
223  
224 2 return @ret;
225 }
226  
227 4 ▶sub _parse_hosts {
Subroutine _parse_hosts: called CC=5 SCAR=16.1
228 4 my (@lines) = @_;
229  
230 4 my @ret;
231  
232 4 for my $line (@lines) {
233 24 chomp $line;
234 24 ▶ next if ( $line =~ m/^#/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 4 20 24
235 20 ▶ next if ( !$line );
Branch: 1 branch
BranchTrueFalseTotal
unless $line 4 16 20
236 16 ▶ next if ( $line =~ m/^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/u 0 16 16
237  
238 16 my ( $ip, $_host, @aliases ) = split( /\s+/, $line );
239  
240 16 push @ret,
241 {
242 ip => $ip,
243 host => $_host,
244 aliases => \@aliases,
245 };
246  
247 }
248  
249 4 return @ret;
250 }
251  
252 33 1;
« lib/Rex/Virtualization/LibVirt/list.pm ↑ Summary blib/lib/Rex/Interface/Connection/SSH.pm »