| 1 | package Csistck; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 17 | our $VERSION = '0.09_11'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | # We export function in the main namespace | ||||||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 17 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||
| 12 | host | ||||||||||||||||||||||||||||||||
| 13 | role | ||||||||||||||||||||||||||||||||
| 14 | check | ||||||||||||||||||||||||||||||||
| 15 | option | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | file | ||||||||||||||||||||||||||||||||
| 18 | noop | ||||||||||||||||||||||||||||||||
| 19 | pkg | ||||||||||||||||||||||||||||||||
| 20 | script | ||||||||||||||||||||||||||||||||
| 21 | template | ||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | # Imports for base | ||||||||||||||||||||||||||||||||
| 25 | 17 | ▶ | use Csistck::Config qw/option/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | 17 | ▶ | use Csistck::Test::NOOP qw/noop/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | 17 | ▶ | use Csistck::Test::File qw/file/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | 17 | ▶ | use Csistck::Test::Pkg qw/pkg/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 17 | ▶ | use Csistck::Test::Script qw/script/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | 17 | ▶ | use Csistck::Test::Template qw/template/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 17 | ▶ | use Csistck::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | 17 | ▶ | use Csistck::Term; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 17 | ▶ | use Sys::Hostname::Long qw//; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | 17 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 17 | ▶ | use Scalar::Util qw/blessed reftype/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 38 | 17 | ▶ | use List::Util qw/sum/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | # Package wide | ||||||||||||||||||||||||||||||||
| 41 | 17 | my $Hosts = {}; | |||||||||||||||||||||||||||||||
| 42 | 17 | my $Roles = {}; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | Csistck - Perl system consistency check framework | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | use Csistck; | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | for my $host (qw/a b/) { | ||||||||||||||||||||||||||||||||
| 54 | host $host => role('test'); | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | host 'c' => role('test'); | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | role 'test' => | ||||||||||||||||||||||||||||||||
| 60 | template(".files/test.tt", "/tmp/test", { text => "Some text here" }), | ||||||||||||||||||||||||||||||||
| 61 | permission("/tmp/test*", mode => '0777', uid => 100, gid => 100); | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | check; | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | The script can then be called directly, using command line arguements below | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | Csistck is a small Perl framework for writing scripts to maintain system | ||||||||||||||||||||||||||||||||
| 70 | configuration and consistency. The focus of csistck is to stay lightweight, | ||||||||||||||||||||||||||||||||
| 71 | simple, and flexible. | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =head1 EXTENDING ROLES | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | Roles can be defined using the C<role> keyword syntax, however a more flexible | ||||||||||||||||||||||||||||||||
| 76 | method is to extend a new object from L<Csistck::Role>: | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | use Csistck; | ||||||||||||||||||||||||||||||||
| 79 | use base 'Csistck::Role'; | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | sub defaults { | ||||||||||||||||||||||||||||||||
| 82 | my $self = shift; | ||||||||||||||||||||||||||||||||
| 83 | $self->{config} = '/etc/example.conf'; | ||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | sub tests { | ||||||||||||||||||||||||||||||||
| 87 | my $self = shift; | ||||||||||||||||||||||||||||||||
| 88 | $self->add(pkg({ | ||||||||||||||||||||||||||||||||
| 89 | dpkg => 'test-server', | ||||||||||||||||||||||||||||||||
| 90 | pkg_info => 'net-test' | ||||||||||||||||||||||||||||||||
| 91 | }), | ||||||||||||||||||||||||||||||||
| 92 | template('files/example.conf', $self->{config}, { example => $self }); | ||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 1; | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | See L<Csistck::Role> for information on extending roles | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =head2 host($host, $checks) | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | Add tests to host C<$host> test array. Tests are Csistck::Test blessed references, code | ||||||||||||||||||||||||||||||||
| 106 | references, or arrays of either. To process host tests, use C<check()>. | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | sub host { | ||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | my $hostname = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
host: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | # Add domain if option is set? | ||||||||||||||||||||||||||||||||
| 114 | 3 | my $domain_name = Csistck::Config::option('domain_name'); | |||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | $hostname = join '.', $hostname, $domain_name | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | if (defined $domain_name); | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | while (my $require = shift) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 3 | push(@{$Hosts->{$hostname}}, $require); | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 3 | return $Hosts->{$hostname}; | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | =head2 role($role, $checks) | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | Define a weak role. Constructed similar to a host check, however roles are not | ||||||||||||||||||||||||||||||||
| 128 | called directly, rather they are used to define groups of common tests that can | ||||||||||||||||||||||||||||||||
| 129 | be used by multiple hosts. | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | See L<EXTENDING ROLES> above for an object-based style of defining roles, which | ||||||||||||||||||||||||||||||||
| 132 | allows for passing role configuration. | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | sub role { | ||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | my $role = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
role: called CC=2 SCAR=7.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | # If tests specified, add now | ||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | while (my $require = shift) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 1 | push(@{$Roles->{$role}}, $require); | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | return sub { | ||||||||||||||||||||||||||||||||
| 145 | # Run required role or die | ||||||||||||||||||||||||||||||||
| 146 | die ("What's this, \"${role}\"? That role is bupkis.") | ||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | unless (defined $Roles->{$role}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 0 | process($Roles->{$role}); | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | 1 | } | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | =head2 check($target) | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | Runs processing on C<$target>. If C<$target> is C<undef>, then look up the | ||||||||||||||||||||||||||||||||
| 156 | system's full hostname. If C<$target> is a string, use that string for a | ||||||||||||||||||||||||||||||||
| 157 | hostname lookup. If C<$target> is a C<Csistck::Test> reference, a coderef, or an | ||||||||||||||||||||||||||||||||
| 158 | arrayref, then process that object directly. This is useful for writing scripts | ||||||||||||||||||||||||||||||||
| 159 | where hostname is not important. | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | sub check { | ||||||||||||||||||||||||||||||||
| 164 | 5 | ▶ | my $target = shift // Sys::Hostname::Long::hostname_long(); | ||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=5 SCAR=16.4
Pod
documented
Condition33%1/3
shift() // Sys::Hostname::Long::hostname_long()
MC/DC0%0/2
shift() // Sys::Hostname::Long::hostname_long()
shift()
Sys::Hostname::Long::hostname_long()
Truth table33%1/3
A // B
A= shift()B= Sys::Hostname::Long::hostname_long()
| |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | # Process cli arguments for mode/etc, usage | ||||||||||||||||||||||||||||||||
| 167 | 5 | Csistck::Oper::set_mode_by_cli(); | |||||||||||||||||||||||||||||||
| 168 | 5 | ▶ | return if (Csistck::Oper::usage()); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | # If target is a string, process as hostname reference. Otherwise, assume a | ||||||||||||||||||||||||||||||||
| 171 | # test object was passed | ||||||||||||||||||||||||||||||||
| 172 | 5 | ▶ | if (!defined(reftype($target))) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | die ("What's this, \"${target}\"? That host is bupkis.") | ||||||||||||||||||||||||||||||||
| 174 | 2 | ▶ | unless (defined $Hosts->{$target}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | 1 | return process($Hosts->{$target}); | |||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||||||
| 178 | 3 | return process($target); | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | # For recursive testing based on type | ||||||||||||||||||||||||||||||||
| 183 | sub process { | ||||||||||||||||||||||||||||||||
| 184 | 7 | ▶ | my $obj = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=11 SCAR=35.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | # Iterate through array and recursively call process, call code refs, | ||||||||||||||||||||||||||||||||
| 187 | # and run tests | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 7 | given (ref $obj) { | |||||||||||||||||||||||||||||||
| 190 | 7 | when ('ARRAY') { | |||||||||||||||||||||||||||||||
| 191 | 2 | return map(process($_), @{$obj}); | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | 5 | when ('CODE') { | |||||||||||||||||||||||||||||||
| 194 | 0 | return &{$obj}; | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | 5 | default { | |||||||||||||||||||||||||||||||
| 197 | 5 | ▶ | if (blessed($obj) and $obj->isa('Csistck::Test')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Csistck::Test")
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Csistck::Test")
builtin::blessed($obj)
$obj->isa("Csistck::Test")
Truth table33%1/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Csistck::Test")
| |||||||||||||||||||||||||||||||||
| 198 | # Check is mandatory, if auto repair is set, repair, otherwise prompt | ||||||||||||||||||||||||||||||||
| 199 | 5 | my $check = $obj->execute('check'); | |||||||||||||||||||||||||||||||
| 200 | 5 | ▶ | return if ($check->passed); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 1 | ▶ | if (Csistck::Oper::repair()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | 1 | my $repair = $obj->execute('repair'); | |||||||||||||||||||||||||||||||
| 204 | 1 | ▶ | if ($repair->passed and $obj->on_repair) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$repair->passed and $obj->on_repair
MC/DC0%0/2
$repair->passed and $obj->on_repair
$repair->passed
$obj->on_repair
Truth table33%1/3
A and B
A= $repair->passedB= $obj->on_repair
| |||||||||||||||||||||||||||||||||
| 205 | 0 | &{$obj->on_repair}; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | 1 | return $repair; | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | else { | ||||||||||||||||||||||||||||||||
| 210 | 0 | my $repair = Csistck::Term::prompt($obj); | |||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | if ($repair->passed and $obj->on_repair) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$repair->passed and $obj->on_repair
MC/DC0%0/2
$repair->passed and $obj->on_repair
$repair->passed
$obj->on_repair
Truth table0%0/3
A and B
A= $repair->passedB= $obj->on_repair
| |||||||||||||||||||||||||||||||||
| 212 | 0 | &{$obj->on_repair}; | |||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | 0 | return $repair; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | ▶ | elsif (blessed($obj) and $obj->isa('Csistck::Role')) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
builtin::blessed($obj) and $obj->isa("Csistck::Role")
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Csistck::Role")
builtin::blessed($obj)
$obj->isa("Csistck::Role")
Truth table0%0/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Csistck::Role")
| |||||||||||||||||||||||||||||||||
| 218 | 0 | return process($obj->get_tests); | |||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | else { | ||||||||||||||||||||||||||||||||
| 221 | 0 | die(sprintf("Unkown object reference: ref=<%s>", ref $obj)); | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 17 | 1; | |||||||||||||||||||||||||||||||
| 228 | __END__ | ||||||||||||||||||||||||||||||||