Skip to content

Commit 068a94d

Browse files
committed
feat(tofs): implement backwards-compatible TOFSv2 for configurability
* 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
1 parent 69bc840 commit 068a94d

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

template/config.sls

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
# -*- coding: utf-8 -*-
22
# vim: ft=sls
33

4-
{% from "template/map.jinja" import template with context %}
5-
{% from "template/macros.jinja" import files_switch with context %}
4+
{%- from "template/map.jinja" import template with context %}
5+
{%- from "template/macros.jinja" import files_switch with context %}
66
77
include:
88
- template.install
99
1010
template-config:
1111
file.managed:
1212
- name: {{ template.config }}
13-
- source: {{ files_switch('template', ['example.tmpl']) }}
13+
- source: {{ files_switch(
14+
salt['config.get'](
15+
tpldir ~ ':tofs:files:template-config',
16+
['example.tmpl', 'example.tmpl.jinja']
17+
)
18+
) }}
1419
- mode: 644
1520
- user: root
1621
- group: root
22+
- template: jinja
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
# Managed by saltstack
1+
########################################################################
2+
# File managed by Salt at <{{ source }}>.
3+
# Your changes will be overwritten.
4+
########################################################################
25

36
This is an example file from SaltStack template-formula.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
########################################################################
2+
# File managed by Salt at <{{ source }}>.
3+
# Your changes will be overwritten.
4+
########################################################################
5+
6+
This is another example file from SaltStack template-formula.

template/macros.jinja

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
{%- macro files_switch(prefix,
2-
files,
1+
{%- macro files_switch(files,
32
default_files_switch=['id', 'os_family'],
43
indent_width=6) %}
5-
{#
4+
{#-
65
Returns a valid value for the "source" parameter of a "file.managed"
76
state function. This makes easier the usage of the Template Override and
87
Files Switch (TOFS) pattern.
@@ -43,22 +42,42 @@
4342
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
4443
- template: jinja
4544
#}
46-
{%- set path_prefix = prefix|replace(':', '/') %}
47-
{%- set files_switch_list = salt['pillar.get'](prefix ~ ':files_switch',
48-
default_files_switch) %}
49-
{%- for grain in files_switch_list if grains.get(grain) is defined %}
50-
{%- for file in files %}
51-
{%- set url = '- salt://' ~ '/'.join([path_prefix,
52-
'files',
53-
grains.get(grain),
54-
file.lstrip('/')]) %}
45+
{#- Get the `topdir` from `tpldir` #}
46+
{%- set topdir = tpldir.split('/')[0] %}
47+
{%- set path_prefix = salt['config.get'](topdir ~ ':tofs:path_prefix', topdir) %}
48+
{%- set files_dir = salt['config.get'](topdir ~ ':tofs:dirs:files', 'files') %}
49+
{%- set files_switch_list = salt['config.get'](
50+
topdir ~ ':tofs:files_switch',
51+
default_files_switch
52+
) %}
53+
{#- Only add to [''] when supporting older TOFS implementations #}
54+
{%- for path_prefix_ext in [''] %}
55+
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
56+
{#- For older TOFS implementation, use `files_switch` from the pillar #}
57+
{#- Use the default, new method otherwise #}
58+
{%- set fsl = salt['pillar.get'](
59+
topdir ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
60+
files_switch_list
61+
) %}
62+
{#- Append an empty value to evaluate as `default` in the loop below #}
63+
{%- if '' not in fsl %}
64+
{%- do fsl.append('') %}
65+
{%- endif %}
66+
{%- for fs in fsl %}
67+
{%- for file in files %}
68+
{%- if fs %}
69+
{%- set fs_dir = salt['config.get'](fs, fs) %}
70+
{%- else %}
71+
{%- set fs_dir = salt['config.get'](topdir ~ ':tofs:dirs:default', 'default') %}
72+
{%- endif %}
73+
{%- set url = '- salt://' ~ '/'.join([
74+
path_prefix_inc_ext,
75+
files_dir,
76+
fs_dir,
77+
file.lstrip('/')
78+
]) %}
5579
{{ url | indent(indent_width, true) }}
80+
{%- endfor %}
5681
{%- endfor %}
5782
{%- endfor %}
58-
{%- for file in files %}
59-
{%- set url = '- salt://' ~ '/'.join([path_prefix,
60-
'files/default',
61-
file.lstrip('/')]) %}
62-
{{ url | indent(indent_width, true) }}
63-
{%- endfor %}
6483
{%- endmacro %}

0 commit comments

Comments
 (0)