QVolution2019.2/AoM_Service/AoM_Configs/webapp/views.py

70 lines
2.5 KiB
Python
Executable File

# views.py
import glob
import json
import os
import yaml
from flask import session
from library.logger import AlertLogging
from webapp import app, render_template, request, render
logger = AlertLogging('aom')
logger.start()
logger.start_log_file("logs/aom_service.log")
@app.route('/', methods=['GET', 'POST'])
def index():
logger.debug("Request Method: {}".format(request.method))
if request.method == 'GET':
# GET BLOB OF FILES
service_config = yaml.load(open('service.yaml', 'r').read())
alert_list = sorted([os.path.splitext(os.path.basename(x))[0] for x in
glob.glob(service_config['alert_folder'] + "/*.yaml")])
if 'yaml_config' in session:
return render_template('index.html', **json.loads(session['yaml_config']), alert_list=alert_list)
else:
return render_template('index.html', alert_list=alert_list)
elif request.method == 'POST':
logger.info("Got a form")
if 'go' in request.form['generate'].lower():
return re_build(request.form['loadFile'])
yaml_config = dict()
ret = ''
try:
for field_name, value in request.form.items():
yaml_config[field_name] = value
code, ret = render.render_config(yaml_config)
assert code == 0
return render_template('debug.html', query=yaml_config,
file_path='alert_configs/{}.yaml'.format(yaml_config['alert_name']),
file_contents=ret.split('\n'))
except AssertionError:
session['yaml_config'] = json.dumps(yaml_config)
return render_template('error.html', message="Failed to render to file: {}".format(ret))
except Exception as e:
return render_template('error.html', message=str(e))
@app.route('/build/<alert_id>', methods=['POST'])
def re_build(alert_id):
# READ IN CONFIG FROM ID
config = render.render_yaml(alert_id)
service_config = yaml.load(open('service.yaml', 'r').read())
alert_list = sorted([os.path.splitext(os.path.basename(x))[0] for x in
glob.glob(service_config['alert_folder'] + "/*.yaml")])
return render_template('index.html', **config, alert_list=alert_list)
@app.route("/debug/")
def toggle_debug():
if logger.debug_handler:
logger.stop_debug()
logger.info("Debug Stopped")
else:
logger.start_debug()
logger.debug("Debug Started")
return index()