lib/Rex/Commands/Cron.pm

filter: Statementstmt 26.9% 24 / 89 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 20.7% 33 / 159 CC 25 SCAR 51.1
CC
25
Cov
17%
cron_entry
272.0
cron
110.0
BEGIN
1.0
CRAP 165.4
« blib/lib/Rex.pm ↑ Summary lib/Rex/Hardware/Network/Solaris.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Cron - Simple Cron Management
8  
9 =head1 DESCRIPTION
10  
11 With this Module you can manage your cronjobs.
12  
13 =head1 SYNOPSIS
14  
15 use Rex::Commands::Cron;
16  
17 cron add => "root", {
18 minute => '5',
19 hour => '*',
20 day_of_month => '*',
21 month => '*',
22 day_of_week => '*',
23 command => '/path/to/your/cronjob',
24 };
25  
26 cron list => "root";
27  
28 cron delete => "root", 3;
29  
30 =head1 EXPORTED FUNCTIONS
31  
32 =cut
33  
34 package Rex::Commands::Cron;
35  
36 32 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 32 our $VERSION = '1.16.1'; # VERSION
40  
41 32 require Rex::Exporter;
42 32 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 32 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 32 ▶use Rex::Cron;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 32 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 32 @EXPORT = qw(cron cron_entry);
50  
51 =head2 cron_entry($name, %option)
52  
53 Manage cron entries.
54  
55 cron_entry "reload-httpd",
56 ensure => "present",
57 command => "/etc/init.d/httpd restart",
58 minute => "1,5",
59 hour => "11,23",
60 month => "1,5",
61 day_of_week => "1,3",
62 day_of_month => "1,3,5",
63 user => "root",
64 on_change => sub { say "cron added"; };
65  
66 # remove an entry
67 cron_entry "reload-httpd",
68 ensure => "absent",
69 command => "/etc/init.d/httpd restart",
70 minute => "1,5",
71 hour => "11,23",
72 month => "1,5",
73 day_of_week => "1,3",
74 day_of_month => "1,3,5",
75 user => "root",
76 on_change => sub { say "cron removed."; };
77  
78 =cut
79  
80 0 ▶sub cron_entry {
Subroutine cron_entry: not called CC=16 SCAR=56.1
Pod documented
81 0 my ( $name, %option ) = @_;
82  
83 0 ▶ $option{ensure} ||= "present";
Condition0%0/2
$option{"ensure"} ||= "present"
l!l
00
MC/DC0%0/1
$option{"ensure"} ||= "present"
$option{"ensure"}
Truth table0%0/2
A
A= $option{"ensure"}
Aresult
11
00
84 0 ▶ confess "No 'user' given." if ( !exists $option{user} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{"user"} 0 0 0
85  
86 Rex::get_current_connection()->{reporter}
87 0 ->report_resource_start( type => "cron_entry", name => $name );
88  
89 0 my $changed = 0;
90 0 my $ensure = $option{ensure};
91  
92 0 ▶ if ( $option{ensure} eq "present" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($option{'ensure'} eq 'present') { } 0 0 0
elsif ($option{'ensure'} eq 'absent') { } 0 0 0
93 0 Rex::Logger::debug("Creating new cron_entry: $name");
94 0 my $user = $option{user};
95  
96 0 delete $option{user};
97 0 delete $option{ensure};
98 0 $changed = &cron( add => $user, \%option );
99 }
100 elsif ( $option{ensure} eq "absent" ) {
101 0 Rex::Logger::debug("Removing cron_entry: $name");
102 0 my $user = $option{user};
103  
104 0 my $c = Rex::Cron->create();
105 0 %option = $c->_create_defaults(%option);
106  
107 0 my @crons = &cron( list => $user );
108 0 my $i = 0;
109 0 my $cron_id;
110 0 for my $cron (@crons) {
111 0 ▶ if ( $cron->{minute} eq $option{minute}
Branch: 1 branch
BranchTrueFalseTotal
if ($cron->{"minute"} eq $option{"minute"} and $cron->{"hour"} eq $option{"hour"} and $cron->{"month"} eq $option{"month"} and $cron->{"day_of_week"} eq $option{"day_of_week"} and $cron->{"day_of_month"} eq $option{"day_of_month"} and $cron->{"command"} eq $option{"command"}) 0 0 0
Condition0%0/15
$cron->{"minute"} eq $option{"minute"} and $cron->{"hour"} eq $option{"hour"} and $cron->{"month"} eq $option{"month"} and $cron->{"day_of_week"} eq $option{"day_of_week"} and $cron->{"day_of_month"} eq $option{"day_of_month"} and $cron->{"command"} eq $option{"command"}
!ll&&!rl&&r
000
$cron->{"minute"} eq $option{"minute"} and $cron->{"hour"} eq $option{"hour"} and $cron->{"month"} eq $option{"month"} and $cron->{"day_of_week"} eq $option{"day_of_week"} and $cron->{"day_of_month"} eq $option{"day_of_month"}
!ll&&!rl&&r
000
$cron->{"minute"} eq $option{"minute"} and $cron->{"hour"} eq $option{"hour"} and $cron->{"month"} eq $option{"month"} and $cron->{"day_of_week"} eq $option{"day_of_week"}
!ll&&!rl&&r
000
$cron->{"minute"} eq $option{"minute"} and $cron->{"hour"} eq $option{"hour"} and $cron->{"month"} eq $option{"month"}
!ll&&!rl&&r
000
$cron->{"minute"} eq $option{"minute"} and $cron->{"hour"} eq $option{"hour"}
!ll&&!rl&&r
000
MC/DC0%0/6
$cron->{"minute"} eq $option{"minute"} and $cron->{"hour"} eq $option{"hour"} and $cron->{"month"} eq $option{"month"} and $cron->{"day_of_week"} eq $option{"day_of_week"} and $cron->{"day_of_month"} eq $option{"day_of_month"} and $cron->{"command"} eq $option{"command"}
$cron->{"minute"} eq $option{"minute"} $cron->{"hour"} eq $option{"hour"} $cron->{"month"} eq $option{"month"} $cron->{"day_of_week"} eq $option{"day_of_week"} $cron->{"day_of_month"} eq $option{"day_of_month"} $cron->{"command"} eq $option{"command"}
Truth table0%0/7
A and B and C and D and E and F
A= $cron->{"minute"} eq $option{"minute"}B= $cron->{"hour"} eq $option{"hour"}C= $cron->{"month"} eq $option{"month"}D= $cron->{"day_of_week"} eq $option{"day_of_week"}E= $cron->{"day_of_month"} eq $option{"day_of_month"}F= $cron->{"command"} eq $option{"command"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
112 && $cron->{hour} eq $option{hour}
113 && $cron->{month} eq $option{month}
114 && $cron->{day_of_week} eq $option{day_of_week}
115 && $cron->{day_of_month} eq $option{day_of_month}
116 && $cron->{command} eq $option{command} )
117 {
118 # cron found
119 0 $cron_id = $i;
120 0 last;
121 }
122 0 $i++;
123 }
124  
125 0 delete $option{user};
126 0 delete $option{ensure};
127 0 ▶ if ( defined $cron_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cron_id) { } 0 0 0
128 0 &cron( delete => $user, $cron_id );
129 0 $changed = 1;
130 }
131 else {
132 0 Rex::Logger::debug("Cron $name not found.");
133 0 Rex::Logger::debug( Dumper( \%option ) );
134 }
135 }
136  
137 0 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) { } 0 0 0
138 0 ▶ if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"on_change"} and ref $option{"on_change"} eq "CODE") 0 0 0
Condition0%0/3
exists $option{"on_change"} and ref $option{"on_change"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option{"on_change"} and ref $option{"on_change"} eq "CODE"
exists $option{"on_change"} ref $option{"on_change"} eq "CODE"
Truth table0%0/3
A and B
A= exists $option{"on_change"}B= ref $option{"on_change"} eq "CODE"
ABresult
0X0
100
111
139 0 $option{on_change}->( $name, %option );
140 }
141  
142 Rex::get_current_connection()->{reporter}->report(
143 0 changed => 1,
144 message => "Resource cron_entry status changed to $ensure."
145 );
146 }
147 else {
148 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
149 }
150  
151 Rex::get_current_connection()->{reporter}
152 0 ->report_resource_end( type => "cron_entry", name => $name );
153 }
154  
155 =head2 cron($action => $user, ...)
156  
157 With this function you can manage cronjobs.
158  
159 List cronjobs.
160  
161 use Rex::Commands::Cron;
162 use Data::Dumper;
163  
164 task "listcron", "server1", sub {
165 my @crons = cron list => "root";
166 print Dumper(\@crons);
167 };
168  
169 Add a cronjob.
170  
171 This example will add a cronjob running on minute 1, 5, 19 and 40. Every hour and every day.
172  
173 use Rex::Commands::Cron;
174 use Data::Dumper;
175  
176 task "addcron", "server1", sub {
177 cron add => "root", {
178 minute => "1,5,19,40",
179 command => '/path/to/your/cronjob',
180 };
181 };
182  
183 This example will add a cronjob running on the 1st, 3rd and 5th day of January and May, but only when it's a Monday or Wednesday. On those days, the job will run when the hour is 11 or 23, and the minute is 1 or 5 (in other words at 11:01, 11:05, 23:01 and 23:05).
184  
185 task "addcron", "server1", sub {
186 cron add => "root", {
187 minute => "1,5",
188 hour => "11,23",
189 month => "1,5",
190 day_of_week => "1,3",
191 day_of_month => "1,3,5",
192 command => '/path/to/your/cronjob',
193 };
194 };
195  
196 Delete a cronjob.
197  
198 This example will delete the 4th cronjob. Counting starts with zero (0).
199  
200 task "delcron", "server1", sub {
201 cron delete => "root", 3;
202 };
203  
204 Managing Environment Variables inside cron.
205  
206 task "mycron", "server1", sub {
207 cron env => user => add => {
208 MYVAR => "foo",
209 };
210  
211 cron env => user => delete => $index;
212 cron env => user => delete => 1;
213  
214 cron env => user => "list";
215 };
216  
217 =cut
218  
219 0 ▶sub cron {
Subroutine cron: not called CC=10 SCAR=47.0
Pod documented
220  
221 0 my ( $action, $user, $config, @more ) = @_;
222  
223 0 my $c = Rex::Cron->create();
224 0 $c->read_user_cron($user); # this must always be the first action
225  
226 0 ▶ if ( $action eq "list" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($action eq 'list') { } 0 0 0
elsif ($action eq 'add') { } 0 0 0
elsif ($action eq 'delete') { } 0 0 0
elsif ($action eq 'env') { } 0 0 0
227 0 return $c->list_jobs;
228 }
229  
230 elsif ( $action eq "add" ) {
231 0 ▶ if ( $c->add( %{$config} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->add(%{$config;})) 0 0 0
232 0 my $rnd_file = $c->write_cron;
233 0 $c->activate_user_cron( $rnd_file, $user );
234 0 return 1; # something changed
235 }
236 0 return 0; # nothing changed
237 }
238  
239 elsif ( $action eq "delete" ) {
240 0 my $to_delete = $config;
241 0 $c->delete_job($to_delete);
242 0 my $rnd_file = $c->write_cron;
243 0 $c->activate_user_cron( $rnd_file, $user );
244 }
245  
246 elsif ( $action eq "env" ) {
247 0 my $env_action = $config;
248  
249 0 ▶ if ( $env_action eq "add" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($env_action eq 'add') { } 0 0 0
elsif ($env_action eq 'list') { } 0 0 0
elsif ($env_action eq 'delete') { } 0 0 0
250 0 my $data = shift @more;
251  
252 0 for my $key ( keys %{$data} ) {
253 0 $c->add_env( $key => $data->{$key}, );
254 }
255  
256 0 my $rnd_file = $c->write_cron;
257 0 $c->activate_user_cron( $rnd_file, $user );
258 }
259  
260 elsif ( $env_action eq "list" ) {
261 0 return $c->list_envs;
262 }
263  
264 elsif ( $env_action eq "delete" ) {
265 0 my $num = shift @more;
266 0 $c->delete_env($num);
267  
268 0 my $rnd_file = $c->write_cron;
269 0 $c->activate_user_cron( $rnd_file, $user );
270 }
271  
272 }
273  
274 }
275  
276 32 1;
« blib/lib/Rex.pm ↑ Summary lib/Rex/Hardware/Network/Solaris.pm »