From 8797eba26ddf0cec10a152309aa705e763c6ae62 Mon Sep 17 00:00:00 2001 From: Daniel Dehennin Date: Wed, 31 Mar 2021 13:11:08 +0200 Subject: [PATCH] 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 | 4 ++-- 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 | 4 ++-- 12 files changed, 46 insertions(+), 19 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..cd926303 100644 --- a/TEMPLATE/config/file.sls +++ b/TEMPLATE/config/file.sls @@ -4,8 +4,8 @@ {#- 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 ~ "/libtofs.jinja" import files_switch with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} +{%- from tplroot ~ "/libtofs.jinja" import files_switch without context %} include: - {{ sls_package_install }} 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..dab4a1d8 100644 --- a/TEMPLATE/subcomponent/config/file.sls +++ b/TEMPLATE/subcomponent/config/file.sls @@ -4,8 +4,8 @@ {#- 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 ~ "/libtofs.jinja" import files_switch with context %} +{%- from tplroot ~ "/map.jinja" import mapdata as TEMPLATE without context %} +{%- from tplroot ~ "/libtofs.jinja" import files_switch without context %} include: - {{ sls_config_file }}