-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-zone.py
61 lines (51 loc) · 2.01 KB
/
create-zone.py
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
#!/usr/bin/env python
import json
from os import environ
from sys import exit
from datetime import datetime
from jinja2 import Environment, FileSystemLoader
def load_zones(path='zones.json'):
""" load zones from json file
returns python object """
try:
with open(path) as config:
zones = json.load(config)
return zones
except ValueError as detail:
raise detail
def load_templates(dir='templates'):
""" load jinja2 templates """
file_loader = FileSystemLoader(dir)
return Environment(loader=file_loader)
def create_file(name, content):
""" creates a file with content """
with open(name, 'w') as file:
file.write(content)
def generate_zones(zones, templates):
""" create zone file and reverse zone """
try:
serial = environ['BUILD_NUMBER']
except KeyError:
print('$BUILD_NUMBER env variable not available, not running from Jenkins ?')
exit(1)
for zone, config in zones.items():
config['domain'] = zone
config['serial'] = serial
config['file'] = 'db.{}'.format(zone)
config['reverse'] = {}
config['reverse']['file'] = 'db' + ''.join(map(
lambda x: '.{}'.format(x),
config['network'].split('.')[:3]))
reversed_octets = ''.join(map(
lambda x: '{}.'.format(x),
config['network'].split('.')[::-1][-3:]))
config['reverse']['domain'] = "{}in-addr.arpa".format(reversed_octets)
config['raw'] = templates.get_template('zone.jinja2').render(zone=config)
config['reverse']['raw'] = templates.get_template('reverse-zone.jinja2').render(zone=config)
return zones
if __name__ == "__main__":
zones = load_zones()
templates = load_templates()
generate_zones(zones, templates)
config_yaml = templates.get_template('config.yaml.jinja2').render(zones=zones)
create_file('manifests/config.yaml', config_yaml)