2020-04-24 16:34:29 +00:00
|
|
|
import json
|
|
|
|
import socket
|
2020-11-05 09:34:31 +00:00
|
|
|
|
2020-05-14 13:45:37 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2018-06-13 08:50:36 +00:00
|
|
|
from django.http import HttpResponse, Http404
|
2020-04-24 16:34:29 +00:00
|
|
|
from libvirt import libvirtError
|
2020-11-05 09:34:31 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
from accounts.models import UserInstance, UserSSHKey
|
2020-05-14 13:45:37 +00:00
|
|
|
from computes.models import Compute
|
2018-06-21 12:53:35 +00:00
|
|
|
from vrtManager.instance import wvmInstance
|
2020-04-24 16:34:29 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
OS_VERSIONS = ["latest", ""]
|
2018-06-13 08:50:36 +00:00
|
|
|
OS_UUID = "iid-dswebvirtcloud"
|
|
|
|
|
2018-09-05 06:30:08 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
def os_index(request):
|
2020-04-24 16:34:29 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-11-05 09:34:31 +00:00
|
|
|
response = "\n".join(OS_VERSIONS)
|
2018-06-13 08:50:36 +00:00
|
|
|
return HttpResponse(response)
|
|
|
|
|
2018-09-05 06:30:08 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
def os_metadata_json(request, version):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:param version:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
if version == "latest":
|
2018-06-13 08:50:36 +00:00
|
|
|
ip = get_client_ip(request)
|
|
|
|
hostname = get_hostname_by_ip(ip)
|
2020-11-05 09:34:31 +00:00
|
|
|
response = {"uuid": OS_UUID, "hostname": hostname}
|
2018-06-13 08:50:36 +00:00
|
|
|
return HttpResponse(json.dumps(response))
|
|
|
|
else:
|
2020-11-05 09:34:31 +00:00
|
|
|
err = "Invalid version: %(version)s" % {"version": version}
|
2018-06-13 08:50:36 +00:00
|
|
|
raise Http404(err)
|
|
|
|
|
2018-09-05 06:30:08 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
def os_userdata(request, version):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:param version:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-11-05 09:34:31 +00:00
|
|
|
if version == "latest":
|
2018-06-13 08:50:36 +00:00
|
|
|
ip = get_client_ip(request)
|
|
|
|
hostname = get_hostname_by_ip(ip)
|
2020-11-05 09:34:31 +00:00
|
|
|
vname = hostname.split(".")[0]
|
2020-09-25 12:57:17 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
instance_keys = []
|
|
|
|
userinstances = UserInstance.objects.filter(instance__name=vname)
|
2020-09-25 12:57:17 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
for ui in userinstances:
|
|
|
|
keys = UserSSHKey.objects.filter(user=ui.user)
|
|
|
|
for k in keys:
|
|
|
|
instance_keys.append(k.keypublic)
|
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return render(request, "user_data", locals())
|
2018-06-13 08:50:36 +00:00
|
|
|
else:
|
2020-11-05 09:34:31 +00:00
|
|
|
err = "Invalid version: %(version)s" % {"version": version}
|
2018-06-13 08:50:36 +00:00
|
|
|
raise Http404(err)
|
|
|
|
|
2018-09-05 06:30:08 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
def get_client_ip(request):
|
2020-04-24 16:34:29 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-11-05 09:34:31 +00:00
|
|
|
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
|
2018-06-13 08:50:36 +00:00
|
|
|
if x_forwarded_for:
|
2020-11-05 09:34:31 +00:00
|
|
|
ip = x_forwarded_for.split(",")[-1].strip()
|
2018-06-13 08:50:36 +00:00
|
|
|
else:
|
2020-11-05 09:34:31 +00:00
|
|
|
ip = request.META.get("REMOTE_ADDR")
|
2018-06-13 08:50:36 +00:00
|
|
|
return ip
|
|
|
|
|
2018-09-05 06:30:08 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
def get_hostname_by_ip(ip):
|
2020-04-24 16:34:29 +00:00
|
|
|
"""
|
|
|
|
:param ip:
|
|
|
|
:return:
|
|
|
|
"""
|
2018-09-05 06:30:08 +00:00
|
|
|
try:
|
|
|
|
addrs = socket.gethostbyaddr(ip)
|
2019-09-10 06:48:31 +00:00
|
|
|
except:
|
|
|
|
addrs = [ip]
|
2018-06-13 08:50:36 +00:00
|
|
|
return addrs[0]
|
2018-06-21 12:53:35 +00:00
|
|
|
|
2018-09-05 06:30:08 +00:00
|
|
|
|
2020-05-14 13:45:37 +00:00
|
|
|
def get_vdi_url(request, compute_id, vname):
|
2020-04-24 16:34:29 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:param vname:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-05-14 13:45:37 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2020-09-25 12:57:17 +00:00
|
|
|
|
2018-06-21 12:53:35 +00:00
|
|
|
try:
|
2022-11-02 05:54:35 +00:00
|
|
|
conn = wvmInstance(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
vname
|
|
|
|
)
|
2018-06-21 12:53:35 +00:00
|
|
|
|
|
|
|
fqdn = get_hostname_by_ip(compute.hostname)
|
2020-05-14 13:45:37 +00:00
|
|
|
url = f"{conn.get_console_type()}://{fqdn}:{conn.get_console_port()}"
|
2018-06-21 12:53:35 +00:00
|
|
|
response = url
|
|
|
|
return HttpResponse(response)
|
2020-09-25 12:57:17 +00:00
|
|
|
except libvirtError:
|
2020-11-05 09:34:31 +00:00
|
|
|
err = "Error getting VDI URL for %(name)s" % {"name": vname}
|
2018-06-21 12:53:35 +00:00
|
|
|
raise Http404(err)
|