From f84b205f15a34663b4395bcd89d38d89a08b392f Mon Sep 17 00:00:00 2001 From: Jordan Rodgers Date: Mon, 19 Feb 2018 23:31:02 -0500 Subject: [PATCH] rework console so migrating vms isnt dumb and ssh tunnels can be torn down --- proxstar/__init__.py | 29 ++++++++++++++++++++++------- proxstar/proxmox.py | 7 ++++--- proxstar/static/js/script.js | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/proxstar/__init__.py b/proxstar/__init__.py index 45562c2..588c2d4 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -68,7 +68,7 @@ if 'generate_pool_cache' not in scheduler: id='generate_pool_cache', scheduled_time=datetime.datetime.utcnow(), func=generate_pool_cache_task, - interval=60) + interval=90) if 'process_expiring_vms' not in scheduler: scheduler.cron( @@ -176,7 +176,7 @@ def vm_power(vmid, action): return '', 403 -@app.route("/vm//console", methods=['POST']) +@app.route("/console/vm/", methods=['POST']) @auth.oidc_auth def vm_console(vmid): user = build_user_dict(session, db) @@ -186,13 +186,28 @@ def vm_console(vmid): port = str(5900 + int(vmid)) token = add_vnc_target(port) node = "{}.csh.rit.edu".format(get_vm_node(proxmox, vmid)) - print("Creating SSH tunnel to {} for VM {}.".format(node, vmid)) - try: + tunnel = next((tunnel for tunnel in ssh_tunnels + if tunnel.local_bind_port == int(port)), None) + if tunnel: + if tunnel.ssh_host != node: + print( + "Tunnel already exists for VM {} to the wrong Proxmox node.". + format(vmid)) + tunnel.stop() + ssh_tunnels.remove(tunnel) + print("Creating SSH tunnel to {} for VM {}.".format( + node, vmid)) + tunnel = start_ssh_tunnel(node, port) + ssh_tunnels.append(tunnel) + start_vm_vnc(proxmox, vmid, port) + else: + print("Tunnel already exists to {} for VM {}.".format( + node, vmid)) + else: + print("Creating SSH tunnel to {} for VM {}.".format(node, vmid)) tunnel = start_ssh_tunnel(node, port) ssh_tunnels.append(tunnel) - except: - print('SSH tunnel already exists.') - start_vm_vnc(proxmox, vmid) + start_vm_vnc(proxmox, vmid, port) return token, 200 else: return '', 403 diff --git a/proxstar/proxmox.py b/proxstar/proxmox.py index 91e0044..07d1e4b 100644 --- a/proxstar/proxmox.py +++ b/proxstar/proxmox.py @@ -25,7 +25,7 @@ def get_vms_for_user(proxmox, db, user): pools = get_pools(proxmox, db) if user not in pools: if is_user(user) and not is_rtp(user): - proxmox.pools.post(poolid=user, comment='Managed by Proxstar') + proxmox.pools.post(poolid=user, comment='Managed by Proxstar') else: return [] vms = proxmox.pools(user).get()['members'] @@ -253,10 +253,11 @@ def change_vm_mem(proxmox, vmid, mem): node.qemu(vmid).config.put(memory=mem) -def start_vm_vnc(proxmox, vmid): +def start_vm_vnc(proxmox, vmid, port): node = proxmox.nodes(get_vm_node(proxmox, vmid)) + port = str(int(port) - 5900) node.qemu(vmid).monitor.post( - command="change vnc 127.0.0.1:{}".format(vmid)) + command="change vnc 127.0.0.1:{}".format(port)) def get_isos(proxmox, storage): diff --git a/proxstar/static/js/script.js b/proxstar/static/js/script.js index af774dc..f30d53f 100644 --- a/proxstar/static/js/script.js +++ b/proxstar/static/js/script.js @@ -808,7 +808,7 @@ function hide_for_template(obj) { $("#console-vm").click(function(){ const vmname = $(this).data('vmname'); const vmid = $(this).data('vmid'); - fetch(`/vm/${vmid}/console`, { + fetch(`https://proxstar-vnc.csh.rit.edu/console/vm/${vmid}`, { credentials: 'same-origin', method: 'post' }).then((response) => {