diff --git a/.gitignore b/.gitignore index c3f3cf1c..b6715f5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .bundle/ -vendor/gems +vendor/gems/ +.ruby-version diff --git a/README.md b/README.md index 7e07da14..1f83fa4d 100644 --- a/README.md +++ b/README.md @@ -74,17 +74,17 @@ missingok - A Boolean specifying whether logrotate should ignore missing log files or issue an error (optional). olddir - A String path to a directory that rotated logs should be moved to (optional). -postrotate - A command String that should be executed by /bin/sh after - the log file is rotated (optional). -prerotate - A command String that should be executed by /bin/sh before - the log file is rotated and only if it will be rotated - (optional). -firstaction - A command String that should be executed by /bin/sh once - before all log files that match the wildcard pattern are - rotated (optional). -lastaction - A command String that should be execute by /bin/sh once - after all the log files that match the wildcard pattern are - rotated (optional). +postrotate - A command String or an Array of Strings that should be + executed by /bin/sh after the log file is rotated (optional). +prerotate - A command String or an Array of Strings that should be + executed by /bin/sh before the log file is rotated and only + if it will be rotated (optional). +firstaction - A command String or an Array of Strings that should be + executed by /bin/sh once before all log files that match the + wildcard pattern are rotated (optional). +lastaction - A command String or an Array of Strings that should be + executed by /bin/sh once after all the log files that match + the wildcard pattern are rotated (optional). rotate - The Integer number of rotated log files to keep on disk (optional). rotate_every - How often the log files should be rotated as a String. diff --git a/files/etc/cron.daily/logrotate b/files/etc/cron.daily/logrotate index 05c4721f..f48c56a7 100644 --- a/files/etc/cron.daily/logrotate +++ b/files/etc/cron.daily/logrotate @@ -2,5 +2,10 @@ # THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET. ANY CHANGES WILL BE # OVERWRITTEN. -test -x /usr/sbin/logrotate || exit 0 -/usr/sbin/logrotate /etc/logrotate.conf +OUTPUT=$(/usr/sbin/logrotate /etc/logrotate.conf 2>&1) +EXITVALUE=$? +if [ $EXITVALUE != 0 ]; then + /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" + echo "${OUTPUT}" +fi +exit $EXITVALUE diff --git a/files/etc/cron.hourly/logrotate b/files/etc/cron.hourly/logrotate index 28b38f0f..53a179c0 100644 --- a/files/etc/cron.hourly/logrotate +++ b/files/etc/cron.hourly/logrotate @@ -2,5 +2,10 @@ # 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 +OUTPUT=$(/usr/sbin/logrotate /etc/logrotate.d/hourly 2>&1) +EXITVALUE=$? +if [ $EXITVALUE != 0 ]; then + /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" + echo "${OUTPUT}" +fi +exit $EXITVALUE diff --git a/manifests/rule.pp b/manifests/rule.pp index e9003ac2..22b52019 100644 --- a/manifests/rule.pp +++ b/manifests/rule.pp @@ -54,17 +54,18 @@ # log files or issue an error (optional). # olddir - A String path to a directory that rotated logs should be # moved to (optional). -# postrotate - A command String that should be executed by /bin/sh after -# the log file is rotated (optional). -# prerotate - A command String that should be executed by /bin/sh before -# the log file is rotated and only if it will be rotated -# (optional). -# firstaction - A command String that should be executed by /bin/sh once -# before all log files that match the wildcard pattern are -# rotated (optional). -# lastaction - A command String that should be execute by /bin/sh once -# after all the log files that match the wildcard pattern are -# rotated (optional). +# postrotate - A command String or an Array of Strings that should be +# executed by /bin/sh after the log file is rotated +# optional). +# prerotate - A command String or an Array of Strings that should be +# executed by /bin/sh before the log file is rotated and +# only if it will be rotated (optional). +# firstaction - A command String or an Array of Strings that should be +# executed by /bin/sh once before all log files that match +# the wildcard pattern are rotated (optional). +# lastaction - A command String or an Array of Strings that should be +# executed by /bin/sh once after all the log files that match +# the wildcard pattern are rotated (optional). # rotate - The Integer number of rotated log files to keep on disk # (optional). # rotate_every - How often the log files should be rotated as a String. @@ -406,9 +407,17 @@ 'hour', 'hourly': { include logrotate::hourly $rule_path = "/etc/logrotate.d/hourly/${name}" + + file { "/etc/logrotate.d/${name}": + ensure => absent, + } } default: { $rule_path = "/etc/logrotate.d/${name}" + + file { "/etc/logrotate.d/hourly/${name}": + ensure => absent, + } } } diff --git a/spec/defines/rule_spec.rb b/spec/defines/rule_spec.rb index 6b237215..4caa8ffa 100644 --- a/spec/defines/rule_spec.rb +++ b/spec/defines/rule_spec.rb @@ -671,6 +671,17 @@ end end + context "and postrotate => ['/bin/true', '/bin/false']" do + let(:params) { + {:path => '/var/log/foo.log', :postrotate => ['/bin/true', '/bin/false']} + } + + it do + should contain_file('/etc/logrotate.d/test') \ + .with_content(/postrotate\n \/bin\/true\n \/bin\/false\n endscript/) + end + end + ########################################################################### # PREROTATE context 'and prerotate => /bin/true' do @@ -684,6 +695,17 @@ end end + context "and prerotate => ['/bin/true', '/bin/false']" do + let(:params) { + {:path => '/var/log/foo.log', :prerotate => ['/bin/true', '/bin/false']} + } + + it do + should contain_file('/etc/logrotate.d/test') \ + .with_content(/prerotate\n \/bin\/true\n \/bin\/false\n endscript/) + end + end + ########################################################################### # FIRSTACTION context 'and firstaction => /bin/true' do @@ -697,6 +719,17 @@ end end + context "and firstaction => ['/bin/true', '/bin/false']" do + let(:params) { + {:path => '/var/log/foo.log', :firstaction => ['/bin/true', '/bin/false']} + } + + it do + should contain_file('/etc/logrotate.d/test') \ + .with_content(/firstaction\n \/bin\/true\n \/bin\/false\n endscript/) + end + end + ########################################################################### # LASTACTION context 'and lastaction => /bin/true' do @@ -710,6 +743,17 @@ end end + context "and lastaction => ['/bin/true', '/bin/false']" do + let(:params) { + {:path => '/var/log/foo.log', :lastaction => ['/bin/true', '/bin/false']} + } + + it do + should contain_file('/etc/logrotate.d/test') \ + .with_content(/lastaction\n \/bin\/true\n \/bin\/false\n endscript/) + end + end + ########################################################################### # ROTATE context 'and rotate => 3' do @@ -744,6 +788,7 @@ it { should contain_class('logrotate::hourly') } it { should contain_file('/etc/logrotate.d/hourly/test') } + it { should contain_file('/etc/logrotate.d/test').with_ensure('absent') } end context 'and rotate_every => day' do @@ -755,6 +800,11 @@ should contain_file('/etc/logrotate.d/test') \ .with_content(/^ daily$/) end + + it do + should contain_file('/etc/logrotate.d/hourly/test') \ + .with_ensure('absent') + end end context 'and rotate_every => week' do @@ -766,6 +816,11 @@ should contain_file('/etc/logrotate.d/test') \ .with_content(/^ weekly$/) end + + it do + should contain_file('/etc/logrotate.d/hourly/test') \ + .with_ensure('absent') + end end context 'and rotate_every => month' do @@ -777,6 +832,11 @@ should contain_file('/etc/logrotate.d/test') \ .with_content(/^ monthly$/) end + + it do + should contain_file('/etc/logrotate.d/hourly/test') \ + .with_ensure('absent') + end end context 'and rotate_every => year' do @@ -788,6 +848,11 @@ should contain_file('/etc/logrotate.d/test') \ .with_content(/^ yearly$/) end + + it do + should contain_file('/etc/logrotate.d/hourly/test') \ + .with_ensure('absent') + end end context 'and rotate_every => foo' do diff --git a/templates/etc/logrotate.d/rule.erb b/templates/etc/logrotate.d/rule.erb index d6d9deba..67ca0148 100644 --- a/templates/etc/logrotate.d/rule.erb +++ b/templates/etc/logrotate.d/rule.erb @@ -51,22 +51,34 @@ <% end -%> <% if @postrotate != 'undef' -%> postrotate - <%= @postrotate %> + <%- @postrotate = [@postrotate] unless @postrotate.is_a?(Array) -%> + <%- @postrotate.each do |val| -%> + <%= val %> + <%- end -%> endscript <% end -%> <% if @prerotate != 'undef' -%> prerotate - <%= @prerotate %> + <%- @prerotate = [@prerotate] unless @prerotate.is_a?(Array) -%> + <%- @prerotate.each do |val| -%> + <%= val %> + <%- end -%> endscript <% end -%> <% if @firstaction != 'undef' -%> firstaction - <%= @firstaction %> + <%- @firstaction = [@firstaction] unless @firstaction.is_a?(Array) -%> + <%- @firstaction.each do |val| -%> + <%= val %> + <%- end -%> endscript <% end -%> <% if @lastaction != 'undef' -%> lastaction - <%= @lastaction %> + <%- @lastaction = [@lastaction] unless @lastaction.is_a?(Array) -%> + <%- @lastaction.each do |val| -%> + <%= val %> + <%- end -%> endscript <% end -%> }