aboutsummaryrefslogtreecommitdiffstats
path: root/utils/generate_config.py
blob: 353cd47234c7c7ee4f2c046e1070881dd0058ce5 (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
#!/usr/bin/python
"""This module does blah blah."""
import argparse
import ipaddress
import yaml
from jinja2 import Environment, FileSystemLoader

PARSER = argparse.ArgumentParser()
PARSER.add_argument("--yaml", "-y", type=str, required=True)
PARSER.add_argument("--jinja2", "-j", type=str, required=True)
ARGS = PARSER.parse_args()

# Custom filter to allow simple IP address operations returning
# a new address from an upper or lower (negative) index
def ipaddr_index(base_address, index):
    """Return IP address in given network at given index"""
    try:
        base_address_str = unicode(base_address)
    #pylint: disable=unused-variable
    except NameError as ex:
        base_address_str = str(base_address)
    return ipaddress.ip_address(base_address_str) + int(index)

ENV = Environment(loader=FileSystemLoader('./'))
ENV.filters['ipaddr_index'] = ipaddr_index

with open(ARGS.yaml) as _:
    DICT = yaml.safe_load(_)

# Print dictionary generated from yaml (uncomment for debug)
#print DICT

# Render template and print generated conf to console
TEMPLATE = ENV.get_template(ARGS.jinja2)
#pylint: disable=superfluous-parens
print(TEMPLATE.render(conf=DICT))