-
Notifications
You must be signed in to change notification settings - Fork 1
/
templating.py
260 lines (205 loc) · 8.07 KB
/
templating.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/python3
import argparse
import traceback
import sys
import os
import errno
import aiohttp
import asyncio
import netaddr
import aiofiles
from pathlib import Path
import yaml
from urllib.parse import urlparse
from flask import Flask, request
from jinja2 import Environment, FileSystemLoader, TemplateNotFound, TemplateError
from datetime import datetime
objects = {}
def try_as(loader, s, on_error):
try:
loader(s)
return True
except on_error:
return False
def is_yaml(s):
return try_as(yaml.cSafeLoader, s, yaml.scanner.ScannerError)
def createObjName(i):
return str(Path(*Path(os.path.splitext(i)[0]).parts[-2:]))
def load_conf_file(config_file):
with open(config_file, "r") as f:
config = yaml.load(f.read(), Loader=yaml.CSafeLoader)
return config
async def getEndpoint(session, url, **options):
async with session.get(url, timeout=args.timeout, **options) as resp:
try:
resp_body = await resp.json(content_type=None)
except:
resp.raise_for_status()
return url, resp_body
async def loadUrls(urls):
async with aiohttp.ClientSession() as session:
tasks = []
for i in urls:
options = dict(list(i.values())[0])
url = list(i.keys())[0]
tasks.append(asyncio.ensure_future(getEndpoint(session, url, **options)))
responses = await asyncio.gather(*tasks)
for response in responses:
key = '/'.join(urlparse(response[0]).path.split('/')[-2:])
objects[key] = response[1]
return 1
async def readFile(file):
async with aiofiles.open(file, mode='r') as f:
content = await f.read()
out = yaml.load(content, Loader=yaml.CSafeLoader)
return file, out
async def loadFiles(files):
tasks = []
for file in files:
options = list(file.values())[0]
file = list(file.keys())[0]
if is_yaml:
pass
else:
raise ValueError(
f'Failed to load {file}. Not a JSON or YAML formatted file')
tasks.append(asyncio.ensure_future(readFile(file)))
contents = await asyncio.gather(*tasks)
for content in contents:
objects[createObjName(content[0])] = content[1]
return 1
async def runTasks(tasks):
await asyncio.gather(*tasks)
def updateData():
config = load_conf_file(args.config)
files_tasks = []
urls_tasks = []
for item in config["get"]:
if isinstance(item, dict):
pass
elif isinstance(item, str):
item = {item: ''}
else:
sys.exit(f"{item} is not a str, or dict. Exit")
options = list(item.values())[0]
item = list(item.keys())[0]
item = urlparse(item)
if item.scheme == "file":
# Make this a function again, someday
path = item.netloc + item.path
if not os.path.isabs(path):
path = os.path.normpath(os.getcwd() + item.path)
if os.path.isdir(path):
for subdir, dirs, files in os.walk(path):
for file in files:
path = os.path.join(subdir, file)
d = {path: options}
files_tasks.append(d)
elif os.path.isfile(path):
d = {path: options}
files_tasks.append(d)
else:
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), path)
elif item.scheme == "https" or item.scheme == "http":
item = item.geturl()
d = {item: options}
urls_tasks.append(d)
tasks = []
tasks.append(loadUrls(urls_tasks))
tasks.append(loadFiles(files_tasks))
asyncio.run(runTasks(tasks))
env = Environment(extensions=['jinja2.ext.do'], loader=FileSystemLoader([]), trim_blocks=True, lstrip_blocks=True)
env.filters["netmask"] = lambda ip: netaddr.IPNetwork(ip).netmask
env.filters["cidr"] = lambda ip: netaddr.IPNetwork(ip).prefixlen
env.filters["networkId"] = lambda ip: netaddr.IPNetwork(ip).ip
env.filters["getFirstDhcpIp"] = lambda ip: netaddr.IPNetwork(ip)[2]
env.filters["getLastDhcpIp"] = lambda ip: netaddr.IPNetwork(ip)[-2]
env.filters["getIp"] = lambda ip, num: netaddr.IPNetwork(ip)[num]
env.filters["agentDistro"] = lambda src: src.split(":")[0]
env.filters["agentPort"] = lambda src: src.split(":")[1]
env.filters["getFirstFapIP"] = lambda ip: netaddr.IPNetwork(
ip)[netaddr.IPNetwork(ip).size / 2]
env.tests['inList'] = lambda list, item: True if item in list else False
app = Flask(__name__)
@app.after_request
def add_header(response):
if response.status_code == 200:
response.cache_control.max_age = 5
response.cache_control.s_maxage = 1
return response
@app.route("/<path>", methods=["GET"])
def root_get(path):
return render_template(path, options=request.args)
def render_template(tpl, options):
try:
updateData()
template = env.get_template(tpl)
body = template.render(objects=objects, options=options)
except (aiohttp.client_exceptions.ClientConnectorError, aiohttp.client_exceptions.ClientResponseError) as err:
return f'Connection error trying to get: {err}', 500
except TemplateNotFound:
return f'Template "{tpl}" not found\n', 404
except TemplateError as err:
return f'Templating of "{tpl}" failed to render. Most likely due to an error in the template. Error transcript:\n\n{err}\n----\n\n{traceback.format_exc()}\n', 400
except FileNotFoundError as err:
return f'File error: {err}', 500
except ValueError as err:
return f'Parsing Error: {err}', 500
except Exception as err:
print(traceback.format_exc())
return f'Uncaught error: {err}', 500
return body, 200
@app.route("/<path>", methods=["POST"])
def root_post(path):
try:
updateData()
content = request.stream.read(int(request.headers["Content-Length"]))
template = env.from_string(content.decode("utf-8"))
body = template.render(objects=objects, options=request.args)
except Exception as err:
return 'Templating of "{path}" failed to render. Most likely due to an error in the template. Error transcript:\n\n{err}\n----\n\n{traceback.format_exc()}\n', 400
return body, 200
parser = argparse.ArgumentParser(
description="Process templates for gondul.", add_help=False)
parser.add_argument("-t", "--templates", type=str,
nargs="+", required=True, help="location of templates")
parser.add_argument("-c", "--config", type=str,
default="config.yaml", required=True, help="Location of config file")
parser.add_argument("-h", "--host", type=str,
default="127.0.0.1", help="host address")
parser.add_argument("-p", "--port", type=int, default=8080, help="host port")
parser.add_argument("--debug", action="store_true",
help="enable debug mode")
parser.add_argument("-x", "--timeout", type=int, default=20,
help="gondul server timeout")
parser.add_argument("-o", "--once", type=str, default="",
help="Run once with the provided template")
parser.add_argument("-i", "--options", type=str, default="", nargs="+",
help="Options to send to template, like query params in the API")
parser.add_argument("-f", "--outfile", type=str, default="",
help="Output file, otherwise prints to stdout")
parser.add_argument("--help", type=str, default="",
help="Print help")
try:
args = parser.parse_args()
except:
parser.print_help()
sys.exit(0)
env.loader.searchpath = args.templates
if not sys.argv[1:]:
parser.print_help()
sys.exit(1)
if args.once:
options = {}
for option in args.options:
key, value = option.split('=')
options[key] = value
body, _ = render_template(args.once, options=options)
if args.outfile:
with open(args.outfile, 'w') as f:
f.write(body)
else:
print(body)
sys.exit(0)
app.run(host=args.host, port=args.port, debug=args.debug)