diff --git a/app/admin/routes.py b/app/admin/routes.py index c14f21c..18dd391 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -186,26 +186,22 @@ def dashboard(user_pid=0): #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 unit_id in inv_deploycubeids: + rrd[unit_id] = {} + data = { 'unit_id': int(unit_id), + 'type': 'deploy' } + 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'] for graph in graphs_list: raw = query[graph]['image'].encode('raw_unicode_escape') - rrd[cubeid][graph] = base64.b64encode(raw).decode() - status = { cubeid : query['status'] } + rrd[unit_id][graph] = base64.b64encode(raw).decode() + status = { unit_id : 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 ) - #current_app.logger.warning('[ADMIN] {} deployments: {}, services: {}, domains: {}, services: {}'.format(cuser.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) diff --git a/app/main/routes.py b/app/main/routes.py index 740acaf..2bb01e0 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -94,15 +94,17 @@ def dashboard(): rrd = {} statuses = {} #current_app.logger.warning(str(inv_deploycubeids)) - for cubeid in inv_deploycubeids: - rrd[cubeid] = {} + for user_id in inv_deploycubeids: + rrd[user_id] = {} + data = { 'user_id': int(user_id), + 'type': 'deploy' } try: - query = contact_proxmaster({}, 'vmrrd', cubeid) + query = contact_proxmaster(data, 'vmrrd') 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) + flash('Deploy #{} unreachable. Support is notified'.format(str(unit_id))) + send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(unit_id), + 'vmanager/email/adm_unreachable', user=current_user, unit_id=unit_id) #current_app.logger.info('debug query:') #current_app.logger.info(query) @@ -110,14 +112,14 @@ def dashboard(): 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'] } + rrd[unit_id][graph] = base64.b64encode(raw).decode() + status = { unit_id : 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 ) + flash('Deploy #{} unreachable. Support is notified'.format(str(unit_id))) + send_email(current_app.config['MAIL_USERNAME'], 'Cube {} is unreachable'.format(unit_id), + 'vmanager/email/adm_unreachable', user=current_user, unit_id=unit_id ) 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) diff --git a/app/models.py b/app/models.py index d851249..8ee5e0c 100644 --- a/app/models.py +++ b/app/models.py @@ -213,23 +213,16 @@ lm.anonymous_user = AnonymousUser def load_user(user_id): return User.query.get(int(user_id)) -def contact_proxmaster(data, method, cubeid=0): - prxurl = current_app.config['PROXMASTER_URL'] +def contact_proxmaster(data, method): data['apikey'] = current_app.config['APIKEY'] data_json = json.dumps(data) - - url = '{}/{}/{}'.format(prxurl, method, cubeid) - if method == 'vmcreate': - url = '{}/vmcreate'.format(prxurl) - if method == 'vmremove': - url = '{}/vmremove/{}'.format(prxurl, cubeid) - + url = current_app.config['PROXMASTER_URL'] + '/' + str(method) try: db_result = requests.post( url, data=data_json, headers={"content-type": "application/json"}, timeout=30 ) proxjson = db_result.json() return proxjson except: - return None + return { 'status': 'UNREACHABLE' } class Router(db.Model): __tablename__ = 'routers' @@ -238,7 +231,7 @@ class Router(db.Model): date_created = db.Column(db.DateTime, default=datetime.utcnow) deleted = db.Column(db.Boolean, default=False) - machine_id = db.Column(db.BigInteger) #cubeid + machine_id = db.Column(db.BigInteger) #unit_id class Bridge(db.Model): __tablename__ = 'bridge' @@ -263,7 +256,7 @@ class Deployment(db.Model): period = db.Column(db.Integer) daysleft = db.Column(db.Integer) - machine_id = db.Column(db.BigInteger) #cubeid + machine_id = db.Column(db.BigInteger) #unit_id machine_alias = db.Column(db.String) #dns name machine_cpu = db.Column(db.Integer) machine_mem = db.Column(db.Integer) diff --git a/app/templates/main/dashboard.html b/app/templates/main/dashboard.html index d356db5..53070a1 100644 --- a/app/templates/main/dashboard.html +++ b/app/templates/main/dashboard.html @@ -180,14 +180,14 @@ addEventListener("DOMContentLoaded", function() { {% if deploy.enabled == True %} {% if status[deploy.machine_id] == 'running' %} - - + +

{% else %} - + {% endif %} {% endif %}

diff --git a/app/templates/vmanager/_misc.html b/app/templates/vmanager/_misc.html index 13fd91f..740a997 100644 --- a/app/templates/vmanager/_misc.html +++ b/app/templates/vmanager/_misc.html @@ -52,7 +52,7 @@ addEventListener("DOMContentLoaded", function() {
{{ value['hostname'] }} ({{ key }})

- + diff --git a/app/templates/vmanager/email/adm_unreachable.html b/app/templates/vmanager/email/adm_unreachable.html index b51466a..52abd1f 100644 --- a/app/templates/vmanager/email/adm_unreachable.html +++ b/app/templates/vmanager/email/adm_unreachable.html @@ -1,4 +1,4 @@ -

{{ user.email }} encountered an error working with cube id: {{ cubeid }}
+

{{ user.email }} encountered an error working with id: {{ unit_id }}

Regards, diff --git a/app/templates/vmanager/email/adm_unreachable.txt b/app/templates/vmanager/email/adm_unreachable.txt index cfa0f93..dbb7ddd 100644 --- a/app/templates/vmanager/email/adm_unreachable.txt +++ b/app/templates/vmanager/email/adm_unreachable.txt @@ -1,4 +1,4 @@ -User {{ user.email }} encountered an error working with cube id: {{ cubeid }} +User {{ user.email }} encountered an error working with id: {{ unit_id }} Regards, Proxadmin diff --git a/app/vmanager/routes.py b/app/vmanager/routes.py index 212e160..2ef43c6 100644 --- a/app/vmanager/routes.py +++ b/app/vmanager/routes.py @@ -49,9 +49,11 @@ def createvm(): #no switches in the account. create one... data = { 'clientid': str(current_user.pid), 'clientemail': str(current_user.email), - 'region': str(selected_region.name) + 'region': str(selected_region.name), + 'type': 'bridge' } - query = contact_proxmaster(data, 'brcreate') + #create bridge unit + query = contact_proxmaster(data, 'create') if query is not None: bridge = Bridge(user_id=int(current_user.pid)) db.session.add(bridge) @@ -63,12 +65,13 @@ def createvm(): return redirect(url_for('main.dashboard')) else: #bridge found. lets see on which slave it is so we can create the instance on the same slave. - data = {} - query = contact_proxmaster(data, 'brquery', str(selected_bridge.bridge_id)) + data = { 'unit_id': int(selected_bridge.bridge_id), + 'type': 'bridge' } + query = contact_proxmaster(data, 'status') if query is not None: newbridge = False else: - flash('Point found but cannot be used1') + flash('Point found but cannot be used!') return redirect(url_for('main.dashboard')) #machine will be installed where the switch physically is @@ -78,24 +81,23 @@ def createvm(): #create new machine... data = { 'clientid': str(current_user.pid), 'clientemail': str(current_user.email), - 'hostname': str(form.servername.data) + '-c' + str(current_user.pid), + 'hostname': 'c' + str(current_user.pid) + '-' + str(form.servername.data), 'region': str(selected_region.name), 'slave': str(slave_name), - 'type': 'kvm', + 'type': 'deploy', 'cpu': '1', 'mem': '512', 'hdd': '20', - 'eth0br': str(bridge_id), - 'eth0ip': 'AUTO' + 'bridge': str(bridge_id) } try: - query = contact_proxmaster(data, 'vmcreate') + query = contact_proxmaster(data, 'create') except: flash('Region not available! Please try again later...') return redirect(url_for('main.dashboard')) if query is not None: - deployment = Deployment(user_id=int(current_user.pid), machine_alias=query['hostname'], machine_id=query['cube'], machine_cpu=data['cpu'], machine_mem=data['mem'], machine_hdd=data['hdd'], enabled=True, protected=False, daysleft=15, warning=True, discount=0) + deployment = Deployment(user_id=int(current_user.pid), machine_alias=query['hostname'], machine_id=query['unit_id'], machine_cpu=data['cpu'], machine_mem=data['mem'], machine_hdd=data['hdd'], enabled=True, protected=False, daysleft=15, warning=True, discount=0) db.session.add(deployment) db.session.commit() flash('New device created successfully in region "{}".'.format(str(selected_region.description))) @@ -171,7 +173,7 @@ def activate(itemid=0): 'eth1ip': str(selected_address.ip) } try: - query = contact_proxmaster(data, 'vmcreate') + query = contact_proxmaster(data, 'create') except: flash('Region unreachable! Cannot create router. Please try again later...') return redirect(url_for('main.dashboard')) @@ -204,34 +206,35 @@ def activate(itemid=0): return redirect(url_for('main.dashboard')) return render_template('vmanager/activate.html', form=form, deploy=deploy, cpu_cost=cpu_cost, mem_cost=mem_cost, hdd_cost=hdd_cost, ppm=ppm, discount=discount, total=total, currency=owner.currency) -@vmanager.route('/vmremove/', methods=['GET', 'POST']) +@vmanager.route('/vmremove/', methods=['GET', 'POST']) @login_required -def remove(itemid=0): - data = {} - deploy = Deployment.query.filter_by(machine_id=int(itemid)).first() +def remove(unit_id=0): + data = { 'unit_id': int(unit_id), + 'type': 'deploy' } + deploy = Deployment.query.filter_by(machine_id=int(unit_id)).first() if current_user.is_administrator(): if deploy.protected is not True: try: - query = contact_proxmaster(data, 'vmremove', int(itemid)) - flash('Machine {} terminated'.format(itemid)) + query = contact_proxmaster(data, 'remove') + flash('Machine {} terminated'.format(unit_id)) deploy.deleted = True deploy.enabled = False deploy.warning = False db.session.commit() except: - flash('Cannot delete machine {}'.format(itemid)) + flash('Cannot delete machine {}'.format(unit_id)) return redirect(url_for('admin.list_recyclebin')) else: - current_app.logger.warning('Deployment id:{} is protected! Cannot be removed'.format(itemid)) + current_app.logger.warning('Deployment id:{} is protected! Cannot be removed'.format(unit_id)) else: - current_app.logger.warning('[WARNING] Unauthorized attempt to remove Deployment id:{}'.format(itemid)) + current_app.logger.warning('[WARNING] Unauthorized attempt to remove Deployment id:{}'.format(unit_id)) abort(404) -@vmanager.route('/command//') +@vmanager.route('/command//') @login_required -def command(cmd=None, vmid=0): +def command(cmd=None, unit_id=0): #checks whether this is a valid command - valid_commands = ['vmstatus', 'vmstart', 'vmshutdown', 'vmstop', 'vmvnc'] + valid_commands = ['status', 'start', 'shutdown', 'stop', 'vmvnc'] if not cmd in valid_commands: current_app.logger.warning(cmd + ' is not a valid command!') abort(404) @@ -242,20 +245,21 @@ def command(cmd=None, vmid=0): for invcls in result: inventory.extend([invcls.machine_id]) - data = {} + data = { 'type': 'deploy', + 'unit_id': int(unit_id) } if current_user.is_administrator(): - #current_app.logger.warning('[ADMIN] Access override for cube id:{}'.format(vmid)) - db_result = contact_proxmaster(data, cmd, int(vmid)) + #current_app.logger.warning('[ADMIN] Access override for cube id:{}'.format(unitunit__id)) + db_result = contact_proxmaster(data, cmd) if cmd == 'vmvnc': return redirect(db_result['url']) else: - #checks if current user owns this vmid - if not vmid in inventory: - current_app.logger.warning('[{}] Access violation with cube id: {}'.format(current_user.email, vmid)) + #checks if current user owns this unit_id + if not unit_id in inventory: + current_app.logger.warning('[{}] Access violation with unit id: {}'.format(current_user.email, unit_id)) #TODO: log ips else: - db_result = contact_proxmaster(data, cmd, int(vmid)) + db_result = contact_proxmaster(data, cmd) #print(db_result) if cmd == 'vmvnc': return redirect(db_result['url'])