2017-03-08 13:53:09 -05:00
|
|
|
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
|
|
|
|
|
2017-03-08 19:55:12 -05:00
|
|
|
from . import vmanager
|
2017-08-01 07:21:22 -04:00
|
|
|
from .forms import CreateForm, ActivateForm
|
2017-03-08 13:53:09 -05:00
|
|
|
from .. import db
|
|
|
|
from ..email import send_email
|
2017-10-15 07:38:38 -04:00
|
|
|
from ..models import User, Permission, Transaction, Bridge, Router, Deployment, Service, Region, Address, Domain, contact_proxmaster
|
2017-03-08 13:53:09 -05:00
|
|
|
from ..decorators import admin_required, permission_required
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import string
|
|
|
|
import random
|
|
|
|
from datetime import datetime, timedelta, date, time
|
2017-07-15 00:13:57 -04:00
|
|
|
from dateutil.relativedelta import relativedelta
|
2017-06-01 18:27:17 -04:00
|
|
|
import ast
|
2017-12-14 18:00:02 -05:00
|
|
|
import time
|
2017-03-08 13:53:09 -05:00
|
|
|
|
|
|
|
def randstr(n):
|
|
|
|
return ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(n))
|
|
|
|
|
2017-03-08 19:55:12 -05:00
|
|
|
@vmanager.after_app_request
|
2017-03-08 13:53:09 -05:00
|
|
|
def after_request(response):
|
|
|
|
for query in get_debug_queries():
|
|
|
|
if query.duration >= current_app.config['SLOW_DB_QUERY_TIME']:
|
|
|
|
current_app.logger.warning('Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n' % (query.statement, query.parameters, query.duration, query.context))
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2017-07-28 21:31:44 -04:00
|
|
|
@vmanager.route('/createvm', methods=['GET', 'POST'])
|
2017-03-08 13:53:09 -05:00
|
|
|
@login_required
|
2017-07-28 21:31:44 -04:00
|
|
|
def createvm():
|
2017-03-08 13:53:09 -05:00
|
|
|
if current_user.name is None:
|
2017-05-25 01:54:33 -04:00
|
|
|
flash('Please update profile info for this operation.')
|
2017-07-28 21:31:44 -04:00
|
|
|
return redirect(url_for('settings.profile'))
|
2017-03-08 13:53:09 -05:00
|
|
|
|
2017-10-07 11:39:50 -04:00
|
|
|
deployment_seeds = current_user.inv_deployments.filter_by(deleted=False).filter_by(protected=False).all()
|
2017-07-28 21:31:44 -04:00
|
|
|
if deployment_seeds != []:
|
2017-08-01 07:21:22 -04:00
|
|
|
flash('Offline deployments still exist.')
|
2017-07-28 21:31:44 -04:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2017-03-08 13:53:09 -05:00
|
|
|
|
2017-08-01 07:21:22 -04:00
|
|
|
form = CreateForm()
|
2017-03-08 13:53:09 -05:00
|
|
|
if current_user.confirmed and form.validate_on_submit():
|
2017-08-01 07:21:22 -04:00
|
|
|
selected_region = Region.query.filter_by(pid=int(form.region.data)).first()
|
2017-09-28 07:10:19 -04:00
|
|
|
|
2017-10-29 17:06:54 -04:00
|
|
|
#TODO: Filter bridges for the selected region only. switch should return slave name
|
|
|
|
selected_bridge = current_user.inv_bridges.filter_by(deleted=False).first()
|
|
|
|
if selected_bridge is None:
|
|
|
|
#no bridges in the account. create one...
|
2017-10-15 07:38:38 -04:00
|
|
|
data = { 'clientid': str(current_user.pid),
|
|
|
|
'clientemail': str(current_user.email),
|
2017-10-19 11:59:17 -04:00
|
|
|
'region': str(selected_region.name),
|
2017-10-29 17:06:54 -04:00
|
|
|
'type': 'br'
|
2017-10-15 07:38:38 -04:00
|
|
|
}
|
2017-10-19 11:59:17 -04:00
|
|
|
#create bridge unit
|
|
|
|
query = contact_proxmaster(data, 'create')
|
2017-10-29 17:06:54 -04:00
|
|
|
if query['status'] == 'bridge_created':
|
|
|
|
newbridge = True
|
|
|
|
#machine will be installed where the bridge physically is
|
|
|
|
region_name = query['region']
|
|
|
|
slave_name = query['slave']
|
|
|
|
bridge_id = query['unit_id']
|
2017-11-05 15:20:39 -05:00
|
|
|
bridge_phy_id = query['phy_id']
|
2017-10-29 17:06:54 -04:00
|
|
|
bridge = Bridge(user_id=int(current_user.pid), bridge_id=bridge_id)
|
2017-10-15 07:38:38 -04:00
|
|
|
db.session.add(bridge)
|
|
|
|
db.session.commit()
|
|
|
|
flash('New point created successfully in region "{}".'.format(str(selected_region.description)))
|
|
|
|
else:
|
|
|
|
flash('Point could not be created! Please try again later...')
|
|
|
|
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.
|
2017-10-19 11:59:17 -04:00
|
|
|
data = { 'unit_id': int(selected_bridge.bridge_id),
|
2017-10-29 17:06:54 -04:00
|
|
|
'type': 'br' }
|
|
|
|
query = contact_proxmaster(data, 'query')
|
|
|
|
if query['status'] == 'query_success':
|
2017-10-15 07:38:38 -04:00
|
|
|
newbridge = False
|
2017-10-29 17:06:54 -04:00
|
|
|
#machine will be installed where the switch physically is
|
|
|
|
region_name = query['region']
|
|
|
|
slave_name = query['slave']
|
2017-11-05 15:20:39 -05:00
|
|
|
bridge_phy_id = query['phy_id']
|
2017-10-15 07:38:38 -04:00
|
|
|
else:
|
2017-10-19 11:59:17 -04:00
|
|
|
flash('Point found but cannot be used!')
|
2017-10-15 07:38:38 -04:00
|
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
|
2017-10-29 17:06:54 -04:00
|
|
|
###
|
2017-10-15 07:38:38 -04:00
|
|
|
#create new machine...
|
2017-07-30 17:10:08 -04:00
|
|
|
data = { 'clientid': str(current_user.pid),
|
2017-03-08 13:53:09 -05:00
|
|
|
'clientemail': str(current_user.email),
|
2017-10-19 11:59:17 -04:00
|
|
|
'hostname': 'c' + str(current_user.pid) + '-' + str(form.servername.data),
|
2017-10-29 17:06:54 -04:00
|
|
|
'region': str(region_name),
|
2017-10-15 07:38:38 -04:00
|
|
|
'slave': str(slave_name),
|
2017-10-29 17:06:54 -04:00
|
|
|
'type': 'kvm',
|
2017-07-30 17:10:08 -04:00
|
|
|
'cpu': '1',
|
|
|
|
'mem': '512',
|
2017-10-15 07:38:38 -04:00
|
|
|
'hdd': '20',
|
2017-11-05 15:20:39 -05:00
|
|
|
'net0if': 'vmbr' + str(bridge_phy_id)
|
2017-07-30 17:10:08 -04:00
|
|
|
}
|
2017-03-08 13:53:09 -05:00
|
|
|
try:
|
2017-10-19 11:59:17 -04:00
|
|
|
query = contact_proxmaster(data, 'create')
|
2017-03-08 13:53:09 -05:00
|
|
|
except:
|
2017-10-15 07:38:38 -04:00
|
|
|
flash('Region not available! Please try again later...')
|
2017-07-11 12:20:44 -04:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2017-03-08 13:53:09 -05:00
|
|
|
|
2017-10-29 17:06:54 -04:00
|
|
|
if query['status'] == 'kvm_created':
|
2017-11-05 15:20:39 -05:00
|
|
|
deployment = Deployment(user_id=int(current_user.pid), machine_alias=str(form.servername.data), 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)
|
2017-03-08 13:53:09 -05:00
|
|
|
db.session.add(deployment)
|
|
|
|
db.session.commit()
|
2017-10-15 07:38:38 -04:00
|
|
|
flash('New device created successfully in region "{}".'.format(str(selected_region.description)))
|
|
|
|
return redirect(url_for('main.dashboard'))
|
2017-03-08 13:53:09 -05:00
|
|
|
else:
|
2017-10-15 07:38:38 -04:00
|
|
|
flash('Device could not be created! Please try again later...')
|
2017-03-08 13:53:09 -05:00
|
|
|
|
2017-10-15 07:38:38 -04:00
|
|
|
#TODO: cleanup bridge if the machine is new and we were not be able to create it
|
|
|
|
|
2017-07-11 12:20:44 -04:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2017-08-01 07:21:22 -04:00
|
|
|
return render_template('vmanager/create.html', form=form)
|
2017-03-08 13:53:09 -05:00
|
|
|
|
2017-07-14 09:04:27 -04:00
|
|
|
@vmanager.route('/activate/<int:itemid>', methods=['GET', 'POST'])
|
2017-07-06 17:15:02 -04:00
|
|
|
@login_required
|
2017-07-14 09:00:04 -04:00
|
|
|
def activate(itemid=0):
|
2017-10-07 11:39:50 -04:00
|
|
|
result = current_user.inv_deployments.filter_by(deleted=False).all()
|
2017-07-06 17:15:02 -04:00
|
|
|
inventory = []
|
|
|
|
for invcls in result:
|
|
|
|
inventory.extend([invcls.machine_id])
|
2017-07-13 19:46:51 -04:00
|
|
|
if current_user.is_administrator():
|
2017-07-15 20:48:18 -04:00
|
|
|
current_app.logger.warning('[ADMIN] Access override for deployment id:{}'.format(itemid))
|
2017-07-14 09:00:04 -04:00
|
|
|
elif not itemid in inventory:
|
2017-07-15 20:48:18 -04:00
|
|
|
current_app.logger.error('[{}] Access violation with deployment id: {}'.format(current_user.email, itemid))
|
2017-10-15 07:38:38 -04:00
|
|
|
abort(403)
|
2017-07-13 19:46:51 -04:00
|
|
|
|
2017-07-15 00:13:57 -04:00
|
|
|
deploy = Deployment.query.filter_by(machine_id=itemid).first()
|
2017-10-15 07:38:38 -04:00
|
|
|
if deploy is None:
|
2017-07-20 19:30:09 -04:00
|
|
|
abort(404)
|
2017-10-15 07:38:38 -04:00
|
|
|
if deploy.enabled == True and deploy.warning == False:
|
|
|
|
abort(403)
|
2017-07-15 00:13:57 -04:00
|
|
|
cpu_cost = deploy.machine_cpu * current_app.config['CPU_RATIO']
|
|
|
|
mem_cost = ( deploy.machine_mem / 1024 ) * current_app.config['MEM_RATIO']
|
|
|
|
hdd_cost = deploy.machine_hdd * current_app.config['HDD_RATIO']
|
2017-09-09 02:32:04 -04:00
|
|
|
ppm = cpu_cost + mem_cost + hdd_cost
|
|
|
|
discount = round(( ppm * deploy.discount ) / 100)
|
|
|
|
ppm = round(ppm - discount)
|
2017-08-01 07:21:22 -04:00
|
|
|
|
2017-09-09 02:32:04 -04:00
|
|
|
#default period = 1 for virgin deployments
|
2017-08-01 07:21:22 -04:00
|
|
|
if deploy.period is None:
|
|
|
|
form = ActivateForm(period=1)
|
|
|
|
total = ppm * 1
|
|
|
|
else:
|
|
|
|
form = ActivateForm(period=int(deploy.period))
|
|
|
|
total = ppm * deploy.period
|
2017-07-15 00:13:57 -04:00
|
|
|
|
2017-07-15 20:48:18 -04:00
|
|
|
owner = deploy.owner
|
|
|
|
if owner.confirmed and form.validate_on_submit():
|
2017-07-20 19:30:09 -04:00
|
|
|
total = ppm * form.period.data
|
2017-07-15 20:48:18 -04:00
|
|
|
if owner.wallet < total:
|
2017-07-20 19:30:09 -04:00
|
|
|
flash('Activation costs {} {}. Insufficient Funds'.format(total, owner.currency))
|
2017-09-20 20:08:58 -04:00
|
|
|
if current_user.is_administrator():
|
|
|
|
return redirect(url_for('admin.list_users'))
|
|
|
|
else:
|
|
|
|
return redirect(url_for('uinvoice.transactions'))
|
2017-07-15 20:48:18 -04:00
|
|
|
current_app.logger.info('[{}] Charge deployment: {}'.format(owner.email, deploy.machine_id))
|
2017-10-15 07:38:38 -04:00
|
|
|
|
2017-10-29 17:06:54 -04:00
|
|
|
#TODO: Filter routers for the selected region only. switch should return slave name
|
2017-11-05 15:20:39 -05:00
|
|
|
selected_router = owner.inv_routers.filter_by(deleted=False).first()
|
2017-10-29 17:06:54 -04:00
|
|
|
if selected_router is None:
|
|
|
|
#TODO: Filter bridges for the selected region only. switch should return slave name
|
2017-11-05 15:20:39 -05:00
|
|
|
selected_bridge = owner.inv_bridges.filter_by(deleted=False).first()
|
2017-10-29 17:06:54 -04:00
|
|
|
if selected_bridge is None:
|
2017-12-14 18:00:02 -05:00
|
|
|
flash('No private network found.')
|
2017-10-29 17:06:54 -04:00
|
|
|
else:
|
|
|
|
#bridge found. lets see on which slave it is so we can create the instance on the same slave.
|
|
|
|
data = { 'unit_id': int(selected_bridge.bridge_id),
|
|
|
|
'type': 'br' }
|
|
|
|
query = contact_proxmaster(data, 'query')
|
|
|
|
if query['status'] == 'query_success':
|
2017-12-14 18:00:02 -05:00
|
|
|
#TODO: selected random ip address from the pool.
|
|
|
|
selected_ip = '87.120.110.41'
|
2017-10-29 17:06:54 -04:00
|
|
|
#machine will be installed where the switch physically is
|
|
|
|
region_name = query['region']
|
|
|
|
slave_name = query['slave']
|
2017-11-05 15:20:39 -05:00
|
|
|
bridge_phy_id = query['phy_id']
|
2017-12-14 18:00:02 -05:00
|
|
|
data = { 'clientid': str(owner.pid),
|
|
|
|
'clientemail': str(owner.email),
|
|
|
|
'hostname': 'c' + str(owner.pid) + '-r' + selected_ip,
|
|
|
|
'region': str(region_name),
|
|
|
|
'slave': str(slave_name),
|
|
|
|
'type': 'lxc',
|
|
|
|
'cpu': '1',
|
|
|
|
'mem': '256',
|
|
|
|
'hdd': '1',
|
|
|
|
'net0if': 'vmbr' + str(bridge_phy_id),
|
|
|
|
'net0ip': '192.168.9.1',
|
|
|
|
'net0mask': '24',
|
|
|
|
'net1if': 'vmbr0',
|
|
|
|
'net1ip': selected_ip,
|
|
|
|
'net1mask': '24',
|
|
|
|
'net1gw': '87.120.110.1' #should be queried from the current region
|
|
|
|
}
|
|
|
|
query = contact_proxmaster(data, 'create')
|
|
|
|
if query['status'] == 'lxc_created':
|
|
|
|
router = Router(user_id=int(owner.pid), machine_id=query['unit_id'])
|
|
|
|
db.session.add(router)
|
|
|
|
db.session.commit()
|
2017-10-29 17:06:54 -04:00
|
|
|
else:
|
2017-12-14 18:00:02 -05:00
|
|
|
flash('Router cannot be created.')
|
2017-10-29 17:06:54 -04:00
|
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
|
2017-07-15 00:13:57 -04:00
|
|
|
today = datetime.utcnow()
|
2017-07-15 20:48:18 -04:00
|
|
|
expiry = today + relativedelta(today, months=+(form.period.data))
|
|
|
|
daysleft = expiry - today
|
|
|
|
extradays = relativedelta(today, days=+(deploy.daysleft))
|
|
|
|
deploy.date_last_charge = today + extradays
|
2017-07-15 00:13:57 -04:00
|
|
|
deploy.period = form.period.data
|
2017-07-20 19:30:09 -04:00
|
|
|
deploy.daysleft = daysleft.days + extradays.days
|
2017-07-15 00:13:57 -04:00
|
|
|
deploy.warning = False
|
2017-07-15 00:28:04 -04:00
|
|
|
deploy.enabled = True
|
2017-08-01 07:21:22 -04:00
|
|
|
deploy.protected = True
|
2017-10-15 07:38:38 -04:00
|
|
|
db.session.commit()
|
2017-10-07 11:39:50 -04:00
|
|
|
|
2017-07-15 20:48:18 -04:00
|
|
|
transaction = Transaction(user_id=int(owner.pid), description='Deployment {} activated for {} month(s)'.format(str(deploy.machine_alias), form.period.data), value=-total)
|
2017-07-15 00:13:57 -04:00
|
|
|
db.session.add(transaction)
|
|
|
|
db.session.commit()
|
2017-07-15 20:48:18 -04:00
|
|
|
owner.wallet = owner.wallet - total
|
2017-07-15 00:13:57 -04:00
|
|
|
db.session.commit()
|
2017-07-20 19:30:09 -04:00
|
|
|
flash('Deployment {} activated for {} month(s)'.format(str(deploy.machine_alias), form.period.data))
|
2017-12-14 18:00:02 -05:00
|
|
|
if current_user.is_administrator():
|
2017-07-25 10:33:23 -04:00
|
|
|
return redirect(url_for('admin.list_deployments'))
|
2017-07-15 20:48:18 -04:00
|
|
|
else:
|
|
|
|
return redirect(url_for('main.dashboard'))
|
2017-09-09 02:32:04 -04:00
|
|
|
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)
|
2017-07-06 17:15:02 -04:00
|
|
|
|
2017-10-19 11:59:17 -04:00
|
|
|
@vmanager.route('/vmremove/<int:unit_id>', methods=['GET', 'POST'])
|
2017-10-15 07:38:38 -04:00
|
|
|
@login_required
|
2017-10-19 11:59:17 -04:00
|
|
|
def remove(unit_id=0):
|
|
|
|
data = { 'unit_id': int(unit_id),
|
2017-10-29 17:06:54 -04:00
|
|
|
'type': 'kvm' }
|
2017-10-19 11:59:17 -04:00
|
|
|
deploy = Deployment.query.filter_by(machine_id=int(unit_id)).first()
|
2017-10-15 07:38:38 -04:00
|
|
|
if current_user.is_administrator():
|
|
|
|
if deploy.protected is not True:
|
|
|
|
try:
|
2017-12-14 18:00:02 -05:00
|
|
|
query = contact_proxmaster(data, 'status')
|
|
|
|
if query['status'] == 'running':
|
|
|
|
query = contact_proxmaster(data, 'stop')
|
|
|
|
flash('Machine {} force stopped'.format(unit_id))
|
|
|
|
time.sleep(7)
|
2017-10-19 11:59:17 -04:00
|
|
|
query = contact_proxmaster(data, 'remove')
|
|
|
|
flash('Machine {} terminated'.format(unit_id))
|
2017-10-15 07:38:38 -04:00
|
|
|
deploy.deleted = True
|
|
|
|
deploy.enabled = False
|
|
|
|
deploy.warning = False
|
|
|
|
db.session.commit()
|
2017-12-14 18:00:02 -05:00
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.error(e)
|
2017-10-19 11:59:17 -04:00
|
|
|
flash('Cannot delete machine {}'.format(unit_id))
|
2017-10-15 07:38:38 -04:00
|
|
|
return redirect(url_for('admin.list_recyclebin'))
|
|
|
|
else:
|
2017-10-19 11:59:17 -04:00
|
|
|
current_app.logger.warning('Deployment id:{} is protected! Cannot be removed'.format(unit_id))
|
2017-10-15 07:38:38 -04:00
|
|
|
else:
|
2017-10-19 11:59:17 -04:00
|
|
|
current_app.logger.warning('[WARNING] Unauthorized attempt to remove Deployment id:{}'.format(unit_id))
|
2017-10-15 07:38:38 -04:00
|
|
|
abort(404)
|
2017-07-28 21:31:44 -04:00
|
|
|
|
2017-10-19 11:59:17 -04:00
|
|
|
@vmanager.route('/command/<cmd>/<int:unit_id>')
|
2017-03-08 13:53:09 -05:00
|
|
|
@login_required
|
2017-10-19 11:59:17 -04:00
|
|
|
def command(cmd=None, unit_id=0):
|
2017-03-08 13:53:09 -05:00
|
|
|
#checks whether this is a valid command
|
2017-10-19 11:59:17 -04:00
|
|
|
valid_commands = ['status', 'start', 'shutdown', 'stop', 'vmvnc']
|
2017-03-08 13:53:09 -05:00
|
|
|
if not cmd in valid_commands:
|
2017-06-06 09:49:32 -04:00
|
|
|
current_app.logger.warning(cmd + ' is not a valid command!')
|
2017-03-08 13:53:09 -05:00
|
|
|
abort(404)
|
|
|
|
|
2017-07-30 17:10:08 -04:00
|
|
|
#work with enabled deploys only that you own.
|
2017-07-06 17:15:02 -04:00
|
|
|
result = current_user.inv_deployments.filter_by(enabled=True)
|
2017-03-08 13:53:09 -05:00
|
|
|
inventory = []
|
|
|
|
for invcls in result:
|
2017-06-26 10:31:30 -04:00
|
|
|
inventory.extend([invcls.machine_id])
|
2017-06-06 09:49:32 -04:00
|
|
|
|
2017-10-29 17:06:54 -04:00
|
|
|
data = { 'type': 'kvm',
|
2017-10-19 11:59:17 -04:00
|
|
|
'unit_id': int(unit_id) }
|
2017-09-20 20:08:58 -04:00
|
|
|
|
2017-06-06 09:49:32 -04:00
|
|
|
if current_user.is_administrator():
|
2017-10-19 11:59:17 -04:00
|
|
|
#current_app.logger.warning('[ADMIN] Access override for cube id:{}'.format(unitunit__id))
|
|
|
|
db_result = contact_proxmaster(data, cmd)
|
2017-07-06 17:15:02 -04:00
|
|
|
if cmd == 'vmvnc':
|
|
|
|
return redirect(db_result['url'])
|
|
|
|
else:
|
2017-10-19 11:59:17 -04:00
|
|
|
#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))
|
2017-07-06 17:15:02 -04:00
|
|
|
#TODO: log ips
|
|
|
|
else:
|
2017-10-19 11:59:17 -04:00
|
|
|
db_result = contact_proxmaster(data, cmd)
|
2017-07-06 17:15:02 -04:00
|
|
|
#print(db_result)
|
2017-07-15 20:48:18 -04:00
|
|
|
if cmd == 'vmvnc':
|
|
|
|
return redirect(db_result['url'])
|
2017-03-08 13:53:09 -05:00
|
|
|
abort(404)
|
|
|
|
|