File Coverage

lib/Rex/Commands/Cron.pm
Criterion Covered Total %
statement 20 85 23.5
branch 0 30 0.0
condition 0 20 0.0
subroutine 7 9 77.7
pod 2 2 100.0
total 29 146 19.8


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
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     32   449 use v5.12.5;
  32         205  
37 32     32   162 use warnings;
  32         63  
  32         1565  
38              
39             our $VERSION = '1.14.2.3'; # TRIAL VERSION
40              
41             require Rex::Exporter;
42 32     32   190 use base qw(Rex::Exporter);
  32         64  
  32         2280  
43 32     32   207 use vars qw(@EXPORT);
  32         152  
  32         1264  
44 32     32   222 use Carp;
  32         76  
  32         1868  
45              
46 32     32   358 use Rex::Cron;
  32         103  
  32         390  
47 32     32   1118 use Data::Dumper;
  32         74  
  32         26634  
48              
49             @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             sub cron_entry {
81 0     0 1   my ( $name, %option ) = @_;
82              
83 0   0       $option{ensure} ||= "present";
84 0 0         confess "No 'user' given." if ( !exists $option{user} );
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 0         if ( $option{ensure} eq "present" ) {
    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 0 0       if ( $cron->{minute} eq $option{minute}
      0        
      0        
      0        
      0        
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 0         if ( defined $cron_id ) {
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 0         if ($changed) {
138 0 0 0       if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) {
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             sub cron {
220              
221 0     0 1   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 0         if ( $action eq "list" ) {
    0          
    0          
    0          
227 0           return $c->list_jobs;
228             }
229              
230             elsif ( $action eq "add" ) {
231 0 0         if ( $c->add( %{$config} ) ) {
  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 0         if ( $env_action eq "add" ) {
    0          
    0          
250 0           my $data = shift @more;
251              
252 0           for my $key ( keys %{$data} ) {
  0            
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             1;