diff --git a/docs/TOFS_pattern.rst b/docs/TOFS_pattern.rst index 20233c8..4fea5dd 100644 --- a/docs/TOFS_pattern.rst +++ b/docs/TOFS_pattern.rst @@ -444,3 +444,75 @@ Resulting in: Note: This does *not* override the default value. Rather, the value from the pillar/config is prepended to the default. +Using sub-directories for ``components`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If your formula is composed of several components, you may prefer to provides files under sub-directories, like in the `systemd-formula `_. + +.. code-block:: + + /srv/saltstack/systemd-formula/ + systemd/ + init.sls + libtofs.jinja + map.jinja + networkd/ + init.sls + files/ + default/ + network/ + 99-default.link + resolved/ + init.sls + files/ + default/ + resolved.conf + timesyncd/ + init.sls + files/ + Arch/ + resolved.conf + Debian/ + resolved.conf + default/ + resolved.conf + Ubuntu/ + resolved.conf + +For example, the following ``formula.component.config`` SLS: + +.. code-block:: sls + + {%- from "formula/libtofs.jinja" import files_switch with context %} + + formula configuration file: + file.managed: + - name: /etc/formula.conf + - user: root + - group: root + - mode: 644 + - template: jinja + - source: {{ files_switch(['formula.conf'], + lookup='formula', + use_subpath=True + ) + }} + +will be rendered on a ``Debian`` minion named ``salt-formula.ci.local`` as: + +.. code-block:: sls + + formula configuration file: + file.managed: + - name: /etc/formula.conf + - user: root + - group: root + - mode: 644 + - template: jinja + - source: + - salt://formula/component/files/salt-formula.ci.local/formula.conf + - salt://formula/component/files/Debian/formula.conf + - salt://formula/component/files/default/formula.conf + - salt://formula/files/salt-formula.ci.local/formula.conf + - salt://formula/files/Debian/formula.conf + - salt://formula/files/default/formula.conf diff --git a/nginx/libtofs.jinja b/nginx/libtofs.jinja index 9609be1..900e62b 100644 --- a/nginx/libtofs.jinja +++ b/nginx/libtofs.jinja @@ -2,7 +2,7 @@ lookup=None, default_files_switch=['id', 'os_family'], indent_width=6, - v1_path_prefix='') %} + use_subpath=False) %} {#- Returns a valid value for the "source" parameter of a "file.managed" state function. This makes easier the usage of the Template Override and @@ -17,8 +17,8 @@ use as selector switch of the directories under "/files" * indent_witdh: indentation of the result value to conform to YAML - * v1_path_prefix: (deprecated) only used for injecting a path prefix into - the source, to support older TOFS configs + * use_subpath: defaults to `False` but if set, lookup the source file + recursively from the current state directory up to `tplroot` Example (based on a `tplroot` of `xxx`): @@ -64,10 +64,15 @@ {%- set src_files = src_files + source_files %} {#- Only add to [''] when supporting older TOFS implementations #} {%- set path_prefix_exts = [''] %} - {%- if v1_path_prefix != '' %} - {%- do path_prefix_exts.append(v1_path_prefix) %} + {%- if use_subpath and tplroot != tpldir %} + {#- Walk directory tree to find {{ files_dir }} #} + {%- set subpath_parts = tpldir.lstrip(tplroot).lstrip('/').split('/') %} + {%- for path in subpath_parts %} + {%- set subpath = subpath_parts[0:loop.index] | join('/') %} + {%- do path_prefix_exts.append('/' ~ subpath) %} + {%- endfor %} {%- endif %} - {%- for path_prefix_ext in path_prefix_exts %} + {%- for path_prefix_ext in path_prefix_exts|reverse %} {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %} {#- For older TOFS implementation, use `files_switch` from the config #} {#- Use the default, new method otherwise #}