Skip to content

Commit

Permalink
fix writing cron files
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Jan 18, 2025
1 parent 3eb3431 commit 7d28b83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
25 changes: 19 additions & 6 deletions lib/Thruk/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2171,16 +2171,30 @@ sub update_cron_file {
my @orig_cron;
my $thruk_started = 0;
if(-e $c->config->{'cron_file'}) {
open(my $fh, '<', $c->config->{'cron_file'}) or die('cannot read '.$c->config->{'cron_file'}.': '.$!);
my @lines = Thruk::Utils::IO::read_as_list($c->config->{'cron_file'});
my $lastsection;
while(my $line = <$fh>) {
for(my $x = 0; $x < scalar @lines; $x++) {
my $line = $lines[$x];
chomp($line);
$thruk_started = 1 if $line =~ m/^\#\ THIS\ PART\ IS\ WRITTEN\ BY\ THRUK/mx;
unless($thruk_started) {
if($line =~ m/^\#(\Q THIS PART IS WRITTEN BY THRUK\E|\Q THIS PART IS AUTO GENERATED BY THRUK\E)/mx) {
$thruk_started = 1;
# remove ruler comment right before the marker
my $prev = pop @orig_cron;
if($prev && $prev !~ m/^\#+$/mx) {
push @orig_cron, $prev;
}
}
if(!$thruk_started) {
push @orig_cron, $line;
next;
}
$thruk_started = 0 if $line =~ m/^\#\ END\ OF\ THRUK/mx;
if($line =~ m/^\Q# END OF THRUK/mx) {
$thruk_started = 0;
# remove ruler comment right after the marker
if(scalar @lines > $x+1 && $lines[$x+1] =~ m/^\#+$/mx) {
$x++;
}
}

if($line =~ m/^\#\ ([\w\s]+)$/mx) {
$lastsection = $1;
Expand All @@ -2192,7 +2206,6 @@ sub update_cron_file {
$sections->{$lastsection} = [] unless defined $sections->{$lastsection};
push @{$sections->{$lastsection}}, $line;
}
CORE::close($fh) or die("cannot close file ".$c->config->{'cron_file'}.": ".$!);
}

# write out new file
Expand Down
6 changes: 3 additions & 3 deletions t/100-cli-thruk.t
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ TestUtils::test_command({
like => ['/^OK - recurring downtime saved$/'],
});
TestUtils::test_command({
cmd => '/usr/bin/crontab -l '.$cronuser.' | grep "THIS PART IS WRITTEN BY THRUK" | wc -l',
cmd => '/usr/bin/crontab -l '.$cronuser.' | grep "THIS PART IS AUTO GENERATED BY THRUK" | wc -l',
like => ['/^\s*1$/' ],
});

Expand All @@ -128,7 +128,7 @@ TestUtils::test_command({
like => ['/OK - updated crontab$/'],
});
TestUtils::test_command({
cmd => '/usr/bin/crontab -l '.$cronuser.' | grep "THIS PART IS WRITTEN BY THRUK" | wc -l',
cmd => '/usr/bin/crontab -l '.$cronuser.' | grep "THIS PART IS AUTO GENERATED BY THRUK" | wc -l',
like => ['/^\s*1$/' ],
});

Expand All @@ -154,7 +154,7 @@ TestUtils::test_command({
like => ['/^cron entries removed/'],
});
TestUtils::test_command({
cmd => '/usr/bin/crontab -l '.$cronuser.' | grep "THIS PART IS WRITTEN BY THRUK" | wc -l',
cmd => '/usr/bin/crontab -l '.$cronuser.' | grep "THIS PART IS AUTO GENERATED BY THRUK" | wc -l',
like => ['/^\s*0$/' ],
});

Expand Down

0 comments on commit 7d28b83

Please sign in to comment.