lib/Rex/Cron/Base.pm

filter: Statementstmt 83.0% 127 / 153 Branchbran 59.5% 25 / 42 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 87.5% 21 / 24 Podpod 0.0% 0 / 13 totaltotal 73.7% 177 / 240 CC 28 SCAR 15.9
CC
28
Cov
78%
activate_user_cron
12.0
read_user_cron
12.0
add_env
7.3
CRAP 4.9
« lib/Rex/Cron/FreeBSD.pm ↑ Summary lib/Rex/Exporter.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Cron::Base;
6  
7 2 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '1.16.1'; # VERSION
11  
12 2 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
21 2 my $that = shift;
22 2 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
23 2 my $self = {@_};
24  
25 2 bless( $self, $proto );
26  
27 2 return $self;
28 }
29  
30 6 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod undocumented
31 6 my ($self) = @_;
32 6 return @{ $self->{cron} };
33 }
34  
35 4 ▶sub list_jobs {
Subroutine list_jobs: called CC=1 SCAR=0.0
Pod undocumented
36 4 my ($self) = @_;
37 4 my @jobs = @{ $self->{cron} };
38 my @ret =
39 25 map { { line => $_->{line}, %{ $_->{cron} } } }
40 4 grep { $_->{type} eq "job" } @jobs;
41 }
42  
43 6 ▶sub list_envs {
Subroutine list_envs: called CC=1 SCAR=0.0
Pod undocumented
44 6 my ($self) = @_;
45 6 my @jobs = @{ $self->{cron} };
46 6 my @ret = grep { $_->{type} eq "env" } @jobs;
47 }
48  
49 4 ▶sub add {
Subroutine add: called CC=2 SCAR=7.1
Pod undocumented
50 4 my ( $self, %config ) = @_;
51  
52 4 %config = $self->_create_defaults(%config);
53  
54 my $new_cron = sprintf(
55 "%s %s %s %s %s %s",
56 $config{"minute"}, $config{"hour"}, $config{"day_of_month"},
57 4 $config{"month"}, $config{"day_of_week"}, $config{"command"},
58 );
59  
60 4 my $dupe = grep { $_->{line} eq $new_cron } @{ $self->{cron} };
61 4 ▶ if ($dupe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dupe) 0 4 4
62 0 Rex::Logger::debug("Job \"$new_cron\" already installed, skipping.");
63 0 return 0;
64 }
65  
66 push(
67 4 @{ $self->{cron} },
68 {
69 type => "job",
70 line => $new_cron,
71 cron => \%config,
72 }
73 );
74  
75 4 return 1;
76 }
77  
78 2 ▶sub add_env {
Subroutine add_env: called CC=5 SCAR=19.8
Pod undocumented
79 2 my ( $self, $name, $value ) = @_;
80  
81 2 my $env_index = 0;
82 2 my $exists = 0;
83 2 for my $env ( $self->list_envs ) {
84 7 ▶ if ( $env->{name} eq "$name" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"name"} eq "$name") 0 7 7
85 0 ▶ if ( $env->{value} ne "\"$value\"" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'value'} ne qq["$value"]) { } 0 0 0
86 0 Rex::Logger::debug("Environment variable changed : $name");
87 0 $self->delete_env($env_index);
88 }
89 else {
90 0 Rex::Logger::debug(
91 "Environment variable already exists with same value: $name=$value");
92 0 $exists = 1;
93 }
94 }
95  
96 7 $env_index++;
97 }
98  
99 2 ▶ if ( $exists == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists == 0) 2 0 2
100 unshift(
101 2 @{ $self->{cron} },
102 {
103 type => "env",
104 line => "$name=\"$value\"",
105 name => $name,
106 value => $value,
107 }
108 );
109 }
110 }
111  
112 2 ▶sub delete_job {
Subroutine delete_job: called CC=4 SCAR=13.9
Pod undocumented
113 2 my ( $self, $num ) = @_;
114 2 my @jobs = $self->list_jobs;
115  
116 2 my $i = 0;
117 2 my $to_delete;
118 2 for my $j ( @{ $self->{cron} } ) {
119 23 ▶ if ( $j->{line} eq $jobs[$num]->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j->{"line"} eq $jobs[$num]{"line"}) 2 21 23
120 2 $to_delete = $i;
121 2 last;
122 }
123  
124 21 $i++;
125 }
126  
127 2 ▶ unless ( defined $to_delete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to_delete) 0 2 2
128 0 die("Cron Entry $num not found.");
129 }
130  
131 2 $self->delete($to_delete);
132 }
133  
134 2 ▶sub delete_env {
Subroutine delete_env: called CC=4 SCAR=13.9
Pod undocumented
135 2 my ( $self, $num ) = @_;
136  
137 2 my @jobs = $self->list_envs;
138  
139 2 my $i = 0;
140 2 my $to_delete;
141 2 for my $j ( @{ $self->{cron} } ) {
142 3 ▶ if ( $j->{line} eq $jobs[$num]->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j->{"line"} eq $jobs[$num]{"line"}) 2 1 3
143 2 $to_delete = $i;
144 2 last;
145 }
146  
147 1 $i++;
148 }
149  
150 2 ▶ unless ( defined $to_delete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to_delete) 0 2 2
151 0 die("Cron Entry $num not found.");
152 }
153  
154 2 $self->delete($to_delete);
155 }
156  
157 6 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
158 6 my ( $self, $num ) = @_;
159 6 splice( @{ $self->{cron} }, $num, 1 );
160 }
161  
162 # returns a filename where the new cron is written to
163 # after that the cronfile must be activated
164 1 ▶sub write_cron {
Subroutine write_cron: called CC=1 SCAR=0.0
Pod undocumented
165 1 my ($self) = @_;
166  
167 1 my $rnd_file = get_tmp_file;
168  
169 1 my @lines = map { $_->{line} } @{ $self->{cron} };
170  
171 1 my $fh = file_write $rnd_file;
172 1 $fh->write( join( "\n", @lines ) . "\n" );
173 1 $fh->close;
174  
175 1 return $rnd_file;
176 }
177  
178 0 ▶sub activate_user_cron {
Subroutine activate_user_cron: not called CC=3 SCAR=24.8
Pod undocumented
179 0 my ( $self, $file, $user ) = @_;
180 0 ▶ $user = undef if $user eq &_whoami;
Branch: 1 branch
BranchTrueFalseTotal
if $user eq &_whoami 0 0 0
181  
182 0 my $command = 'crontab';
183 0 ▶ $command .= " -u $user" if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 0 0 0
184  
185 0 i_run "$command $file";
186 0 unlink $file;
187 }
188  
189 0 ▶sub read_user_cron {
Subroutine read_user_cron: not called CC=3 SCAR=24.8
Pod undocumented
190 0 my ( $self, $user ) = @_;
191 0 ▶ $user = undef if $user eq &_whoami;
Branch: 1 branch
BranchTrueFalseTotal
if $user eq &_whoami 0 0 0
192  
193 0 my $command = 'crontab -l';
194 0 ▶ $command .= " -u $user" if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 0 0 0
195 0 $command .= ' 2> /dev/null';
196  
197 0 my @lines = i_run $command, fail_ok => 1;
198 0 $self->parse_cron(@lines);
199 }
200  
201 2 ▶sub parse_cron {
Subroutine parse_cron: called CC=6 SCAR=18.3
Pod undocumented
202 2 my ( $self, @lines ) = @_;
203  
204 2 chomp @lines;
205  
206 2 my @cron;
207  
208 2 for my $line (@lines) {
209  
210 # comment
211 32 ▶ if ( $line =~ m/^#/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^#/u) { } 12 20 32
elsif ($line =~ /^\s*$/u) { } 0 20 20
elsif ($line =~ /^(@|\*|[0-9])/u) { } 14 6 20
elsif ($line =~ /=/u) { } 6 0 6
212 12 push(
213 @cron,
214 {
215 type => "comment",
216 line => $line,
217 }
218 );
219 }
220  
221 # empty line
222 elsif ( $line =~ m/^\s*$/ ) {
223 0 push(
224 @cron,
225 {
226 type => "empty",
227 line => $line,
228 }
229 );
230 }
231  
232 # job
233 elsif ( $line =~ m/^(@|\*|[0-9])/ ) {
234 14 my ( $min, $hour, $day, $month, $dow, $cmd ) = split( /\s+/, $line, 6 );
235 14 push(
236 @cron,
237 {
238 type => "job",
239 line => $line,
240 cron => {
241 minute => $min,
242 hour => $hour,
243 day_of_month => $day,
244 month => $month,
245 day_of_week => $dow,
246 command => $cmd,
247 },
248 }
249 );
250 }
251  
252 elsif ( $line =~ m/=/ ) {
253 6 my ( $name, $value ) = split( /=/, $line, 2 );
254 6 $name =~ s/^\s+//;
255 6 $name =~ s/\s+$//;
256 6 $value =~ s/^\s+//;
257 6 $value =~ s/\s+$//;
258  
259 6 push(
260 @cron,
261 {
262 type => "env",
263 line => $line,
264 name => $name,
265 value => $value,
266 }
267 );
268 }
269  
270 else {
271 0 Rex::Logger::debug("Error parsing cron line: $line");
272 0 next;
273 }
274  
275 }
276  
277 2 $self->{cron} = \@cron;
278 2 return @cron;
279 }
280  
281 4 ▶sub _create_defaults {
Subroutine _create_defaults: called CC=7 SCAR=19.5
282 4 my ( $self, %config ) = @_;
283  
284 4 ▶ $config{"minute"} = "*" unless defined $config{minute};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config{"minute"} 2 2 4
285 4 ▶ $config{"hour"} = "*" unless defined $config{hour};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config{"hour"} 1 3 4
286 4 ▶ $config{"day_of_month"} = "*" unless defined $config{day_of_month};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config{"day_of_month"} 3 1 4
287 4 ▶ $config{"month"} = "*" unless defined $config{month};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config{"month"} 2 2 4
288 4 ▶ $config{"day_of_week"} = "*" unless defined $config{day_of_week};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config{"day_of_week"} 2 2 4
289 4 ▶ $config{"command"} ||= "false";
Condition100%2/2
$config{"command"} ||= "false"
l!l
31
MC/DC100%1/1
$config{"command"} ||= "false"
$config{"command"}
Truth table100%2/2
A
A= $config{"command"}
Aresult
11
00
290  
291 4 return %config;
292 }
293  
294 0 ▶sub _whoami {
Subroutine _whoami: not called CC=1 SCAR=6.9
295 0 return i_run q(perl -e 'print scalar getpwuid($<)');
296 }
297  
298 2 1;
« lib/Rex/Cron/FreeBSD.pm ↑ Summary lib/Rex/Exporter.pm »