-
Notifications
You must be signed in to change notification settings - Fork 8
/
netpalm-admin.py
434 lines (368 loc) · 13 KB
/
netpalm-admin.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
from flask import Flask, render_template, request, jsonify, redirect, url_for
import base64
from backend.confload.confload import Config
from backend.netpalm.netpalm_adapter import NetpalmAdapter
from backend.parseatron.parseatron import ParseAtron
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True
conf = Config()
netpalm = NetpalmAdapter()
parseatron = ParseAtron()
@app.route("/")
def home():
# poorly generate some container stats
container_data = netpalm.get_containers()
container_names = []
failed_jobs = []
successful_jobs = []
for container in container_data:
container_names.append(container)
successful_jobs.append(container_data[container]["successful_job_count"])
failed_jobs.append(container_data[container]["failed_job_count"])
total_successful_jobs = sum(successful_jobs)
total_failed_jobs = sum(failed_jobs)
total_jobs = total_successful_jobs + total_failed_jobs
try:
total_success_percent = "{:.2f}".format((total_successful_jobs / total_jobs) * 100)
total_failed_percent = "{:.2f}".format((total_failed_jobs / total_jobs) * 100)
except ZeroDivisionError:
total_success_percent = 0
total_failed_percent = 0
# poorly generate some process stats
worker_data = netpalm.get("workers/")
worker_names = []
worker_failed_jobs = []
worker_successful_jobs = []
total_processes = 0
container_types = {"fifo":0,"pinned":0}
container_set = set()
for worker in worker_data:
worker_names.append(worker["name"])
worker_failed_jobs.append(worker["successful_job_count"])
worker_successful_jobs.append(worker["failed_job_count"])
if worker["hostname"] not in container_set:
if "fifo" in worker["name"]:
container_types["fifo"] += 1
container_set.add(worker["hostname"])
if ("fifo" not in worker["name"]) and ("processworker" not in worker["name"]):
container_types["pinned"] += 1
container_set.add(worker["hostname"])
total_processes += 1
total_running_containers = len(container_set)
total_devices_inventory = len(conf.inventory_hosts)
return render_template(
"home.html",
container_names=container_names,
failed_jobs=failed_jobs,
successful_jobs=successful_jobs,
total_successful_jobs=total_successful_jobs,
total_failed_jobs=total_failed_jobs,
total_jobs=total_jobs,
total_success_percent=total_success_percent,
total_failed_percent=total_failed_percent,
worker_names=worker_names,
worker_failed_jobs=worker_failed_jobs,
worker_successful_jobs=worker_successful_jobs,
container_types=container_types,
total_processes=total_processes,
total_running_containers=total_running_containers,
total_devices_inventory=total_devices_inventory
)
@app.route("/template_editor/<template_type>")
def template_editor(template_type=None):
return render_template(
"template-editor-form.html",
heading=template_type,
tmp_name=None,
tmp_payload=None
)
@app.route("/script_editor/<script_type>")
def script_editor(script_type=None):
return render_template(
"python-editor-form.html",
script_type=script_type,
scrip_name=None,
scrip_payload=None
)
@app.route("/parser/ttp")
def ttp_parser():
ttpdata = netpalm.get("ttptemplate")
return render_template(
"universal-template-table.html",
heading="ttp",
data=ttpdata
)
@app.route("/parser/tfsm")
def tfsm_parser():
tfsmdata = netpalm.get("template")
return render_template("tfsm-parser-table.html", tfsmdata=tfsmdata)
@app.route("/template/service")
def service_template():
data = netpalm.get("j2template/service/")
return render_template(
"universal-template-table.html",
heading="servicej2",
data=data
)
@app.route("/template/webhook")
def webhook_template():
data = netpalm.get("j2template/webhook/")
return render_template(
"universal-template-table.html",
heading="webhookj2",
data=data
)
@app.route("/template/config")
def config_template():
data = netpalm.get("j2template/config/")
return render_template(
"universal-template-table.html",
heading="configj2",
data=data
)
@app.route("/script")
def script():
data = netpalm.get("script")
return render_template(
"universal-template-table.html",
heading="script",
data=data
)
@app.route("/webhook")
def webhook():
data = netpalm.get("webhook")
return render_template(
"universal-template-table.html",
heading="webhook",
data=data
)
@app.route("/containers")
def containers():
data = netpalm.get_containers()
return render_template(
"containers-table.html",
data=data
)
@app.route("/serviceinstance")
def service_instance():
data = netpalm.get("service/instances/")
return render_template(
"service-instance-table.html",
data=data
)
@app.route('/service/instance/delete/<sid>', methods=['POST'])
def remove_service_instance(sid=None):
try:
res = netpalm.post(
route=f"service/instance/delete/{sid}",
payload=None
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/service/instance/<sid>', methods=['GET'])
def get_service_instance(sid=None):
try:
res = netpalm.get(
route=f"service/instance/{sid}"
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route("/process")
def process():
data = netpalm.get("workers/")
return render_template(
"process-table.html",
data=data
)
@app.route("/queue")
def queue():
data = netpalm.get("taskqueue/")
return render_template(
"queue-depth-table.html",
data=data
)
@app.route("/taskatron")
def taskatron():
return render_template(
"taskatron.html"
)
@app.route("/getcfg")
def getcfg():
return render_template(
"commandatron_get_set_config.html",
cfgtype="get",
devices=conf.inventory_hosts
)
@app.route("/setcfg")
def setcfg():
return render_template(
"commandatron_get_set_config.html",
cfgtype="set",
devices=conf.inventory_hosts
)
@app.route("/execnetpalm/", methods=["POST"])
def execnetpalm():
posted_data = request.json
res = netpalm.send_netpalm(
posted_data
)
return res
@app.route("/task/<task_id>")
def checktask(task_id):
res = netpalm.check_task(
task_id
)
return res
@app.route('/fsm', methods=['POST'])
def fsm():
try:
data = request.form.to_dict(flat=False)
clitxt = data["inputtext"][0]
fsmtemplate = data["fsmtxt"][0]
res = parseatron.parsefsm(
cli_txt=clitxt,
fsm_template=fsmtemplate
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/j2webhook', methods=['POST'])
@app.route('/j2', methods=['POST'])
def j2():
try:
data = request.form.to_dict(flat=False)
clitxt = data["inputtext"][0]
fsmtemplate = data["fsmtxt"][0]
res = parseatron.parsej2(
cli_txt=clitxt,
fsm_template=fsmtemplate
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/ttp', methods=['POST'])
def ttp():
try:
data = request.form.to_dict(flat=False)
clitxt = data["inputtext"][0]
fsmtemplate = data["fsmtxt"][0]
res = parseatron.parsettp(
cli_txt=clitxt,
fsm_template=fsmtemplate
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/ttp/add', methods=['POST'])
def add_ttp():
try:
data = request.json
res = netpalm.post(
route="ttptemplate",
payload=data
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/j2/add', methods=['POST'])
def add_j2_config():
try:
data = request.json
res = netpalm.post(
route="j2template/config/",
payload=data
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/j2webhook/add', methods=['POST'])
def add_j2_webhook():
try:
data = request.json
res = netpalm.post(
route="j2template/webhook/",
payload=data
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/script/add', methods=['POST'])
def add_script():
try:
data = request.json
res = netpalm.post(
route="script/add/",
payload=data
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/webhook/add', methods=['POST'])
def add_webhook():
try:
data = request.json
res = netpalm.post(
route="webhook/add/",
payload=data
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route('/<remove_temp>/remove', methods=['POST'])
def remove_webhook(remove_temp=None):
try:
rt = {
"script": "script/remove/",
"webhook": "webhook/remove/",
"webhookj2": "j2template/webhook/",
"configj2": "j2template/config/",
"servicej2": "j2template/service/",
"ttp": "ttptemplate"
}
data = request.json
res = netpalm.delete(
route=rt[remove_temp],
payload=data
)
return jsonify(res)
except Exception as e:
return str(e)
@app.route("/servicej2/<tmpname>")
@app.route("/configj2/<tmpname>")
@app.route("/webhookj2/<tmpname>")
@app.route("/ttp/<tmpname>")
def get_template(tmpname=None):
rt = {
"script": "script/",
"webhook": "webhook/",
"webhookj2": "j2template/webhook/",
"configj2": "j2template/config/",
"servicej2": "j2template/service/",
"ttp": "ttptemplate/"
}
template_type = request.path.split("/")[1]
url_path = rt[template_type]+tmpname
data = netpalm.get(url_path)
template_payload = base64.b64decode(data["data"]["task_result"]["base64_payload"]).decode()
return render_template(
"template-editor-form.html",
heading=template_type,
tmp_name=tmpname,
tmp_payload=template_payload
)
@app.route("/script/<tmpname>")
@app.route("/webhook/<tmpname>")
def get_script(tmpname=None):
data = netpalm.get(request.path[1:])
script_type = request.path.split("/")[1]
scrip_payload = base64.b64decode(data["data"]["task_result"]["base64_payload"]).decode()
return render_template(
"python-editor-form.html",
script_type=script_type,
scrip_name=tmpname,
scrip_payload=scrip_payload
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=10001, threaded=True, debug=True)