App-Slaughter
view release on metacpan or search on metacpan
lib/Slaughter/API/generic.pm view on Meta::CPAN
=item Sendmail [default: "/usr/lib/sendmail -t"]
The path to the sendmail binary.
=item Subject [mandatory]
The subject to send.
=item To [mandatory]
The recipient of the message.
=back
=cut
sub Alert
{
my (%params) = (@_);
my $message = $params{ 'Message' } || "No message";
my $subject = $params{ 'Subject' } || "No subject";
my $to = $params{ 'To' } || $params{ 'Email' } || "root";
my $from = $params{ 'From' } || "root";
my $sendmail = $params{ 'Sendmail' } || "/usr/lib/sendmail -t";
open( my $handle, "|-", "$sendmail -f $from" ) or
die "Failed to sendmail: $!";
print $handle <<EOF;
To: $to
From: $from
Subject: $subject
$message
EOF
close($handle);
}
=head2 AppendIfMissing
This primitive will open a local file, and append a line to it if it is not
already present.
=for example begin
AppendIfMissing( File => "/etc/hosts.allow",
Line => "All: 1.2.3.4" );
=for example end
The following parameters are available:
=over
=item File [mandatory]
The filename which should be examined and potentially updated.
=item Line [mandatory]
The line which should be searched for and potentially appended.
=back
=cut
sub AppendIfMissing
{
my (%params) = (@_);
my $line = $params{ 'Line' };
my $file = $params{ 'File' };
my $found = 0;
if ( open( my $handle, "<", $file ) )
{
foreach my $read (<$handle>)
{
chomp($read);
if ( $line eq $read )
{
$found = 1;
}
}
close($handle);
}
#
# If it wasn't found append
#
if ( !$found )
{
if ( open( my $handle, ">>", $file ) )
{
print $handle $line . "\n";
close($handle);
return 1;
}
else
{
return -1;
}
}
return 0;
}
=head2 CommentLinesMatching
This primitive will open a local file, and comment out any line which matches
the specified regular expression.
=for example begin
if ( CommentLinesMatching( Pattern => "telnet|ftp",
File => "/etc/inetd.conf" ) )
{
RunCommand( Cmd => "/etc/init.d/inetd restart" );
}
=for example end
The following parameters are available:
=over
=item Comment [default: "#"]
The value to comment out the line with.
=item File [mandatory]
The filename which should be examined and potentially updated.
=item Pattern [mandatory]
The regular expression to match with.
=back
The return value of this function is the number of lines updated,
or -1 if the file could not be opened.
=cut
sub CommentLinesMatching
{
my (%params) = (@_);
my $pattern = $params{ 'Pattern' };
my $comment = $params{ 'Comment' } || "#";
my $file = $params{ 'File' };
if ( open( my $handle, "<", $file ) )
{
my @lines;
my $found = 0;
foreach my $read (<$handle>)
{
chomp($read);
if ( $read =~ /$pattern/ )
{
$read = $comment . $read;
$found += 1;
}
push( @lines, $read );
}
close($handle);
#
# Now write out the modified file.
#
if ($found)
{
if ( open( my $handle, ">", $file ) )
{
foreach my $line (@lines)
{
print $handle $line . "\n";
}
close($handle);
$::verbose &&
print "Commented $found lines matching $pattern in $file\n";
return $found;
}
}
else
{
$::verbose && print "No lines matching $pattern found in $file\n";
return 0;
}
}
else
{
$::verbose && print "Couldn't open $file to check for $pattern\n";
return -1;
}
lib/Slaughter/API/generic.pm view on Meta::CPAN
my (%params) = (@_);
#
# The files we'll compare
#
my $a = $params{ 'File1' };
my $b = $params{ 'File2' };
if ( !$a || !$b )
{
$::verbose && print "\tMissing File1 or File2.\n";
return -1;
}
#
# Missing files are an error
#
return -1 unless ( -e $a );
return -1 unless ( -e $b );
#
# Same size? If not then they can't have the same
# contents.
#
my $size_a = -s $a;
my $size_b = -s $b;
return 0 if ( $size_a != $size_b );
#
# Same hash?
#
my $sum_a = Slaughter::Private::checksumFile($a);
my $sum_b = Slaughter::Private::checksumFile($b);
return 0 if ( $sum_a ne $sum_b );
#
# OK they're "identical".
#
return 1;
}
=head2 FetchFile
The FetchFile primitive is used to copy a file from the remote server
to the local system. The file will have be moved into place if the
local file is missing OR if it exists but contains different contents
to the remote version.
The following is an example of usage:
=for example begin
if ( FetchFile( Source => "/etc/motd",
Dest => "/etc/motd",
Owner => "root",
Group => "root",
Mode => "644" ) )
{
# File was created/updated.
}
else
{
# File already existed locally with the same contents.
}
=for example end
The following parameters are available:
=over
=item Dest [mandatory]
The destination file to write to, on the local system.
=item Expand [default: false]
This is used to enable template-expansion, documented later.
=item Group
The unix group which should own the file.
=item Mode
The Unix mode to set for the file. B<NOTE> If this doesn't start with "0" it will
be passed through the perl "oct" function.
=item Owner
The Unix owner who should own the file.
=item Source [default: value of Dest]
The path to the remote file. This is relative to the /files/ prefix beneath
the transport root. If no value is specified the destination path is used.
=back
When a file fetch is attempted several variations are attempted, not just the
literal filename. The first file which exists and matches is returned, and the
fetch is aborted:
=over 8
=item /etc/motd.$fqdn
=item /etc/motd.$hostname
=item /etc/motd.$os
=item /etc/motd.$arch
=item /etc/motd
=back
Template template expansion involves the use of the L<Text::Template> module, of
"Expand => true". This will convert the following text:
lib/Slaughter/API/generic.pm view on Meta::CPAN
#
my $point = $params{ 'Path' } || "/";
my $perc = 0;
#
# Call df to get the output, use posix mode.
#
my $out = `$path -P $point`;
foreach my $line ( split( /[\r\n]/, $out ) )
{
next unless ( $line =~ /%/ );
if ( $line =~ /[ \t]([0-9]*)%[ \t]/ )
{
$perc = $1;
}
}
return ($perc);
}
=head2 RemovePackage
This method is a stub which does nothing but output a line of text to
inform the caller that the method is not implemented.
For an implementation, and documentation, please consult C<Slaughter::API::linux>.
=cut
sub RemovePackage
{
print "RemovePackage - not implemented for $^O\n";
}
=head2 ReplaceRegexp
This primitive will open a local file, and replace any lines matching a given
regular expression.
=for example begin
ReplaceRegexp( File => "/etc/ssh/sshd_config",
Pattern => "^PermitRootLogin.*yes.*",
Replace => "PermitRootLogin no" );
=for example end
The following parameters are available:
=over
=item File [mandatory]
The filename which should be examined and potentially updated.
=item Pattern [mandatory]
The pattern to test and potentially replace.
=item Replace [mandatory]
The replacement text to use.
=back
The return value of this function is the number of lines updated,
0 if none, or -1 if the file could not be opened.
=cut
sub ReplaceRegexp
{
my (%params) = (@_);
my $pattern = $params{ 'Pattern' };
my $replace = $params{ 'Replace' } || "";
my $file = $params{ 'File' };
my $found = 0;
if ( open( my $handle, "<", $file ) )
{
my @lines;
# Read and replace as appropriate.
foreach my $read (<$handle>)
{
chomp($read);
my $orig = $read;
if ( $replace =~ /\$/ )
{
$read =~ s/$pattern/$replace/gee;
}
else
{
$read =~ s/$pattern/$replace/g;
}
$found += 1 if ( $read ne $orig );
push( @lines, $read );
}
close($handle);
# Now write out the possibly modified fils.
if ($found)
{
if ( open( my $handle, ">", $file ) )
{
foreach my $line (@lines)
{
print $handle $line . "\n";
}
close($handle);
return $found;
}
}
else
{
return 0;
}
}
else
{
return -1;
( run in 1.059 second using v1.01-cache-2.11-cpan-39bf76dae61 )