From 7333bed8b5d1dfe855d71cbdc0a2066a529cdd5a Mon Sep 17 00:00:00 2001 From: Daniel Dehennin Date: Mon, 6 Sep 2021 14:50:59 +0200 Subject: [PATCH 1/2] feat(map): enable import of `mapdata` from outside the formula `tplroot` can't be set correctly when the import: - is done from another top directory than the formula directory - the import is done `with context` In this case, the `tpldir` is set to the directory of the importer `.sls` file instead of the `.jinja` imported one. We force the `without context` which permits to directly use `tpldir` as the `tplroot` which is the directory of the imported file. BREAKING CHANGE: `map.jinja` import must use `without context` BREAKING CHANGE: `libmapstack.jinja` import must use `without context` BREAKING CHANGE: `libmatchers.jinja` import must use `without context` --- TEMPLATE/_mapdata/init.sls | 2 +- TEMPLATE/config/clean.sls | 2 +- TEMPLATE/config/file.sls | 2 +- TEMPLATE/libmapstack.jinja | 13 +++++++++++-- TEMPLATE/libmatchers.jinja | 11 ++++++++++- TEMPLATE/map.jinja | 19 ++++++++++++++----- TEMPLATE/package/clean.sls | 2 +- TEMPLATE/package/install.sls | 2 +- TEMPLATE/service/clean.sls | 2 +- TEMPLATE/service/running.sls | 2 +- TEMPLATE/subcomponent/config/clean.sls | 2 +- TEMPLATE/subcomponent/config/file.sls | 2 +- 12 files changed, 44 insertions(+), 17 deletions(-) diff --git a/TEMPLATE/_mapdata/init.sls b/TEMPLATE/_mapdata/init.sls index 3529a8df..0252877a 100644 --- a/TEMPLATE/_mapdata/init.sls +++ b/TEMPLATE/_mapdata/init.sls @@ -3,7 +3,7 @@ --- {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split("/")[0] %} -{%- from tplroot ~ "/map.jinja" import mapdata with context %} +{%- from tplroot ~ "/map.jinja" import mapdata without context %} {%- set _mapdata = { "values": mapdata, diff --git a/TEMPLATE/config/clean.sls b/TEMPLATE/config/clean.sls index 19b3f99f..9857622c 100644 --- a/TEMPLATE/config/clean.sls +++ b/TEMPLATE/config/clean.sls @@ -4,7 +4,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_service_clean = tplroot ~ '.service.clean' %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} include: - {{ sls_service_clean }} diff --git a/TEMPLATE/config/file.sls b/TEMPLATE/config/file.sls index 661d8138..6690d1e7 100644 --- a/TEMPLATE/config/file.sls +++ b/TEMPLATE/config/file.sls @@ -4,7 +4,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_package_install = tplroot ~ '.package.install' %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} include: diff --git a/TEMPLATE/libmapstack.jinja b/TEMPLATE/libmapstack.jinja index c671c0e0..78f2fbca 100644 --- a/TEMPLATE/libmapstack.jinja +++ b/TEMPLATE/libmapstack.jinja @@ -1,8 +1,17 @@ {#- -*- coding: utf-8 -*- #} {#- vim: ft=jinja #} -{#- Get the `tplroot` from `tpldir` #} -{%- set tplroot = tpldir.split("/")[0] %} +{%- if not tplfile.endswith("/libmapstack.jinja") %} +{#- Force import `without context` #} +{#- `with context` override the `tplfile` and `tpldir` variables #} +{{- raise("Import error: libmapstack.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }} +{%- elif tplfile.startswith("../") %} +{#- Force import with absolute path #} +{{- raise("Import error: libmapstack.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }} +{%- endif %} + +{#- `tplroot` is the directory of the current imported file #} +{%- set tplroot = tpldir %} {%- from tplroot ~ "/libmatchers.jinja" import parse_matchers, query_map %} {%- set _default_config_dirs = [ diff --git a/TEMPLATE/libmatchers.jinja b/TEMPLATE/libmatchers.jinja index e9aaed32..d6a5cc4a 100644 --- a/TEMPLATE/libmatchers.jinja +++ b/TEMPLATE/libmatchers.jinja @@ -1,8 +1,17 @@ {#- -*- coding: utf-8 -*- #} {#- vim: ft=jinja #} +{%- if not tplfile.endswith("/libmatchers.jinja") %} +{#- Force import `without context` #} +{#- `with context` override the `tplfile` and `tpldir` variables #} +{{- raise("Import error: libmatchers.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }} +{%- elif tplfile.startswith("../") %} +{#- Force import with absolute path #} +{{- raise("Import error: libmatchers.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }} +{%- endif %} + {#- Get the `tplroot` from `tpldir` #} -{%- set tplroot = tpldir.split("/")[0] %} +{%- set tplroot = tpldir %} {%- from tplroot ~ "/libsaltcli.jinja" import cli %} {%- set query_map = { diff --git a/TEMPLATE/map.jinja b/TEMPLATE/map.jinja index 7340d53d..380ba001 100644 --- a/TEMPLATE/map.jinja +++ b/TEMPLATE/map.jinja @@ -1,9 +1,18 @@ -# -*- coding: utf-8 -*- -# vim: ft=jinja +{#- -*- coding: utf-8 -*- #} +{#- vim: ft=jinja #} -{#- Get the `tplroot` from `tpldir` #} -{%- set tplroot = tpldir.split("/")[0] %} -{%- from tplroot ~ "/libmapstack.jinja" import mapstack %} +{%- if not tplfile.endswith("/map.jinja") %} +{#- Force import `without context` #} +{#- `with context` override the `tplfile` and `tpldir` variables #} +{{- raise("Import error: map.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }} +{%- elif tplfile.startswith("../") %} +{#- Force import with absolute path #} +{{- raise("Import error: map.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }} +{%- endif %} + +{#- `tplroot` is the directory of the current imported file #} +{%- set tplroot = tpldir %} +{%- from tplroot ~ "/libmapstack.jinja" import mapstack without context %} {#- Where to lookup parameters source files #} {%- set formula_param_dir = tplroot ~ "/parameters" %} diff --git a/TEMPLATE/package/clean.sls b/TEMPLATE/package/clean.sls index 883b37d8..9f437713 100644 --- a/TEMPLATE/package/clean.sls +++ b/TEMPLATE/package/clean.sls @@ -4,7 +4,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_config_clean = tplroot ~ '.config.clean' %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} include: - {{ sls_config_clean }} diff --git a/TEMPLATE/package/install.sls b/TEMPLATE/package/install.sls index 631e1899..716366e3 100644 --- a/TEMPLATE/package/install.sls +++ b/TEMPLATE/package/install.sls @@ -3,7 +3,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} TEMPLATE-package-install-pkg-installed: pkg.installed: diff --git a/TEMPLATE/service/clean.sls b/TEMPLATE/service/clean.sls index 56167034..42bdad08 100644 --- a/TEMPLATE/service/clean.sls +++ b/TEMPLATE/service/clean.sls @@ -3,7 +3,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} TEMPLATE-service-clean-service-dead: service.dead: diff --git a/TEMPLATE/service/running.sls b/TEMPLATE/service/running.sls index 9c9a6259..2f626eb1 100644 --- a/TEMPLATE/service/running.sls +++ b/TEMPLATE/service/running.sls @@ -4,7 +4,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_config_file = tplroot ~ '.config.file' %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} include: - {{ sls_config_file }} diff --git a/TEMPLATE/subcomponent/config/clean.sls b/TEMPLATE/subcomponent/config/clean.sls index 7cbe308e..c41fea00 100644 --- a/TEMPLATE/subcomponent/config/clean.sls +++ b/TEMPLATE/subcomponent/config/clean.sls @@ -4,7 +4,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_service_clean = tplroot ~ '.service.clean' %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} include: - {{ sls_service_clean }} diff --git a/TEMPLATE/subcomponent/config/file.sls b/TEMPLATE/subcomponent/config/file.sls index dc3404f6..ccd00a32 100644 --- a/TEMPLATE/subcomponent/config/file.sls +++ b/TEMPLATE/subcomponent/config/file.sls @@ -4,7 +4,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_config_file = tplroot ~ '.config.file' %} -{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} include: From 1555e42c138c1cf8433baeaf34512e9ee68f1c78 Mon Sep 17 00:00:00 2001 From: Daniel Dehennin Date: Mon, 6 Sep 2021 14:54:29 +0200 Subject: [PATCH 2/2] docs(map): use `without context` on `map.jinja` import examples --- docs/map.jinja.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/map.jinja.rst b/docs/map.jinja.rst index 4d0b8a87..438695b0 100644 --- a/docs/map.jinja.rst +++ b/docs/map.jinja.rst @@ -458,7 +458,7 @@ Here is the best way to use it in an ``sls`` file: {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split("/")[0] %} - {%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} + {%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} test-does-nothing-but-display-TEMPLATE-as-json: test.nop: @@ -480,7 +480,7 @@ Here is an example based on `template-formula/TEMPLATE/config/file.sls`_: {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_package_install = tplroot ~ '.package.install' %} - {%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE with context %} + {%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} include: