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 panel from .forms import OrderForm from .. import db from ..email import send_email from ..models import User, Permission, Recipe, Order, Server, Deployment, Service, Region, Address, Domain, contact_proxmaster import base64 @panel.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 @panel.route("/deploy", methods=['GET', 'POST']) @login_required def deploy(): if current_user.name is None: return redirect(url_for('settings.profile')) form = OrderForm() if form.validate_on_submit(): region = Region.query.filter_by(pid=int(form.region.data)).first() recipe = Recipe.query.filter_by(pid=int(form.recipe.data)).first() new_order = Order(user_id=int(current_user.pid), region_id=int(region.pid), recipe_id=int(recipe.pid), parameter1=str(form.alias.data), parameter2=str(form.cpu.data), parameter3=str(form.memory.data), parameter4=str(form.storage.data), status='new') db.session.add(new_order) db.session.commit() send_email(current_app.config['MAIL_USERNAME'], 'New order from {}'.format(current_user.email), 'panel/email/adm_neworder', user=current_user) return redirect(request.args.get('next') or url_for('panel.dashboard')) return render_template('panel/deploy.html', form=form) #DASHBOARD @panel.route("/dashboard", defaults={'user_pid': 0}, methods=['GET']) @panel.route("/dashboard/", methods=['GET']) @login_required def dashboard(user_pid): sys_regions = Region.query.all() if user_pid == 0: cuser = current_user else: cuser = User.query.filter_by(pid=user_pid).first() if cuser == None: abort(404) if not current_user.is_administrator(): abort(404) #hidden 403 inv_addresses = cuser.inv_addresses.order_by(Address.ip.asc()).all() inv_deployments = cuser.inv_deployments.filter_by(deleted=False).order_by(Deployment.date_created.desc()).all() regions = {} for region in sys_regions: regions[region.pid] = region.description inv_deploycubeids = [] warnflag = False for invcls in inv_deployments: if invcls.user_id == cuser.pid: inv_deploycubeids.extend([invcls.machine_id]) #warning detector if invcls.warning == True or invcls.enabled == False: warnflag = True 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: data = { 'unit_id': int(unit_id), 'type': 'kvm' } try: query = contact_proxmaster(data, 'vmrrd') graphs_list = ['net', 'cpu', 'mem', 'hdd'] rrd[unit_id] = {} 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) for invcls in inv_deployments: if invcls.machine_id == unit_id: inv_deployments.remove(invcls) flash('Support is notified about {}.'.format(str(cuser.inv_deployments.filter_by(machine_id=unit_id).first().machine_alias))) if not current_user.is_administrator(): send_email(current_app.config['MAIL_USERNAME'], '{} experienced an error'.format(cuser.email), 'vmanager/email/adm_unreachable', user=current_user, unit_id=unit_id, error=repr(e)) continue return render_template('panel/dashboard.html', sys_regions=sys_regions, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, rrd=rrd, status=statuses, warnflag=warnflag, regions=regions)