Skip to content

Commit

Permalink
Merge pull request #60 from mdschmitt/add_hourly_capability
Browse files Browse the repository at this point in the history
feat(hourly): Add configuration to enable hourly jobs
  • Loading branch information
myii authored Jan 14, 2022
2 parents 6b3042d + 69677b0 commit ee43fac
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 20 deletions.
8 changes: 4 additions & 4 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,14 @@ suites:
- name: default
provisioner:
dependencies:
- name: states
path: ./test/salt
- name: cron
repo: git
source: https://github.com/saltstack-formulas/cron-formula.git
state_top:
base:
'G@os_family:Suse or G@os_family:Debian':
- states.cron
'*':
- logrotate._mapdata
- cron
- logrotate
pillars:
top.sls:
Expand Down
51 changes: 45 additions & 6 deletions logrotate/config.sls
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,61 @@
include:
- logrotate
{% set ns = namespace(hourly=False) %}
{% for key, value in logrotate.jobs.items() %}
{% set contents = value.get('contents', False) %}
{% if 'hourly' in (contents or value.config) %}
{% set ns.hourly = True %}
{% break %}
{% endif %}
{% endfor %}
logrotate-config:
file.managed:
- name: {{ logrotate.conf_file }}
- source: salt://logrotate/templates/logrotate.conf.tmpl
- template: jinja
- user: {{ salt['pillar.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['pillar.get']('logrotate:config:group', logrotate.group) }}
- mode: {{ salt['pillar.get']('logrotate:config:mode', '644') }}
- user: {{ salt['config.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['config.get']('logrotate:config:group', logrotate.group) }}
- mode: {{ salt['config.get']('logrotate:config:mode', '0644') }}
- context:
logrotate: {{ logrotate|tojson }}
logrotate-directory:
file.directory:
- name: {{ logrotate.include_dir }}
- user: {{ salt['pillar.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['pillar.get']('logrotate:config:group', logrotate.group) }}
- mode: 755
- user: {{ salt['config.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['config.get']('logrotate:config:group', logrotate.group) }}
- mode: '0755'
- makedirs: True
{%- if ns.hourly %}
logrotate-hourly-config:
file.managed:
- name: {{ logrotate.hourly_conf_file }}
- user: {{ salt['config.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['config.get']('logrotate:config:group', logrotate.group) }}
- mode: {{ salt['config.get']('logrotate:config:mode', '0644') }}
- contents:
- include {{ logrotate.hourly_include_dir }}
logrotate-hourly-directory:
file.directory:
- name: {{ logrotate.hourly_include_dir }}
- user: {{ logrotate.user }}
- group: {{ logrotate.group }}
- mode: '0755'
- makedirs: True
logrotate-hourly-cron:
file.managed:
- name: "/etc/cron.hourly/logrotate"
- source: salt://logrotate/templates/logrotate.hourly.tmpl
- template: jinja
- user: {{ salt['config.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['config.get']('logrotate:config:group', logrotate.group) }}
- mode: '0775'
- context:
logrotate: {{ logrotate|tojson }}
{%- endif %}
4 changes: 4 additions & 0 deletions logrotate/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
# vim: ft=yaml
---
logrotate:
bin: /usr/sbin/logrotate
status_dir: /var/lib/logrotate
pkg: logrotate
conf_file: '/etc/logrotate.conf'
include_dir: '/etc/logrotate.d'
hourly_conf_file: '/etc/logrotate.hourly.conf'
hourly_include_dir: '/etc/logrotate.hourly.d'
user: root
group: root
service: cron
Expand Down
9 changes: 8 additions & 1 deletion logrotate/jobs.sls
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ include:
{% for key, value in jobs.items() %}
{% set contents = value.get('contents', False) %}
logrotate-{{ key }}:
file.managed:
{% if 'hourly' in (contents or value.config) %}
- name: {{ logrotate.hourly_include_dir }}/{{ key.split("/")[-1] }}
- require:
- file: {{ logrotate.hourly_include_dir }}
{% else %}
- name: {{ logrotate.include_dir }}/{{ key.split("/")[-1] }}
{% endif %}
- user: {{ salt['pillar.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['pillar.get']('logrotate:config:group', logrotate.group) }}
- mode: {{ salt['pillar.get']('logrotate:config:mode', '644') }}
Expand All @@ -29,5 +36,5 @@ logrotate-{{ key }}:
data: {{ value | json }}
{% endif %}
{% endif %}
{%- endfor -%}
{% endfor %}
4 changes: 4 additions & 0 deletions logrotate/osfamilymap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Gentoo:
tabooext: + .keep
dateext: true
FreeBSD:
bin: /usr/local/sbin/logrotate
status_dir: /var/run
conf_file: /usr/local/etc/logrotate.conf
hourly_conf_file: /usr/local/etc/logrotate.hourly.conf
include_dir: /usr/local/etc/logrotate.d
hourly_include_dir: /usr/local/etc/logrotate.hourly.d
group: wheel
9 changes: 9 additions & 0 deletions logrotate/templates/logrotate.hourly.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

{{ logrotate.bin }} -s {{ logrotate.status_dir }}/logrotate.hourly.status {{ logrotate.hourly_conf_file }}
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi

exit 0
16 changes: 16 additions & 0 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ logrotate:
- postrotate
- /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
- endscript
nginx_high_traf:
path:
- /var/log/nginx_high_traf/*.log
config:
- hourly
- missingok
- rotate {{ 30 * 24 }} # keep 1 month's worth of hourly logs
- compress
- notifempty
- dateext
- dateformat .%Y-%m-%d-%H00
- olddir /var/log/nginx_high_traf/archive
- sharedscripts
- postrotate
- 'kill -USR1 $(cat /var/run/nginx_high_traf.pid)'
- endscript
nginx:
contents: |
/var/log/nginx/*.log{
Expand Down
8 changes: 3 additions & 5 deletions test/integration/default/controls/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@
control 'logrotate.service.running' do
title 'The service should be installed, enabled and running'

only_if('Disabled on Arch Linux') do
!%w[arch].include?(platform[:name])
end

service =
case platform[:family]
case system.platform[:family]
when 'arch'
'cronie'
when 'redhat', 'fedora'
'crond'
else
Expand Down
19 changes: 19 additions & 0 deletions test/integration/default/controls/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,22 @@
its('content') { should include 'postrotate' }
its('content') { should include ' invoke-rc.d nginx rotate >/dev/null 2>&1' }
end

describe file('/etc/logrotate.hourly.d/nginx_high_traf') do
it { should be_file }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('mode') { should cmp '0644' }
its('content') { should include '/var/log/nginx_high_traf/*.log' }
its('content') { should include 'hourly' }
its('content') { should include 'missingok' }
its('content') { should include 'rotate 720' }
its('content') { should include 'compress' }
its('content') { should include 'notifempty' }
its('content') { should include 'dateext' }
its('content') { should include 'dateformat .%Y-%m-%d-%H00' }
its('content') { should include 'olddir /var/log/nginx_high_traf/archive' }
its('content') { should include 'sharedscripts' }
its('content') { should include 'postrotate' }
its('content') { should include 'kill -USR1 $(cat /var/run/nginx_high_traf.pid)' }
end
4 changes: 0 additions & 4 deletions test/salt/states/cron.sls

This file was deleted.

0 comments on commit ee43fac

Please sign in to comment.