summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-07-03 13:10:50 +0200
committerChristian Poessinger <christian@poessinger.com>2020-07-03 13:10:51 +0200
commit5d9a89a522079eeb3d86cafc480b37f0616e4658 (patch)
treebd94aa495500a9b837d4365a4b6f4ee4caa99e18 /python
parent3395cefd5af205ab45b2db9269bbb4daa10ba7df (diff)
downloadvyos-1x-5d9a89a522079eeb3d86cafc480b37f0616e4658.tar.gz
vyos-1x-5d9a89a522079eeb3d86cafc480b37f0616e4658.zip
vyos.template: T2676: add custom Jinja2 filter for IP handling
NTP configuration file requires the IP address and a netmask for client subnets but the CLI will only provide a prefix based ntoation. Use custom, reusable JInja2 template to transform a CIDR based prefix into its address and netmask portion for IPv4 and IPv6. Jinja2 custom filters are regular python functions - thus they can be re-used directly when e.g. verifying the configuration in vyos-smoketests.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/template.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py
index e4b253ed3..85a0143c0 100644
--- a/python/vyos/template.py
+++ b/python/vyos/template.py
@@ -17,11 +17,9 @@ import os
from jinja2 import Environment
from jinja2 import FileSystemLoader
-
from vyos.defaults import directories
from vyos.util import chmod, chown, makedir
-
# reuse the same Environment to improve performance
_templates_env = {
False: Environment(loader=FileSystemLoader(directories['templates'])),
@@ -32,6 +30,13 @@ _templates_mem = {
True: {},
}
+def vyos_address_from_cidr(text):
+ from ipaddress import ip_network
+ return ip_network(text).network_address
+
+def vyos_netmask_from_cidr(text):
+ from ipaddress import ip_network
+ return ip_network(text).netmask
def render(destination, template, content, trim_blocks=False, formater=None, permission=None, user=None, group=None):
"""
@@ -42,8 +47,8 @@ def render(destination, template, content, trim_blocks=False, formater=None, per
This classes cache the renderer, so rendering the same file multiple time
does not cause as too much overhead. If use everywhere, it could be changed
- and load the template from python environement variables from an import
- python module generated when the debian package is build
+ and load the template from python environement variables from an import
+ python module generated when the debian package is build
(recovering the load time and overhead caused by having the file out of the code)
"""
@@ -54,11 +59,15 @@ def render(destination, template, content, trim_blocks=False, formater=None, per
# Setup a renderer for the given template
# This is cached and re-used for performance
if template not in _templates_mem[trim_blocks]:
- _templates_mem[trim_blocks][template] = _templates_env[trim_blocks].get_template(template)
+ _env = _templates_env[trim_blocks]
+ _env.filters['address_from_cidr'] = vyos_address_from_cidr
+ _env.filters['netmask_from_cidr'] = vyos_netmask_from_cidr
+ _templates_mem[trim_blocks][template] = _env.get_template(template)
+
template = _templates_mem[trim_blocks][template]
# As we are opening the file with 'w', we are performing the rendering
- # before calling open() to not accidentally erase the file if the
+ # before calling open() to not accidentally erase the file if the
# templating fails
content = template.render(content)