Browse Source

Merge pull request #60 from mdschmitt/add_hourly_capability

feat(hourly): Add configuration to enable hourly jobs
tags/v0.13.0
Imran Iqbal 2 years ago
parent
commit
ee43fac6e8
No account linked to committer's email address
10 changed files with 112 additions and 20 deletions
  1. +4
    -4
      kitchen.yml
  2. +45
    -6
      logrotate/config.sls
  3. +4
    -0
      logrotate/defaults.yaml
  4. +8
    -1
      logrotate/jobs.sls
  5. +4
    -0
      logrotate/osfamilymap.yaml
  6. +9
    -0
      logrotate/templates/logrotate.hourly.tmpl
  7. +16
    -0
      pillar.example
  8. +3
    -5
      test/integration/default/controls/base.rb
  9. +19
    -0
      test/integration/default/controls/jobs.rb
  10. +0
    -4
      test/salt/states/cron.sls

+ 4
- 4
kitchen.yml View File

- name: default - name: default
provisioner: provisioner:
dependencies: dependencies:
- name: states
path: ./test/salt
- name: cron
repo: git
source: https://github.com/saltstack-formulas/cron-formula.git
state_top: state_top:
base: base:
'G@os_family:Suse or G@os_family:Debian':
- states.cron
'*': '*':
- logrotate._mapdata - logrotate._mapdata
- cron
- logrotate - logrotate
pillars: pillars:
top.sls: top.sls:

+ 45
- 6
logrotate/config.sls View File

include: include:
- logrotate - 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: logrotate-config:
file.managed: file.managed:
- name: {{ logrotate.conf_file }} - name: {{ logrotate.conf_file }}
- source: salt://logrotate/templates/logrotate.conf.tmpl - source: salt://logrotate/templates/logrotate.conf.tmpl
- template: jinja - 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: - context:
logrotate: {{ logrotate|tojson }} logrotate: {{ logrotate|tojson }}


logrotate-directory: logrotate-directory:
file.directory: file.directory:
- name: {{ logrotate.include_dir }} - 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 - 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
- 0
logrotate/defaults.yaml View File

# vim: ft=yaml # vim: ft=yaml
--- ---
logrotate: logrotate:
bin: /usr/sbin/logrotate
status_dir: /var/lib/logrotate
pkg: logrotate pkg: logrotate
conf_file: '/etc/logrotate.conf' conf_file: '/etc/logrotate.conf'
include_dir: '/etc/logrotate.d' include_dir: '/etc/logrotate.d'
hourly_conf_file: '/etc/logrotate.hourly.conf'
hourly_include_dir: '/etc/logrotate.hourly.d'
user: root user: root
group: root group: root
service: cron service: cron

+ 8
- 1
logrotate/jobs.sls View File



{% for key, value in jobs.items() %} {% for key, value in jobs.items() %}
{% set contents = value.get('contents', False) %} {% set contents = value.get('contents', False) %}

logrotate-{{ key }}: logrotate-{{ key }}:
file.managed: 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] }} - name: {{ logrotate.include_dir }}/{{ key.split("/")[-1] }}
{% endif %}
- user: {{ salt['pillar.get']('logrotate:config:user', logrotate.user) }} - user: {{ salt['pillar.get']('logrotate:config:user', logrotate.user) }}
- group: {{ salt['pillar.get']('logrotate:config:group', logrotate.group) }} - group: {{ salt['pillar.get']('logrotate:config:group', logrotate.group) }}
- mode: {{ salt['pillar.get']('logrotate:config:mode', '644') }} - mode: {{ salt['pillar.get']('logrotate:config:mode', '644') }}
data: {{ value | json }} data: {{ value | json }}
{% endif %} {% endif %}
{% endif %} {% endif %}
{%- endfor -%}


{% endfor %}

+ 4
- 0
logrotate/osfamilymap.yaml View File

tabooext: + .keep tabooext: + .keep
dateext: true dateext: true
FreeBSD: FreeBSD:
bin: /usr/local/sbin/logrotate
status_dir: /var/run
conf_file: /usr/local/etc/logrotate.conf conf_file: /usr/local/etc/logrotate.conf
hourly_conf_file: /usr/local/etc/logrotate.hourly.conf
include_dir: /usr/local/etc/logrotate.d include_dir: /usr/local/etc/logrotate.d
hourly_include_dir: /usr/local/etc/logrotate.hourly.d
group: wheel group: wheel

+ 9
- 0
logrotate/templates/logrotate.hourly.tmpl View File

#!/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
- 0
pillar.example View File

- postrotate - postrotate
- /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true - /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
- endscript - 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: nginx:
contents: | contents: |
/var/log/nginx/*.log{ /var/log/nginx/*.log{

+ 3
- 5
test/integration/default/controls/base.rb View File

control 'logrotate.service.running' do control 'logrotate.service.running' do
title 'The service should be installed, enabled and running' title 'The service should be installed, enabled and running'


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

service = service =
case platform[:family]
case system.platform[:family]
when 'arch'
'cronie'
when 'redhat', 'fedora' when 'redhat', 'fedora'
'crond' 'crond'
else else

+ 19
- 0
test/integration/default/controls/jobs.rb View File

its('content') { should include 'postrotate' } its('content') { should include 'postrotate' }
its('content') { should include ' invoke-rc.d nginx rotate >/dev/null 2>&1' } its('content') { should include ' invoke-rc.d nginx rotate >/dev/null 2>&1' }
end 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

+ 0
- 4
test/salt/states/cron.sls View File


cron_package:
pkg.installed:
- name: cron

Loading…
Cancel
Save