Skip to content

Commit 011bf32

Browse files
authored
Merge pull request #251 from myii/chore/standardise-structure
chore: use `semantic-release` cross-formula standard structure
2 parents df649c9 + 34af78a commit 011bf32

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

docs/TOFS_pattern.rst

+72
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,75 @@ Resulting in:
444444
Note: This does *not* override the default value.
445445
Rather, the value from the pillar/config is prepended to the default.
446446

447+
Using sub-directories for ``components``
448+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
449+
450+
If your formula is composed of several components, you may prefer to provides files under sub-directories, like in the `systemd-formula <https://github.com/saltstack-formulas/systemd-formula>`_.
451+
452+
.. code-block::
453+
454+
/srv/saltstack/systemd-formula/
455+
systemd/
456+
init.sls
457+
libtofs.jinja
458+
map.jinja
459+
networkd/
460+
init.sls
461+
files/
462+
default/
463+
network/
464+
99-default.link
465+
resolved/
466+
init.sls
467+
files/
468+
default/
469+
resolved.conf
470+
timesyncd/
471+
init.sls
472+
files/
473+
Arch/
474+
resolved.conf
475+
Debian/
476+
resolved.conf
477+
default/
478+
resolved.conf
479+
Ubuntu/
480+
resolved.conf
481+
482+
For example, the following ``formula.component.config`` SLS:
483+
484+
.. code-block:: sls
485+
486+
{%- from "formula/libtofs.jinja" import files_switch with context %}
487+
488+
formula configuration file:
489+
file.managed:
490+
- name: /etc/formula.conf
491+
- user: root
492+
- group: root
493+
- mode: 644
494+
- template: jinja
495+
- source: {{ files_switch(['formula.conf'],
496+
lookup='formula',
497+
use_subpath=True
498+
)
499+
}}
500+
501+
will be rendered on a ``Debian`` minion named ``salt-formula.ci.local`` as:
502+
503+
.. code-block:: sls
504+
505+
formula configuration file:
506+
file.managed:
507+
- name: /etc/formula.conf
508+
- user: root
509+
- group: root
510+
- mode: 644
511+
- template: jinja
512+
- source:
513+
- salt://formula/component/files/salt-formula.ci.local/formula.conf
514+
- salt://formula/component/files/Debian/formula.conf
515+
- salt://formula/component/files/default/formula.conf
516+
- salt://formula/files/salt-formula.ci.local/formula.conf
517+
- salt://formula/files/Debian/formula.conf
518+
- salt://formula/files/default/formula.conf

nginx/libtofs.jinja

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
lookup=None,
33
default_files_switch=['id', 'os_family'],
44
indent_width=6,
5-
v1_path_prefix='') %}
5+
use_subpath=False) %}
66
{#-
77
Returns a valid value for the "source" parameter of a "file.managed"
88
state function. This makes easier the usage of the Template Override and
@@ -17,8 +17,8 @@
1717
use as selector switch of the directories under
1818
"<path_prefix>/files"
1919
* indent_witdh: indentation of the result value to conform to YAML
20-
* v1_path_prefix: (deprecated) only used for injecting a path prefix into
21-
the source, to support older TOFS configs
20+
* use_subpath: defaults to `False` but if set, lookup the source file
21+
recursively from the current state directory up to `tplroot`
2222
2323
Example (based on a `tplroot` of `xxx`):
2424
@@ -64,10 +64,15 @@
6464
{%- set src_files = src_files + source_files %}
6565
{#- Only add to [''] when supporting older TOFS implementations #}
6666
{%- set path_prefix_exts = [''] %}
67-
{%- if v1_path_prefix != '' %}
68-
{%- do path_prefix_exts.append(v1_path_prefix) %}
67+
{%- if use_subpath and tplroot != tpldir %}
68+
{#- Walk directory tree to find {{ files_dir }} #}
69+
{%- set subpath_parts = tpldir.lstrip(tplroot).lstrip('/').split('/') %}
70+
{%- for path in subpath_parts %}
71+
{%- set subpath = subpath_parts[0:loop.index] | join('/') %}
72+
{%- do path_prefix_exts.append('/' ~ subpath) %}
73+
{%- endfor %}
6974
{%- endif %}
70-
{%- for path_prefix_ext in path_prefix_exts %}
75+
{%- for path_prefix_ext in path_prefix_exts|reverse %}
7176
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
7277
{#- For older TOFS implementation, use `files_switch` from the config #}
7378
{#- Use the default, new method otherwise #}

0 commit comments

Comments
 (0)