/usr/share/perl5/Rex/Commands/Host.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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
=head1 NAME
Rex::Commands::Host - Edit /etc/hosts
=head1 DESCRIPTION
With this module you can manage the host entries in /etc/hosts.
=head1 SYNOPSIS
task "create-host", "remoteserver", sub {
create_host "rexify.org" => {
ip => "88.198.93.110",
aliases => ["www.rexify.org"],
};
};
=head1 EXPORTED FUNCTIONS
=cut
package Rex::Commands::Host;
use strict;
use warnings;
our $VERSION = '1.4.1'; # VERSION
require Rex::Exporter;
use Rex::Commands::Run;
use Rex::Commands::Fs;
use Rex::Commands::File;
use Rex::Commands::MD5;
use Rex::Logger;
use Data::Dumper;
use vars qw(@EXPORT);
use base qw(Rex::Exporter);
@EXPORT = qw(create_host get_host delete_host host_entry);
=head2 host_entry($name, %option)
Manages the entries in /etc/hosts.
host_entry "rexify.org",
ensure => "present",
ip => "88.198.93.110",
aliases => ["www.rexify.org"],
on_change => sub { say "added host entry"; };
host_entry "rexify.org",
ensure => "absent",
on_change => sub { say "removed host entry"; };
=cut
sub host_entry {
my ( $res_name, %option ) = @_;
$option{ensure} ||= "present";
my $name = $res_name;
if ( exists $option{host} ) {
$name = $option{host};
}
my $file = "/etc/hosts";
if ( exists $option{file} ) {
$file = $option{file};
}
Rex::get_current_connection()->{reporter}
->report_resource_start( type => "host_entry", name => $res_name );
my $old_md5 = md5($file);
if ( $option{ensure} eq "present" ) {
&create_host( $name, \%option );
}
else {
&delete_host($name);
}
my $new_md5 = md5($file);
if ( $new_md5 ne $old_md5 ) {
if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) {
$option{on_change}->( $name, %option );
}
Rex::get_current_connection()->{reporter}->report(
changed => 1,
message => "Resource host_entry changed to $option{ensure}"
);
}
Rex::get_current_connection()->{reporter}
->report_resource_end( type => "host_entry", name => $res_name );
}
=head2 create_host($)
Update or create a /etc/hosts entry.
create_host "rexify.org", {
ip => "88.198.93.110",
aliases => ["www.rexify.org", ...]
};
=cut
sub create_host {
my ( $host, $data ) = @_;
if ( !defined $data->{"ip"} ) {
Rex::Logger::info("You need to set an ip for $host");
die("You need to set an ip for $host");
}
$data->{file} ||= "/etc/hosts";
Rex::Logger::debug("Creating host $host");
my @cur_host = get_host( $host, { file => $data->{file} } );
if ( !@cur_host ) {
my $fh = file_append $data->{file};
$fh->write( $data->{"ip"} . "\t" . $host );
if ( exists $data->{"aliases"} ) {
$fh->write( " " . join( " ", @{ $data->{"aliases"} } ) );
}
$fh->write("\n");
$fh->close;
}
else {
my @host = get_host( $host, { file => $data->{file} } );
if ( $data->{"ip"} eq $host[0]->{"ip"}
&& join( " ", @{ $data->{"aliases"} || [] } ) eq
join( " ", @{ $host[0]->{"aliases"} } ) )
{
Rex::Logger::debug("Nothing to update for host $host");
return;
}
Rex::Logger::debug("Host already exists. Updating...");
delete_host( $host, $data->{file} );
return create_host(@_);
}
}
=head2 delete_host($host)
Delete a host from /etc/hosts.
delete_host "www.rexify.org";
=cut
sub delete_host {
my ( $host, $file ) = @_;
Rex::Logger::debug("Deleting host $host");
$file ||= "/etc/hosts";
if ( get_host( $host, { file => $file } ) ) {
my $fh = file_read $file;
my @content = $fh->read_all;
$fh->close;
my @new_content = grep { !/\s$host\s?/ } @content;
$fh = file_write $file;
$fh->write(@new_content);
$fh->close;
}
else {
Rex::Logger::debug("Host does not exists.");
}
}
=head2 get_host($host)
Returns the information of $host in /etc/hosts.
my @host_info = get_host "localhost";
say "Host-IP: " . $host_info[0]->{"ip"};
=cut
sub get_host {
my ( $hostname, @lines ) = @_;
Rex::Logger::debug("Getting host ($hostname) information");
my $file = "/etc/hosts";
my @content;
if ( @lines && !ref $lines[0] ) {
@content = @lines;
}
else {
if ( ref $lines[0] eq "HASH" ) {
$file = $lines[0]->{file};
}
my $fh = file_read $file;
@content = $fh->read_all;
$fh->close;
}
my @hosts = _parse_hosts(@content);
my @ret;
for my $item (@hosts) {
if ( $item->{host} eq $hostname ) {
push @ret, $item;
}
else {
push @ret, $item if ( grep { $_ eq $hostname } @{ $item->{aliases} } );
}
}
return @ret;
}
sub _parse_hosts {
my (@lines) = @_;
my @ret;
for my $line (@lines) {
chomp $line;
next if ( $line =~ m/^#/ );
next if ( !$line );
next if ( $line =~ m/^\s*$/ );
my ( $ip, $_host, @aliases ) = split( /\s+/, $line );
push @ret,
{
ip => $ip,
host => $_host,
aliases => \@aliases,
};
}
return @ret;
}
1;
|