Commit da677b59 authored by janis.streib's avatar janis.streib 🦉
Browse files

Clenaup of python middleware

parent a3c21846
Pipeline #164810 passed with stages
in 9 minutes and 35 seconds
from json import JSONEncoder from json import JSONEncoder
from flask import Flask, url_for, jsonify from flask import Flask
from beaker.middleware import SessionMiddleware from beaker.middleware import SessionMiddleware
from kitnet.lib.netdb import PostgreSQL from kitnet.lib.netdb import PostgreSQL
import sys
import os import os
import inspect
import subprocess import subprocess
from .model import DBObject, MetaDBObject, SimpleKITUser from .model import DBObject, MetaDBObject, SimpleKITUser
import importlib import importlib
...@@ -26,7 +24,7 @@ session_opts = { ...@@ -26,7 +24,7 @@ session_opts = {
class ModMetaData(object): class ModMetaData(object):
def __init__(self, name, version, contact_email, printable_name, mod_path, gitlab_url=None, search_func=None, def __init__(self, name, version, contact_email, printable_name, mod_path, gitlab_url=None, search_func=None,
search_opts_func=None, is_tool=False): search_opts_func=None):
self.version = version self.version = version
self.contact_mail = contact_email self.contact_mail = contact_email
self.gitlab_url = gitlab_url self.gitlab_url = gitlab_url
...@@ -35,7 +33,6 @@ class ModMetaData(object): ...@@ -35,7 +33,6 @@ class ModMetaData(object):
self.name = name self.name = name
self.search_func = search_func self.search_func = search_func
self.search_opts_func = search_opts_func if search_func is not None else None self.search_opts_func = search_opts_func if search_func is not None else None
self.is_tool = is_tool
class CustomJSONEncoder(JSONEncoder): class CustomJSONEncoder(JSONEncoder):
...@@ -62,6 +59,7 @@ class CustomJSONEncoder(JSONEncoder): ...@@ -62,6 +59,7 @@ class CustomJSONEncoder(JSONEncoder):
class MyApp(Flask): class MyApp(Flask):
pass pass
# Define the WSGI application object # Define the WSGI application object
app = MyApp(__name__) app = MyApp(__name__)
app.json_encoder = CustomJSONEncoder app.json_encoder = CustomJSONEncoder
...@@ -95,10 +93,11 @@ def load_mods(): ...@@ -95,10 +93,11 @@ def load_mods():
mod = importlib.import_module(m['mod']) mod = importlib.import_module(m['mod'])
MODS.append(mod.METADATA) MODS.append(mod.METADATA)
app.register_blueprint(blueprint=getattr(mod, m['mod'].split('.')[-1]), app.register_blueprint(blueprint=getattr(mod, m['mod'].split('.')[-1]),
url_prefix='{prefix}{url}'.format(prefix='/tools' if mod.METADATA.is_tool else '', url_prefix='{prefix}{url}'.format(prefix='', url=m['url']))
url=m['url']))
def finalize_init(): def finalize_init():
db_conn.close() db_conn.close()
from . import views from . import views
This diff is collapsed.
...@@ -2,7 +2,6 @@ from flask import Blueprint ...@@ -2,7 +2,6 @@ from flask import Blueprint
from . import dhcp_leases_model from . import dhcp_leases_model
from net_suite import app, get_git_version, ModMetaData from net_suite import app, get_git_version, ModMetaData
# we are assuming, that at the time of loading the app is initializes and the config is loaded
bb_name = 'dhcp_leases' bb_name = 'dhcp_leases'
dhcp_leases = Blueprint(bb_name, __name__, template_folder='templates') dhcp_leases = Blueprint(bb_name, __name__, template_folder='templates')
......
...@@ -5,13 +5,14 @@ from . import dhcp_leases_model ...@@ -5,13 +5,14 @@ from . import dhcp_leases_model
from net_suite import db from net_suite import db
from net_suite.model import BCD from net_suite.model import BCD
def get_dhcpleaseDB(): def get_dhcpleaseDB():
if not hasattr(g, 'dhcpleaseDB'): if not hasattr(g, 'dhcpleaseDB'):
g.dhcpleaseDB = dhcp_leases_model.DhcpLeaseDB(database=current_app.config['MINIDHCPVS_DB'], g.dhcpleaseDB = dhcp_leases_model.DhcpLeaseDB(database=current_app.config['MINIDHCPVS_DB'],
user=current_app.config['MINIDHCPVS_USER'], user=current_app.config['MINIDHCPVS_USER'],
host=current_app.config['MINIDHCPVS_HOST'], host=current_app.config['MINIDHCPVS_HOST'],
port=current_app.config['MINIDHCPVS_PORT'], port=current_app.config['MINIDHCPVS_PORT'],
password=current_app.config['MINIDHCPVS_PW']) password=current_app.config['MINIDHCPVS_PW'])
return g.dhcpleaseDB return g.dhcpleaseDB
...@@ -28,7 +29,7 @@ def usr_bcds(): ...@@ -28,7 +29,7 @@ def usr_bcds():
user = request.environ['beaker.session']['login'] user = request.environ['beaker.session']['login']
user_bcds = user.get_bcds(db, get_db_conn()) user_bcds = user.get_bcds(db, get_db_conn())
bcd_counts = dhcpDB.get_active_mac_counts([b.name for b in user_bcds]) bcd_counts = dhcpDB.get_active_mac_counts([b.name for b in user_bcds])
return jsonify(bcd_counts) return jsonify(bcd_counts)
@dhcp_leases.route("/bcds/<bcd_name>/active") @dhcp_leases.route("/bcds/<bcd_name>/active")
...@@ -43,4 +44,4 @@ def active_leases(bcd_name): ...@@ -43,4 +44,4 @@ def active_leases(bcd_name):
if bcd is None: if bcd is None:
return abort(404) return abort(404)
leases = dhcpDB.get_active_macs(bcd_name) leases = dhcpDB.get_active_macs(bcd_name)
return jsonify(leases) return jsonify(leases)
...@@ -8,6 +8,6 @@ ip_contact = Blueprint(bb_name, __name__, template_folder='templates') ...@@ -8,6 +8,6 @@ ip_contact = Blueprint(bb_name, __name__, template_folder='templates')
METADATA = ModMetaData(name=bb_name, mod_path=__name__, gitlab_url='https://git.scc.kit.edu/scc-net/net-suite/net-suite-ip_contact', METADATA = ModMetaData(name=bb_name, mod_path=__name__, gitlab_url='https://git.scc.kit.edu/scc-net/net-suite/net-suite-ip_contact',
printable_name='Adressbetreuer kontaktieren', version=get_git_version(__file__), printable_name='Adressbetreuer kontaktieren', version=get_git_version(__file__),
contact_email='dns-betrieb@scc.kit.edu', is_tool=True) contact_email='dns-betrieb@scc.kit.edu')
MENU = {METADATA.printable_name: 'ip_contact.main'} MENU = {METADATA.printable_name: 'ip_contact.main'}
from . import views from . import views
...@@ -10,12 +10,12 @@ app.logger.info('Loading OUI databse...') ...@@ -10,12 +10,12 @@ app.logger.info('Loading OUI databse...')
ouis = requests.get('http://standards-oui.ieee.org/oui.txt').text.split('\n') ouis = requests.get('http://standards-oui.ieee.org/oui.txt').text.split('\n')
OUI_DB = dict() OUI_DB = dict()
for line in ouis: for line in ouis:
if not '(hex)' in line: if '(hex)' not in line:
continue continue
parts = line.split('(hex)') parts = line.split('(hex)')
OUI_DB[parts[0].strip().lower().replace('-', ':')] = parts[1].strip() OUI_DB[parts[0].strip().lower().replace('-', ':')] = parts[1].strip()
METADATA = ModMetaData(name=bb_name, mod_path=__name__, gitlab_url='https://git.scc.kit.edu/scc-net/net-suite/net-suite-macfinder', METADATA = ModMetaData(name=bb_name, mod_path=__name__, gitlab_url='https://git.scc.kit.edu/scc-net/net-suite/net-suite-macfinder',
printable_name='MACFinder', version=get_git_version(__file__), printable_name='MACFinder', version=get_git_version(__file__),
contact_email='dns-betrieb@scc.kit.edu', is_tool=False) contact_email='dns-betrieb@scc.kit.edu')
from . import views from . import views
<html>
<body>
<h1>DEBUG: Forward user-data to {{new_target}}?</h1>
<form method="post" action="{{new_target}}">
<input type="hidden" name="redir" value="{{redir}}">
<ul>
{% for key, value in userinfo.items() %}
<dt>{{ key }}</dt>
<dd>{{ value }}</dd>
<input type="hidden" name="{{key}}" value="{{value}}">
{% endfor %}
</ul>
<button type="submit">OK</button>
</form>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment