view release on metacpan or search on metacpan
}),
template(
'/etc/mysql/my.cnf',
src => 'mysql/my.cnf',
mysql => {
bind => '127.0.0.1',
keysize => '1G'
},
mode => '0644',
uid => 100,
gid => 100,
on_restart => \&sig_hup_mysql
),
script('services.sh');
check;
The script can then be called directly, using command line arguments below
=head1 DESCRIPTION
host 'hostname' => role('test');
Returns a reference to the role object.
=head2 noop($return)
"No operation" test, used only for testing or placeholders.
role 'test' => noop(1);
=head2 file($target, :$src, :$mode, :$uid, :$gid)
Copy file C<$src> to C<$target>, setting additional options with named arguments
such as mode and uid.
role 'test' => file(
'/etc/lighttpd/lighttpd.conf',
src => 'lighttpd/lighttpd.conf',
mode => '0644'
);
See L<Csistck::Test::File>
=head2 template($target, :$src, :$mode, :$uid, :$gid, [:$args])
Process file C<$src> as a Template Toolkit template, output to path C<$target>.
Optional named arguments can be used to alter the mode, uid, etc. All parameters
passed into the C<Csistck::Test::Template> object are available in the actual
template, so any additional named arguments are available in the template using
the argument's name -- these arguments should be hasrefs.
role 'test' => template(
'/etc/motd',
src => 'sys/motd',
foo => { bar => 1 },
uid => 0,
gid => 0,
mode => '0640'
);
See L<Csistck::Test::Template>
=head2 script($script, [@arguments])
Call script with specified arguments
role 'test' => script("apache2/mod-check", "rewrite");
lib/Csistck.pm view on Meta::CPAN
}),
template(
'/etc/mysql/my.cnf',
src => 'mysql/my.cnf',
mysql => {
bind => '127.0.0.1',
keysize => '1G'
},
mode => '0644',
uid => 100,
gid => 100,
on_restart => \&sig_hup_mysql
),
script('services.sh');
check;
The script can then be called directly, using command line arguments below
=head1 DESCRIPTION
lib/Csistck.pm view on Meta::CPAN
Returns a reference to the role object.
=head2 noop($return)
"No operation" test, used only for testing or placeholders.
role 'test' => noop(1);
=head2 file($target, :$src, :$mode, :$uid, :$gid)
Copy file C<$src> to C<$target>, setting additional options with named arguments
such as mode and uid.
role 'test' => file(
'/etc/lighttpd/lighttpd.conf',
src => 'lighttpd/lighttpd.conf',
mode => '0644'
);
See L<Csistck::Test::File>
=head2 template($target, :$src, :$mode, :$uid, :$gid, [:$args])
Process file C<$src> as a Template Toolkit template, output to path C<$target>.
Optional named arguments can be used to alter the mode, uid, etc. All parameters
passed into the C<Csistck::Test::Template> object are available in the actual
template, so any additional named arguments are available in the template using
the argument's name -- these arguments should be hasrefs.
role 'test' => template(
'/etc/motd',
src => 'sys/motd',
foo => { bar => 1 },
uid => 0,
gid => 0,
mode => '0640'
);
See L<Csistck::Test::Template>
=head2 permission($glob, %args)
Change permissions on files matching file glob pattern
role 'test' => permission("/etc/couchdb/*", {
mode => '0640',
uid => 130,
gid => 130
});
See L<Csistck::Test::Permission>
=head2 script($script, [@arguments])
Call script with specified arguments
role 'test' => script("apache2/mod-check", "rewrite");
lib/Csistck/Role.pm view on Meta::CPAN
sub defaults {
my $self = shift;
$self->{some_arg} = 42;
$self->{config} = '/etc/service.conf';
}
sub tests {
my $self = shift;
$self->add([
template(".files/test.tt", "/tmp/test", { service => $self }),
permission("/tmp/test*", mode => '0777', uid => 100, gid => 100)
]);
}
1;
Create a new instance of the class to add the checks to a host definition:
host 'example.com' =>
noop(0),
Service::Base->new(
lib/Csistck/Test/File.pm view on Meta::CPAN
__END__
=head1 NAME
Csistck::Test::File - Csistck file check
=head1 DESCRIPTION
=head1 METHODS
=head2 file($target, :$src, :$mode, :$uid, :$gid, :\&on_repair)
Copy file C<$src> to C<$target>, setting additional options with named
arguments such as mode and uid.
role 'test' => file(
'/etc/lighttpd/lighttpd.conf',
src => 'lighttpd/lighttpd.conf',
mode => '0644'
);
lib/Csistck/Test/File.pm view on Meta::CPAN
=item B<mode>
Change target file mode. This should be a string representation of the octal
mode of the target file -- eg. '0644'
=item B<uid>
Change target UID to the specified integer value.
=item B<gid>
Change target GID to the specified integer value.
=item B<on_repair>
If a repair operation is run, this coderef is called by the process method.
=back
=head1 AUTHOR
lib/Csistck/Test/FileBase.pm view on Meta::CPAN
use File::Copy;
use FindBin;
use File::stat;
use Sys::Hostname::Long qw//;
sub desc { sprintf("File check on %s", shift->{target}); }
sub dest { shift->{target}; }
sub src { shift->{src}; }
sub mode { shift->{mode}; }
sub uid { shift->{uid}; }
sub gid { shift->{gid}; }
sub check {
my $self = shift;
my $ret = 1;
die("Destination path not found")
if (! -e $self->dest);
# If we defined a source file
if (defined($self->src) and $self->can('file_check')) {
$ret &= $self->file_check;
}
$ret &= $self->mode_process(\&mode_check);
$ret &= $self->uid_process(\&uid_check);
$ret &= $self->gid_process(\&gid_check);
return (($ret == 1) ? $self->pass('File matches') :
$self->fail("File doesn't match"));
}
sub repair {
my $self = shift;
my $ret = 1;
# If we defined a source file
lib/Csistck/Test/FileBase.pm view on Meta::CPAN
if (-d $self->dest);
die("Destination ${\$self->dest} exists is is not writable")
if (-f $self->dest and ! -w $self->dest);
backup_file($self->dest);
}
$ret &= $self->file_repair;
}
$ret &= $self->mode_process(\&mode_repair);
$ret &= $self->uid_process(\&uid_repair);
$ret &= $self->gid_process(\&gid_repair);
return (($ret == 1) ? $self->pass('File repaired') :
$self->fail('File not repaired'));
}
# Diff for files
sub diff {
my $self = shift;
die("Destination file does not exist: dest=<${\$self->dest}>")
unless (-f -e -r $self->dest);
# If we defined a source file
if (defined($self->src) and $self->can('file_diff')) {
$self->file_diff();
}
# TODO mode, uid, gid diff functions
}
# Wrapper functions to perform sanity tests on arguments
# Return pass if arguments are missing, die if invalid
sub mode_process {
my ($self, $func) = @_;
return 1 unless($self->mode);
my $mode = $self->mode;
die("Invalid file mode")
lib/Csistck/Test/FileBase.pm view on Meta::CPAN
sub uid_process {
my ($self, $func) = @_;
return 1 unless ($self->uid);
die("Invalid user id")
if ($self->uid !~ m/^[0-9]+$/);
&{$func}($self->dest, $self->uid);
}
sub gid_process {
my ($self, $func) = @_;
return 1 unless ($self->gid);
die("Invalid group id")
if ($self->gid !~ m/^[0-9]+$/);
&{$func}($self->dest, $self->gid);
}
# Mode operations
sub mode_check {
my ($file, $mode) = @_;
my $fh = stat($file);
if ($fh) {
my $curmode = sprintf "%04o", $fh->mode & 07777;
debug("File mode: file=<$file> mode=<$curmode>");
return 1 if ($curmode eq $mode);
lib/Csistck/Test/FileBase.pm view on Meta::CPAN
return ($curuid == $uid);
}
sub uid_repair {
my ($file, $uid) = @_;
debug("Chown file: file=<$file> uid=<$uid>");
chown($uid, -1, $file);
}
# GID operations
sub gid_check {
my ($file, $gid) = @_;
my $fh = stat($file);
my $curgid = undef;
if ($fh) {
$curgid = $fh->gid;
debug("File group: file=<$file> gid=<$gid>");
}
return ($curgid == $gid);
}
sub gid_repair {
my ($file, $gid) = @_;
debug("Chown file: file=<$file> gid=<$gid>");
chown(-1, $gid, $file);
}
# Compare hashes between two files
sub file_compare {
my @files = @_;
return 0 unless (scalar @files == 2);
# Get hashes and return compare
my ($hasha, $hashb) = map hash_file($_), @files;
debug(sprintf "File compare result: <hash=%s> <hash=%s>", $hasha, $hashb);
lib/Csistck/Test/Template.pm view on Meta::CPAN
__END__
=head1 NAME
Csistck::Test::Template - Csistck template check
=head1 DESCRIPTION
=head1 METHODS
=head2 template($target, :$src, :$uid, :$gid, :\&on_repair, [:ARGS])
Process file C<$src> as a Template Toolkit template, output to path C<$target>.
Optional named arguments can be used to alter the mode, uid, etc. All parameters
passed into the C<Csistck::Test::Template> object are available in the actual
template, so any additional named arguments are available in the template using
the argument's name -- these arguments should be hasrefs.
role 'test' => template(
'/etc/motd',
src => 'sys/motd',
foo => { bar => 1 },
uid => 0,
gid => 0,
mode => '0640'
);
This method takes the following named parameters, as well as additional
named parameters to be passed in to the template as variables:
=over
=item B<src>
lib/Csistck/Test/Template.pm view on Meta::CPAN
=item B<mode>
Change targer file mode. This should be a string representation of the octal
mode of the target file -- eg. '0644'
=item B<uid>
Change target UID to the specified integer value.
=item B<gid>
Change target GID to the specified integer value.
=item B<on_repair>
If a repair operation is run, this coderef is called by the process method.
=back
Some arguments are automatically passed to the template processor: