proxadmin/app/main/routes.py
2018-01-09 13:05:45 +02:00

118 lines
4.8 KiB
Python

from flask import render_template, abort, redirect, url_for, abort, flash, request, current_app, make_response, g
from flask_login import login_required, login_user, logout_user, current_user
from flask_sqlalchemy import get_debug_queries
from . import main
from .. import db
from ..email import send_email
from ..models import User, Permission, Server, Deployment, Service, Region, Address, Bridge, Router, Domain, contact_proxmaster
import base64
@main.after_app_request
def after_request(response):
for query in get_debug_queries():
if query.duration >= current_app.config['SLOW_DB_QUERY_TIME']:
current_app.logger.warning('Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n' % (query.statement, query.parameters, query.duration, query.context))
return response
#STATIC PAGES
@main.route("/", methods=['GET'])
def index():
allservers = Server.query.all()
return render_template('main/index.html', servers=allservers)
@main.route("/chat", methods=['GET'])
def chat():
return render_template('main/livechat.html')
#@main.route("/aboutus", methods=['GET'])
#def about():
# return render_template('main/aboutus.html')
@main.route("/terms", methods=['GET'])
def terms():
return render_template('main/terms.html')
#APP STORE
@main.route('/market/<int:group_id>', methods=['GET'])
@login_required
def market(group_id=0):
page = { 'title': 'Market' }
allproducts = Product.get_products()
allgroups = current_app.config['GROUPS']
if group_id == 0:
return render_template('main/market.html', groups=allgroups, products=allproducts)
filtered_products = {}
for key, value in allproducts.items():
if value['group'] == group_id:
filtered_products[key] = value
if filtered_products == {}:
abort(404)
return render_template('main/marketgroup.html', groupname=allgroups[group_id], products=filtered_products)
#DASHBOARD
@main.route("/dashboard", methods=['GET', 'POST'])
@login_required
def dashboard():
sys_regions = Region.query.all()
cuser = current_user
inv_bridges = cuser.inv_bridges.order_by(Bridge.bridge_id.asc()).all()
inv_addresses = cuser.inv_addresses.order_by(Address.ip.asc()).all()
inv_routers = cuser.inv_routers.order_by(Router.date_created.asc()).all()
inv_deployments = cuser.inv_deployments.filter_by(deleted=False).order_by(Deployment.date_created.desc()).all()
inv_legacy = cuser.inv_deployments.filter_by(deleted=False).filter_by(bridge_id=None).order_by(Deployment.date_created.desc()).all()
regions = {}
for region in sys_regions:
regions[region.pid] = region.description
inv_deploycubeids = []
for invcls in inv_deployments:
if invcls.user_id == cuser.pid:
inv_deploycubeids.extend([invcls.machine_id])
for invcls in inv_legacy:
if invcls.user_id == cuser.pid:
inv_deploycubeids.extend([invcls.machine_id])
inv_services = cuser.inv_services.filter_by(deleted=False).order_by(Service.date_last_charge.asc()).all()
inv_domains = cuser.inv_domains.filter_by(deleted=False).order_by(Domain.date_created.desc()).all()
#extract rrd and status from the deployments
rrd = {}
statuses = {}
#current_app.logger.warning(str(inv_deploycubeids))
for unit_id in inv_deploycubeids:
rrd[unit_id] = {}
data = { 'unit_id': int(unit_id),
'type': 'kvm' }
try:
query = contact_proxmaster(data, 'vmrrd')
except Exception as e:
current_app.logger.error(e)
flash('Support is notified.'.format(str(unit_id)))
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(unit_id),
'vmanager/email/adm_unreachable', user=current_user, unit_id=unit_id, error=str(e))
#current_app.logger.info('debug query:')
#current_app.logger.info(query)
graphs_list = ['net', 'cpu', 'mem', 'hdd']
try:
for graph in graphs_list:
raw = query[graph]['image'].encode('raw_unicode_escape')
rrd[unit_id][graph] = base64.b64encode(raw).decode()
status = { unit_id : query['status'] }
statuses.update(status)
except Exception as e:
current_app.logger.error(e)
flash('Support is notified.'.format(str(unit_id)))
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(unit_id),
'vmanager/email/adm_unreachable', user=current_user, unit_id=unit_id, error=str(e))
return render_template('main/dashboard.html', sys_regions=sys_regions, inv_bridges=inv_bridges, inv_deployments=inv_deployments, inv_legacy=inv_legacy, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, rrd=rrd, status=statuses, regions=regions)