From c6c2a1dfd80b5651cd5a18089a5aed353db98dae Mon Sep 17 00:00:00 2001 From: David Seira Date: Fri, 28 Jul 2017 01:43:45 +0200 Subject: [PATCH] Initial commit --- README.rst | 42 ++++++++++++++++++++++++++++++++++++++++++ cron/config.sls | 38 ++++++++++++++++++++++++++++++++++++++ cron/init.sls | 7 +++++++ cron/install.sls | 8 ++++++++ cron/map.jinja | 20 ++++++++++++++++++++ cron/service.sls | 15 +++++++++++++++ pillar.example | 27 +++++++++++++++++++++++++++ 7 files changed, 157 insertions(+) create mode 100644 README.rst create mode 100644 cron/config.sls create mode 100644 cron/init.sls create mode 100644 cron/install.sls create mode 100644 cron/map.jinja create mode 100644 cron/service.sls create mode 100644 pillar.example diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..647d680 --- /dev/null +++ b/README.rst @@ -0,0 +1,42 @@ +============== +cron-formula +============== + +Install and configure cron and set up cron tasks. + +.. note:: + + See the full `Salt Formulas installation and usage instructions + `_. + +Available states +================ + +.. contents:: + :local: + +|OS|Compatibility| +|:----:|:--------------:| +|Ubuntu 12|Yes| +|Ubuntu 14|Yes| +|Ubuntu 16|Yes| +|CentOS 6|Yes| +|CentOS 7|Yes| + +``cron`` +-------- +Join install, configure and service states. + +``install`` +----------- +Install required package to run cron. + +``config`` +---------- +Configure cron tasks based on the pillar configuration. + +Remember that is you need to delete a cron task you have to set ``type`` as ``absent`` if not, the crontask will not be deleted. You can also disable a crontask using the ``commented`` keyword. + +``service`` +----------- +Manage the ``cron`` service. Stopping or starting it. If needed, you can disable the cron service by setting the ``enabled: False`` option in the pillar. diff --git a/cron/config.sls b/cron/config.sls new file mode 100644 index 0000000..312a45e --- /dev/null +++ b/cron/config.sls @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{% from "cron/map.jinja" import cron_settings with context %} + +{% if 'tasks' in cron_settings %} +{% for task,task_options in cron_settings.tasks.iteritems() %} + +cron.{{ task }}: + cron.{{ task_options.type|default('present') }}: + - name: '{{ task_options.name }}' + {% if 'user' in task_options %} + - user: {{ task_options.user|default('root') }} + {% endif %} + {% if 'minute' in task_options %} + - minute: {{ task_options.minute }} + {% endif %} + {% if 'hour' in task_options %} + - hour: {{ task_options.hour }} + {% endif %} + {% if 'daymonth' in task_options %} + - daymonth: {{ task_options.daymonth }} + {% endif %} + {% if 'dayweek' in task_options %} + - dayweek: {{ task_options.dayweek }} + {% endif %} + {% if 'commented' in task_options and task_options.commented %} + - commented: True + {% endif %} + {% if 'special' in task_options %} + - special: '{{ task_options.special }}' + {% endif %} + - identifier: '{{ task }}' + {% if 'comment' in task_options %} + - comment: '{{ task_options.comment }}' + {% endif %} +{% endfor %} +{% endif %} diff --git a/cron/init.sls b/cron/init.sls new file mode 100644 index 0000000..eff3fc8 --- /dev/null +++ b/cron/init.sls @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +include: + - cron.install + - cron.config + - cron.service diff --git a/cron/install.sls b/cron/install.sls new file mode 100644 index 0000000..6083ed1 --- /dev/null +++ b/cron/install.sls @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{% from "cron/map.jinja" import cron_settings with context %} + +cron.install: + pkg.installed: + - name: {{ cron_settings.pkg }} diff --git a/cron/map.jinja b/cron/map.jinja new file mode 100644 index 0000000..9650409 --- /dev/null +++ b/cron/map.jinja @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# vim: ft=jinja + +{% set os_family_map = salt['grains.filter_by']({ + 'RedHat': { + 'pkg': 'cronie', + 'service': 'crond', + }, + 'Debian': { + 'pkg': 'cron', + 'service': 'cron', + }, + }, grain='os_family', merge=salt['pillar.get']('cron:lookup')) %} + +{% set cron_settings = salt['pillar.get']( + 'cron', + default=os_family_map, + merge=True + ) +%} diff --git a/cron/service.sls b/cron/service.sls new file mode 100644 index 0000000..ea11acc --- /dev/null +++ b/cron/service.sls @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{% from "cron/map.jinja" import cron_settings with context %} + +cron.service: +{% if 'enabled' not in cron_settings or ( 'enabled' in cron_settings and cron_settings.enabled ) %} + service.running: + - name: {{ cron_settings.service }} + - enable: True +{% else %} + service.dead: + - name: {{ cron_settings.service }} + - enable: False +{% endif %} diff --git a/pillar.example b/pillar.example new file mode 100644 index 0000000..8435e8c --- /dev/null +++ b/pillar.example @@ -0,0 +1,27 @@ +cron: + enabled: True # Default + lookup: # Not needed, just if you want to use another cron program + pkg: 'cronie' + tasks: + task1: + type: 'present' # Default + name: 'echo test > /tmp/test' + user: 'root' + minute: 0 + hour: 0 + daymonth: 7 + month: 1 + dayweek: 6 + comment: 'comment1' + task2: + type: 'absent' # To remove that crontask + name: 'echo task2 > /tmp/test2' + user: 'root' + minute: 'random' + hour: 1 + task3: + type: 'absent' + name: 'echo task3 > /tmp/test3' + user: 'root' + special: '@hourly' + comment: 'comment3'