1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 23:55:24 +00:00
webvirtcloud/console/views.py

116 lines
3.9 KiB
Python
Raw Normal View History

2015-02-27 12:25:41 +00:00
import re
2020-07-17 08:04:21 +00:00
from vrtManager.util import randomUUID
from django.http.response import HttpResponseServerError
2015-02-27 12:25:41 +00:00
from django.shortcuts import render
from django.utils.translation import gettext_lazy as _
2020-04-24 16:34:29 +00:00
from libvirt import libvirtError
2020-07-17 08:04:21 +00:00
from accounts.models import UserInstance
2020-07-17 08:04:21 +00:00
from appsettings.settings import app_settings
2015-02-27 12:25:41 +00:00
from instances.models import Instance
from vrtManager.instance import wvmInstance
from webvirtcloud.settings import (
WS_PUBLIC_HOST,
WS_PUBLIC_PATH,
WS_PUBLIC_PORT,
SOCKETIO_PUBLIC_HOST,
SOCKETIO_PUBLIC_PORT,
2022-11-02 05:54:35 +00:00
SOCKETIO_PUBLIC_PATH,
)
2015-02-27 12:25:41 +00:00
def console(request):
"""
:param request:
:return:
"""
2018-05-31 09:38:03 +00:00
console_error = None
2015-02-27 12:25:41 +00:00
2020-09-25 12:57:17 +00:00
if request.method == "GET":
token = request.GET.get("token", "")
view_type = request.GET.get("view", "lite")
2022-11-02 05:54:35 +00:00
view_only = request.GET.get("view_only", app_settings.CONSOLE_VIEW_ONLY.lower())
2020-09-25 12:57:17 +00:00
scale = request.GET.get("scale", app_settings.CONSOLE_SCALE.lower())
resize_session = request.GET.get(
2022-11-02 05:54:35 +00:00
"resize_session", app_settings.CONSOLE_RESIZE_SESSION.lower()
)
2020-09-25 12:57:17 +00:00
clip_viewport = request.GET.get(
2022-11-02 05:54:35 +00:00
"clip_viewport", app_settings.CONSOLE_CLIP_VIEWPORT.lower()
)
2015-02-27 12:25:41 +00:00
try:
2020-09-25 12:57:17 +00:00
temptoken = token.split("-", 1)
2015-02-27 12:25:41 +00:00
host = int(temptoken[0])
uuid = temptoken[1]
2022-11-02 05:54:35 +00:00
if not request.user.is_superuser and not request.user.has_perm(
"instances.view_instances"
):
try:
userInstance = UserInstance.objects.get(
2022-11-02 05:54:35 +00:00
instance__compute_id=host,
instance__uuid=uuid,
user__id=request.user.id,
)
instance = Instance.objects.get(compute_id=host, uuid=uuid)
except UserInstance.DoesNotExist:
instance = None
2022-11-02 05:54:35 +00:00
console_error = _(
"User does not have permission to access console or host/instance not exist"
)
return HttpResponseServerError(console_error)
else:
instance = Instance.objects.get(compute_id=host, uuid=uuid)
2022-11-02 05:54:35 +00:00
2020-07-13 09:33:09 +00:00
conn = wvmInstance(
instance.compute.hostname,
instance.compute.login,
instance.compute.password,
instance.compute.type,
instance.name,
)
2015-02-27 12:25:41 +00:00
console_type = conn.get_console_type()
console_websocket_port = conn.get_console_websocket_port()
console_passwd = conn.get_console_passwd()
except libvirtError:
2015-02-27 12:25:41 +00:00
console_type = None
console_websocket_port = None
console_passwd = None
2019-11-08 17:07:02 +00:00
ws_port = console_websocket_port if console_websocket_port else WS_PUBLIC_PORT
2015-02-27 12:25:41 +00:00
ws_host = WS_PUBLIC_HOST if WS_PUBLIC_HOST else request.get_host()
2020-09-25 12:57:17 +00:00
ws_path = WS_PUBLIC_PATH if WS_PUBLIC_PATH else "/"
2015-02-27 12:25:41 +00:00
2020-09-25 12:57:17 +00:00
if ":" in ws_host:
ws_host = re.sub(":[0-9]+", "", ws_host)
2015-02-27 12:25:41 +00:00
2020-09-25 12:57:17 +00:00
if console_type == "vnc" or console_type == "spice":
2020-07-17 12:32:06 +00:00
console_page = "console-" + console_type + "-" + view_type + ".html"
response = render(request, console_page, locals())
elif console_type == "pty":
2022-11-02 05:54:35 +00:00
socketio_host = (
SOCKETIO_PUBLIC_HOST if SOCKETIO_PUBLIC_HOST else request.get_host()
)
socketio_port = SOCKETIO_PUBLIC_PORT if SOCKETIO_PUBLIC_PORT else 6081
socketio_path = SOCKETIO_PUBLIC_PATH if SOCKETIO_PUBLIC_PATH else "/"
if ":" in socketio_host:
socketio_host = re.sub(":[0-9]+", "", socketio_host)
response = render(request, "console-xterm.html", locals())
2015-02-27 12:25:41 +00:00
else:
2020-07-17 12:32:06 +00:00
if console_type is None:
2022-11-02 05:54:35 +00:00
console_error = _(
"Fail to get console. Please check the console configuration of your VM."
)
2020-07-17 12:32:06 +00:00
else:
2022-11-02 05:54:35 +00:00
console_error = _("Console type '%(type)s' has not support") % {
"type": console_type
}
2020-09-25 12:57:17 +00:00
response = render(request, "console-vnc-lite.html", locals())
2015-02-27 12:25:41 +00:00
2020-09-25 12:57:17 +00:00
response.set_cookie("token", token)
2015-02-27 12:25:41 +00:00
return response