send vnc teardown commands to proxstar-vnc

This commit is contained in:
Jordan Rodgers 2018-04-04 15:30:19 -04:00
parent 03baef5be0
commit 8808389b46
3 changed files with 23 additions and 4 deletions

View file

@ -207,10 +207,10 @@ def vm_power(vmid, action):
vm.start() vm.start()
elif action == 'stop': elif action == 'stop':
vm.stop() vm.stop()
stop_ssh_tunnel(vmid, ssh_tunnels) send_stop_ssh_tunnel(vmid)
elif action == 'shutdown': elif action == 'shutdown':
vm.shutdown() vm.shutdown()
stop_ssh_tunnel(vmid, ssh_tunnels) send_stop_ssh_tunnel(vmid)
elif action == 'reset': elif action == 'reset':
vm.reset() vm.reset()
elif action == 'suspend': elif action == 'suspend':
@ -222,6 +222,15 @@ def vm_power(vmid, action):
return '', 403 return '', 403
@app.route("/console/vm/<string:vmid>/stop", methods=['POST'])
def vm_console_stop(vmid):
if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']:
stop_ssh_tunnel(vmid, ssh_tunnels)
return '', 200
else:
return '', 403
@app.route("/console/vm/<string:vmid>", methods=['POST']) @app.route("/console/vm/<string:vmid>", methods=['POST'])
@auth.oidc_auth @auth.oidc_auth
def vm_console(vmid): def vm_console(vmid):
@ -367,7 +376,7 @@ def delete(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
proxmox = connect_proxmox() proxmox = connect_proxmox()
if user.rtp or int(vmid) in user.allowed_vms: if user.rtp or int(vmid) in user.allowed_vms:
stop_ssh_tunnel(vmid, ssh_tunnels) send_stop_ssh_tunnel(vmid)
# Submit the delete VM task to RQ # Submit the delete VM task to RQ
q.enqueue(delete_vm_task, vmid) q.enqueue(delete_vm_task, vmid)
return '', 200 return '', 200

View file

@ -199,6 +199,6 @@ def setup_template_task(template_id, name, user, password, cores, memory):
def cleanup_vnc_task(): def cleanup_vnc_task():
requests.post( requests.post(
'https://proxstar.csh.rit.edu/console/cleanup', "https://{}/console/cleanup".format(app.config['SERVER_NAME']),
data={'token': app.config['VNC_CLEANUP_TOKEN']}, data={'token': app.config['VNC_CLEANUP_TOKEN']},
verify=False) verify=False)

View file

@ -1,5 +1,6 @@
import os import os
import time import time
import requests
import subprocess import subprocess
from sshtunnel import SSHTunnelForwarder from sshtunnel import SSHTunnelForwarder
from proxstar.util import * from proxstar.util import *
@ -91,9 +92,18 @@ def stop_ssh_tunnel(vmid, ssh_tunnels):
tunnel = next((tunnel for tunnel in ssh_tunnels tunnel = next((tunnel for tunnel in ssh_tunnels
if tunnel.local_bind_port == port), None) if tunnel.local_bind_port == port), None)
if tunnel: if tunnel:
print("Tearing down SSH tunnel for VM {}.".format(vmid))
try: try:
tunnel.stop() tunnel.stop()
except: except:
pass pass
ssh_tunnels.remove(tunnel) ssh_tunnels.remove(tunnel)
delete_vnc_target(port) delete_vnc_target(port)
def send_stop_ssh_tunnel(vmid):
requests.post(
"https://{}/console/vm/{}/stop".format(app.config['SERVER_NAME'],
vmid),
data={'token': app.config['VNC_CLEANUP_TOKEN']},
verify=False)