0
0
mirror of https://github.com/saltstack-formulas/cron-formula.git synced 2024-11-28 01:17:49 +01:00

refactor(config): minimise and standardise between .sls & .tst

This commit is contained in:
Imran Iqbal 2019-10-17 21:32:38 +01:00
parent cce5e67c33
commit 18585bdc90
No known key found for this signature in database
GPG Key ID: 6D8629439D2B7819
3 changed files with 38 additions and 101 deletions

View File

@ -5,23 +5,21 @@
{%- set tplroot = tpldir.split('/')[0] %} {%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import cron with context %} {%- from tplroot ~ "/map.jinja" import cron with context %}
{%- if 'tasks' in cron %} {%- for task, task_options in cron.get('tasks', {}).items() %}
{%- for task, task_options in cron.tasks.items() %} {%- set cron_type = task_options.type|d('present') %}
cron.{{ task }}: cron.{{ task }}:
cron.{{ task_options.type|d('present') }}: cron.{{ cron_type }}:
- name: {{ task_options.name }} - name: {{ task_options.name }}
- identifier: '{{ task }}'
{%- if 'user' in task_options %}
- user: {{ task_options.user|d('root') }} - user: {{ task_options.user|d('root') }}
- identifier: '{{ task }}'
{%- if cron_type == 'present' %}
- commented: {{ task_options.commented|d(False) }}
{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %}
{%- if section in task_options %}
- {{ section }}: '{{ task_options[section] }}'
{%- endif %}
{%- endfor %}
{%- endif %} {%- endif %}
{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'special'] %}
{%- if section in task_options %} {%- endfor %}
- {{ section }}: '{{ task_options.get(section) }}'
{%- endif %}
{%- endfor %}
{%- if task_options.commented|d(False) %}
- commented: True
{%- endif %}
{%- endfor %}
{%- endif %}

View File

@ -3,59 +3,45 @@
{%- set cron = salt['pillar.get']('cron', {}) %} {%- set cron = salt['pillar.get']('cron', {}) %}
{%- if 'tasks' in cron %} {%- for task, task_options in cron.get('tasks', {}).items() %}
{%- for task, task_options in cron.tasks.items() %} {%- set cron_type = task_options.type|d('present') %}
{%- if task_options.type == 'absent' %} validate_cron.{{ task }}_{{ cron_type }}:
validate_cron.{{ task }}_absent:
module_and_function: cron.get_entry module_and_function: cron.get_entry
args: args:
- {{ task_options.user|d('root') }} - {{ task_options.user|d('root') }}
- {{ task }} - {{ task }}
{%- if cron_type == 'absent' %}
assertion: assertFalse assertion: assertFalse
{%- else %}
{%- elif task_options.type == 'present' %}
validate_cron.{{ task }}_exists:
module_and_function: cron.get_entry
args:
- {{ task_options.user|d('root') }}
- {{ task }}
assertion: assertEqual assertion: assertEqual
assertion_section: identifier assertion_section: identifier
expected-return: {{ task }} expected-return: {{ task }}
{%- endif %}
{#- Note: `special` is `spec` in the module #} {%- if cron_type == 'present' %}
{%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'spec'] %}
{%- if section in task_options %}
{%- set assertion = 'assertEqual' %}
{%- set expected = task_options.get(section) %}
{%- if expected == 'random' %}
{%- set assertion = 'assertLessEqual' %}
{%- set expected = 0 %}
{%- endif %}
validate_cron.{{ task }}_{{ section }}:
module_and_function: cron.get_entry
args:
- {{ task_options.user|d('root') }}
- {{ task }}
assertion: {{ assertion }}
assertion_section: {{ section }}
expected-return: '{{ expected }}'
{%- endif %}
{%- endfor %}
{%- set assertion = 'assertFalse' %}
{%- if task_options.commented|d(False) %}
{%- set assertion = 'assertTrue' %}
{%- endif %}
validate_cron.{{ task }}_commented: validate_cron.{{ task }}_commented:
module_and_function: cron.get_entry module_and_function: cron.get_entry
args: args:
- {{ task_options.user|d('root') }} - {{ task_options.user|d('root') }}
- {{ task }} - {{ task }}
assertion: {{ assertion }} assertion: {{ 'assertTrue' if task_options.commented|d(False) else 'assertFalse' }}
assertion_section: commented assertion_section: commented
{%- endif %}
{%- endfor %} {#- Note: `special` is `spec` in the module #}
{%- endif %} {%- for section in ['minute', 'hour', 'daymonth', 'month', 'dayweek', 'comment', 'spec'] %}
{%- if section in task_options %}
{%- set expected = task_options[section] %}
validate_cron.{{ task }}_{{ section }}:
module_and_function: cron.get_entry
args:
- {{ task_options.user|d('root') }}
- {{ task }}
assertion: {{ 'assertLessEqual' if expected == 'random' else 'assertEqual' }}
assertion_section: {{ section }}
expected-return: '{{ 0 if expected == 'random' else expected }}'
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endfor %}

View File

@ -1,47 +0,0 @@
# -*- coding: utf-8 -*-
# vim: ft=jinja
{#- Start imports as #}
{%- import_yaml "cron" ~ "/defaults.yaml" as default_settings %}
{%- import_yaml "cron" ~ "/osarchmap.yaml" as osarchmap %}
{%- import_yaml "cron" ~ "/osfamilymap.yaml" as osfamilymap %}
{%- import_yaml "cron" ~ "/osmap.yaml" as osmap %}
{%- import_yaml "cron" ~ "/osfingermap.yaml" as osfingermap %}
{#- Retrieve the config dict only once #}
{%- set _config = salt['config.get']("cron", default={}) %}
{%- set defaults = salt['grains.filter_by'](
default_settings,
default="cron",
merge=salt['grains.filter_by'](
osarchmap,
grain='osarch',
merge=salt['grains.filter_by'](
osfamilymap,
grain='os_family',
merge=salt['grains.filter_by'](
osmap,
grain='os',
merge=salt['grains.filter_by'](
osfingermap,
grain='osfinger',
merge=salt['grains.filter_by'](
_config,
default='lookup'
)
)
)
)
)
)
%}
{%- set config = salt['grains.filter_by'](
{'defaults': defaults},
default='defaults',
merge=_config
)
%}
{%- set cron = config %}