]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add a directory-specific nameserver data to templates
authorNicki Křížek <nicki@isc.org>
Wed, 1 Apr 2026 15:10:08 +0000 (17:10 +0200)
committerNicki Křížek <nicki@isc.org>
Wed, 27 May 2026 14:19:31 +0000 (14:19 +0000)
If a template is being rendered into a directory that represents a
nameserver (e.g. "ns1"), include a nameserver-specific information in
the data - variable called "ns" which has information about the
nameserver this file belongs to.

Ensure the "ns" variable is only exposed to the template when rendered,
without affecting the environment variables (always work with a copy of
the env_vars).

bin/tests/system/isctest/template.py

index d25e2926d084e318e45ca3acfc42d1cefcfa8a03..f81b423f10d193e8cc95a2176b91d6132603fc1a 100644 (file)
@@ -13,6 +13,7 @@
 
 from dataclasses import dataclass
 from pathlib import Path
+from re import compile as Re
 from typing import Any
 
 import re
@@ -22,6 +23,8 @@ import jinja2
 from .log import debug
 from .vars import ALL
 
+NS_DIR_RE = Re(r"^(a?ns([0-9]+))/")
+
 
 class TemplateEngine:
     """
@@ -63,10 +66,16 @@ class TemplateEngine:
             raise RuntimeError('No jinja2 template found for "{output}"')
 
         if data is None:
-            data = self.env_vars
+            data = {**self.env_vars}
         else:
             data = {**self.env_vars, **data}
 
+        # directory-specific "ns" var
+        assert "ns" not in data, '"ns" variable is reserved for nameserver data'
+        match = NS_DIR_RE.search(output)
+        if match:
+            data["ns"] = Nameserver(match.group(1))
+
         debug("rendering template `%s` to file `%s`", template, output)
         stream = self.j2env.get_template(template).stream(data)
         stream.dump(output, encoding="utf-8")