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
|
2017-10-15 07:38:38 -04:00
|
|
|
from flask_login import fresh_login_required, login_user, logout_user
|
2017-06-04 10:10:38 -04:00
|
|
|
from flask_sqlalchemy import get_debug_queries
|
|
|
|
|
|
|
|
from . import admin
|
2017-07-24 06:33:33 -04:00
|
|
|
from .forms import ChargeForm, Addr2PoolForm
|
2017-06-04 11:22:59 -04:00
|
|
|
|
2017-06-04 10:10:38 -04:00
|
|
|
from .. import db
|
|
|
|
from ..email import send_email
|
2018-01-24 19:00:06 -05:00
|
|
|
from ..models import User, Transaction, Server, 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'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-06-04 10:10:38 -04:00
|
|
|
@admin_required
|
2017-06-13 19:24:39 -04:00
|
|
|
def index():
|
|
|
|
return redirect(url_for('admin.list_users'))
|
|
|
|
|
2017-07-24 08:16:47 -04:00
|
|
|
|
|
|
|
@admin.route("/listdeployments", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-06-13 19:24:39 -04:00
|
|
|
@admin_required
|
2017-07-24 08:16:47 -04:00
|
|
|
def list_deployments():
|
2017-10-07 11:39:50 -04:00
|
|
|
alldeployments = Deployment.query.filter_by(deleted=False).order_by(Deployment.daysleft.asc()).all()
|
2017-07-24 08:16:47 -04:00
|
|
|
return render_template('admin/list_deployments.html', deployments=alldeployments)
|
|
|
|
|
|
|
|
@admin.route("/listservices", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-07-24 08:16:47 -04:00
|
|
|
@admin_required
|
|
|
|
def list_services():
|
2017-10-07 11:39:50 -04:00
|
|
|
allservices = Service.query.filter_by(deleted=False).order_by(Service.daysleft.asc()).all()
|
2017-07-24 08:16:47 -04:00
|
|
|
return render_template('admin/list_services.html', services=allservices)
|
|
|
|
|
|
|
|
@admin.route("/listdomains", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-07-24 08:16:47 -04:00
|
|
|
@admin_required
|
|
|
|
def list_domains():
|
2017-10-07 11:39:50 -04:00
|
|
|
alldomains = Domain.query.filter_by(deleted=False).order_by(Domain.daysleft.asc()).all()
|
2017-07-24 08:16:47 -04:00
|
|
|
return render_template('admin/list_domains.html', domains=alldomains)
|
|
|
|
|
2017-09-24 13:24:11 -04:00
|
|
|
@admin.route("/listrecyclebin", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-08-01 07:21:22 -04:00
|
|
|
@admin_required
|
2017-09-24 13:24:11 -04:00
|
|
|
def list_recyclebin():
|
|
|
|
deployments = Deployment.query.filter_by(protected=False).order_by(Deployment.daysleft.asc()).all()
|
2017-10-07 11:39:50 -04:00
|
|
|
services = Service.query.filter_by(deleted=True).all()
|
|
|
|
domains = Domain.query.filter_by(deleted=True).all()
|
2017-09-24 13:24:11 -04:00
|
|
|
return render_template('admin/list_recyclebin.html', deployments=deployments, services=services, domains=domains)
|
2017-08-01 07:21:22 -04:00
|
|
|
|
2018-01-09 06:05:45 -05:00
|
|
|
@admin.route("/listusers", methods=['GET'])
|
|
|
|
@fresh_login_required
|
|
|
|
@admin_required
|
|
|
|
def list_users():
|
|
|
|
allusers = User.query.filter_by(active=True).order_by(User.last_seen.desc()).all()
|
|
|
|
return render_template('admin/list_users.html', users=allusers)
|
|
|
|
|
|
|
|
@admin.route("/charge/<int:user_pid>", methods=['GET', 'POST'])
|
|
|
|
@fresh_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():
|
|
|
|
transaction = Transaction(user_id=int(cuser.pid), description='Account charged by staff', value=float(form.amount.data))
|
|
|
|
db.session.add(transaction)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
cuser.wallet += float(form.amount.data)
|
|
|
|
db.session.add(cuser)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('admin.list_users'))
|
|
|
|
return render_template('admin/charge.html', form=form, usr=cuser)
|
|
|
|
|
2017-07-24 08:16:47 -04:00
|
|
|
@admin.route("/listaddresses", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-07-24 08:16:47 -04:00
|
|
|
@admin_required
|
|
|
|
def list_addresses():
|
2017-08-01 07:27:32 -04:00
|
|
|
alladdresses = Address.query.order_by(Address.ip.asc()).all()
|
2018-01-24 19:00:06 -05:00
|
|
|
return render_template('admin/list_addresses.html', addresses=alladdresses)
|
2017-07-24 08:16:47 -04:00
|
|
|
|
|
|
|
@admin.route("/addr2pool", methods=['GET', 'POST'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-07-24 08:16:47 -04:00
|
|
|
@admin_required
|
|
|
|
def addr2pool():
|
|
|
|
alladdrlist = []
|
|
|
|
alladdr = Address.query.all()
|
|
|
|
for addr in alladdr:
|
|
|
|
alladdrlist.append(str(addr.ip))
|
|
|
|
current_app.logger.info('Current IP pool: {}'.format(alladdrlist))
|
|
|
|
form = Addr2PoolForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if form.ip.data in alladdrlist:
|
|
|
|
flash('IP address {} is already in the pool!'.format(form.ip.data))
|
|
|
|
return redirect(url_for('admin.addr2pool'))
|
|
|
|
address = Address(ip=form.ip.data, region_id=form.region.data, enabled=True, reserved=form.reserved.data)
|
|
|
|
db.session.add(address)
|
|
|
|
db.session.commit()
|
|
|
|
flash('Address {} added to region {}'.format(form.ip.data, form.region.data))
|
|
|
|
return redirect(url_for('admin.list_addresses'))
|
|
|
|
return render_template('admin/addr2pool.html', form=form, alladdresses=alladdrlist)
|
2017-06-06 09:49:32 -04:00
|
|
|
|
2018-01-09 06:05:45 -05:00
|
|
|
@admin.route("/listservers", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-06-06 09:49:32 -04:00
|
|
|
@admin_required
|
2018-01-09 06:05:45 -05:00
|
|
|
def list_servers():
|
|
|
|
allservers = Server.query.all()
|
|
|
|
return render_template('admin/list_servers.html', servers=allservers)
|
2017-06-04 11:22:59 -04:00
|
|
|
|
2017-07-24 08:16:47 -04:00
|
|
|
@admin.route("/listtransactions", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-07-24 06:33:33 -04:00
|
|
|
@admin_required
|
2017-07-24 08:16:47 -04:00
|
|
|
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'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-07-24 08:16:47 -04:00
|
|
|
@admin_required
|
|
|
|
def transaction(user_pid=0):
|
|
|
|
cuser = User.query.filter_by(pid=user_pid).first()
|
2018-01-24 03:58:28 -05:00
|
|
|
transactions = cuser.inv_transactions.order_by(Transaction.date_created.desc()).limit(20)
|
2017-07-24 08:16:47 -04:00
|
|
|
|
|
|
|
labelslist = ['today']
|
|
|
|
translist = [cuser.wallet]
|
|
|
|
prevvalue = cuser.wallet
|
|
|
|
for tr in transactions:
|
|
|
|
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)
|
|
|
|
|
|
|
|
#current_app.logger.info('[{}] transactions: {} {} '.format(cuser.email, translist, labelslist))
|
|
|
|
return render_template('uinvoice/transactions.html', transactions=transactions, translist=translist, labelslist=labelslist, cuser=cuser)
|
2017-06-10 23:26:10 -04:00
|
|
|
|
|
|
|
@admin.route("/dashboard/<int:user_pid>", methods=['GET'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@fresh_login_required
|
2017-06-04 10:10:38 -04:00
|
|
|
@admin_required
|
|
|
|
def dashboard(user_pid=0):
|
|
|
|
cuser = User.query.filter_by(pid=user_pid).first()
|
2017-06-28 10:42:21 -04:00
|
|
|
|
2017-10-07 11:39:50 -04:00
|
|
|
inv_deployments = cuser.inv_deployments.filter_by(deleted=False).order_by(Deployment.date_created.desc()).all()
|
2017-06-04 10:10:38 -04:00
|
|
|
inv_deploycubeids = []
|
2017-06-26 10:31:30 -04:00
|
|
|
inv_deployments_list = []
|
2017-06-28 10:42:21 -04:00
|
|
|
for invcls in inv_deployments:
|
2017-12-14 18:00:02 -05:00
|
|
|
inv_deploycubeids.extend([invcls.machine_id])
|
|
|
|
inv_deployments_list.extend([invcls.machine_alias])
|
2017-06-28 10:42:21 -04:00
|
|
|
|
2017-10-07 11:39:50 -04:00
|
|
|
inv_services = cuser.inv_services.filter_by(deleted=False).order_by(Service.date_last_charge.asc()).all()
|
2017-06-28 10:42:21 -04:00
|
|
|
inv_services_list = []
|
|
|
|
for invcls in inv_services:
|
2017-12-14 18:00:02 -05:00
|
|
|
inv_services_list.extend([invcls.description])
|
2017-06-28 10:42:21 -04:00
|
|
|
|
2017-10-07 11:39:50 -04:00
|
|
|
inv_domains = cuser.inv_domains.filter_by(deleted=False).order_by(Domain.date_created.desc()).all()
|
2017-06-26 10:31:30 -04:00
|
|
|
inv_domains_list = []
|
|
|
|
for invcls in inv_domains:
|
2017-12-14 18:00:02 -05:00
|
|
|
inv_domains_list.extend([invcls.fqdn])
|
2017-06-04 10:10:38 -04:00
|
|
|
|
2017-06-28 10:42:21 -04:00
|
|
|
inv_addresses = cuser.inv_addresses.order_by(Address.ip.asc()).all()
|
2017-06-26 10:31:30 -04:00
|
|
|
inv_addresses_list = []
|
|
|
|
for invcls in inv_addresses:
|
2017-12-14 18:00:02 -05:00
|
|
|
inv_addresses_list.extend([invcls.ip])
|
|
|
|
|
2017-06-28 10:42:21 -04:00
|
|
|
sys_regions = Region.query.all()
|
|
|
|
regions = {}
|
|
|
|
for region in sys_regions:
|
|
|
|
regions[region.pid] = region.description
|
|
|
|
|
2017-06-04 10:10:38 -04:00
|
|
|
#extract rrd and status from the deployments
|
|
|
|
rrd = {}
|
|
|
|
statuses = {}
|
2017-10-19 11:59:17 -04:00
|
|
|
for unit_id in inv_deploycubeids:
|
|
|
|
rrd[unit_id] = {}
|
|
|
|
data = { 'unit_id': int(unit_id),
|
2017-10-29 17:06:54 -04:00
|
|
|
'type': 'kvm' }
|
2017-10-19 11:59:17 -04:00
|
|
|
query = contact_proxmaster(data, 'vmrrd')
|
|
|
|
if query['status'] == 'UNREACHABLE':
|
|
|
|
flash('Deploy #{} is unreachable. Support is notified.'.format(str(unit_id)))
|
|
|
|
send_email(current_app.config['MAIL_USERNAME'], 'Deploy {} is unreachable'.format(unit_id),
|
|
|
|
'vmanager/email/adm_unreachable', user=cuser, unit_id=unit_id )
|
|
|
|
else:
|
|
|
|
graphs_list = ['net', 'cpu', 'mem', 'hdd']
|
2017-06-04 10:10:38 -04:00
|
|
|
for graph in graphs_list:
|
|
|
|
raw = query[graph]['image'].encode('raw_unicode_escape')
|
2017-10-19 11:59:17 -04:00
|
|
|
rrd[unit_id][graph] = base64.b64encode(raw).decode()
|
|
|
|
status = { unit_id : query['status'] }
|
2017-06-04 10:10:38 -04:00
|
|
|
statuses.update(status)
|
2018-01-24 19:00:06 -05:00
|
|
|
current_app.logger.info('[{}] Enabled deployments: {}, services: {}, domains: {}, addresses: {}'.format(cuser.email, inv_deployments_list, inv_services_list, inv_domains_list, inv_addresses_list))
|
|
|
|
return render_template('panel/dashboard.html', rrd=rrd, status=statuses, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, regions=regions)
|
2017-06-04 10:10:38 -04:00
|
|
|
|
|
|
|
|