diff --git a/files/etc/cron.hourly/logrotate b/files/etc/cron.hourly/logrotate new file mode 100644 index 00000000..28b38f0f --- /dev/null +++ b/files/etc/cron.hourly/logrotate @@ -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 diff --git a/manifests/hourly.pp b/manifests/hourly.pp new file mode 100644 index 00000000..1dc2b766 --- /dev/null +++ b/manifests/hourly.pp @@ -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'], + ]; + } +} diff --git a/manifests/rule.pp b/manifests/rule.pp index 73ebdb6a..321414d8 100644 --- a/manifests/rule.pp +++ b/manifests/rule.pp @@ -272,6 +272,7 @@ case $rotate_every { 'undef': {} + 'hour', 'hourly': {} 'day': { $_rotate_every = 'daily' } 'week': { $_rotate_every = 'weekly' } 'month': { $_rotate_every = 'monthly' } @@ -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', diff --git a/spec/classes/hourly_spec.rb b/spec/classes/hourly_spec.rb new file mode 100644 index 00000000..213f7ac8 --- /dev/null +++ b/spec/classes/hourly_spec.rb @@ -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 diff --git a/spec/defines/rule_spec.rb b/spec/defines/rule_spec.rb index b3c41bed..74371bdf 100644 --- a/spec/defines/rule_spec.rb +++ b/spec/defines/rule_spec.rb @@ -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'}