Skip to content

Commit

Permalink
feat(tofs): implement backwards-compatible TOFSv2 for configurability
Browse files Browse the repository at this point in the history
* As discussed between comments:
  - https://freenode.logbot.info/saltstack-formulas/20190214#c1995273
  - https://freenode.logbot.info/saltstack-formulas/20190214#c1995487

* Squashed original PR with these commits present:
  - Improve TOFS implementation for increased configurability
  - Improve TOFS to allow custom files selection as well
  - Simplify `files_switch` macro to use only one loop
  - Make TOFS pattern fully-portable by using `tpldir`
  - Update `pillar.example` with current scheme for TOFS
  - Update `pillar.example` with the proposed scheme for TOFS
  - Fix bug where duplicate default lines are produced by the loop
  - feat(tofs): fix to work with old & new pillars and `tpldir` context

* Already tested and applied over existing TOFS in `systemd-formula`:
  - saltstack-formulas/systemd-formula#17
  • Loading branch information
myii committed Feb 23, 2019
1 parent 69bc840 commit 068a94d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
12 changes: 9 additions & 3 deletions template/config.sls
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# -*- coding: utf-8 -*-
# vim: ft=sls

{% from "template/map.jinja" import template with context %}
{% from "template/macros.jinja" import files_switch with context %}
{%- from "template/map.jinja" import template with context %}
{%- from "template/macros.jinja" import files_switch with context %}
include:
- template.install
template-config:
file.managed:
- name: {{ template.config }}
- source: {{ files_switch('template', ['example.tmpl']) }}
- source: {{ files_switch(
salt['config.get'](
tpldir ~ ':tofs:files:template-config',
['example.tmpl', 'example.tmpl.jinja']
)
) }}
- mode: 644
- user: root
- group: root
- template: jinja
5 changes: 4 additions & 1 deletion template/files/default/example.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Managed by saltstack
########################################################################
# File managed by Salt at <{{ source }}>.
# Your changes will be overwritten.
########################################################################

This is an example file from SaltStack template-formula.
6 changes: 6 additions & 0 deletions template/files/default/example.tmpl.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
########################################################################
# File managed by Salt at <{{ source }}>.
# Your changes will be overwritten.
########################################################################

This is another example file from SaltStack template-formula.
55 changes: 37 additions & 18 deletions template/macros.jinja
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{%- macro files_switch(prefix,
files,
{%- macro files_switch(files,
default_files_switch=['id', 'os_family'],
indent_width=6) %}
{#
{#-
Returns a valid value for the "source" parameter of a "file.managed"
state function. This makes easier the usage of the Template Override and
Files Switch (TOFS) pattern.
Expand Down Expand Up @@ -43,22 +42,42 @@
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
- template: jinja
#}
{%- set path_prefix = prefix|replace(':', '/') %}
{%- set files_switch_list = salt['pillar.get'](prefix ~ ':files_switch',
default_files_switch) %}
{%- for grain in files_switch_list if grains.get(grain) is defined %}
{%- for file in files %}
{%- set url = '- salt://' ~ '/'.join([path_prefix,
'files',
grains.get(grain),
file.lstrip('/')]) %}
{#- Get the `topdir` from `tpldir` #}
{%- set topdir = tpldir.split('/')[0] %}
{%- set path_prefix = salt['config.get'](topdir ~ ':tofs:path_prefix', topdir) %}
{%- set files_dir = salt['config.get'](topdir ~ ':tofs:dirs:files', 'files') %}
{%- set files_switch_list = salt['config.get'](
topdir ~ ':tofs:files_switch',
default_files_switch
) %}
{#- Only add to [''] when supporting older TOFS implementations #}
{%- for path_prefix_ext in [''] %}
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
{#- For older TOFS implementation, use `files_switch` from the pillar #}
{#- Use the default, new method otherwise #}
{%- set fsl = salt['pillar.get'](
topdir ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
files_switch_list
) %}
{#- Append an empty value to evaluate as `default` in the loop below #}
{%- if '' not in fsl %}
{%- do fsl.append('') %}
{%- endif %}
{%- for fs in fsl %}
{%- for file in files %}
{%- if fs %}
{%- set fs_dir = salt['config.get'](fs, fs) %}
{%- else %}
{%- set fs_dir = salt['config.get'](topdir ~ ':tofs:dirs:default', 'default') %}
{%- endif %}
{%- set url = '- salt://' ~ '/'.join([
path_prefix_inc_ext,
files_dir,
fs_dir,
file.lstrip('/')
]) %}
{{ url | indent(indent_width, true) }}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- for file in files %}
{%- set url = '- salt://' ~ '/'.join([path_prefix,
'files/default',
file.lstrip('/')]) %}
{{ url | indent(indent_width, true) }}
{%- endfor %}
{%- endmacro %}

0 comments on commit 068a94d

Please sign in to comment.