From 7b5676b9e267fb9bc7b610040a113fa870ee3d23 Mon Sep 17 00:00:00 2001 From: Michael Schmitt Date: Tue, 23 Nov 2021 13:28:41 -0700 Subject: [PATCH] feat(hourly): add configuration to enable hourly jobs --- logrotate/config.sls | 52 ++++++++++++++++++++--- logrotate/defaults.yaml | 4 ++ logrotate/jobs.sls | 9 +++- logrotate/osfamilymap.yaml | 4 ++ logrotate/templates/logrotate.hourly.tmpl | 9 ++++ 5 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 logrotate/templates/logrotate.hourly.tmpl diff --git a/logrotate/config.sls b/logrotate/config.sls index e509c6b..37da712 100644 --- a/logrotate/config.sls +++ b/logrotate/config.sls @@ -3,22 +3,62 @@ include: - logrotate +{% set ns = namespace(hourly=False) %} +{% set jobs = salt['config.get']('logrotate:jobs', {}) %} +{% 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 %} diff --git a/logrotate/defaults.yaml b/logrotate/defaults.yaml index d388275..0c439bf 100644 --- a/logrotate/defaults.yaml +++ b/logrotate/defaults.yaml @@ -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 diff --git a/logrotate/jobs.sls b/logrotate/jobs.sls index 2fd905a..9877a6b 100644 --- a/logrotate/jobs.sls +++ b/logrotate/jobs.sls @@ -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') }} @@ -29,5 +36,5 @@ logrotate-{{ key }}: data: {{ value | json }} {% endif %} {% endif %} -{%- endfor -%} +{% endfor %} diff --git a/logrotate/osfamilymap.yaml b/logrotate/osfamilymap.yaml index b0b5390..8c0c396 100644 --- a/logrotate/osfamilymap.yaml +++ b/logrotate/osfamilymap.yaml @@ -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 diff --git a/logrotate/templates/logrotate.hourly.tmpl b/logrotate/templates/logrotate.hourly.tmpl new file mode 100644 index 0000000..f045611 --- /dev/null +++ b/logrotate/templates/logrotate.hourly.tmpl @@ -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