File Coverage

blib/lib/Armadito/Agent/Scheduler/Cron.pm
Criterion Covered Total %
statement 24 64 37.5
branch n/a
condition n/a
subroutine 8 16 50.0
pod 1 2 50.0
total 33 82 40.2


line stmt bran cond sub pod time code
1             package Armadito::Agent::Scheduler::Cron;
2              
3 1     1   9389131 use strict;
  1         2  
  1         52  
4 1     1   5 use warnings;
  1         1  
  1         67  
5 1     1   3 use base 'Armadito::Agent::Task::Scheduler';
  1         33  
  1         436  
6              
7 1     1   5 use Armadito::Agent::Tools::File qw (writeFile readFile);
  1         1  
  1         43  
8 1     1   352 use Armadito::Agent::Tools::Time qw (nowToISO8601);
  1         2  
  1         51  
9 1     1   402 use Armadito::Agent::Patterns::Matcher;
  1         2  
  1         10  
10 1     1   25 use Cwd 'abs_path';
  1         1  
  1         43  
11 1     1   4 use Data::Dumper;
  1         1  
  1         465  
12              
13             sub _loadConf {
14 0     0     my ( $self, %params ) = @_;
15              
16 0           $self->{config} = $self->_parseConf( $self->_getConfPath() );
17             }
18              
19             sub _parseConf {
20 0     0     my ( $self, $conf_path ) = @_;
21              
22 0           my $conf_file = readFile( filepath => $conf_path );
23              
24 0           my $parser = Armadito::Agent::Patterns::Matcher->new( logger => $self->{logger} );
25              
26 0           $parser->addExclusionPattern('^#');
27 0           $parser->addPattern( 'logfile', 'Logfile\s*=\s*(.*)\s*$' );
28 0           $parser->addPattern( 'user', 'User\s*=\s*(.*)\s*$' );
29              
30 0           my $labels = [ 'freq', 'name', 'args' ];
31 0           my $pattern = '^([\s\*\d\/,]*?);(.*?);(.*?)$';
32 0           $parser->addPattern( 'tasks', $pattern, $labels );
33              
34 0           $parser->run( $conf_file, '\n' );
35 0           $parser->addHookForLabel( 'freq', \&trimSpaces );
36 0           $parser->addHookForLabel( 'name', \&trimSpaces );
37 0           $parser->addHookForLabel( 'args', \&trimSpaces );
38              
39 0           return $parser->getResults();
40             }
41              
42             sub trimSpaces {
43 0     0 0   my ($match) = @_;
44              
45 0           $match =~ s/\s+$//ms;
46 0           $match =~ s/^\s+//ms;
47              
48 0           return $match;
49             }
50              
51             sub _getDefaultConf {
52 0     0     my ($self) = @_;
53              
54             return {
55 0           'user' => undef,
56             'Logfile' => undef
57             };
58             }
59              
60             sub _getConfPath {
61 0     0     my ($self) = @_;
62              
63 0           return $self->{agent}->{confdir} . "/scheduler-" . lc( $self->{scheduler}->{name} ) . ".cfg";
64             }
65              
66             sub _updateCronTab {
67 0     0     my ($self) = @_;
68              
69 0           my $cron_path = "/etc/cron.d/armadito-agent";
70 0           my $content = "#\n# Cron configuration for armadito-agent\n#\n";
71              
72 0           $content .= "# last modification by armadito-agent : " . nowToISO8601('Local') . "\n\n";
73 0           $content .= "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n\n";
74              
75 0           foreach ( @{ $self->{config}->{tasks} } ) {
  0            
76 0           $content .= $self->_addCronTask($_);
77             }
78              
79             writeFile(
80 0           content => $content,
81             mode => '>',
82             filepath => $cron_path
83             );
84             }
85              
86             sub _addCronTask {
87 0     0     my ( $self, $task ) = @_;
88              
89             return
90             $task->{freq} . "\t"
91             . $self->{config}->{user}[0] . "\t"
92             . abs_path($0)
93             . " -t \""
94             . $task->{name} . "\" "
95             . $task->{args} . " " . ">>"
96 0           . $self->{config}->{logfile}[0] . ' 2>&1' . "\n";
97             }
98              
99             sub run {
100 0     0 1   my ( $self, %params ) = @_;
101              
102 0           $self = $self->SUPER::run(%params);
103 0           $self->_loadConf();
104 0           $self->_updateCronTab();
105              
106 0           return $self;
107             }
108              
109             1;
110              
111             __END__
112              
113             =head1 NAME
114              
115             Armadito::Agent::Scheduler::Cron - base class used for task scheduling management
116              
117             =head1 DESCRIPTION
118              
119             This task inherits from L<Armadito::Agent::Task::Scheduler>. It allows remote management of agent's crontab configuration.
120              
121             =head1 FUNCTIONS
122              
123             =head2 run ( $self, %params )
124              
125             Run the task.
126