diff --git a/app/admin/routes.py b/app/admin/routes.py index 18dd391..3d1dc0a 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -189,7 +189,7 @@ def dashboard(user_pid=0): for unit_id in inv_deploycubeids: rrd[unit_id] = {} data = { 'unit_id': int(unit_id), - 'type': 'deploy' } + 'type': 'kvm' } query = contact_proxmaster(data, 'vmrrd') if query['status'] == 'UNREACHABLE': flash('Deploy #{} is unreachable. Support is notified.'.format(str(unit_id))) diff --git a/app/main/routes.py b/app/main/routes.py index 2bb01e0..465b2b2 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -94,10 +94,10 @@ def dashboard(): rrd = {} statuses = {} #current_app.logger.warning(str(inv_deploycubeids)) - for user_id in inv_deploycubeids: - rrd[user_id] = {} - data = { 'user_id': int(user_id), - 'type': 'deploy' } + for unit_id in inv_deploycubeids: + rrd[unit_id] = {} + data = { 'user_id': int(unit_id), + 'type': 'kvm' } try: query = contact_proxmaster(data, 'vmrrd') except Exception as e: @@ -121,6 +121,6 @@ def dashboard(): 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 )) + #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 8ee5e0c..4361a7b 100644 --- a/app/models.py +++ b/app/models.py @@ -220,6 +220,7 @@ def contact_proxmaster(data, method): try: db_result = requests.post( url, data=data_json, headers={"content-type": "application/json"}, timeout=30 ) proxjson = db_result.json() + #current_app.logger.info('grid> {}'.format(str(db_result))) return proxjson except: return { 'status': 'UNREACHABLE' } @@ -234,7 +235,7 @@ class Router(db.Model): machine_id = db.Column(db.BigInteger) #unit_id class Bridge(db.Model): - __tablename__ = 'bridge' + __tablename__ = 'bridges' pid = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.pid')) #FK date_created = db.Column(db.DateTime, default=datetime.utcnow) diff --git a/app/templates/main/dashboard.html b/app/templates/main/dashboard.html index 53070a1..3139043 100644 --- a/app/templates/main/dashboard.html +++ b/app/templates/main/dashboard.html @@ -375,7 +375,7 @@ addEventListener("DOMContentLoaded", function() { - + {% else %} {% endif %} diff --git a/app/templates/main/footer_colored.html b/app/templates/main/footer_colored.html index 223b8eb..0c2ee6a 100644 --- a/app/templates/main/footer_colored.html +++ b/app/templates/main/footer_colored.html @@ -1,7 +1,7 @@ diff --git a/app/vmanager/routes.py b/app/vmanager/routes.py index 2ef43c6..f73582a 100644 --- a/app/vmanager/routes.py +++ b/app/vmanager/routes.py @@ -43,52 +43,58 @@ def createvm(): if current_user.confirmed and form.validate_on_submit(): selected_region = Region.query.filter_by(pid=int(form.region.data)).first() - #TODO: Filter switches for the selected region only! - selected_bridge = current_user.inv_bridges.filter_by(deleted=False).all() - if selected_bridge == []: - #no switches in the account. create one... + #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... data = { 'clientid': str(current_user.pid), 'clientemail': str(current_user.email), 'region': str(selected_region.name), - 'type': 'bridge' + 'type': 'br' } #create bridge unit query = contact_proxmaster(data, 'create') - if query is not None: - bridge = Bridge(user_id=int(current_user.pid)) + 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'] + bridge_phyid = query['phyid'] + bridge = Bridge(user_id=int(current_user.pid), bridge_id=bridge_id) db.session.add(bridge) db.session.commit() flash('New point created successfully in region "{}".'.format(str(selected_region.description))) - newbridge = True 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. data = { 'unit_id': int(selected_bridge.bridge_id), - 'type': 'bridge' } - query = contact_proxmaster(data, 'status') - if query is not None: + 'type': 'br' } + query = contact_proxmaster(data, 'query') + if query['status'] == 'query_success': newbridge = False + #machine will be installed where the switch physically is + region_name = query['region'] + slave_name = query['slave'] + bridge_phyid = query['phyid'] else: flash('Point found but cannot be used!') return redirect(url_for('main.dashboard')) - #machine will be installed where the switch physically is - slave_name = query['slave_name'] - bridge_id = query['bridge_id'] - + ### #create new machine... data = { 'clientid': str(current_user.pid), 'clientemail': str(current_user.email), 'hostname': 'c' + str(current_user.pid) + '-' + str(form.servername.data), - 'region': str(selected_region.name), + 'region': str(region_name), 'slave': str(slave_name), - 'type': 'deploy', + 'type': 'kvm', 'cpu': '1', 'mem': '512', 'hdd': '20', - 'bridge': str(bridge_id) + 'net0if': 'vmbr' + str(bridge_phyid) } try: query = contact_proxmaster(data, 'create') @@ -96,8 +102,8 @@ def createvm(): 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['unit_id'], machine_cpu=data['cpu'], machine_mem=data['mem'], machine_hdd=data['hdd'], enabled=True, protected=False, daysleft=15, warning=True, discount=0) + if query['status'] == 'kvm_created': + deployment = Deployment(user_id=int(current_user.pid), machine_alias=, 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))) @@ -154,23 +160,43 @@ def activate(itemid=0): return redirect(url_for('uinvoice.transactions')) current_app.logger.info('[{}] Charge deployment: {}'.format(owner.email, deploy.machine_id)) - router = current_user.inv_routers.filter_by(deleted=False).all() - if router == []: - + #TODO: Filter routers for the selected region only. switch should return slave name + selected_router = current_user.inv_routers.filter_by(deleted=False).first() + if selected_router is None: + #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: + flash('No bridge created yet. Cannot activate.') + 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 = { 'unit_id': int(selected_bridge.bridge_id), + 'type': 'br' } + query = contact_proxmaster(data, 'query') + if query['status'] == 'query_success': + #machine will be installed where the switch physically is + region_name = query['region'] + slave_name = query['slave'] + bridge_phyid = query['phyid'] + else: + flash('Point found but cannot be used!') + return redirect(url_for('main.dashboard')) + #no router. creating... data = { 'clientid': str(current_user.pid), 'clientemail': str(current_user.email), - 'hostname': 'c' + str(current_user.pid) + 'router', - 'region': str(selected_region.name), - 'slave': str(selected_slave), + 'hostname': 'r' + str(selected_address.ip) + '-b' + str(bridge_phyid), + 'region': str(region_name), + 'slave': str(slave_name), 'type': 'lxc', 'cpu': '1', 'mem': '128', 'hdd': '1', - 'eth0br': str(bridge_id), - 'eth0ip': '192.168.9.1', - 'eth1br': 'vmbr0', - 'eth1ip': str(selected_address.ip) + 'net0if': 'vmbr' + bridge_phyid, + 'net0ip': '192.168.9.1', + 'net1if': 'vmbr0', + 'net1ip': str(selected_address.ip), + 'net1gw': '87.120.110.1' #should be queried from the current region } try: query = contact_proxmaster(data, 'create') @@ -210,7 +236,7 @@ def activate(itemid=0): @login_required def remove(unit_id=0): data = { 'unit_id': int(unit_id), - 'type': 'deploy' } + 'type': 'kvm' } deploy = Deployment.query.filter_by(machine_id=int(unit_id)).first() if current_user.is_administrator(): if deploy.protected is not True: @@ -245,7 +271,7 @@ def command(cmd=None, unit_id=0): for invcls in result: inventory.extend([invcls.machine_id]) - data = { 'type': 'deploy', + data = { 'type': 'kvm', 'unit_id': int(unit_id) } if current_user.is_administrator():