diff --git a/config.py b/config.py index b1f24c9..d83d099 100644 --- a/config.py +++ b/config.py @@ -53,5 +53,7 @@ REDIS_HOST = environ.get('PROXSTAR_REDIS_HOST', 'localhost') REDIS_PORT = int(environ.get('PROXSTAR_REDIS_PORT', '6379')) # VNC -WEBSOCKIFY_PATH = environ.get('PROXSTAR_WEBSOCKIFY_PATH', '/opt/app-root/bin/websockify') -WEBSOCKIFY_TARGET_FILE = environ.get('PROXSTAR_WEBSOCKIFY_TARGET_FILE', '/opt/app-root/src/targets') +WEBSOCKIFY_PATH = environ.get('PROXSTAR_WEBSOCKIFY_PATH', + '/opt/app-root/bin/websockify') +WEBSOCKIFY_TARGET_FILE = environ.get('PROXSTAR_WEBSOCKIFY_TARGET_FILE', + '/opt/app-root/src/targets') diff --git a/proxstar/__init__.py b/proxstar/__init__.py index 2e10090..8b028b3 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -33,7 +33,8 @@ app.config["GIT_REVISION"] = subprocess.check_output( with open('proxmox_ssh_key', 'w') as key: key.write(app.config['PROXMOX_SSH_KEY']) -start_websockify(app.config['WEBSOCKIFY_PATH'], app.config['WEBSOCKIFY_TARGET_FILE']) +start_websockify(app.config['WEBSOCKIFY_PATH'], + app.config['WEBSOCKIFY_TARGET_FILE']) ssh_tunnels = [] @@ -196,6 +197,7 @@ def vm_console(vmid): rtp = 'rtp' in session['userinfo']['groups'] proxmox = connect_proxmox() if rtp or int(vmid) in get_user_allowed_vms(proxmox, db, user): + start_vm_vnc(proxmox, vmid) port = str(5900 + int(vmid)) token = add_vnc_target(port) node = "{}.csh.rit.edu".format(get_vm_node(proxmox, vmid)) @@ -464,6 +466,5 @@ def exit_handler(): atexit.register(exit_handler) - if __name__ == "__main__": app.run() diff --git a/proxstar/proxmox.py b/proxstar/proxmox.py index e993dc6..ba6e43c 100644 --- a/proxstar/proxmox.py +++ b/proxstar/proxmox.py @@ -259,6 +259,13 @@ def change_vm_mem(proxmox, vmid, mem): node.qemu(vmid).config.put(memory=mem) +def start_vm_vnc(proxmox, vmid): + node = proxmox.nodes(get_vm_node(proxmox, vmid)) + port = str(5900 + int(vmid)) + node.qemu(vmid).monitor.post( + command="change vnc 127.0.0.1:{}".format(port)) + + def get_isos(proxmox, storage): isos = [] for iso in proxmox.nodes('proxmox01').storage(storage).content.get(): diff --git a/proxstar/vnc.py b/proxstar/vnc.py index 378c377..45ee9e8 100644 --- a/proxstar/vnc.py +++ b/proxstar/vnc.py @@ -1,3 +1,4 @@ +import os import time import subprocess from sshtunnel import SSHTunnelForwarder @@ -10,9 +11,8 @@ def start_websockify(websockify_path, target_file): if not result.stdout: subprocess.call( [ - websockify_path, '8081', '--token-plugin', - 'TokenFile', '--token-source', target_file, - '-D' + websockify_path, '8081', '--token-plugin', 'TokenFile', + '--token-source', target_file, '-D' ], stdout=subprocess.PIPE) @@ -33,16 +33,17 @@ def stop_websockify(): def get_vnc_targets(): - target_file = open(app.config['WEBSOCKIFY_TARGET_FILE']) targets = [] - for line in target_file: - target_dict = dict() - values = line.strip().split(':') - target_dict['token'] = values[0] - target_dict['port'] = values[2] - targets.append(target_dict) - target_file.close() - return (targets) + if os.path.exists(app.config['WEBSOCKIFY_TARGET_FILE']): + target_file = open(app.config['WEBSOCKIFY_TARGET_FILE']) + for line in target_file: + target_dict = dict() + values = line.strip().split(':') + target_dict['token'] = values[0] + target_dict['port'] = values[2] + targets.append(target_dict) + target_file.close() + return targets def add_vnc_target(port):