openstack-ansible/playbooks/roles/system_crontab_coordination/templates/crontab.j2
git-harry e148635e78 Add role system-crontab-coordination
Currently every host, both containers and bare metal, has a crontab
configured with the same values for minute, hour, day of week etc. This
means that there is the potential for a service interruption if, for
example, a cron job were to cause a service to restart.

This commit adds a new role which attempts to adjust the times defined
in the entries in the default /etc/crontab to reduce the overlap
between hosts.

Change-Id: I18bf0ac0c0610283a19c40c448ac8b6b4c8fd8f5
Closes-bug: #1424705
2015-06-30 10:06:11 +01:00

52 lines
3.6 KiB
Django/Jinja

# {{ ansible_managed }}
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
{% macro normalise(value, start, end, minimum, maximum, period) -%}
{% set v = ((value | int) % (period | int)) + (start | int) %}
{%- if ((v | int) > (maximum | int)) -%}
{{ ((v | int) - (maximum | int) + (minimum | int) - 1) }}
{%- else -%}
{{ v }}
{%- endif -%}
{%- endmacro %}
{% macro set_period(start, end, maximum, minimum) -%}
{{ (((start | int) > (end | int)) | ternary(((maximum | int) - (minimum | int) - (start | int) + (end | int) + 2), ((end | int) - (start | int) + 1))) | int }}
{%- endmacro %}
{% macro set_offset(period, hosts) -%}
{%- if ((hosts | length) | int) > (period | int) -%}
0
{%- else -%}
{{ (((period | int) / ((hosts | length) | int)) | int) | random }}
{%- endif -%}
{%- endmacro %}
{% for host in play_hosts %}
{% if host == inventory_hostname %}
{% set minute_period = set_period(minute_start, minute_end, 59, 0) | int %}
{% set minute_offset = set_offset(minute_period, play_hosts) | int %}
{% set number_of_minute_jobs = (4 | int) %}
{% set m = normalise((((minute_period * loop.index) / loop.length) + ((minute_offset | int) / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) | int %}
{% set hour_period = set_period(hour_start, hour_end, 23, 0) | int %}
{% set hour_offset = set_offset(hour_period, play_hosts) | int %}
{% set number_of_hour_jobs = (3 | int) %}
{% set h = normalise((((hour_period * loop.index) / loop.length) + ((hour_offset | int) / (number_of_hour_jobs | int))), hour_start, hour_end, 0, 23, hour_period) | int %}
{% set day_of_week_period = set_period(day_of_week_start, day_of_week_end, 6, 0) | int %}
{% set day_of_week_offset = set_offset(day_of_week_period, play_hosts) | int %}
{% set dow = normalise((((day_of_week_period * loop.index) / loop.length) + (day_of_week_offset | int)), day_of_week_start, day_of_week_end, 0, 6, day_of_week_period) | int %}
{% set day_of_month_period = set_period(day_of_month_start, day_of_month_end, 28, 1) | int %}
{% set day_of_month_offset = set_offset(day_of_month_period, play_hosts) | int %}
{% set dom = normalise((((day_of_month_period * loop.index) / loop.length) + (day_of_month_offset | int)), day_of_month_start, day_of_month_end, 1, 28, day_of_month_period) | int %}
{{ m }} * * * * root cd / && run-parts --report /etc/cron.hourly
{{ normalise((m + (minute_period * 1 / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) }} {{ h }} * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
{{ normalise((m + (minute_period * 2 / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) }} {{ normalise((h + (hour_period * 1 / (number_of_hour_jobs | int))), hour_start, hour_end, 0, 23, hour_period) }} * * {{ dow }} root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
{{ normalise((m + (minute_period * 3 / (number_of_minute_jobs | int))), minute_start, minute_end, 0, 59, minute_period) }} {{ normalise((h + (hour_period * 2 / (number_of_hour_jobs | int))), hour_start, hour_end, 0, 23, hour_period) }} {{ dom }} * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
{% endif %}
{% endfor %}
#