| 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"
MC/DC0%0/1
$option{"ensure"} ||= "present"
$option{"ensure"}
Truth table0%0/2
A
A= $option{"ensure"}
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $name = $res_name; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( exists $option{host} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $name = $option{host}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $file = "/etc/hosts"; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( exists $option{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $option{"on_change"} and ref $option{"on_change"} eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$data->{"file"} ||= "/etc/hosts"
$data->{"file"}
Truth table0%0/2
A
A= $data->{"file"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$data->{"ip"} eq $cur_host[0]{"ip"} and join(" ", @{$data->{"aliases"} or [];}) eq join(" ", @{$cur_host[0]{"aliases"};})
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"};})
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | && join( " ", @{ $data->{"aliases"} || [] } ) eq | ||||||||||||||||||||||||||
|
Condition0%0/2
$data->{'aliases'} || []
MC/DC0%0/1
$data->{'aliases'} || []
$data->{'aliases'}
Truth table0%0/2
A
A= $data->{'aliases'}
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$file ||= "/etc/hosts"
$file
Truth table0%0/2
A
A= $file
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ( get_host( $host, { file => $file } ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
@lines and not ref $lines[0]
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]
| |||||||||||||||||||||||||||||
| 202 | 2 | @content = @lines; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | else { | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ( ref $lines[0] eq "HASH" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 217 | 1 | push @ret, $item; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||
| 220 | 7 | ▶ | push @ret, $item if ( grep { $_ eq $hostname } @{ $item->{aliases} } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 235 | 20 | ▶ | next if ( !$line ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 16 | ▶ | next if ( $line =~ m/^\s*$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||