Skip to content

Commit 4bb5a11

Browse files
author
Łukasz Stelmach
committed
remote/config: support drop-in files
Pass "dropins" variable to a template being rendered containing a list of *.yaml files in a directory named after the template being rendered with ".d" appended. For example when rendering /etc/labgrid/configuration.yaml a list of files matching /etc/labgrid/configuration.yaml.d/*.yaml will be available. Then configuration.yaml may be as simple as {% for f in drop_ins %} {% include f %} {% endfor %} And adding a target is possible by simply creating a file in /etc/labgrid/configuration.yaml.d directory. Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
1 parent 0c01c19 commit 4bb5a11

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

doc/configuration.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,12 +3995,31 @@ something like ``{{ env['FOOBAR'] }}`` to insert the content of environment
39953995
variable ``FOOBAR``.
39963996
In addition to ``env`` the template also has access to the following variables:
39973997

3998-
isolated
3999-
``True`` or ``False``, depending on the :code:`--isolated` command line option.
3998+
drop_ins
3999+
An alphabetically sorted list of files in a directory named after the
4000+
currently processed template with ".d" appended. For example if the template
4001+
is named ``/etc/labgrid/configuration.yaml`` the list will contain files
4002+
matching ``/etc/labgrid/configuration.yaml.d/*.yaml`` pattern. The paths are
4003+
relative to the directory containing the template (i.e. ``/etc/labgrid``
4004+
in our example). These files can be included using the ``include`` directive.
4005+
A template loading all its drop-ins at would look like this
4006+
4007+
.. code-block:: yaml
4008+
# for f in drop_ins
4009+
{% include f %}
4010+
# endfor
4011+
4012+
.. warning::
4013+
Use the ``{% include f %}`` form instead of ``# include f``, otherwise
4014+
Jinja2 would strip newline characters before concatenation, and thus break
4015+
the YAML formatting.
40004016

40014017
hostname
40024018
The hostname of the exporter host. Can be used to e.g. construct URLs to the
40034019
current host (``http://{{ hostname }}/``).
40044020

4021+
isolated
4022+
``True`` or ``False``, depending on the :code:`--isolated` command line option.
4023+
40054024
name
40064025
The name of the exporter.

labgrid/remote/config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pprint import pprint
22

3+
import glob
34
import os
45

56
import attr
@@ -15,15 +16,21 @@ class ResourceConfig:
1516
template_env = attr.ib(default=attr.Factory(dict), validator=attr.validators.instance_of(dict))
1617

1718
def __attrs_post_init__(self):
19+
_dirname = os.path.dirname(self.filename)
1820
env = jinja2.Environment(
19-
loader=jinja2.FileSystemLoader(os.path.dirname(self.filename)),
21+
loader=jinja2.FileSystemLoader(_dirname),
2022
line_statement_prefix="#",
2123
line_comment_prefix="##",
2224
)
2325
try:
2426
template = env.get_template(os.path.basename(self.filename))
2527
except jinja2.TemplateNotFound:
2628
raise NoConfigFoundError(f"{self.filename} could not be found")
29+
drop_ins = [
30+
os.path.relpath(p, _dirname) for p in sorted(glob.glob(os.path.join(self.filename + '.d','*.yaml')))
31+
]
32+
33+
self.template_env["drop_ins"] = drop_ins
2734
rendered = template.render(self.template_env)
2835
pprint(("rendered", rendered))
2936
self.data = load(rendered)

0 commit comments

Comments
 (0)