mirror of
https://github.com/ComputerScienceHouse/proxstar.git
synced 2025-03-09 15:40:09 +00:00
Use Redis to link VNC tokens to VMID
This commit is contained in:
parent
aa1c4834f4
commit
433c845a35
4 changed files with 22 additions and 7 deletions
2
HACKING/build_env.sh
Executable file
2
HACKING/build_env.sh
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/bin/bash
|
||||||
|
podman build . --tag=proxstar
|
|
@ -252,6 +252,9 @@ def vm_power(vmid, action):
|
||||||
connect_proxmox()
|
connect_proxmox()
|
||||||
if user.rtp or int(vmid) in user.allowed_vms:
|
if user.rtp or int(vmid) in user.allowed_vms:
|
||||||
vm = VM(vmid)
|
vm = VM(vmid)
|
||||||
|
vnc_token_key = f'vnc_token|{vmid}'
|
||||||
|
vnc_token = redis_conn.get(vnc_token_key).decode('utf-8') # For deleting the token from redis later
|
||||||
|
print(f'vnc_token = {vnc_token}')
|
||||||
if action == 'start':
|
if action == 'start':
|
||||||
vmconfig = vm.config
|
vmconfig = vm.config
|
||||||
usage_check = user.check_usage(vmconfig['cores'], vmconfig['memory'], 0)
|
usage_check = user.check_usage(vmconfig['cores'], vmconfig['memory'], 0)
|
||||||
|
@ -260,16 +263,18 @@ def vm_power(vmid, action):
|
||||||
vm.start()
|
vm.start()
|
||||||
elif action == 'stop':
|
elif action == 'stop':
|
||||||
vm.stop()
|
vm.stop()
|
||||||
# TODO (willnilges): Replace with remove target function or something
|
delete_vnc_target(token=vnc_token)
|
||||||
# send_stop_ssh_tunnel(vmid)
|
redis_conn.delete(vnc_token_key)
|
||||||
elif action == 'shutdown':
|
elif action == 'shutdown':
|
||||||
vm.shutdown()
|
vm.shutdown()
|
||||||
# send_stop_ssh_tunnel(vmid)
|
delete_vnc_target(token=vnc_token)
|
||||||
|
redis_conn.delete(vnc_token_key)
|
||||||
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)
|
delete_vnc_target(token=vnc_token)
|
||||||
|
redis_conn.delete(vnc_token_key)
|
||||||
elif action == 'resume':
|
elif action == 'resume':
|
||||||
vm.resume()
|
vm.resume()
|
||||||
return '', 200
|
return '', 200
|
||||||
|
@ -290,6 +295,7 @@ def vm_console(vmid):
|
||||||
)
|
)
|
||||||
node = f'{vm.node}.csh.rit.edu'
|
node = f'{vm.node}.csh.rit.edu'
|
||||||
token = add_vnc_target(node, vnc_port)
|
token = add_vnc_target(node, vnc_port)
|
||||||
|
redis_conn.set(f'vnc_token|{vmid}', str(token)) # Store the VNC token in Redis.
|
||||||
return {
|
return {
|
||||||
'host': app.config['VNC_HOST'],
|
'host': app.config['VNC_HOST'],
|
||||||
'port': app.config['VNC_PORT'],
|
'port': app.config['VNC_PORT'],
|
||||||
|
|
|
@ -6,6 +6,7 @@ import psycopg2
|
||||||
import requests
|
import requests
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from rq import get_current_job
|
from rq import get_current_job
|
||||||
|
from redis import Redis
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
@ -32,7 +33,6 @@ else:
|
||||||
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), 'config.py')
|
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), 'config.py')
|
||||||
app.config.from_pyfile(config)
|
app.config.from_pyfile(config)
|
||||||
|
|
||||||
|
|
||||||
def connect_db():
|
def connect_db():
|
||||||
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
|
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
|
||||||
Base.metadata.bind = engine
|
Base.metadata.bind = engine
|
||||||
|
|
|
@ -56,15 +56,22 @@ def add_vnc_target(node, port):
|
||||||
return token
|
return token
|
||||||
|
|
||||||
|
|
||||||
def delete_vnc_target(node, port):
|
def delete_vnc_target(node=None, port=None, token=None):
|
||||||
targets = get_vnc_targets()
|
targets = get_vnc_targets()
|
||||||
target = next((target for target in targets if target['host'] == f'{node}:{port}'), None)
|
if node is not None and port is not None:
|
||||||
|
target = next((target for target in targets if target['host'] == f'{node}:{port}'), None)
|
||||||
|
elif token is not None:
|
||||||
|
target = next((target for target in targets if target['token'] == f'{token}'), None)
|
||||||
|
else:
|
||||||
|
raise ValueError("Need either a node and port, or a token.")
|
||||||
if target:
|
if target:
|
||||||
targets.remove(target)
|
targets.remove(target)
|
||||||
target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w')
|
target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w')
|
||||||
for target in targets:
|
for target in targets:
|
||||||
target_file.write(f"{target['token']}: {target['host']}\n")
|
target_file.write(f"{target['token']}: {target['host']}\n")
|
||||||
target_file.close()
|
target_file.close()
|
||||||
|
else:
|
||||||
|
raise LookupError("Target does not exist")
|
||||||
|
|
||||||
|
|
||||||
def open_vnc_session(vmid, node, proxmox_user, proxmox_pass):
|
def open_vnc_session(vmid, node, proxmox_user, proxmox_pass):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue