From 03baef5be054cbd0ddf9d5a659b54b144ecaa8a7 Mon Sep 17 00:00:00 2001 From: Jordan Rodgers Date: Mon, 2 Apr 2018 13:47:57 -0400 Subject: [PATCH] teardown ssh/vnc stuff after shutdown and reset requests --- proxstar/__init__.py | 14 +++----------- proxstar/vnc.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/proxstar/__init__.py b/proxstar/__init__.py index 4d07a2b..2f1ffdb 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -207,8 +207,10 @@ def vm_power(vmid, action): vm.start() elif action == 'stop': vm.stop() + stop_ssh_tunnel(vmid, ssh_tunnels) elif action == 'shutdown': vm.shutdown() + stop_ssh_tunnel(vmid, ssh_tunnels) elif action == 'reset': vm.reset() elif action == 'suspend': @@ -365,17 +367,7 @@ def delete(vmid): user = User(session['userinfo']['preferred_username']) proxmox = connect_proxmox() if user.rtp or int(vmid) in user.allowed_vms: - # Tear down the SSH tunnel and VNC stuff for the VM - port = 5900 + int(vmid) - tunnel = next((tunnel for tunnel in ssh_tunnels - if tunnel.local_bind_port == port), None) - if tunnel: - try: - tunnel.stop() - except: - pass - ssh_tunnels.remove(tunnel) - delete_vnc_target(port) + stop_ssh_tunnel(vmid, ssh_tunnels) # Submit the delete VM task to RQ q.enqueue(delete_vm_task, vmid) return '', 200 diff --git a/proxstar/vnc.py b/proxstar/vnc.py index 5397849..eb07f0b 100644 --- a/proxstar/vnc.py +++ b/proxstar/vnc.py @@ -83,3 +83,17 @@ def start_ssh_tunnel(node, port): local_bind_address=('127.0.0.1', port)) server.start() return server + + +def stop_ssh_tunnel(vmid, ssh_tunnels): + # Tear down the SSH tunnel and VNC target entry for a given VM + port = 5900 + int(vmid) + tunnel = next((tunnel for tunnel in ssh_tunnels + if tunnel.local_bind_port == port), None) + if tunnel: + try: + tunnel.stop() + except: + pass + ssh_tunnels.remove(tunnel) + delete_vnc_target(port)