summaryrefslogtreecommitdiff
path: root/python/vyos/template.py
blob: 85a0143c08efa07540a1f46afa166fd5dba47a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright 2019 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
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see <http://www.gnu.org/licenses/>.

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'])),
    True:  Environment(loader=FileSystemLoader(directories['templates']), trim_blocks=True),
}
_templates_mem = {
    False: {},
    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):
    """
    render a template from the template directory, it will raise on any errors
    destination: the file where the rendered template must be saved
    template: the path to the template relative to the template folder
    content: the dictionary to use to render the template

    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
    (recovering the load time and overhead caused by having the file out of the code)
    """

    # Create the directory if it does not exists
    folder = os.path.dirname(destination)
    makedir(folder, user, group)

    # Setup a renderer for the given template
    # This is cached and re-used for performance
    if template not in _templates_mem[trim_blocks]:
        _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
    # templating fails
    content = template.render(content)

    if formater:
        content = formater(content)

    # Write client config file
    with open(destination, 'w') as f:
        f.write(content)

    chmod(destination, permission)
    chown(destination, user, group)