2018-06-13 08:50:36 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.http import HttpResponse, Http404
|
|
|
|
from accounts.models import UserInstance, UserSSHKey
|
2018-06-21 12:53:35 +00:00
|
|
|
from instances.models import Instance
|
|
|
|
from vrtManager.instance import wvmInstance
|
|
|
|
from libvirt import libvirtError
|
2018-06-13 08:50:36 +00:00
|
|
|
import json
|
|
|
|
import socket
|
|
|
|
|
2018-09-05 06:30:08 +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):
|
|
|
|
response = '\n'.join(OS_VERSIONS)
|
|
|
|
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:
|
|
|
|
"""
|
|
|
|
|
|
|
|
if version == 'latest':
|
|
|
|
ip = get_client_ip(request)
|
|
|
|
hostname = get_hostname_by_ip(ip)
|
2019-09-10 06:48:31 +00:00
|
|
|
response = {'uuid': OS_UUID, 'hostname': hostname}
|
2018-06-13 08:50:36 +00:00
|
|
|
return HttpResponse(json.dumps(response))
|
|
|
|
else:
|
2018-09-05 06:30:08 +00:00
|
|
|
err = 'Invalid version: {}'.format(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:
|
|
|
|
"""
|
|
|
|
if version == 'latest':
|
|
|
|
ip = get_client_ip(request)
|
|
|
|
hostname = get_hostname_by_ip(ip)
|
|
|
|
vname = hostname.split('.')[0]
|
|
|
|
|
|
|
|
instance_keys = []
|
|
|
|
userinstances = UserInstance.objects.filter(instance__name=vname)
|
|
|
|
|
|
|
|
for ui in userinstances:
|
|
|
|
keys = UserSSHKey.objects.filter(user=ui.user)
|
|
|
|
for k in keys:
|
|
|
|
instance_keys.append(k.keypublic)
|
|
|
|
|
|
|
|
return render(request, 'user_data', locals())
|
|
|
|
else:
|
2018-09-05 06:30:08 +00:00
|
|
|
err = 'Invalid version: {}'.format(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):
|
|
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
|
|
|
if x_forwarded_for:
|
|
|
|
ip = x_forwarded_for.split(',')[-1].strip()
|
|
|
|
else:
|
|
|
|
ip = request.META.get('REMOTE_ADDR')
|
|
|
|
return ip
|
|
|
|
|
2018-09-05 06:30:08 +00:00
|
|
|
|
2018-06-13 08:50:36 +00:00
|
|
|
def get_hostname_by_ip(ip):
|
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
|
|
|
|
2018-06-21 12:53:35 +00:00
|
|
|
def get_vdi_url(request, vname):
|
|
|
|
instance = Instance.objects.get(name=vname)
|
|
|
|
compute = instance.compute
|
|
|
|
data = {}
|
|
|
|
try:
|
|
|
|
conn = wvmInstance(compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
instance.name)
|
|
|
|
|
|
|
|
fqdn = get_hostname_by_ip(compute.hostname)
|
|
|
|
url = "{}://{}:{}".format(conn.get_console_type(), fqdn, conn.get_console_port())
|
|
|
|
response = url
|
|
|
|
return HttpResponse(response)
|
|
|
|
|
|
|
|
except libvirtError as lib_err:
|
|
|
|
err = "Error getting vdi url for {}".format(vname)
|
|
|
|
raise Http404(err)
|
|
|
|
|