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,
)
from proxstar.vnc import (
send_stop_ssh_tunnel,
stop_ssh_tunnel,
add_vnc_target,
start_ssh_tunnel,
get_vnc_targets,
delete_vnc_target,
stop_websockify,
@ -165,7 +162,8 @@ def not_found(e):
try:
user = User(session['userinfo']['preferred_username'])
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
@ -174,7 +172,8 @@ def forbidden(e):
try:
user = User(session['userinfo']['preferred_username'])
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
@ -273,15 +272,16 @@ def vm_power(vmid, action):
vm.start()
elif action == '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':
vm.shutdown()
send_stop_ssh_tunnel(vmid)
# send_stop_ssh_tunnel(vmid)
elif action == 'reset':
vm.reset()
elif action == 'suspend':
vm.suspend()
send_stop_ssh_tunnel(vmid)
# send_stop_ssh_tunnel(vmid)
elif action == 'resume':
vm.resume()
return '', 200
@ -289,13 +289,13 @@ def vm_power(vmid, action):
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>/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'])
@ -430,7 +430,7 @@ def delete(vmid):
user = User(session['userinfo']['preferred_username'])
connect_proxmox()
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
q.enqueue(delete_vm_task, vmid)
return '', 200
@ -606,7 +606,7 @@ def cleanup_vnc():
with open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w') as targets:
targets.truncate()
return '', 200
print("Got bad cleanup request")
print('Got bad cleanup request')
return '', 403
# if request.form['token'] == app.config['VNC_CLEANUP_TOKEN']:
# 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.user import User, get_vms_for_rtp
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)
@ -151,7 +150,7 @@ def process_expiring_vms_task():
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)
if 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.
# The whole scheduling system needs a lotta work.
try:
requests.post(
f'https://proxstar.csh.rit.edu/console/cleanup',
'https://proxstar.csh.rit.edu/console/cleanup',
data={'token': app.config['VNC_CLEANUP_TOKEN']},
verify=False,
)
except Exception as e:
except Exception as e: # pylint: disable=W0703
print(e)

View file

@ -1,10 +1,8 @@
import json
from sqlite3 import connect
import urllib
from flask import current_app as app
from tenacity import retry, stop_after_attempt, wait_fixed
import paramiko
from proxstar import db, starrs
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)
# )
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
with a pre-determined password, which proxstar can then proxy to a noVNC
instance.
FIXME (willnilges): Dead Code. Remove this function.
"""
# 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
# with a pre-determined password, which proxstar can then proxy to a noVNC
# instance.
# FIXME (willnilges): Dead Code. Remove this function.
# """
# proxmox = connect_proxmox()
# 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}'

View file

@ -17,12 +17,13 @@ def stop_websockify():
pids = result.stdout.splitlines()
for pid in pids:
subprocess.run(['kill', pid], stdout=subprocess.PIPE, check=False)
time.sleep(1)
if subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE, check=False).stdout:
time.sleep(5)
# FIXME (willnilges): Willard is lazy.
time.sleep(1)
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)
time.sleep(5)
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():
@ -119,11 +120,11 @@ def start_ssh_tunnel(node, port):
return server
def stop_ssh_tunnel(): # vmid, ssh_tunnels):
# FIXME (willnilges): Dead code. Delete this function.
# Tear down the SSH tunnel and VNC target entry for a given VM
print(f'This code is useless')
pass
# def stop_ssh_tunnel(): # vmid, ssh_tunnels):
# # FIXME (willnilges): Dead code. Delete this function.
# # Tear down the SSH tunnel and VNC target entry for a given VM
# print(f'This code is useless')
# pass
# port = 5900 + int(vmid)
# tunnel = next((tunnel for tunnel in ssh_tunnels if tunnel.local_bind_port == port), None)
# if tunnel:
@ -136,9 +137,9 @@ def stop_ssh_tunnel(): # vmid, ssh_tunnels):
# 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,
)
# 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,
# )