From 0e60bedda952d7f653e4d2053ba30fd29e1e193a Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Mon, 30 Dec 2013 21:41:09 +1100 Subject: [PATCH 01/10] Ensure old config is removed when chaning to/from hourly rotation When changing an existing rule to hourly, the config should be removed from /etc/logrotate.d/. Similarly, when changing a rule from hourly, the config should be removed from /etc/logrotate.d/hourly/. Fixes #22 --- manifests/rule.pp | 8 ++++++++ spec/defines/rule_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/manifests/rule.pp b/manifests/rule.pp index 8a5eaa37..af1bd422 100644 --- a/manifests/rule.pp +++ b/manifests/rule.pp @@ -406,9 +406,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..bc84b871 100644 --- a/spec/defines/rule_spec.rb +++ b/spec/defines/rule_spec.rb @@ -744,6 +744,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 +756,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 +772,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 +788,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 +804,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 From 141df8bce78d6ebca101d4ac4cff751fb72ce8d1 Mon Sep 17 00:00:00 2001 From: Keith McDuffee Date: Fri, 14 Feb 2014 09:23:17 -0500 Subject: [PATCH 02/10] Changed postrotate option to accept array value This allows for multi-line postrotate values when a rotation requires multiple commands to be executed upon completion. --- templates/etc/logrotate.d/rule.erb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templates/etc/logrotate.d/rule.erb b/templates/etc/logrotate.d/rule.erb index 4aa179e0..01e852c4 100644 --- a/templates/etc/logrotate.d/rule.erb +++ b/templates/etc/logrotate.d/rule.erb @@ -51,7 +51,9 @@ <% end -%> <% if @postrotate != 'undef' -%> postrotate - <%= @postrotate %> +<% @postrotate.each do |val| -%> + <%= val %> +<% end -%> endscript <% end -%> <% if @prerotate != 'undef' -%> From 136db0935c7a3855e0efdb95cdac8c1a8911a748 Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Tue, 31 Dec 2013 12:30:00 +1100 Subject: [PATCH 03/10] Add .ruby-version to gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From 4c943f1f3eedb7bc14deea20167e03dbf3982665 Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Wed, 19 Mar 2014 19:40:09 +1100 Subject: [PATCH 04/10] Update documentation to reflect the array of postrotate commands --- README.md | 4 ++-- manifests/rule.pp | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7e07da14..9564b08d 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,8 @@ 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). +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 that should be executed by /bin/sh before the log file is rotated and only if it will be rotated (optional). diff --git a/manifests/rule.pp b/manifests/rule.pp index af1bd422..7ddef0f0 100644 --- a/manifests/rule.pp +++ b/manifests/rule.pp @@ -54,8 +54,9 @@ # 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). +# 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 that should be executed by /bin/sh before # the log file is rotated and only if it will be rotated # (optional). From 67daa6f3435df4050b8dba087c125bda5eb1f518 Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Wed, 19 Mar 2014 19:40:44 +1100 Subject: [PATCH 05/10] Add test case for array of postrotate commands --- spec/defines/rule_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/defines/rule_spec.rb b/spec/defines/rule_spec.rb index bc84b871..f7b78952 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 From b3cee76da6be5657858a99b57869e7291087dc5e Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Wed, 19 Mar 2014 19:42:19 +1100 Subject: [PATCH 06/10] support postrotate as both an array and a string --- templates/etc/logrotate.d/rule.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/etc/logrotate.d/rule.erb b/templates/etc/logrotate.d/rule.erb index 01e852c4..d92d0e25 100644 --- a/templates/etc/logrotate.d/rule.erb +++ b/templates/etc/logrotate.d/rule.erb @@ -51,6 +51,7 @@ <% end -%> <% if @postrotate != 'undef' -%> postrotate + <%- @postrotate = [@postrotate] unless @postrotate.is_a?(Array) -%> <% @postrotate.each do |val| -%> <%= val %> <% end -%> From 93cc853dd3059ed6026f00c5f375e4bc1e587242 Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Wed, 19 Mar 2014 20:08:38 +1100 Subject: [PATCH 07/10] Add specs for prerotate, firstaction & lastaction taking arrays --- spec/defines/rule_spec.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spec/defines/rule_spec.rb b/spec/defines/rule_spec.rb index f7b78952..4caa8ffa 100644 --- a/spec/defines/rule_spec.rb +++ b/spec/defines/rule_spec.rb @@ -695,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 @@ -708,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 @@ -721,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 From bfb47c288cbdf59a44c6934084403ec68ba21a5b Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Wed, 19 Mar 2014 20:10:38 +1100 Subject: [PATCH 08/10] add support for arrays in prerotate, firstaction & lastaction --- templates/etc/logrotate.d/rule.erb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/templates/etc/logrotate.d/rule.erb b/templates/etc/logrotate.d/rule.erb index d92d0e25..9547353d 100644 --- a/templates/etc/logrotate.d/rule.erb +++ b/templates/etc/logrotate.d/rule.erb @@ -52,24 +52,33 @@ <% if @postrotate != 'undef' -%> postrotate <%- @postrotate = [@postrotate] unless @postrotate.is_a?(Array) -%> -<% @postrotate.each do |val| -%> + <%- @postrotate.each do |val| -%> <%= val %> -<% end -%> + <%- 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 -%> } From f61b16d18cf4f9b9e85bdaf801a258f7978cea9a Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Wed, 19 Mar 2014 20:10:46 +1100 Subject: [PATCH 09/10] Update docs --- README.md | 18 +++++++++--------- manifests/rule.pp | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9564b08d..1f83fa4d 100644 --- a/README.md +++ b/README.md @@ -76,15 +76,15 @@ olddir - A String path to a directory that rotated logs should be moved to (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 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). +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/manifests/rule.pp b/manifests/rule.pp index 7ddef0f0..3f42ee9f 100644 --- a/manifests/rule.pp +++ b/manifests/rule.pp @@ -57,15 +57,15 @@ # 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 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). +# 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. From 830338ae1073fba511a845ae4fc2373aefa19ed9 Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Wed, 19 Mar 2014 20:27:10 +1100 Subject: [PATCH 10/10] Improve cron script to hide command output unless it fails Fixes #24 Fixes #16 --- files/etc/cron.daily/logrotate | 9 +++++++-- files/etc/cron.hourly/logrotate | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) 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