dashboard to main, autodisable and autowarn scripts, new model

This commit is contained in:
deflax 2017-07-11 19:20:44 +03:00
parent 4ae398a499
commit 34f8693c17
10 changed files with 232 additions and 163 deletions

View file

@ -38,9 +38,9 @@ def index():
@login_required
@admin_required
def list_items():
alldeployments = Deployment.query.order_by(Deployment.date_last_charge.asc()).all()
alldomains = Domain.query.order_by(Domain.date_expire.asc()).all()
allservices = Service.query.order_by(Service.date_last_charge.asc()).all()
alldeployments = Deployment.query.order_by(Deployment.daysleft.asc()).all()
alldomains = Domain.query.order_by(Domain.daysleft.asc()).all()
allservices = Service.query.order_by(Service.daysleft.asc()).all()
alladdresses = Address.query.order_by(Address.user_id.asc()).all()
return render_template('admin/list_items.html', deployments=alldeployments, domains=alldomains, services=allservices, addresses=alladdresses)
@ -130,7 +130,7 @@ def dashboard(user_pid=0):
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(cubeid),
'vmanager/email/adm_unreachable', user=cuser, cubeid=cubeid )
current_app.logger.info('[ADMIN] {} deployments: {}, services: {}, domains: {}, services: {}'.format(cuser.email, inv_deployments_list, inv_services_list, inv_domains_list, inv_addresses_list ))
return render_template('vmanager/dashboard.html', rrd=rrd, status=statuses, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, region=regions)
return render_template('main/dashboard.html', rrd=rrd, status=statuses, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, region=regions)
@admin.route("/listtransactions", methods=['GET'])
@login_required

View file

@ -71,7 +71,7 @@ def login():
db.session.commit()
#send_email(current_app.config['MAIL_USERNAME'], user.email + ' logged in.', 'auth/email/adm_loginnotify', user=user, ipaddr=lastip )
flash('Last Login: {} from {}'.format(user.last_seen.strftime("%a %d %B %Y %H:%M"), previp))
return redirect(request.args.get('next') or url_for('vmanager.dashboard'))
return redirect(request.args.get('next') or url_for('main.dashboard'))
else:
flash('Invalid username or password.')
@ -103,7 +103,7 @@ def twofactor():
db.session.add(user)
db.session.commit()
#send_email(current_app.config['MAIL_USERNAME'], user.email + ' logged in.', 'auth/email/adm_loginnotify', user=user, ipaddr=lastip )
return redirect(request.args.get('next') or url_for('vmanager.dashboard'))
return redirect(request.args.get('next') or url_for('main.dashboard'))
else:
flash('Invalid token.')
return render_template('auth/2fa.html', page=page, form=form)
@ -146,9 +146,9 @@ def register():
user = User(email=form.email.data, password=form.password.data, wallet=current_app.config['REGISTER_BONUS'])
db.session.add(user)
db.session.commit()
transaction = Transaction(user_id=int(user.pid), description='Registered account bonus', value=current_app.config['REGISTER_BONUS'])
db.session.add(transaction)
db.session.commit()
#transaction = Transaction(user_id=int(user.pid), description='Registered account bonus', value=current_app.config['REGISTER_BONUS'])
#db.session.add(transaction)
#db.session.commit()
token = user.generate_confirmation_token()
send_email(user.email, 'Потвърдете Вашата регистрация', 'auth/email/confirm', user=user, token=token)
#notify admin

View file

@ -1,6 +1,18 @@
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, Deployment, Service, Region, Address, Domain
@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'])
@ -18,3 +30,69 @@ def chat():
@main.route("/terms", methods=['GET'])
def terms():
return render_template('main/terms.html')
#DASHBOARD
@main.route("/dashboard", methods=['GET', 'POST'])
@login_required
def dashboard():
cuser = current_user
inv_deployments = cuser.inv_deployments.order_by(Deployment.date_created.desc()).all()
inv_deploycubeids = []
inv_deployments_list = []
for invcls in inv_deployments:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_deploycubeids.extend([invcls.machine_id])
inv_deployments_list.extend([invcls.machine_alias])
inv_services = cuser.inv_services.order_by(Service.date_last_charge.asc()).all()
inv_services_list = []
for invcls in inv_services:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_services_list.extend([invcls.description])
inv_domains = cuser.inv_domains.order_by(Domain.date_created.desc()).all()
inv_domains_list = []
for invcls in inv_domains:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_domains_list.extend([invcls.fqdn])
inv_addresses = cuser.inv_addresses.order_by(Address.ip.asc()).all()
inv_addresses_list = []
for invcls in inv_addresses:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_addresses_list.extend([invcls.ip])
sys_regions = Region.query.all()
regions = {}
for region in sys_regions:
regions[region.pid] = region.description
#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. Support is notified'.format(str(cubeid)))
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(cubeid),
'vmanager/email/adm_unreachable', user=current_user, cubeid=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:
current_app.logger.error(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=current_user, cubeid=cubeid )
current_app.logger.info('[{}] Enabled deployments: {}, services: {}, domains: {}, addresses: {}'.format(current_user.email, inv_deployments_list, inv_services_list, inv_domains_list, inv_addresses_list ))
return render_template('main/dashboard.html', rrd=rrd, status=statuses, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, region=regions)

View file

@ -24,7 +24,7 @@ class Permission:
class Role(db.Model):
__tablename__ = 'roles'
pid = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
name = db.Column(db.String, unique=True)
default = db.Column(db.Boolean, default=False, index=True)
permissions = db.Column(db.Integer)
users = db.relationship('User', backref='role', lazy='dynamic')
@ -52,37 +52,36 @@ class User(db.Model, UserMixin):
__tablename__ = 'users'
pid = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(64), unique=True, index=True)
email = db.Column(db.String, unique=True, index=True)
role_id = db.Column(db.Integer, db.ForeignKey('roles.pid')) #FK
password_hash = db.Column(db.String(128))
password_hash = db.Column(db.String)
tokens = db.Column(db.Text)
confirmed = db.Column(db.Boolean, default=False)
active = db.Column(db.Boolean, default=True)
member_since = db.Column(db.DateTime(), default=datetime.utcnow)
last_seen = db.Column(db.DateTime(), default=datetime.utcnow)
last_ip = db.Column(db.String(128))
last_ip = db.Column(db.String)
twofactor = db.Column(db.Boolean, default=False) #optional 2factor auth
otp_secret = db.Column(db.String(16))
avatar_hash = db.Column(db.String(32))
otp_secret = db.Column(db.String)
avatar_hash = db.Column(db.String)
name = db.Column(db.Unicode(256))
address = db.Column(db.Unicode(256))
city = db.Column(db.Unicode(64))
postcode = db.Column(db.String(10))
country = db.Column(db.String(64), default='BG')
phone = db.Column(db.String(64))
name = db.Column(db.Unicode)
address = db.Column(db.Unicode)
city = db.Column(db.Unicode)
postcode = db.Column(db.String)
country = db.Column(db.String, default='BG')
phone = db.Column(db.String)
org_account = db.Column(db.Boolean, default=False)
org_companyname = db.Column(db.Unicode(64))
org_regaddress = db.Column(db.Unicode(128))
org_responsible = db.Column(db.Unicode(128))
org_vatnum = db.Column(db.String(16))
org_companyname = db.Column(db.Unicode)
org_regaddress = db.Column(db.Unicode)
org_responsible = db.Column(db.Unicode)
org_vatnum = db.Column(db.String)
group = db.Column(db.String(24), default='User')
language = db.Column(db.String(2), default='BG')
group = db.Column(db.String, default='User')
language = db.Column(db.String, default='BG')
wallet = db.Column(db.Float)
overdraft = db.Column(db.Float)
currency = db.Column(db.String(3), default='BGN')
currency = db.Column(db.String, default='BGN')
inv_deployments = db.relationship('Deployment', backref='owner', lazy='dynamic')
inv_services = db.relationship('Service', backref='owner', lazy='dynamic')
@ -232,10 +231,12 @@ class Service(db.Model):
date_created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
date_last_charge = db.Column(db.DateTime)
period = db.Column(db.Integer)
daysleft = db.Column(db.Integer)
warning = db.Column(db.Boolean, default=False)
enabled = db.Column(db.Boolean, default=False)
category = db.Column(db.String(64))
description = db.Column(db.Unicode(128))
category = db.Column(db.String)
description = db.Column(db.Unicode)
price = db.Column(db.Float)
class Deployment(db.Model):
@ -245,6 +246,8 @@ class Deployment(db.Model):
date_created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
date_last_charge = db.Column(db.DateTime)
period = db.Column(db.Integer)
daysleft = db.Column(db.Integer)
warning = db.Column(db.Boolean, default=False)
enabled = db.Column(db.Boolean, default=False)
machine_id = db.Column(db.BigInteger) #cubeid
@ -260,8 +263,8 @@ class Region(db.Model):
pid = db.Column(db.Integer, primary_key=True)
enabled = db.Column(db.Boolean)
name = db.Column(db.String(64))
description = db.Column(db.String(128))
name = db.Column(db.String)
description = db.Column(db.String)
extraprice = db.Column(db.Float)
class Address(db.Model):
@ -274,9 +277,9 @@ class Address(db.Model):
region_id = db.Column(db.Integer, db.ForeignKey('regions.pid')) #FK
deploy_id = db.Column(db.Integer, db.ForeignKey('deployments.pid')) #FK
ip = db.Column(db.String(64))
mac = db.Column(db.String(128))
rdns = db.Column(db.String(256))
ip = db.Column(db.String)
mac = db.Column(db.String)
rdns = db.Column(db.String)
reserved = db.Column(db.Boolean, default=False) #this ip SHOULD NOT be listed as available to assign even if its not currently owned by anyone
def __init__(self):
@ -301,6 +304,8 @@ class Address(db.Model):
class Domain(db.Model):
__tablename__ = 'domains'
pid = db.Column(db.Integer, primary_key=True)
daysleft = db.Column(db.Integer)
warning = db.Column(db.Boolean, default=False)
enabled = db.Column(db.Boolean, default=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.pid')) #FK
@ -327,7 +332,7 @@ class Invoice(db.Model):
date_created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
currency = db.Column(db.String, default='BGN')
tax = db.Column(db.Float) #VAT
description = db.Column(db.Unicode(255))
description = db.Column(db.Unicode)
def sub_total(self):
items = self.items
@ -352,7 +357,7 @@ class InvoiceItem(db.Model):
pid = db.Column(db.Integer, primary_key=True)
item_number = db.Column(db.Integer)
invoice_id = db.Column(db.Integer, db.ForeignKey('invoice.pid')) #FK
item_title = db.Column(db.Unicode(255))
item_title = db.Column(db.Unicode)
item_quantity = db.Column(db.Float)
item_price = db.Column(db.Float)
amount = db.Column(db.Float)

View file

@ -23,8 +23,8 @@
<th>Mem</th>
<th>HDD</th>
<th>Price</th>
<th>Period</th>
<th>Last Charged</th>
<th>Days Left</th>
</tr>
</thead>
<tbody>
@ -37,8 +37,8 @@
<td>{{ deploy.machine_mem }} MB</td>
<td>{{ deploy.machine_hdd }} GB</td>
<td>{{ deploy.price }}</td>
<td>{{ deploy.period }}</td>
<td>{{ moment(deploy.date_last_charge).format('lll') }} ({{ moment(deploy.date_last_charge).fromNow() }})</td>
<td>{{ deploy.daysleft }}</td>
{% endfor %}
</tbody>
</table>
@ -57,8 +57,8 @@
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Period</th>
<th>Last Charged</th>
<th>Days Left</th>
</tr>
</thead>
<tbody>
@ -68,8 +68,8 @@
<td>{{ service.category }}</td>
<td>{{ service.description }}</td>
<td>{{ service.price }}</td>
<td>{{ service.period }}</td>
<td>{{ moment(service.date_last_charge).format('ll') }} ({{ moment(service.date_last_charge).fromNow() }})</td>
<td>{{ service.daysleft }}</td>
</tr>
{% endfor %}
</tbody>
@ -89,6 +89,7 @@
<th>Owner</th>
<th>Name</th>
<th>Expiry Date</th>
<th>Days Left</th>
</tr>
</thead>
<tbody>
@ -97,6 +98,7 @@
<td><a href="{{ url_for('admin.dashboard', user_pid=domain.user_id) }}">{{ domain.owner.email }}</a></td>
<td><b><a href="http://{{ domain.fqdn }}">{{ domain.fqdn }}</a></b></td>
<td>{{ domain.date_expire }}</td>
<td>{{ domain.daysleft }}</td>
</tr>
{% endfor %}
</tbody>

View file

@ -139,6 +139,7 @@ addEventListener("DOMContentLoaded", function() {
<th>Network</th>
<th>Control</th>
<th></th>
<th>Time Left</th>
</tr>
</thead>
<tbody>
@ -150,10 +151,14 @@ addEventListener("DOMContentLoaded", function() {
<td data-title="Memory">{{ deploy.machine_mem }} MB</td>
<td data-title="Disk">{{ deploy.machine_hdd }} GB</td>
<td data-title="Network">{% for addr in deploy.machine_addresses %} {{ addr.ip }}<br /> {% endfor %}</td>
<td data-title="Control"><button class="btn btn-default btn-success" onclick="window.open('/vmanager/activate/{{ deploy.machine_id }}');"><span class="glyphicon glyphicon-bell" aria-hidden="true"></span> Activate</td>
<td> </td>
<td data-title="Control"> </td>
<td data-title="Time Left"><button class="btn btn-default btn-success" onclick="window.open('/vmanager/activate/{{ deploy.machine_id }}');"><span class="glyphicon glyphicon-bell" aria-hidden="true"></span> Activate</td>
{% else %}
{% if deploy.warning == True %}
<tr class="bg-warning">
{% else %}
<tr>
{% endif %}
<td data-title="Name"><a class="rrd" data-toggle="tooltip" title="ID# {{ deploy.machine_id }}<br />Deployment state: {{ status[deploy.machine_id] }}"><b>{% if status[deploy.machine_id] == 'running' %}<font color="green">{% else %}<font color="red">{% endif %}{{ deploy.machine_alias }}</font></b></a></td>
<td data-title="CPU"><a class="rrd" data-toggle="tooltip" title="<img src='data:image/png;base64,{{ rrd[deploy.machine_id]['cpu'] }}' />">{{ deploy.machine_cpu }} Cores</a></td>
<td data-title="Memory"><a class="rrd" data-toggle="tooltip" title="<img src='data:image/png;base64,{{ rrd[deploy.machine_id]['mem'] }}' />">{{ deploy.machine_mem }} MB</a></td>
@ -167,6 +172,12 @@ addEventListener("DOMContentLoaded", function() {
{% endif %}</td>
<td data-title="Remote">{% if status[deploy.machine_id] == 'running' %}
<button class="btn btn-default btn-info" onclick="window.open('/vmanager/vmvnc/{{ deploy.machine_id }}', '_blank');"><span class="glyphicon glyphicon-console" aria-hidden="true"></span> Console</button>
{% if deploy.warning == True %}
<td data-title="Time Left"><button class="btn btn-default btn-success" onclick="window.open('/vmanager/activate/{{ deploy.machine_id }}');"><span class="glyphicon glyphicon-bell" aria-hidden="true"></span> Activate</button></td>
{% else %}
<td data-title="Time Left">{{ deploy.daysleft }} day(s)</td>
{% endif %}
{% endif %}</td>
</tr>
{% endif %}
@ -174,7 +185,7 @@ addEventListener("DOMContentLoaded", function() {
</tbody>
</table>
</div>
<button class="btn btn-default" onclick="window.open('{{ url_for('vmanager.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Create</button>
<button class="btn btn-default" onclick="window.open('{{ url_for('main.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Create</button>
</div>
</div>
</div>
@ -191,24 +202,30 @@ addEventListener("DOMContentLoaded", function() {
<tr>
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Months</th>
<th>Last Charged</th>
<th>Time Left</th>
</tr>
<tbody>
{% for service in inv_services %}
<tr>
{% if service.enabled == False %}
<tr class="danger">
{% else %}
<tr>
{% endif %}
<td data-title="Category">{{ service.category }}</td>
<td data-title="Description" >{{ service.description }}</td>
<td data-title="Price">{{ service.price }}</td>
<td data-title="Months">{{ service.period }}</td>
<td data-title="Last Charged">{{ moment(service.date_last_charge).format('lll') }} ({{ moment(service.date_last_charge).fromNow() }})</td>
</tr>
{% if service.warning == True %}
<td data-title="Time Left"><button class="btn btn-default btn-success" onclick="window.open('/smanager/activate/{{ service.pid }}');"><span class="glyphicon glyphicon-bell" aria-hidden="true"></span> Renew</button></td>
{% else %}
<td data-title="Time Left">{{ service.daysleft }} day(s)</td>
{% endif %}
</tr >
{% endfor %}
</tbody>
</table>
</div>
<button class="btn btn-default" onclick="window.open('{{ url_for('vmanager.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Order</button>
<button class="btn btn-default" onclick="window.open('{{ url_for('main.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Order</button>
</div>
</div>
</div>
@ -225,20 +242,25 @@ addEventListener("DOMContentLoaded", function() {
<tr>
<th>Name</th>
<th>Expiry Date</th>
<th>Control</th>
<th>Time Left</th>
</tr>
<tbody>
{% for domain in inv_domains %}
<tr>
<td data-title="Domain"><b><a href="http://{{ domain.fqdn }}">{{ domain.fqdn }}</a></b></td>
<td data-title="Expiry Date">{{ domain.date_expire }}</td>
<td data-title="Control">soon...</td>
{% if domain.warning == True %}
<td data-title="Time Left"><button class="btn btn-default btn-success" onclick="window.open('/dmanager/activate/{{ domain.pid }}');"><span class="glyphicon glyphicon-bell" aria-hidden="true"></span> Renew</button></td>
{% else %}
<td data-title="Time Left">{{ domain.daysleft }} day(s)</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
<button class="btn btn-default" onclick="window.open('{{ url_for('vmanager.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Order</button>
<button class="btn btn-default" onclick="window.open('{{ url_for('main.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Order</button>
</div>
</div>
</div>
@ -257,7 +279,6 @@ addEventListener("DOMContentLoaded", function() {
<th>Region</th>
<th>MAC Addr.</th>
<th>Reverse DNS</th>
<th>Control</th>
</tr>
<tbody>
{% for address in inv_addresses %}
@ -266,13 +287,12 @@ addEventListener("DOMContentLoaded", function() {
<td data-title="Region">{{ region[address.region_id] }}</td>
<td data-title="MAC">{{ address.mac }}</td>
<td data-title="RDNS">{{ address.rdns }}</td>
<td data-title="Control">soon...</td>
{% endfor %}
</tr>
</tbody>
</table>
</div>
<!--<button class="btn btn-default" onclick="window.open('{{ url_for('vmanager.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Assign</button> -->
<!--<button class="btn btn-default" onclick="window.open('{{ url_for('main.dashboard') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Assign</button> -->
</div>
</div>
</div>

View file

@ -11,7 +11,7 @@
{% if not current_user.is_authenticated %}
<a class="navbar-brand" href="{{ url_for('main.index') }}" rel="home"></a>
{% else %}
<a class="navbar-brand" href="{{ url_for('vmanager.dashboard') }}" rel="home"></a>
<a class="navbar-brand" href="{{ url_for('main.dashboard') }}" rel="home"></a>
{% endif %}
</div>
@ -49,7 +49,7 @@
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><img class="avatar" src="{{ current_user.gravatar(20) }}"> {{ current_user.email }} <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="{{ url_for('vmanager.dashboard') }}"><span class="glyphicon glyphicon-modal-window"></span> Dashboard</a></li>
<li><a href="{{ url_for('main.dashboard') }}"><span class="glyphicon glyphicon-modal-window"></span> Dashboard</a></li>
<li><a href="{{ url_for('uinvoice.transactions') }}"><span class="glyphicon glyphicon-list-alt"></span> Transactions</a></li>
<li role="separator" class="divider"></li>
<li><a href="{{ url_for('settings.profile') }}"><span class="glyphicon glyphicon-user"></span> Profile</a></li>

View file

@ -5,7 +5,6 @@
<p><br />
<b>Currency:</b> {{ cuser.currency }}<br />
<b>Wallet:</b> {{ cuser.wallet|round(2) }}<br />
<b>Overdraft:</b> {{ cuser.overdraft }}<br />
</p>
</div>
</div>

View file

@ -102,7 +102,7 @@ def deploy(product_id=None):
query = contact_proxmaster(data, 'vmcreate')
except:
flash('Region unreachable! Please try again later...')
return redirect(url_for('vmanager.dashboard'))
return redirect(url_for('main.dashboard'))
if query is not None:
deployment = Deployment(user_id=client_id, product_id=product_id, machine_alias=form.servername.data, machine_id=query['cube'], machine_cpu=form.cpu.data, machine_mem=form.mem.data, machine_hdd=form.hdd.data, date_expire=(datetime.utcnow() + timedelta(days=30)), enabled=True)
@ -113,76 +113,10 @@ def deploy(product_id=None):
else:
flash('Deploy cancelled! Please try again later...')
return redirect(url_for('vmanager.dashboard'))
return redirect(url_for('main.dashboard'))
return render_template('vmanager/deploy.html', page=page, form=form, product_id=product_id, product_pic=product_pic, product_name=product_name, product_description=product_description, product_recipe=product_recipe)
#COMMAND AND CONTROL
@vmanager.route("/dashboard", methods=['GET', 'POST'])
@login_required
def dashboard():
cuser = current_user
inv_deployments = cuser.inv_deployments.order_by(Deployment.date_created.desc()).all()
inv_deploycubeids = []
inv_deployments_list = []
for invcls in inv_deployments:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_deploycubeids.extend([invcls.machine_id])
inv_deployments_list.extend([invcls.machine_alias])
inv_services = cuser.inv_services.order_by(Service.date_last_charge.asc()).all()
inv_services_list = []
for invcls in inv_services:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_services_list.extend([invcls.description])
inv_domains = cuser.inv_domains.order_by(Domain.date_created.desc()).all()
inv_domains_list = []
for invcls in inv_domains:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_domains_list.extend([invcls.fqdn])
inv_addresses = cuser.inv_addresses.order_by(Address.ip.asc()).all()
inv_addresses_list = []
for invcls in inv_addresses:
if invcls.user_id == cuser.pid and invcls.enabled == True:
inv_addresses_list.extend([invcls.ip])
sys_regions = Region.query.all()
regions = {}
for region in sys_regions:
regions[region.pid] = region.description
#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. Support is notified'.format(str(cubeid)))
send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(cubeid),
'vmanager/email/adm_unreachable', user=current_user, cubeid=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:
current_app.logger.error(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=current_user, cubeid=cubeid )
current_app.logger.info('[{}] Enabled deployments: {}, services: {}, domains: {}, addresses: {}'.format(current_user.email, inv_deployments_list, inv_services_list, inv_domains_list, inv_addresses_list ))
return render_template('vmanager/dashboard.html', rrd=rrd, status=statuses, inv_deployments=inv_deployments, inv_services=inv_services, inv_domains=inv_domains, inv_addresses=inv_addresses, region=regions)
@vmanager.route('/activate/<int:vmid>')
@login_required
def activate(vmid=0):
@ -200,7 +134,6 @@ def activate(vmid=0):
current_app.logger.info('[{}] Disabled deployments: {}'.format(current_user.email, inventory))
abort(403)
@vmanager.route('/<cmd>/<int:vmid>')
@login_required
def command(cmd=None, vmid=0):

View file

@ -37,66 +37,98 @@ def deploy():
Role.insert_roles()
@manager.command
def charge():
def autodisable():
from app.models import User, Deployment, Service, Domain
today = datetime.utcnow()
print('[1] Scan for active expired items and set them as inactive {}\n'.format(today))
print('\nScan for active expired items and set them as inactive. Script started at {}'.format(today))
dep_c = 0
srv_c = 0
dom_c = 0
deployments_ena = Deployment.query.filter_by(enabled=True).all()
for deploy in deployments_ena:
lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
if today > expiry:
print('last charged: ' + lastcharge.strftime('%c') + 'expiry date: ' + expiry.strftime('%c'))
print(deploy.machine_alias + ' is past expiration date and will be marked INACTIVE')
print('Deployment "' + deploy.machine_alias + '" is past expiration date and will be marked INACTIVE. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
deploy.warning = False
deploy.enabled = False
deploy.daysleft = 0
db.session.commit()
else:
dep_c += 1
services_ena = Service.query.filter_by(enabled=True).all()
for service in services_ena:
lastcharge = service.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
if today > expiry:
print('last charged: ' + lastcharge.strftime('%c') + 'expiry date: ' + expiry.strftime('%c'))
print('Service "' + service.description + '" is past expiration date and will be marked INACTIVE')
print('Service "' + service.description + '" is past expiration date and will be marked INACTIVE. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
service.warning = False
service.enabled = False
service.daysleft = 0
db.session.commit()
else:
srv_c += 1
domains_ena = Domain.query.filter_by(enabled=True).all()
for domain in domains_ena:
expiry = domain.date_expire
if today > expiry:
print('last charged: ' + lastcharge.strftime('%c') + 'expiry date: ' + expiry.strftime('%c'))
print('Domain "' + domain.fqdn + '" is past expiration date and will be marked as INACTIVE')
print('Domain "' + domain.fqdn + '" is past expiration date and will be marked as INACTIVE. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
domain.warning = False
domain.enabled = False
domain.daysleft = 0
db.session.commmit()
else:
dom_c += 1
print('Total Active Deployments: {}, Services: {}, Domains: {}'.format(dep_c, srv_c, dom_c))
@manager.command
def autowarn():
from app.models import User, Deployment, Service, Domain, Transaction
today = datetime.utcnow()
print('[2] Scan for items that will expire soon and try to autocharge them if they are not active. {}\n'.format(today))
print('\nScan for items that will expire soon and enable the warning flag. Script started at {}'.format(today))
deployments_ena = Deployment.query.filter_by(enabled=True).all()
for deploy in deployments_ena:
lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
daysleft = relativedelta(today, expiry).days()
warn = deploy.period * 5
print(daysleft)
if daysleft < 10:
print(deploy.machine_alias + ' is eligible for charging')
print('last charged: ' + lastcharge.strftime('%c'))
print('expiry date: ' + expiry.strftime('%c'))
cpu_cost = deploy.machine_cpu * app.config['CPU_RATIO']
mem_cost = ( deploy.machine_mem / 1024 ) * app.config['MEM_RATIO']
hdd_cost = deploy.machine_hdd * app.config['HDD_RATIO']
total = cpu_cost + mem_cost + hdd_cost
print('total: ' + str(total))
daysleft = expiry - today
cpu_cost = deploy.machine_cpu * app.config['CPU_RATIO']
mem_cost = ( deploy.machine_mem / 1024 ) * app.config['MEM_RATIO']
hdd_cost = deploy.machine_hdd * app.config['HDD_RATIO']
total = cpu_cost + mem_cost + hdd_cost
deploy.daysleft = daysleft.days
deploy.price = total
db.session.commit()
warndays = deploy.period * 5
if daysleft.days < warndays:
print('Deployment "' + deploy.machine_alias + '" is ' + str(daysleft.days) + ' days until expiration. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
deploy.warning = True
db.session.commit()
services_ena = Service.query.filter_by(enabled=True).all()
for service in services_ena:
lastcharge = service.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
if today > expiry:
print(service.description + ' is eligible for charging')
print('last charged: ' + lastcharge.strftime('%c'))
print('expiry date: ' + expiry.strftime('%c'))
cost = service.price
print('total: ' + str(cost))
daysleft = expiry - today
service.daysleft = daysleft.days
db.session.commit()
warndays = service.period * 5
if daysleft.days < warndays:
print('Service "' + service.description + '" is ' + str(daysleft.days) + ' days until expiration. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
service.warning = True
db.session.commit()
domains_ena = Domain.query.filter_by(enabled=True).all()
for domain in domains_ena:
expire = domain.date_expire
if expire - today < 60:
daysleft = relativedelta(now, expire).days
print(domain.fqdn + ' is expiring after ' + daysleft + ' for charging')
print('expiry date: ' + expire.strftime('%c'))
expiry = domain.date_expire
daysleft = expiry - today
domain.daysleft = daysleft.days
db.session.commit()
warndays = service.period * 5
if daysleft.days < warndays:
print('Domain "' + domain.fqdn + '" is ' + str(daysleft.days)+ ' days until expiration. Expiry date: ' + expiry.strftime('%c'))
domain.warning = True
db.session.commit()
@manager.command
def runserver():