Skip to content

Commit 433c845

Browse files
committed
Use Redis to link VNC tokens to VMID
1 parent aa1c483 commit 433c845

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

HACKING/build_env.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
podman build . --tag=proxstar

proxstar/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ def vm_power(vmid, action):
252252
connect_proxmox()
253253
if user.rtp or int(vmid) in user.allowed_vms:
254254
vm = VM(vmid)
255+
vnc_token_key = f'vnc_token|{vmid}'
256+
vnc_token = redis_conn.get(vnc_token_key).decode('utf-8') # For deleting the token from redis later
257+
print(f'vnc_token = {vnc_token}')
255258
if action == 'start':
256259
vmconfig = vm.config
257260
usage_check = user.check_usage(vmconfig['cores'], vmconfig['memory'], 0)
@@ -260,16 +263,18 @@ def vm_power(vmid, action):
260263
vm.start()
261264
elif action == 'stop':
262265
vm.stop()
263-
# TODO (willnilges): Replace with remove target function or something
264-
# send_stop_ssh_tunnel(vmid)
266+
delete_vnc_target(token=vnc_token)
267+
redis_conn.delete(vnc_token_key)
265268
elif action == 'shutdown':
266269
vm.shutdown()
267-
# send_stop_ssh_tunnel(vmid)
270+
delete_vnc_target(token=vnc_token)
271+
redis_conn.delete(vnc_token_key)
268272
elif action == 'reset':
269273
vm.reset()
270274
elif action == 'suspend':
271275
vm.suspend()
272-
# send_stop_ssh_tunnel(vmid)
276+
delete_vnc_target(token=vnc_token)
277+
redis_conn.delete(vnc_token_key)
273278
elif action == 'resume':
274279
vm.resume()
275280
return '', 200
@@ -290,6 +295,7 @@ def vm_console(vmid):
290295
)
291296
node = f'{vm.node}.csh.rit.edu'
292297
token = add_vnc_target(node, vnc_port)
298+
redis_conn.set(f'vnc_token|{vmid}', str(token)) # Store the VNC token in Redis.
293299
return {
294300
'host': app.config['VNC_HOST'],
295301
'port': app.config['VNC_PORT'],

proxstar/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77
from flask import Flask
88
from rq import get_current_job
9+
from redis import Redis
910
from sqlalchemy import create_engine
1011
from sqlalchemy.orm import sessionmaker
1112

@@ -32,7 +33,6 @@
3233
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), 'config.py')
3334
app.config.from_pyfile(config)
3435

35-
3636
def connect_db():
3737
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
3838
Base.metadata.bind = engine

proxstar/vnc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ def add_vnc_target(node, port):
5656
return token
5757

5858

59-
def delete_vnc_target(node, port):
59+
def delete_vnc_target(node=None, port=None, token=None):
6060
targets = get_vnc_targets()
61-
target = next((target for target in targets if target['host'] == f'{node}:{port}'), None)
61+
if node is not None and port is not None:
62+
target = next((target for target in targets if target['host'] == f'{node}:{port}'), None)
63+
elif token is not None:
64+
target = next((target for target in targets if target['token'] == f'{token}'), None)
65+
else:
66+
raise ValueError("Need either a node and port, or a token.")
6267
if target:
6368
targets.remove(target)
6469
target_file = open(app.config['WEBSOCKIFY_TARGET_FILE'], 'w')
6570
for target in targets:
6671
target_file.write(f"{target['token']}: {target['host']}\n")
6772
target_file.close()
73+
else:
74+
raise LookupError("Target does not exist")
6875

6976

7077
def open_vnc_session(vmid, node, proxmox_user, proxmox_pass):

0 commit comments

Comments
 (0)