diff options
author | Daniil Baturin <daniil@vyos.io> | 2024-03-28 17:38:25 +0000 |
---|---|---|
committer | Daniil Baturin <daniil@vyos.io> | 2024-03-28 17:38:25 +0000 |
commit | 8344e7ba6a0a356e821b2b72d22a9ef2ef7b0fdd (patch) | |
tree | 936c4a4174d48c13460a994434e4a37f4d119c51 /python | |
parent | d15db95d96ead981600140614797b08f9ff3f2f6 (diff) | |
download | vyos-1x-8344e7ba6a0a356e821b2b72d22a9ef2ef7b0fdd.tar.gz vyos-1x-8344e7ba6a0a356e821b2b72d22a9ef2ef7b0fdd.zip |
vyos.template: T3664: add an environment variable for template location
to allow unmodified code to be executed from anywhere,
even outside of VyOS installations
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/template.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py index bde8e3554..392322d46 100644 --- a/python/vyos/template.py +++ b/python/vyos/template.py @@ -1,4 +1,4 @@ -# Copyright 2019-2023 VyOS maintainers and contributors <maintainers@vyos.io> +# Copyright 2019-2024 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -32,8 +32,21 @@ _TESTS = {} # reuse Environments with identical settings to improve performance @functools.lru_cache(maxsize=2) def _get_environment(location=None): + from os import getenv + if location is None: - loc_loader=FileSystemLoader(directories["templates"]) + # Sometimes functions that rely on templates need to be executed outside of VyOS installations: + # for example, installer functions are executed for image builds, + # and anything may be invoked for testing from a developer's machine. + # This environment variable allows running any unmodified code + # with a custom template location. + location_env_var = getenv("VYOS_TEMPLATE_DIR") + if location_env_var: + print(f"Using environment variable {location_env_var}") + template_dir = location_env_var + else: + template_dir = directories["templates"] + loc_loader=FileSystemLoader(template_dir) else: loc_loader=FileSystemLoader(location) env = Environment( |