From d4a96a421b1dbd1045956cc2bcc24a2ccb260049 Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Wed, 18 Sep 2019 14:31:33 +1000
Subject: [PATCH] logrotate: better rotation options

The extant "logrotate_daily" varaible doesn't really do anything and
isn't used.  Modify this to be able to set a range of rotation periods
or a size.

Update mirror rsync mirrors to rotate weekly, as often releases run
overnight and it's a pain to reconstruct.

Change-Id: I121dc5f4fe7f226b66d18b9ec39e7e3839be4d40
---
 playbooks/roles/logrotate/README.rst             | 16 ++++++++++++++--
 playbooks/roles/logrotate/defaults/main.yaml     |  3 ++-
 playbooks/roles/logrotate/tasks/main.yaml        | 11 +++++++++++
 .../roles/logrotate/templates/logrotate.conf.j2  |  6 ++++--
 playbooks/roles/mirror-update/tasks/rsync.yaml   |  1 +
 5 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/playbooks/roles/logrotate/README.rst b/playbooks/roles/logrotate/README.rst
index 4250a90d70..c6bfeab9a1 100644
--- a/playbooks/roles/logrotate/README.rst
+++ b/playbooks/roles/logrotate/README.rst
@@ -36,8 +36,20 @@ not an exhaustive list of directives (contributions are welcome).
 .. zuul:rolevar:: logrotate_rotate
    :default: 7
 
-.. zuul:rolevar:: logrotate_daily
-   :default: yes
+.. zuul:rolevar:: logrotate_frequency
+   :default: daily
+
+   One of ``hourly``, ``daily``, ``weekly``, ``monthly``, ``yearly``
+   or ``size``.
+
+   If choosing ``size``, :zuul:rolevar::`logrotate.logrotate_size` must
+   be specified
+
+.. zuul:rolevar:: logrotate_size
+   :default: None
+
+   Size; e.g. 100K, 10M, 1G.  Only when
+   :zuul:rolevar::`logrotate.logrotate_frequency` is ``size``.
 
 .. zuul:rolevar:: logrotate_notifempty
    :default: yes
diff --git a/playbooks/roles/logrotate/defaults/main.yaml b/playbooks/roles/logrotate/defaults/main.yaml
index 8f9b815c24..f37e7d040f 100644
--- a/playbooks/roles/logrotate/defaults/main.yaml
+++ b/playbooks/roles/logrotate/defaults/main.yaml
@@ -3,5 +3,6 @@ logrotate_copytruncate: yes
 logrotate_delaycompress: yes
 logrotate_missingok: yes
 logrotate_rotate: 7
-logrotate_daily: yes
+logrotate_frequency: daily
+logrotate_size: None
 logrotate_notifempty: yes
\ No newline at end of file
diff --git a/playbooks/roles/logrotate/tasks/main.yaml b/playbooks/roles/logrotate/tasks/main.yaml
index f2767d9f65..81bc7fa0c3 100644
--- a/playbooks/roles/logrotate/tasks/main.yaml
+++ b/playbooks/roles/logrotate/tasks/main.yaml
@@ -3,6 +3,17 @@
     msg: Must set logrotate_file_name for logfile to rotate
   when: logrotate_file_name is not defined
 
+- assert:
+    that:
+      - logrotate_frequency in ('hourly', 'daily', 'weekly', 'monthly', 'yearly', 'size')
+    fail_msg: Invalid logrotate_frequency
+
+- assert:
+    that:
+      - logrotate_size
+    fail_msg: Must specify size for rotation
+  when: logrotate_frequency == 'size'
+
 # Hash the full path to avoid any conflicts but remain idempotent.
 # "/var/log/ansible/ansible.log" becomes "ansible.log.37237.conf" for example
 - name: Create a unique config name
diff --git a/playbooks/roles/logrotate/templates/logrotate.conf.j2 b/playbooks/roles/logrotate/templates/logrotate.conf.j2
index 0ed1039997..a1841c5706 100644
--- a/playbooks/roles/logrotate/templates/logrotate.conf.j2
+++ b/playbooks/roles/logrotate/templates/logrotate.conf.j2
@@ -12,8 +12,10 @@
  missingok
 {% endif %}
  rotate {{ logrotate_rotate }}
-{% if logrotate_daily %}
- daily
+{% if logrotate_frequency != "size" %}
+ {{ logrotate_frequency }}
+{% else %}
+ size {{ logrotate_size }}
 {% endif %}
 {% if logrotate_notifempty %}
  notifempty
diff --git a/playbooks/roles/mirror-update/tasks/rsync.yaml b/playbooks/roles/mirror-update/tasks/rsync.yaml
index 34557d4f07..fbf49e326f 100644
--- a/playbooks/roles/mirror-update/tasks/rsync.yaml
+++ b/playbooks/roles/mirror-update/tasks/rsync.yaml
@@ -53,4 +53,5 @@
     name: logrotate
   vars:
     logrotate_file_name: '/var/log/rsync-mirrors/{{ item }}.log'
+    logrotate_frequency: 'weekly'
   loop: '{{ rsync_update_scripts }}'
\ No newline at end of file