Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request rodjek#11 from rodjek/hourly
Browse files Browse the repository at this point in the history
Add support for hourly logrotate jobs
  • Loading branch information
timtim123456 committed Jun 9, 2013
2 parents e21a0f3 + f86571b commit b1f6aa1
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
6 changes: 6 additions & 0 deletions files/etc/cron.hourly/logrotate
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET. ANY CHANGES WILL BE
# OVERWRITTEN.

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.d/hourly
45 changes: 45 additions & 0 deletions manifests/hourly.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Internal: Configure a host for hourly logrotate jobs.
#
# ensure - The desired state of hourly logrotate support. Valid values are
# 'absent' and 'present' (default: 'present').
#
# Examples
#
# # Set up hourly logrotate jobs
# include logrotate::hourly
#
# # Remove hourly logrotate job support
# class { 'logrotate::hourly':
# ensure => absent,
# }
class logrotate::hourly($ensure='present') {
case $ensure {
'absent': {
$dir_ensure = $ensure
}
'present': {
$dir_ensure = 'directory'
}
default: {
fail("Class[Logrotate::Hourly]: Invalid ensure value '${ensure}'")
}
}

file {
'/etc/logrotate.d/hourly':
ensure => $dir_ensure,
owner => 'root',
group => 'root',
mode => '0755';
'/etc/cron.hourly/logrotate':
ensure => $ensure,
owner => 'root',
group => 'root',
mode => '0555',
source => 'puppet:///modules/logrotate/etc/cron.hourly/logrotate',
require => [
File['/etc/logrotate.d/hourly'],
Package['logrotate'],
];
}
}
13 changes: 12 additions & 1 deletion manifests/rule.pp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@

case $rotate_every {
'undef': {}
'hour', 'hourly': {}
'day': { $_rotate_every = 'daily' }
'week': { $_rotate_every = 'weekly' }
'month': { $_rotate_every = 'monthly' }
Expand Down Expand Up @@ -371,7 +372,17 @@

include logrotate::base

file { "/etc/logrotate.d/${name}":
case $rotate_every {
'hour', 'hourly': {
include logrotate::hourly
$rule_path = "/etc/logrotate.d/hourly/${name}"
}
default: {
$rule_path = "/etc/logrotate.d/${name}"
}
}

file { $rule_path:
ensure => $ensure,
owner => 'root',
group => 'root',
Expand Down
45 changes: 45 additions & 0 deletions spec/classes/hourly_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'spec_helper'

describe 'logrotate::hourly' do
context 'with default values' do
it do
should contain_file('/etc/logrotate.d/hourly').with({
'ensure' => 'directory',
'owner' => 'root',
'group' => 'root',
'mode' => '0755',
})
end

it do
should contain_file('/etc/cron.hourly/logrotate').with({
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0555',
'source' => 'puppet:///modules/logrotate/etc/cron.hourly/logrotate',
'require' => [
'File[/etc/logrotate.d/hourly]',
'Package[logrotate]',
],
})
end
end

context 'with ensure => absent' do
let(:params) { {:ensure => 'absent'} }

it { should contain_file('/etc/logrotate.d/hourly').with_ensure('absent') }
it { should contain_file('/etc/cron.hourly/logrotate').with_ensure('absent') }
end

context 'with ensure => foo' do
let(:params) { {:ensure => 'foo'} }

it do
expect {
should contain_file('/etc/logrotate.d/hourly')
}.to raise_error(Puppet::Error, /Invalid ensure value 'foo'/)
end
end
end
9 changes: 9 additions & 0 deletions spec/defines/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,15 @@

###########################################################################
# ROTATE_EVERY
context 'and rotate_every => hour' do
let(:params) {
{:path => '/var/log/foo.log', :rotate_every => 'hour'}
}

it { should include_class('logrotate::hourly') }
it { should contain_file('/etc/logrotate.d/hourly/test') }
end

context 'and rotate_every => day' do
let(:params) {
{:path => '/var/log/foo.log', :rotate_every => 'day'}
Expand Down

0 comments on commit b1f6aa1

Please sign in to comment.