2017-06-04 10:10:38 -04:00
|
|
|
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
|
|
|
|
from flask_sqlalchemy import get_debug_queries
|
|
|
|
|
|
|
|
from . import admin
|
2017-06-04 11:22:59 -04:00
|
|
|
from .forms import ChargeForm
|
|
|
|
|
2017-06-04 10:10:38 -04:00
|
|
|
from .. import db
|
|
|
|
from ..email import send_email
|
2017-06-10 23:26:10 -04:00
|
|
|
from ..models import User, Transaction, Deployment, Service, Region, Address, Domain, contact_proxmaster
|
2017-06-04 10:10:38 -04:00
|
|
|
from ..decorators import admin_required, permission_required
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import string
|
|
|
|
import random
|
|
|
|
from datetime import datetime, timedelta, date, time
|
|
|
|
import ast
|
|
|
|
|
|
|
|
#@admin.before_app_request
|
|
|
|
#def before_request():
|
|
|
|
# g.user = current_user
|
|
|
|
# print('current_user: %s, g.user: %s, leaving bef_req' % (current_user, g.user))
|
|
|
|
|
|
|
|
@admin.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
|
|
|
|
|
|
|
|
@admin.route("/", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
2017-06-13 19:24:39 -04:00
|
|
|
def index():
|
|
|
|
return redirect(url_for('admin.list_users'))
|
|
|
|
|
|
|
|
@admin.route("/listitems", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
2017-06-10 23:26:10 -04:00
|
|
|
def list_items():
|
2017-06-07 11:04:27 -04:00
|
|
|
alldeployments = Deployment.query.order_by(Deployment.user_id.desc()).all()
|
|
|
|
alldomains = Domain.query.order_by(Domain.user_id.desc()).all()
|
2017-06-10 23:26:10 -04:00
|
|
|
allservices = Service.query.order_by(Service.date_last_charge.asc()).all()
|
2017-06-07 11:04:27 -04:00
|
|
|
alladdresses = Address.query.order_by(Address.user_id.asc()).all()
|
2017-06-10 23:26:10 -04:00
|
|
|
return render_template('admin/list_items.html', deployments=alldeployments, domains=alldomains, services=allservices, addresses=alladdresses)
|
2017-06-06 09:49:32 -04:00
|
|
|
|
2017-06-10 23:26:10 -04:00
|
|
|
@admin.route("/listusers", methods=['GET'])
|
2017-06-06 09:49:32 -04:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2017-06-10 23:26:10 -04:00
|
|
|
def list_users():
|
2017-06-11 22:12:27 -04:00
|
|
|
allusers = User.query.order_by(User.last_seen.desc()).all()
|
2017-06-10 23:26:10 -04:00
|
|
|
return render_template('admin/list_users.html', users=allusers)
|
2017-06-04 10:10:38 -04:00
|
|
|
|
2017-06-04 11:22:59 -04:00
|
|
|
@admin.route("/charge/<int:user_pid>", methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def charge(user_pid=0):
|
|
|
|
cuser = User.query.filter_by(pid=user_pid).first()
|
|
|
|
form = ChargeForm()
|
|
|
|
if form.validate_on_submit():
|
2017-06-10 23:26:10 -04:00
|
|
|
transaction = Transaction(user_id=int(cuser.pid), description='Account charged by staff', value=float(form.amount.data))
|
|
|
|
db.session.add(transaction)
|
|
|
|
db.session.commit()
|
|
|
|
|
2017-06-06 09:49:32 -04:00
|
|
|
cuser.wallet += float(form.amount.data)
|
2017-06-04 11:22:59 -04:00
|
|
|
db.session.add(cuser)
|
|
|
|
db.session.commit()
|
2017-06-10 23:26:10 -04:00
|
|
|
return redirect(url_for('admin.list_users'))
|
2017-06-04 11:22:59 -04:00
|
|
|
return render_template('admin/charge.html', form=form, usr=cuser)
|
|
|
|
|
2017-06-10 23:26:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
@admin.route("/dashboard/<int:user_pid>", methods=['GET'])
|
2017-06-04 10:10:38 -04:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def dashboard(user_pid=0):
|
|
|
|
cuser = User.query.filter_by(pid=user_pid).first()
|
|
|
|
|
|
|
|
deployments = cuser.inv_deployments.order_by(Deployment.date_created.desc()).all()
|
|
|
|
inv_deploycubeids = []
|
|
|
|
inv_deploynames = []
|
|
|
|
for invcls in deployments:
|
|
|
|
if invcls.user_id == cuser.pid and invcls.enabled == True:
|
|
|
|
inv_deploycubeids.extend([invcls.machine_id])
|
|
|
|
inv_deploynames.extend([invcls.machine_alias])
|
|
|
|
|
|
|
|
services = cuser.inv_services.order_by(Service.date_created.desc()).all()
|
|
|
|
inv_services = []
|
|
|
|
for invcls in services:
|
|
|
|
if invcls.user_id == cuser.pid and invcls.enabled == True:
|
|
|
|
inv_services.extend([invcls.description])
|
|
|
|
|
|
|
|
domains = cuser.inv_domains.order_by(Domain.date_created.desc()).all()
|
|
|
|
inv_domains = []
|
|
|
|
for invcls in domains:
|
|
|
|
if invcls.user_id == cuser.pid and invcls.enabled == True:
|
|
|
|
inv_domains.extend([invcls.fqdn])
|
|
|
|
|
2017-06-07 11:04:27 -04:00
|
|
|
addresses = cuser.inv_addresses.order_by(Address.ip.asc()).all()
|
2017-06-04 10:10:38 -04:00
|
|
|
inv_addresses = []
|
|
|
|
for invcls in addresses:
|
|
|
|
if invcls.user_id == cuser.pid and invcls.enabled == True:
|
|
|
|
inv_addresses.extend([invcls.ip])
|
|
|
|
|
|
|
|
#extract rrd and status from the deployments
|
|
|
|
rrd = {}
|
|
|
|
statuses = {}
|
|
|
|
for cubeid in inv_deploycubeids:
|
|
|
|
rrd[cubeid] = {}
|
|
|
|
try:
|
|
|
|
query = contact_proxmaster({}, 'vmrrd', cubeid)
|
|
|
|
except:
|
|
|
|
flash('Deploy #{} unreachable.'.format(str(cubeid)))
|
|
|
|
|
|
|
|
graphs_list = ['net', 'cpu', 'mem', 'hdd']
|
|
|
|
try:
|
|
|
|
for graph in graphs_list:
|
|
|
|
raw = query[graph]['image'].encode('raw_unicode_escape')
|
|
|
|
rrd[cubeid][graph] = base64.b64encode(raw).decode()
|
|
|
|
status = { cubeid : query['status'] }
|
|
|
|
statuses.update(status)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
flash('Deploy #{} unreachable. Support is notified'.format(str(cubeid)))
|
|
|
|
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(cubeid),
|
|
|
|
'vmanager/email/adm_unreachable', user=cuser, cubeid=cubeid )
|
|
|
|
|
2017-06-10 23:26:10 -04:00
|
|
|
current_app.logger.info('[{}] deployments: {}, services: {}, domains: {}, services: {}'.format(cuser.email, inv_deploynames, inv_services, inv_domains, inv_addresses ))
|
2017-06-07 11:04:27 -04:00
|
|
|
return render_template('vmanager/dashboard.html', rrd=rrd, status=statuses, inv_deployments=deployments, inv_services=services, inv_domains=domains, inv_addresses=addresses)
|
2017-06-04 10:10:38 -04:00
|
|
|
|
2017-06-10 23:26:10 -04:00
|
|
|
@admin.route("/listtransactions", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def list_transactions():
|
|
|
|
alltransactions = Transaction.query.order_by(Transaction.date_created.desc()).all()
|
|
|
|
return render_template('admin/list_transactions.html', transactions=alltransactions)
|
|
|
|
|
|
|
|
@admin.route("/transaction/<int:user_pid>", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def transaction(user_pid=0):
|
|
|
|
cuser = User.query.filter_by(pid=user_pid).first()
|
|
|
|
transactions = cuser.inv_transactions.order_by(Transaction.date_created.desc()).all()
|
2017-06-13 19:24:39 -04:00
|
|
|
|
|
|
|
labelslist = ['today']
|
|
|
|
translist = [cuser.wallet]
|
|
|
|
prevvalue = cuser.wallet
|
2017-06-10 23:26:10 -04:00
|
|
|
for tr in transactions:
|
2017-06-13 19:24:39 -04:00
|
|
|
labelslist.insert(0, str(tr.date_created.strftime('%d.%m')))
|
|
|
|
translist.insert(0, prevvalue - tr.value)
|
|
|
|
prevvalue -= tr.value
|
|
|
|
|
|
|
|
if len(labelslist) <= 1:
|
|
|
|
labelslist.insert(0, 'before')
|
|
|
|
translist.insert(0, 0)
|
2017-06-10 23:26:10 -04:00
|
|
|
|
2017-06-13 19:24:39 -04:00
|
|
|
#current_app.logger.info('[{}] transactions: {} {} '.format(cuser.email, translist, labelslist))
|
|
|
|
return render_template('uinvoice/transactions.html', transactions=transactions, translist=translist, labelslist=labelslist)
|
2017-06-04 10:10:38 -04:00
|
|
|
|