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 @login_required
@admin_required @admin_required
def list_items(): def list_items():
alldeployments = Deployment.query.order_by(Deployment.date_last_charge.asc()).all() alldeployments = Deployment.query.order_by(Deployment.daysleft.asc()).all()
alldomains = Domain.query.order_by(Domain.date_expire.asc()).all() alldomains = Domain.query.order_by(Domain.daysleft.asc()).all()
allservices = Service.query.order_by(Service.date_last_charge.asc()).all() allservices = Service.query.order_by(Service.daysleft.asc()).all()
alladdresses = Address.query.order_by(Address.user_id.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) 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), send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(cubeid),
'vmanager/email/adm_unreachable', user=cuser, cubeid=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 )) 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']) @admin.route("/listtransactions", methods=['GET'])
@login_required @login_required

View file

@ -71,7 +71,7 @@ def login():
db.session.commit() db.session.commit()
#send_email(current_app.config['MAIL_USERNAME'], user.email + ' logged in.', 'auth/email/adm_loginnotify', user=user, ipaddr=lastip ) #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)) 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: else:
flash('Invalid username or password.') flash('Invalid username or password.')
@ -103,7 +103,7 @@ def twofactor():
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
#send_email(current_app.config['MAIL_USERNAME'], user.email + ' logged in.', 'auth/email/adm_loginnotify', user=user, ipaddr=lastip ) #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: else:
flash('Invalid token.') flash('Invalid token.')
return render_template('auth/2fa.html', page=page, form=form) 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']) user = User(email=form.email.data, password=form.password.data, wallet=current_app.config['REGISTER_BONUS'])
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
transaction = Transaction(user_id=int(user.pid), description='Registered account bonus', value=current_app.config['REGISTER_BONUS']) #transaction = Transaction(user_id=int(user.pid), description='Registered account bonus', value=current_app.config['REGISTER_BONUS'])
db.session.add(transaction) #db.session.add(transaction)
db.session.commit() #db.session.commit()
token = user.generate_confirmation_token() token = user.generate_confirmation_token()
send_email(user.email, 'Потвърдете Вашата регистрация', 'auth/email/confirm', user=user, token=token) send_email(user.email, 'Потвърдете Вашата регистрация', 'auth/email/confirm', user=user, token=token)
#notify admin #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 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 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 #STATIC PAGES
@main.route("/", methods=['GET']) @main.route("/", methods=['GET'])
@ -18,3 +30,69 @@ def chat():
@main.route("/terms", methods=['GET']) @main.route("/terms", methods=['GET'])
def terms(): def terms():
return render_template('main/terms.html') 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): class Role(db.Model):
__tablename__ = 'roles' __tablename__ = 'roles'
pid = db.Column(db.Integer, primary_key=True) 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) default = db.Column(db.Boolean, default=False, index=True)
permissions = db.Column(db.Integer) permissions = db.Column(db.Integer)
users = db.relationship('User', backref='role', lazy='dynamic') users = db.relationship('User', backref='role', lazy='dynamic')
@ -52,37 +52,36 @@ class User(db.Model, UserMixin):
__tablename__ = 'users' __tablename__ = 'users'
pid = db.Column(db.Integer, primary_key=True) 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 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) tokens = db.Column(db.Text)
confirmed = db.Column(db.Boolean, default=False) confirmed = db.Column(db.Boolean, default=False)
active = db.Column(db.Boolean, default=True) active = db.Column(db.Boolean, default=True)
member_since = db.Column(db.DateTime(), default=datetime.utcnow) member_since = db.Column(db.DateTime(), default=datetime.utcnow)
last_seen = 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 twofactor = db.Column(db.Boolean, default=False) #optional 2factor auth
otp_secret = db.Column(db.String(16)) otp_secret = db.Column(db.String)
avatar_hash = db.Column(db.String(32)) avatar_hash = db.Column(db.String)
name = db.Column(db.Unicode(256)) name = db.Column(db.Unicode)
address = db.Column(db.Unicode(256)) address = db.Column(db.Unicode)
city = db.Column(db.Unicode(64)) city = db.Column(db.Unicode)
postcode = db.Column(db.String(10)) postcode = db.Column(db.String)
country = db.Column(db.String(64), default='BG') country = db.Column(db.String, default='BG')
phone = db.Column(db.String(64)) phone = db.Column(db.String)
org_account = db.Column(db.Boolean, default=False) org_account = db.Column(db.Boolean, default=False)
org_companyname = db.Column(db.Unicode(64)) org_companyname = db.Column(db.Unicode)
org_regaddress = db.Column(db.Unicode(128)) org_regaddress = db.Column(db.Unicode)
org_responsible = db.Column(db.Unicode(128)) org_responsible = db.Column(db.Unicode)
org_vatnum = db.Column(db.String(16)) org_vatnum = db.Column(db.String)
group = db.Column(db.String(24), default='User') group = db.Column(db.String, default='User')
language = db.Column(db.String(2), default='BG') language = db.Column(db.String, default='BG')
wallet = db.Column(db.Float) wallet = db.Column(db.Float)
overdraft = db.Column(db.Float) currency = db.Column(db.String, default='BGN')
currency = db.Column(db.String(3), default='BGN')
inv_deployments = db.relationship('Deployment', backref='owner', lazy='dynamic') inv_deployments = db.relationship('Deployment', backref='owner', lazy='dynamic')
inv_services = db.relationship('Service', 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_created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
date_last_charge = db.Column(db.DateTime) date_last_charge = db.Column(db.DateTime)
period = db.Column(db.Integer) period = db.Column(db.Integer)
daysleft = db.Column(db.Integer)
warning = db.Column(db.Boolean, default=False)
enabled = db.Column(db.Boolean, default=False) enabled = db.Column(db.Boolean, default=False)
category = db.Column(db.String(64)) category = db.Column(db.String)
description = db.Column(db.Unicode(128)) description = db.Column(db.Unicode)
price = db.Column(db.Float) price = db.Column(db.Float)
class Deployment(db.Model): class Deployment(db.Model):
@ -245,6 +246,8 @@ class Deployment(db.Model):
date_created = db.Column(db.DateTime, index=True, default=datetime.utcnow) date_created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
date_last_charge = db.Column(db.DateTime) date_last_charge = db.Column(db.DateTime)
period = db.Column(db.Integer) period = db.Column(db.Integer)
daysleft = db.Column(db.Integer)
warning = db.Column(db.Boolean, default=False)
enabled = db.Column(db.Boolean, default=False) enabled = db.Column(db.Boolean, default=False)
machine_id = db.Column(db.BigInteger) #cubeid machine_id = db.Column(db.BigInteger) #cubeid
@ -260,8 +263,8 @@ class Region(db.Model):
pid = db.Column(db.Integer, primary_key=True) pid = db.Column(db.Integer, primary_key=True)
enabled = db.Column(db.Boolean) enabled = db.Column(db.Boolean)
name = db.Column(db.String(64)) name = db.Column(db.String)
description = db.Column(db.String(128)) description = db.Column(db.String)
extraprice = db.Column(db.Float) extraprice = db.Column(db.Float)
class Address(db.Model): class Address(db.Model):
@ -274,9 +277,9 @@ class Address(db.Model):
region_id = db.Column(db.Integer, db.ForeignKey('regions.pid')) #FK region_id = db.Column(db.Integer, db.ForeignKey('regions.pid')) #FK
deploy_id = db.Column(db.Integer, db.ForeignKey('deployments.pid')) #FK deploy_id = db.Column(db.Integer, db.ForeignKey('deployments.pid')) #FK
ip = db.Column(db.String(64)) ip = db.Column(db.String)
mac = db.Column(db.String(128)) mac = db.Column(db.String)
rdns = db.Column(db.String(256)) 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 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): def __init__(self):
@ -301,6 +304,8 @@ class Address(db.Model):
class Domain(db.Model): class Domain(db.Model):
__tablename__ = 'domains' __tablename__ = 'domains'
pid = db.Column(db.Integer, primary_key=True) 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) enabled = db.Column(db.Boolean, default=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.pid')) #FK 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) date_created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
currency = db.Column(db.String, default='BGN') currency = db.Column(db.String, default='BGN')
tax = db.Column(db.Float) #VAT tax = db.Column(db.Float) #VAT
description = db.Column(db.Unicode(255)) description = db.Column(db.Unicode)
def sub_total(self): def sub_total(self):
items = self.items items = self.items
@ -352,7 +357,7 @@ class InvoiceItem(db.Model):
pid = db.Column(db.Integer, primary_key=True) pid = db.Column(db.Integer, primary_key=True)
item_number = db.Column(db.Integer) item_number = db.Column(db.Integer)
invoice_id = db.Column(db.Integer, db.ForeignKey('invoice.pid')) #FK 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_quantity = db.Column(db.Float)
item_price = db.Column(db.Float) item_price = db.Column(db.Float)
amount = db.Column(db.Float) amount = db.Column(db.Float)

View file

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

View file

@ -139,6 +139,7 @@ addEventListener("DOMContentLoaded", function() {
<th>Network</th> <th>Network</th>
<th>Control</th> <th>Control</th>
<th></th> <th></th>
<th>Time Left</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -150,10 +151,14 @@ addEventListener("DOMContentLoaded", function() {
<td data-title="Memory">{{ deploy.machine_mem }} MB</td> <td data-title="Memory">{{ deploy.machine_mem }} MB</td>
<td data-title="Disk">{{ deploy.machine_hdd }} GB</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="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 data-title="Control"> </td>
<td> </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 %} {% else %}
<tr> <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="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="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> <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> {% endif %}</td>
<td data-title="Remote">{% if status[deploy.machine_id] == 'running' %} <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> <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> {% endif %}</td>
</tr> </tr>
{% endif %} {% endif %}
@ -174,7 +185,7 @@ addEventListener("DOMContentLoaded", function() {
</tbody> </tbody>
</table> </table>
</div> </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> </div>
</div> </div>
@ -191,24 +202,30 @@ addEventListener("DOMContentLoaded", function() {
<tr> <tr>
<th>Category</th> <th>Category</th>
<th>Description</th> <th>Description</th>
<th>Price</th>
<th>Months</th>
<th>Last Charged</th> <th>Last Charged</th>
<th>Time Left</th>
</tr> </tr>
<tbody> <tbody>
{% for service in inv_services %} {% for service in inv_services %}
{% if service.enabled == False %}
<tr class="danger">
{% else %}
<tr> <tr>
{% endif %}
<td data-title="Category">{{ service.category }}</td> <td data-title="Category">{{ service.category }}</td>
<td data-title="Description" >{{ service.description }}</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> <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 %} {% endfor %}
</tbody> </tbody>
</table> </table>
</div> </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> </div>
</div> </div>
@ -225,20 +242,25 @@ addEventListener("DOMContentLoaded", function() {
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Expiry Date</th> <th>Expiry Date</th>
<th>Control</th> <th>Time Left</th>
</tr> </tr>
<tbody> <tbody>
{% for domain in inv_domains %} {% for domain in inv_domains %}
<tr> <tr>
<td data-title="Domain"><b><a href="http://{{ domain.fqdn }}">{{ domain.fqdn }}</a></b></td> <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="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> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
</div> </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> </div>
</div> </div>
@ -257,7 +279,6 @@ addEventListener("DOMContentLoaded", function() {
<th>Region</th> <th>Region</th>
<th>MAC Addr.</th> <th>MAC Addr.</th>
<th>Reverse DNS</th> <th>Reverse DNS</th>
<th>Control</th>
</tr> </tr>
<tbody> <tbody>
{% for address in inv_addresses %} {% for address in inv_addresses %}
@ -266,13 +287,12 @@ addEventListener("DOMContentLoaded", function() {
<td data-title="Region">{{ region[address.region_id] }}</td> <td data-title="Region">{{ region[address.region_id] }}</td>
<td data-title="MAC">{{ address.mac }}</td> <td data-title="MAC">{{ address.mac }}</td>
<td data-title="RDNS">{{ address.rdns }}</td> <td data-title="RDNS">{{ address.rdns }}</td>
<td data-title="Control">soon...</td>
{% endfor %} {% endfor %}
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </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> </div>
</div> </div>

View file

@ -11,7 +11,7 @@
{% if not current_user.is_authenticated %} {% if not current_user.is_authenticated %}
<a class="navbar-brand" href="{{ url_for('main.index') }}" rel="home"></a> <a class="navbar-brand" href="{{ url_for('main.index') }}" rel="home"></a>
{% else %} {% 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 %} {% endif %}
</div> </div>
@ -49,7 +49,7 @@
<li class="dropdown"> <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> <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"> <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><a href="{{ url_for('uinvoice.transactions') }}"><span class="glyphicon glyphicon-list-alt"></span> Transactions</a></li>
<li role="separator" class="divider"></li> <li role="separator" class="divider"></li>
<li><a href="{{ url_for('settings.profile') }}"><span class="glyphicon glyphicon-user"></span> Profile</a></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 /> <p><br />
<b>Currency:</b> {{ cuser.currency }}<br /> <b>Currency:</b> {{ cuser.currency }}<br />
<b>Wallet:</b> {{ cuser.wallet|round(2) }}<br /> <b>Wallet:</b> {{ cuser.wallet|round(2) }}<br />
<b>Overdraft:</b> {{ cuser.overdraft }}<br />
</p> </p>
</div> </div>
</div> </div>

View file

@ -102,7 +102,7 @@ def deploy(product_id=None):
query = contact_proxmaster(data, 'vmcreate') query = contact_proxmaster(data, 'vmcreate')
except: except:
flash('Region unreachable! Please try again later...') flash('Region unreachable! Please try again later...')
return redirect(url_for('vmanager.dashboard')) return redirect(url_for('main.dashboard'))
if query is not None: 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) 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: else:
flash('Deploy cancelled! Please try again later...') 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) 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>') @vmanager.route('/activate/<int:vmid>')
@login_required @login_required
def activate(vmid=0): def activate(vmid=0):
@ -200,7 +134,6 @@ def activate(vmid=0):
current_app.logger.info('[{}] Disabled deployments: {}'.format(current_user.email, inventory)) current_app.logger.info('[{}] Disabled deployments: {}'.format(current_user.email, inventory))
abort(403) abort(403)
@vmanager.route('/<cmd>/<int:vmid>') @vmanager.route('/<cmd>/<int:vmid>')
@login_required @login_required
def command(cmd=None, vmid=0): def command(cmd=None, vmid=0):

View file

@ -37,66 +37,98 @@ def deploy():
Role.insert_roles() Role.insert_roles()
@manager.command @manager.command
def charge(): def autodisable():
from app.models import User, Deployment, Service, Domain from app.models import User, Deployment, Service, Domain
today = datetime.utcnow() 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() deployments_ena = Deployment.query.filter_by(enabled=True).all()
for deploy in deployments_ena: for deploy in deployments_ena:
lastcharge = deploy.date_last_charge lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period)) expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
if today > expiry: if today > expiry:
print('last charged: ' + lastcharge.strftime('%c') + 'expiry date: ' + expiry.strftime('%c')) print('Deployment "' + deploy.machine_alias + '" is past expiration date and will be marked INACTIVE. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
print(deploy.machine_alias + ' is past expiration date and will be marked INACTIVE') 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() services_ena = Service.query.filter_by(enabled=True).all()
for service in services_ena: for service in services_ena:
lastcharge = service.date_last_charge lastcharge = service.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period)) expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
if today > expiry: 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. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
print('Service "' + service.description + '" is past expiration date and will be marked INACTIVE') 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: for domain in domains_ena:
expiry = domain.date_expire expiry = domain.date_expire
if today > expiry: 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. Last charged: ' + lastcharge.strftime('%c') + ' expiry date: ' + expiry.strftime('%c'))
print('Domain "' + domain.fqdn + '" is past expiration date and will be marked as INACTIVE') 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() 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() deployments_ena = Deployment.query.filter_by(enabled=True).all()
for deploy in deployments_ena: for deploy in deployments_ena:
lastcharge = deploy.date_last_charge lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period)) expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
daysleft = relativedelta(today, expiry).days() daysleft = expiry - today
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'] cpu_cost = deploy.machine_cpu * app.config['CPU_RATIO']
mem_cost = ( deploy.machine_mem / 1024 ) * app.config['MEM_RATIO'] mem_cost = ( deploy.machine_mem / 1024 ) * app.config['MEM_RATIO']
hdd_cost = deploy.machine_hdd * app.config['HDD_RATIO'] hdd_cost = deploy.machine_hdd * app.config['HDD_RATIO']
total = cpu_cost + mem_cost + hdd_cost total = cpu_cost + mem_cost + hdd_cost
print('total: ' + str(total)) 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() services_ena = Service.query.filter_by(enabled=True).all()
for service in services_ena: for service in services_ena:
lastcharge = service.date_last_charge lastcharge = service.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period)) expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
if today > expiry: daysleft = expiry - today
print(service.description + ' is eligible for charging') service.daysleft = daysleft.days
print('last charged: ' + lastcharge.strftime('%c')) db.session.commit()
print('expiry date: ' + expiry.strftime('%c')) warndays = service.period * 5
cost = service.price if daysleft.days < warndays:
print('total: ' + str(cost)) 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() domains_ena = Domain.query.filter_by(enabled=True).all()
for domain in domains_ena: for domain in domains_ena:
expire = domain.date_expire expiry = domain.date_expire
if expire - today < 60: daysleft = expiry - today
daysleft = relativedelta(now, expire).days domain.daysleft = daysleft.days
print(domain.fqdn + ' is expiring after ' + daysleft + ' for charging') db.session.commit()
print('expiry date: ' + expire.strftime('%c'))
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 @manager.command
def runserver(): def runserver():