Csistck.pm

filter: Statementstmt 84.3% 86 / 102 Branchbran 57.1% 16 / 28 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 95.2% 20 / 21 Podpod 75.0% 3 / 4 totaltotal 71.1% 128 / 180 CC 20 SCAR 24.3
CC
20
Cov
72%
process
33.6
check
7.2
__ANON__
6.0
CRAP 11.3
« Csistck/Term.pm ↑ Summary Csistck/Test/Template.pm »
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.1003';
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 sub sig_hup_mysql { $ENV{'MAINT_HUP_MYSQL'} = 1; }
54  
55 for (qw/a b/) {
56 host "$_.example.com" => role('mysql');
57 }
58  
59 host 'c' => role('mysql');
60
61 role 'mysql' =>
62 pkg({
63 dpkg => 'mysql-server',
64 emerge => 'mysql'
65 }),
66 template(
67 '/etc/mysql/my.cnf',
68 src => 'mysql/my.cnf',
69 mysql => {
70 bind => '127.0.0.1',
71 keysize => '1G'
72 },
73 mode => '0644',
74 uid => 100,
75 gid => 100,
76 on_restart => \&sig_hup_mysql
77 ),
78 script('services.sh');
79
80 check;
81  
82 The script can then be called directly, using command line arguments below
83  
84 =head1 DESCRIPTION
85  
86 Csistck is a small Perl framework for writing scripts to maintain system
87 configuration and consistency. The focus of csistck is to stay lightweight,
88 simple, and flexible.
89  
90 =head1 EXTENDING ROLES
91  
92 Roles can be defined using the C<role> keyword syntax, however a more flexible
93 method is to extend a new object from L<Csistck::Role>:
94  
95 use Csistck;
96 use base 'Csistck::Role';
97  
98 sub defaults {
99 my $self = shift;
100 $self->{config} = '/etc/example.conf';
101 }
102  
103 sub tests {
104 my $self = shift;
105 $self->add(pkg({
106 dpkg => 'test-server',
107 pkg_info => 'net-test'
108 }),
109 template(
110 $self->{config},
111 src => 'files/example.conf',
112 example => $self
113 );
114  
115 }
116  
117 1;
118  
119 See L<Csistck::Role> for information on extending roles
120  
121  
122 =head1 METHODS
123  
124  
125 =head2 host($host, $checks)
126  
127 Add tests to host C<$host> test array. Tests are Csistck::Test blessed references, code
128 references, or arrays of either. To process host tests, use C<check()>.
129  
130 =cut
131  
132 3 ▶sub host {
Subroutine host: called CC=3 SCAR=11.0
Pod documented
133 3 my $hostname = shift;
134  
135 # Add domain if option is set?
136 3 my $domain_name = Csistck::Config::option('domain_name');
137 3 ▶ $hostname = join '.', $hostname, $domain_name
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain_name 0 3 3
138 if (defined $domain_name);
139  
140 3 ▶ while (my $require = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $require = shift()) 3 3 6
141 3 push(@{$Hosts->{$hostname}}, $require);
142 }
143  
144 3 return $Hosts->{$hostname};
145 }
146  
147 =head2 role($role, $checks)
148  
149 Define a weak role. Constructed similar to a host check, however roles are not
150 called directly, rather they are used to define groups of common tests that can
151 be used by multiple hosts.
152  
153 See L<EXTENDING ROLES> above for an object-based style of defining roles, which
154 allows for passing role configuration.
155  
156 =cut
157  
158 1 ▶sub role {
Subroutine role: called CC=2 SCAR=7.8
Pod documented
159 1 my $role = shift;
160  
161 # If tests specified, add now
162 1 ▶ while (my $require = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $require = shift()) 1 1 2
163 1 push(@{$Roles->{$role}}, $require);
164 }
165  
166 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
167 # Run required role or die
168 die ("What's this, \"${role}\"? That role is bupkis.")
169 0 ▶ unless (defined $Roles->{$role});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Roles->{$role} 0 0 0
170
171 0 process($Roles->{$role});
172 }
173 1 }
174  
175 =head2 check($target)
176  
177 Runs processing on C<$target>. If C<$target> is C<undef>, then look up the
178 system's full hostname. If C<$target> is a string, use that string for a
179 hostname lookup. If C<$target> is a C<Csistck::Test> reference, a coderef, or an
180 arrayref, then process that object directly. This is useful for writing scripts
181 where hostname is not important.
182  
183 =cut
184  
185 5 ▶sub check {
Subroutine check: called CC=6 SCAR=19.7
Pod documented
186 5 ▶ my $target = shift // Sys::Hostname::Long::hostname_long();
Condition33%1/3
shift() // Sys::Hostname::Long::hostname_long()
l!l&&r!l&&!r
500
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()
ABresult
1X1
011
000
187  
188 # Process cli arguments for mode/etc, usage
189 5 Csistck::Oper::set_mode_by_cli();
190 5 ▶ return if (Csistck::Oper::usage());
Branch: 1 branch
BranchTrueFalseTotal
if Csistck::Oper::usage() 0 5 5
191  
192 # Role names specified on the command line via --role have priority. If
193 # target is a string, process as hostname reference. Otherwise, assume a
194 # test object was passed
195 5 ▶ if (scalar @{$Csistck::Oper::Roles} gt 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{$Csistck::Oper::Roles;} gt 0) { } 0 5 5
elsif (not defined builtin::reftype($target)) { } 2 3 5
196 return process(
197 0 map { role($_) } @{$Csistck::Oper::Roles}
198 );
199 }
200 elsif (!defined(reftype($target))) {
201 die ("What's this, \"${target}\"? That host is bupkis.")
202 2 ▶ unless (defined $Hosts->{$target});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Hosts->{$target} 1 1 2
203 1 return process($Hosts->{$target});
204 }
205 else {
206 3 return process($target);
207 }
208 }
209  
210 # For recursive testing based on type
211 7 ▶sub process {
Subroutine process: called CC=11 SCAR=35.1
Pod undocumented
212 7 my $obj = shift;
213
214 # Iterate through array and recursively call process, call code refs,
215 # and run tests
216  
217 7 given (ref $obj) {
218 7 when ('ARRAY') {
219 2 return map(process($_), @{$obj});
220 }
221 5 when ('CODE') {
222 0 return &{$obj};
223 }
224 5 default {
225 5 ▶ if (blessed($obj) and $obj->isa('Csistck::Test')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($obj) and $obj->isa('Csistck::Test')) { } 5 0 5
elsif (builtin::blessed($obj) and $obj->isa('Csistck::Role')) { } 0 0 0
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Csistck::Test")
!ll&&!rl&&r
005
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")
ABresult
0X0
100
111
226 # Check is mandatory, if auto repair is set, repair, otherwise prompt
227 5 my $check = $obj->execute('check');
228 5 ▶ return if ($check->passed);
Branch: 1 branch
BranchTrueFalseTotal
if $check->passed 4 1 5
229
230 1 ▶ if (Csistck::Oper::repair()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Csistck::Oper::repair()) { } 1 0 1
231 1 my $repair = $obj->execute('repair');
232 1 ▶ if ($repair->passed and $obj->on_repair) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repair->passed and $obj->on_repair) 0 1 1
Condition33%1/3
$repair->passed and $obj->on_repair
!ll&&!rl&&r
100
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
ABresult
0X0
100
111
233 0 &{$obj->on_repair};
234 }
235 1 return $repair;
236 }
237 else {
238 0 my $repair = Csistck::Term::prompt($obj);
239 0 ▶ if ($repair->passed and $obj->on_repair) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repair->passed and $obj->on_repair) 0 0 0
Condition0%0/3
$repair->passed and $obj->on_repair
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
240 0 &{$obj->on_repair};
241 }
242 0 return $repair;
243 }
244 }
245 ▶ elsif (blessed($obj) and $obj->isa('Csistck::Role')) {
Condition0%0/3
builtin::blessed($obj) and $obj->isa("Csistck::Role")
!ll&&!rl&&r
000
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")
ABresult
0X0
100
111
246 0 return process($obj->get_tests);
247 }
248 else {
249 0 die(sprintf("Unkown object reference: ref=<%s>", ref $obj));
250 }
251 }
252 }
253 }
254  
255 17 1;
256 __END__
« Csistck/Term.pm ↑ Summary Csistck/Test/Template.pm »