From 068a94d3a242ec818640205626b011f8e53acf1a Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Sat, 23 Feb 2019 23:27:14 +0000 Subject: [PATCH] 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`: - https://github.com/saltstack-formulas/systemd-formula/pull/17 --- template/config.sls | 12 +++-- template/files/default/example.tmpl | 5 ++- template/files/default/example.tmpl.jinja | 6 +++ template/macros.jinja | 55 +++++++++++++++-------- 4 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 template/files/default/example.tmpl.jinja diff --git a/template/config.sls b/template/config.sls index 1dcf4c32..46ef20a3 100644 --- a/template/config.sls +++ b/template/config.sls @@ -1,8 +1,8 @@ # -*- 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 @@ -10,7 +10,13 @@ include: 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 diff --git a/template/files/default/example.tmpl b/template/files/default/example.tmpl index 59d70895..2c9c60f9 100644 --- a/template/files/default/example.tmpl +++ b/template/files/default/example.tmpl @@ -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. diff --git a/template/files/default/example.tmpl.jinja b/template/files/default/example.tmpl.jinja new file mode 100644 index 00000000..76aa70f4 --- /dev/null +++ b/template/files/default/example.tmpl.jinja @@ -0,0 +1,6 @@ +######################################################################## +# File managed by Salt at <{{ source }}>. +# Your changes will be overwritten. +######################################################################## + +This is another example file from SaltStack template-formula. diff --git a/template/macros.jinja b/template/macros.jinja index adef754a..2bf441a2 100644 --- a/template/macros.jinja +++ b/template/macros.jinja @@ -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. @@ -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 %}