2017-05-12 15:10:56 -04:00
|
|
|
from flask import render_template, abort, redirect, url_for, abort, flash, request, current_app, make_response, g
|
2017-07-11 12:20:44 -04:00
|
|
|
from flask_login import login_required, login_user, logout_user, current_user
|
|
|
|
from flask_sqlalchemy import get_debug_queries
|
2017-05-12 15:10:56 -04:00
|
|
|
|
|
|
|
from . import main
|
2017-07-11 12:20:44 -04:00
|
|
|
from .. import db
|
|
|
|
from ..email import send_email
|
2017-12-14 18:00:02 -05:00
|
|
|
from ..models import User, Permission, Deployment, Service, Region, Address, Bridge, Router, Domain, contact_proxmaster
|
2017-07-15 00:28:04 -04:00
|
|
|
|
|
|
|
import base64
|
2017-07-11 12:20:44 -04:00
|
|
|
|
|
|
|
@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
|
2017-05-12 15:10:56 -04:00
|
|
|
|
|
|
|
#STATIC PAGES
|
|
|
|
@main.route("/", methods=['GET'])
|
|
|
|
def index():
|
|
|
|
return render_template('main/index.html')
|
|
|
|
|
|
|
|
@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')
|
2017-07-11 12:20:44 -04:00
|
|
|
|
2017-10-07 11:39:50 -04:00
|
|
|
#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)
|
|
|
|
|
2017-07-11 12:20:44 -04:00
|
|
|
#DASHBOARD
|
|
|
|
@main.route("/dashboard", methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def dashboard():
|
2017-12-17 11:57:28 -05:00
|
|
|
sys_regions = Region.query.all()
|
|
|
|
|
2017-07-11 12:20:44 -04:00
|
|
|
cuser = current_user
|
2017-12-17 11:57:28 -05:00
|
|
|
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()
|
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-12-17 11:57:28 -05:00
|
|
|
|
|
|
|
regions = {}
|
|
|
|
for region in sys_regions:
|
|
|
|
regions[region.pid] = region.description
|
2017-07-11 12:20:44 -04:00
|
|
|
inv_deploycubeids = []
|
|
|
|
for invcls in inv_deployments:
|
2017-09-14 20:28:26 -04:00
|
|
|
if invcls.user_id == cuser.pid:
|
2017-07-11 12:20:44 -04:00
|
|
|
inv_deploycubeids.extend([invcls.machine_id])
|
2017-12-17 11:57:28 -05: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()
|
|
|
|
inv_domains = cuser.inv_domains.filter_by(deleted=False).order_by(Domain.date_created.desc()).all()
|
2017-07-11 12:20:44 -04:00
|
|
|
|
|
|
|
#extract rrd and status from the deployments
|
|
|
|
rrd = {}
|
|
|
|
statuses = {}
|
2017-09-20 20:08:58 -04:00
|
|
|
#current_app.logger.warning(str(inv_deploycubeids))
|
2017-10-29 17:06:54 -04:00
|
|
|
for unit_id in inv_deploycubeids:
|
|
|
|
rrd[unit_id] = {}
|
2017-11-05 15:20:39 -05:00
|
|
|
data = { 'unit_id': int(unit_id),
|
2017-10-29 17:06:54 -04:00
|
|
|
'type': 'kvm' }
|
2017-07-11 12:20:44 -04:00
|
|
|
try:
|
2017-10-19 11:59:17 -04:00
|
|
|
query = contact_proxmaster(data, 'vmrrd')
|
2017-07-30 17:10:08 -04:00
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(e)
|
2017-12-14 18:00:02 -05:00
|
|
|
flash('Support is notified.'.format(str(unit_id)))
|
2017-10-19 11:59:17 -04:00
|
|
|
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(unit_id),
|
2017-12-14 18:00:02 -05:00
|
|
|
'vmanager/email/adm_unreachable', user=current_user, unit_id=unit_id, error=str(e))
|
2017-09-14 20:28:26 -04:00
|
|
|
#current_app.logger.info('debug query:')
|
|
|
|
#current_app.logger.info(query)
|
2017-07-11 12:20:44 -04:00
|
|
|
|
|
|
|
graphs_list = ['net', 'cpu', 'mem', 'hdd']
|
|
|
|
try:
|
|
|
|
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-07-11 12:20:44 -04:00
|
|
|
statuses.update(status)
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(e)
|
2017-12-14 18:00:02 -05:00
|
|
|
flash('Support is notified.'.format(str(unit_id)))
|
2017-10-19 11:59:17 -04:00
|
|
|
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(unit_id),
|
2017-12-14 18:00:02 -05:00
|
|
|
'vmanager/email/adm_unreachable', user=current_user, unit_id=unit_id, error=str(e))
|
2017-12-17 11:57:28 -05:00
|
|
|
|
|
|
|
return render_template('main/dashboard.html', sys_regions=sys_regions, inv_bridges=inv_bridges, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, rrd=rrd, status=statuses)
|
2017-07-11 12:20:44 -04:00
|
|
|
|