This commit is contained in:
Will Nilges 2022-07-11 22:55:16 -04:00
parent 5ca85bf55e
commit 987675326d
4 changed files with 44 additions and 45 deletions

View file

@ -46,10 +46,7 @@ from proxstar.db import (
set_template_info, set_template_info,
) )
from proxstar.vnc import ( from proxstar.vnc import (
send_stop_ssh_tunnel,
stop_ssh_tunnel,
add_vnc_target, add_vnc_target,
start_ssh_tunnel,
get_vnc_targets, get_vnc_targets,
delete_vnc_target, delete_vnc_target,
stop_websockify, stop_websockify,
@ -165,7 +162,8 @@ def not_found(e):
try: try:
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
return render_template('404.html', user=user, e=e), 404 return render_template('404.html', user=user, e=e), 404
except Exception as e: except KeyError as exception:
print(exception)
return render_template('404.html', user='chom', e=e), 404 return render_template('404.html', user='chom', e=e), 404
@ -174,7 +172,8 @@ def forbidden(e):
try: try:
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
return render_template('403.html', user=user, e=e), 403 return render_template('403.html', user=user, e=e), 403
except Exception as e: except KeyError as exception:
print(exception)
return render_template('403.html', user='chom', e=e), 403 return render_template('403.html', user='chom', e=e), 403
@ -273,15 +272,16 @@ def vm_power(vmid, action):
vm.start() vm.start()
elif action == 'stop': elif action == 'stop':
vm.stop() vm.stop()
send_stop_ssh_tunnel(vmid) # TODO (willnilges): Replace with remove target function or something
# send_stop_ssh_tunnel(vmid)
elif action == 'shutdown': elif action == 'shutdown':
vm.shutdown() vm.shutdown()
send_stop_ssh_tunnel(vmid) # send_stop_ssh_tunnel(vmid)
elif action == 'reset': elif action == 'reset':
vm.reset() vm.reset()
elif action == 'suspend': elif action == 'suspend':
vm.suspend() vm.suspend()
send_stop_ssh_tunnel(vmid) # send_stop_ssh_tunnel(vmid)
elif action == 'resume': elif action == 'resume':
vm.resume() vm.resume()
return '', 200 return '', 200
@ -289,13 +289,13 @@ def vm_power(vmid, action):
return '', 403 return '', 403
@app.route('/console/vm/<string:vmid>/stop', methods=['POST']) # @app.route('/console/vm/<string:vmid>/stop', methods=['POST'])
def vm_console_stop(vmid): # def vm_console_stop(vmid):
if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']: # if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']:
stop_ssh_tunnel(vmid, ssh_tunnels) # stop_ssh_tunnel(vmid, ssh_tunnels)
return '', 200 # return '', 200
else: # else:
return '', 403 # return '', 403
@app.route('/console/vm/<string:vmid>', methods=['POST']) @app.route('/console/vm/<string:vmid>', methods=['POST'])
@ -430,7 +430,7 @@ def delete(vmid):
user = User(session['userinfo']['preferred_username']) user = User(session['userinfo']['preferred_username'])
connect_proxmox() connect_proxmox()
if user.rtp or int(vmid) in user.allowed_vms: if user.rtp or int(vmid) in user.allowed_vms:
send_stop_ssh_tunnel(vmid) # 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
@ -606,7 +606,7 @@ def cleanup_vnc():
with open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w') as targets: with open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w') as targets:
targets.truncate() targets.truncate()
return '', 200 return '', 200
print("Got bad cleanup request") print('Got bad cleanup request')
return '', 403 return '', 403
# if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']: # if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']:
# for target in get_vnc_targets(): # for target in get_vnc_targets():

View file

@ -22,7 +22,6 @@ from proxstar.proxmox import connect_proxmox, get_pools
from proxstar.starrs import get_next_ip, register_starrs, delete_starrs from proxstar.starrs import get_next_ip, register_starrs, delete_starrs
from proxstar.user import User, get_vms_for_rtp from proxstar.user import User, get_vms_for_rtp
from proxstar.vm import VM, clone_vm, create_vm from proxstar.vm import VM, clone_vm, create_vm
from proxstar.vnc import send_stop_ssh_tunnel
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO) logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
@ -151,7 +150,7 @@ def process_expiring_vms_task():
vm.name, vm.id vm.name, vm.id
) )
) )
send_stop_ssh_tunnel(vm.id) # send_stop_ssh_tunnel(vm.id) # TODO (willnilges): Remove target from targets file
delete_vm_task(vm.id) delete_vm_task(vm.id)
if expiring_vms: if expiring_vms:
send_vm_expire_email(pool, expiring_vms) send_vm_expire_email(pool, expiring_vms)
@ -239,11 +238,12 @@ def cleanup_vnc_task():
# FIXME (willnilges): This is straight-up not working, no matter what I try. # FIXME (willnilges): This is straight-up not working, no matter what I try.
# The whole scheduling system needs a lotta work. # The whole scheduling system needs a lotta work.
try: try:
requests.post( requests.post(
f'https://proxstar.csh.rit.edu/console/cleanup', 'https://proxstar.csh.rit.edu/console/cleanup',
data={'token': app.config['VNC_CLEANUP_TOKEN']}, data={'token': app.config['VNC_CLEANUP_TOKEN']},
verify=False, verify=False,
) )
except Exception as e: except Exception as e: # pylint: disable=W0703
print(e) print(e)

View file

@ -1,10 +1,8 @@
import json import json
from sqlite3 import connect
import urllib import urllib
from flask import current_app as app from flask import current_app as app
from tenacity import retry, stop_after_attempt, wait_fixed from tenacity import retry, stop_after_attempt, wait_fixed
import paramiko
from proxstar import db, starrs from proxstar import db, starrs
from proxstar.db import delete_vm_expire, get_vm_expire from proxstar.db import delete_vm_expire, get_vm_expire
@ -271,12 +269,12 @@ class VM:
# command='change vnc 127.0.0.1:{}'.format(port) # command='change vnc 127.0.0.1:{}'.format(port)
# ) # )
def configure_vnc_in_vm_config(): # self, ssh_user, ssh_pass): # def configure_vnc_in_vm_config(): # self, ssh_user, ssh_pass):
"""Sets the vm up for VNC. Enables it to open a socket on localhost # """Sets the vm up for VNC. Enables it to open a socket on localhost
with a pre-determined password, which proxstar can then proxy to a noVNC # with a pre-determined password, which proxstar can then proxy to a noVNC
instance. # instance.
FIXME (willnilges): Dead Code. Remove this function. # FIXME (willnilges): Dead Code. Remove this function.
""" # """
# proxmox = connect_proxmox() # proxmox = connect_proxmox()
# config = f'args: -object secret,id=secvnc{self.id},data={self.id} -vnc # config = f'args: -object secret,id=secvnc{self.id},data={self.id} -vnc
# 127.0.0.1:{int(self.id)+5900},password-secret=secvnc{self.id}' # 127.0.0.1:{int(self.id)+5900},password-secret=secvnc{self.id}'

View file

@ -17,12 +17,13 @@ def stop_websockify():
pids = result.stdout.splitlines() pids = result.stdout.splitlines()
for pid in pids: for pid in pids:
subprocess.run(['kill', pid], stdout=subprocess.PIPE, check=False) subprocess.run(['kill', pid], stdout=subprocess.PIPE, check=False)
time.sleep(1) # FIXME (willnilges): Willard is lazy.
if subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE, check=False).stdout: time.sleep(1)
time.sleep(5)
if subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE, check=False).stdout: if subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE, check=False).stdout:
logging.info("websockify didn't stop, killing forcefully") time.sleep(5)
subprocess.run(['kill', '-9', pid], stdout=subprocess.PIPE, check=False) if subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE, check=False).stdout:
logging.info("websockify didn't stop, killing forcefully")
subprocess.run(['kill', '-9', pid], stdout=subprocess.PIPE, check=False)
def get_vnc_targets(): def get_vnc_targets():
@ -119,11 +120,11 @@ def start_ssh_tunnel(node, port):
return server return server
def stop_ssh_tunnel(): # vmid, ssh_tunnels): # def stop_ssh_tunnel(): # vmid, ssh_tunnels):
# FIXME (willnilges): Dead code. Delete this function. # # FIXME (willnilges): Dead code. Delete this function.
# Tear down the SSH tunnel and VNC target entry for a given VM # # Tear down the SSH tunnel and VNC target entry for a given VM
print(f'This code is useless') # print(f'This code is useless')
pass # pass
# port = 5900 + int(vmid) # port = 5900 + int(vmid)
# tunnel = next((tunnel for tunnel in ssh_tunnels if tunnel.local_bind_port == port), None) # tunnel = next((tunnel for tunnel in ssh_tunnels if tunnel.local_bind_port == port), None)
# if tunnel: # if tunnel:
@ -136,9 +137,9 @@ def stop_ssh_tunnel(): # vmid, ssh_tunnels):
# delete_vnc_target(port) # delete_vnc_target(port)
def send_stop_ssh_tunnel(vmid): # def send_stop_ssh_tunnel(vmid):
requests.post( # requests.post(
'https://{}/console/vm/{}/stop'.format(app.config['SERVER_NAME'], vmid), # 'https://{}/console/vm/{}/stop'.format(app.config['SERVER_NAME'], vmid),
data={'token': app.config['VNC_CLEANUP_TOKEN']}, # data={'token': app.config['VNC_CLEANUP_TOKEN']},
verify=False, # verify=False,
) # )