This commit is contained in:
Will Nilges 2022-07-10 19:46:48 -04:00
parent 1bfdf398bb
commit 7555232926
2 changed files with 46 additions and 40 deletions

View file

@ -271,29 +271,32 @@ 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.
""" """
# proxmox = connect_proxmox() # 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}' # config = f'args: -object secret,id=secvnc{self.id},data={self.id} -vnc
path = f'/etc/pve/local/qemu-server/{self.id}.conf' # 127.0.0.1:{int(self.id)+5900},password-secret=secvnc{self.id}'
with paramiko.SSHClient() as ssh: # path = f'/etc/pve/local/qemu-server/{self.id}.conf'
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # with paramiko.SSHClient() as ssh:
ssh.connect( # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.node, # ssh.connect(
port=22, # self.node,
username=ssh_user, # port=22,
key_filename='proxmox_ssh_key', # username=ssh_user,
passphrase=ssh_pass, # key_filename='proxmox_ssh_key',
) # passphrase=ssh_pass,
ssh.exec_command( # )
f"if grep -- '{config}' {path}; then echo identical config found; else sed -i /dev/null '/-vnc/d' {path}" # ssh.exec_command(
) # YOLO # f"if grep -- '{config}' {path}; then echo identical config found;
ssh.exec_command( # else sed -i /dev/null '/-vnc/d' {path}"
f"if grep -- '-vnc' {path}; then echo found config; else echo {config} >> {path}; fi" # ) # YOLO
) # ssh.exec_command(
# f"if grep -- '-vnc' {path}; then echo found config; else echo {config} >> {path}; fi"
# )
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5)) @retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
def eject_iso(self): def eject_iso(self):

View file

@ -2,10 +2,10 @@ import os
import subprocess import subprocess
import time import time
import urllib.parse
import requests import requests
from flask import current_app as app from flask import current_app as app
from sshtunnel import SSHTunnelForwarder from sshtunnel import SSHTunnelForwarder
import urllib.parse
from proxstar import logging from proxstar import logging
from proxstar.util import gen_password from proxstar.util import gen_password
@ -75,27 +75,27 @@ def open_vnc_session(vmid, node, proxmox_user, proxmox_pass):
# Get Proxmox API ticket and CSRF_Prevention_Token # Get Proxmox API ticket and CSRF_Prevention_Token
# TODO (willnilges): Use Proxmoxer to get this information # TODO (willnilges): Use Proxmoxer to get this information
# TODO (willnilges): Report errors # TODO (willnilges): Report errors
data = {"username": proxmox_user, "password": proxmox_pass} data = {'username': proxmox_user, 'password': proxmox_pass}
response_data = requests.post( response_data = requests.post(
f"https://{node}.csh.rit.edu:8006/" + "api2/json/access/ticket", f'https://{node}.csh.rit.edu:8006/' + 'api2/json/access/ticket',
verify=False, verify=False,
data=data, data=data,
).json()["data"] ).json()['data']
if response_data is None: if response_data is None:
raise AuthenticationError( raise requests.AuthenticationError(
"Could not authenticate against `ticket` endpoint! Check uname/password" 'Could not authenticate against `ticket` endpoint! Check uname/password'
) )
csrf_prevention_token = response_data['CSRFPreventionToken'] csrf_prevention_token = response_data['CSRFPreventionToken']
ticket = response_data['ticket'] ticket = response_data['ticket']
proxy_params = {"node": node, "vmid": str(vmid), "websocket": '1', "generate-password": '0'} proxy_params = {'node': node, 'vmid': str(vmid), 'websocket': '1', 'generate-password': '0'}
vncproxy_response_data = requests.post( vncproxy_response_data = requests.post(
f"https://{node}.csh.rit.edu:8006/api2/json/nodes/{node}/qemu/{vmid}/vncproxy", f'https://{node}.csh.rit.edu:8006/api2/json/nodes/{node}/qemu/{vmid}/vncproxy',
verify=False, verify=False,
timeout=5, timeout=5,
params=proxy_params, params=proxy_params,
headers={"CSRFPreventionToken": csrf_prevention_token}, headers={'CSRFPreventionToken': csrf_prevention_token},
cookies={"PVEAuthCookie": ticket}, cookies={'PVEAuthCookie': ticket},
).json()["data"] ).json()['data']
return urllib.parse.quote_plus(vncproxy_response_data['ticket']), vncproxy_response_data['port'] return urllib.parse.quote_plus(vncproxy_response_data['ticket']), vncproxy_response_data['port']
@ -118,18 +118,21 @@ 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.
# 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
port = 5900 + int(vmid) print(f'This code is useless')
tunnel = next((tunnel for tunnel in ssh_tunnels if tunnel.local_bind_port == port), None)
if tunnel:
logging.info('tearing down SSH tunnel for VM %s', vmid)
try:
tunnel.stop()
except:
pass pass
ssh_tunnels.remove(tunnel) # port = 5900 + int(vmid)
delete_vnc_target(port) # tunnel = next((tunnel for tunnel in ssh_tunnels if tunnel.local_bind_port == port), None)
# if tunnel:
# logging.info('tearing down SSH tunnel for VM %s', vmid)
# try:
# tunnel.stop()
# except:
# pass
# ssh_tunnels.remove(tunnel)
# delete_vnc_target(port)
def send_stop_ssh_tunnel(vmid): def send_stop_ssh_tunnel(vmid):