forked from coloclue/kees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gentool
executable file
·86 lines (65 loc) · 2.24 KB
/
gentool
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
83
84
85
86
#!/usr/bin/env python
"""
KEES
The ColoClue Network Automation Toolchain
(c) 2014-2017 Job Snijders <job@instituut.net>
(c) 2017-2019 Network committee Coloclue <routers@coloclue.net>
"""
from jinja2 import Environment, FileSystemLoader
import hiyapyco
import os
import sys
try:
import argparse
except ImportError:
print("ERROR: install argparse manually")
print("HINT: sudo pip install argparse")
sys.exit(2)
def render(tpl_path, context):
path, filename = os.path.split(tpl_path)
return Environment(
loader=FileSystemLoader(path or './')
).get_template(filename).render(context)
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-y', dest='yamldata',
default="vars/main.yml",
type=str,
nargs='*',
help="""Location of the YAML data""")
parser.add_argument('-t', dest='template',
type=str,
required=True,
help="""Location of the template""")
parser.add_argument('-o', dest='output',
type=str,
default='-',
help='Output file (default: STDOUT)')
parser.add_argument('-d', dest='debug',
action='store_true',
help="""Enable debug""")
group = parser.add_mutually_exclusive_group()
group.add_argument('-4', dest='afi',
action='store_const',
const="ipv4",
help="""IPv4 mode""")
group.add_argument('-6', dest='afi',
action='store_const',
const="ipv6",
help="""IPv6 mode""")
args = parser.parse_args()
if args.debug:
print(args)
data = hiyapyco.load(*args.yamldata, method=hiyapyco.METHOD_MERGE)
data['afi'] = args.afi
if args.debug:
print(data)
if args.output == "-":
print(render(args.template, data))
else:
f = open(args.output, 'w')
f.write(render(args.template, data))
f.close()
if __name__ == '__main__':
main()