api redesign

This commit is contained in:
deflax 2017-10-19 18:59:17 +03:00
parent 541bd3663e
commit 79e5777ae3
8 changed files with 72 additions and 77 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -180,14 +180,14 @@ addEventListener("DOMContentLoaded", function() {
{% if deploy.enabled == True %}
<img class="icon icons8-Device-Manager" width="32" height="32" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACl0lEQVRYhe2X70sTcRzHfdhDH/Zn9KAHPrwnejNSwqx01hpBw+1RDyoJrfZk3i0mKerWrtApKJmtQVkSSoPIfbc1vJtzOuFiWeE6QnaJlqbeuydNlj92m/PoBx68nrz5wvfFfd984FNSUuSnYwi1Xyirv7TY+0uq20JL1x8mUoVS2xFJ6xhCFS1gcAsfk7KCQrnxaG7x/xIg71fwckZW5bW4rI3ATa8Ic98Mrg6JObF44toJcETGC1HJSaNWAreeiLjojsLUG8+JpU8jgT9ewrHEVwyGJFVGYmntOtDsm4djXMqJZh3QrIQn7oSPbp/h5baJY9sFnK8+weyJq8KMJPMXqLBP1NIs+U6zBLswli2gSQlpOxmlWTKrY4gxG9pO7tMsQaU9WKatAEv8NBvkt+c6hhhplkDHEOpABCrtwbIqR/jDuS7+c4MzmspQ7xTW6rr59ezsVy7XdfOodwqLepewkRky510C2OfJwgQoq/9IlSOcHuBXVBusxrO5DTQNixgISvkLlNvfHDd5ZhaKvTzDvUAat31i/gI6hlDm/kTqoAQ4IqPFW4BARWvgpJGLLXFEBkdkVLeFYeqJoab9LWyjC8jku2HkpmDqiaHByaPZNw+OyLCNLuDa0BySsoK4tK4uQLMBQ0175IfeFV3Vu6KrBregJGUFTUMJnO2c3Mp3o65rUknKCgZDEs50TsLgnkKDS4DFE8dw5AtolsD29F1hT3C6I4IWrwh9N686Wi/3xNHiFdHYOw3HuPTbEwyGJNAsgXWPPuwpkP2LfbPrOQX6IstbZx9Pr+3oQHj+2z9QwkOBQ4G/QuDU3Uja3J9I7QeTZ1ax9Cc2M1x6ML15wSWs5r2cUlZ/aTErNt0auLIDNmDIdz3/CZaw0mqfwL4CAAAAAElFTkSuQmCC">
{% if status[deploy.machine_id] == 'running' %}
<button class="confirm command command-vmshutdown btn btn-default btn-warning" value="vmshutdown" vmid="{{ deploy.machine_id }}"><span class="glyphicon glyphicon-off" aria-hidden="true"></span> Shutdown</button>
<button class="confirm command command-vmstop btn btn-default btn-danger" value="vmstop" vmid="{{ deploy.machine_id }}"><span class="glyphicon glyphicon-alert" aria-hidden="true"></span> Force Stop</button>
<button class="confirm command command-shutdown btn btn-default btn-warning" value="shutdown" vmid="{{ deploy.machine_id }}"><span class="glyphicon glyphicon-off" aria-hidden="true"></span> Shutdown</button>
<button class="confirm command command-stop btn btn-default btn-danger" value="stop" vmid="{{ deploy.machine_id }}"><span class="glyphicon glyphicon-alert" aria-hidden="true"></span> Force Stop</button>
</p>
<p>
<img class="icon icons8-Remote-Control" width="32" height="32" src="data:image/png;base64,iVBORw0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gAAAABJRU5ErkJggg==">
<button class="btn btn-default btn-info" onclick="window.open('/vmanager/command/vmvnc/{{ deploy.machine_id }}', '_blank');"><span class="glyphicon glyphicon-console" aria-hidden="true"></span> Console</button>
{% else %}
<button class="command command-vmstart btn btn-default btn-success" value="vmstart" vmid="{{ deploy.machine_id }}"><span class="glyphicon glyphicon-play" aria-hidden="true"></span> Start</button>
<button class="command command-start btn btn-default btn-success" value="start" vmid="{{ deploy.machine_id }}"><span class="glyphicon glyphicon-play" aria-hidden="true"></span> Start</button>
{% endif %}
{% endif %}
</p>

View file

@ -52,7 +52,7 @@ addEventListener("DOMContentLoaded", function() {
<div class="panel panel-default">
<div class="panel-heading">{{ value['hostname'] }} ({{ key }})</div>
<div class="panel-body"><p>
<button class="btn btn-default btn-info" onclick="window.open('/vmvnc/{{ value['vmid'] }}','popUpWindow','height=768,width=1280,left=2,top=2,,scrollbars=no,menubar=no'); return false;"><span class="glyphicon glyphicon-console" aria-hidden="true"></span> Console</button>
<button class="btn btn-default btn-info" onclick="window.open('/vnc/{{ value['vmid'] }}','popUpWindow','height=768,width=1280,left=2,top=2,,scrollbars=no,menubar=no'); return false;"><span class="glyphicon glyphicon-console" aria-hidden="true"></span> Console</button>
<button class="command command-vmstart btn btn-default btn-success" value="vmstart" vmid="{{ value['vmid'] }}"><span class="glyphicon glyphicon-play" aria-hidden="true"></span> Start</button>
<button class="command command-vmshutdown btn btn-default btn-warning" value="vmshutdown" vmid="{{ value['vmid'] }}"><span class="glyphicon glyphicon-off" aria-hidden="true"></span> Shutdown</button>
<button class="command command-vmstop btn btn-default btn-danger" value="vmstop" vmid="{{ value['vmid'] }}"><span class="glyphicon glyphicon-alert" aria-hidden="true"></span> Force Stop</button>

View file

@ -1,4 +1,4 @@
<p>{{ user.email }} encountered an error working with cube id: {{ cubeid }}<br />
<p>{{ user.email }} encountered an error working with id: {{ unit_id }}<br />
<br />
</p>
<p>Regards,

View file

@ -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

View file

@ -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/<int:itemid>', methods=['GET', 'POST'])
@vmanager.route('/vmremove/<int:unit_id>', 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/<cmd>/<int:vmid>')
@vmanager.route('/command/<cmd>/<int:unit_id>')
@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'])