mirror of
https://github.com/retspen/webvirtcloud
synced 2024-12-24 15:15:22 +00:00
add new application datasource. provides basic interface for cloud-init tool (hostname, root ssh authorized keys)
This commit is contained in:
parent
43a8fb6dc1
commit
82eb5abe52
10 changed files with 100 additions and 0 deletions
|
@ -5,6 +5,7 @@
|
|||
|
||||
* User can add SSH public key to root in Instance (Tested only Ubuntu)
|
||||
* User can change root password in Instance (Tested only Ubuntu)
|
||||
* Supports cloud-init datasource interface
|
||||
|
||||
### Warning!!!
|
||||
|
||||
|
@ -219,6 +220,14 @@ login: admin
|
|||
password: admin
|
||||
</pre>
|
||||
|
||||
### Cloud-init
|
||||
Currently supports only root ssh authorized keys and hostname. Example configuration of the cloud-init client follows.
|
||||
```
|
||||
datasource:
|
||||
OpenStack:
|
||||
metadata_urls: [ "http://webvirtcloud.domain.com/datasource" ]
|
||||
```
|
||||
|
||||
### How To Update
|
||||
```bash
|
||||
git pull
|
||||
|
|
0
datasource/__init__.py
Normal file
0
datasource/__init__.py
Normal file
3
datasource/admin.py
Normal file
3
datasource/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
0
datasource/migrations/__init__.py
Normal file
0
datasource/migrations/__init__.py
Normal file
3
datasource/models.py
Normal file
3
datasource/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
6
datasource/templates/user_data
Normal file
6
datasource/templates/user_data
Normal file
|
@ -0,0 +1,6 @@
|
|||
#cloud-config
|
||||
{% if instance_keys %}
|
||||
ssh_authorized_keys:
|
||||
{% for key in instance_keys %} - {{ key }}{% endfor %}
|
||||
{% endif %}
|
||||
|
3
datasource/tests.py
Normal file
3
datasource/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
11
datasource/urls.py
Normal file
11
datasource/urls.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
from django.conf.urls import url
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^openstack/$',
|
||||
views.os_index, name='ds_openstack_index'),
|
||||
url(r'^openstack/(?P<version>[\w\-\.]+)/meta_data.json$',
|
||||
views.os_metadata_json, name='ds_openstack_metadata'),
|
||||
url(r'^openstack/(?P<version>[\w\-\.]+)/user_data$',
|
||||
views.os_userdata, name='ds_openstack_userdata'),
|
||||
]
|
64
datasource/views.py
Normal file
64
datasource/views.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
from django.shortcuts import render
|
||||
from django.http import HttpResponse, Http404
|
||||
from accounts.models import UserInstance, UserSSHKey
|
||||
import json
|
||||
import socket
|
||||
|
||||
OS_VERSIONS = [ 'latest', '' ]
|
||||
OS_UUID = "iid-dswebvirtcloud"
|
||||
|
||||
def os_index(request):
|
||||
response = '\n'.join(OS_VERSIONS)
|
||||
return HttpResponse(response)
|
||||
|
||||
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)
|
||||
response = { 'uuid': OS_UUID, 'hostname': hostname }
|
||||
return HttpResponse(json.dumps(response))
|
||||
else:
|
||||
err = 'Invalid version: %s' % version
|
||||
raise Http404(err)
|
||||
|
||||
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:
|
||||
err = 'Invalid version: %s' % version
|
||||
raise Http404(err)
|
||||
|
||||
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
|
||||
|
||||
def get_hostname_by_ip(ip):
|
||||
addrs = socket.gethostbyaddr(ip)
|
||||
return addrs[0]
|
|
@ -9,6 +9,7 @@ urlpatterns = patterns('',
|
|||
url(r'^accounts/', include('accounts.urls')),
|
||||
url(r'^computes/', include('computes.urls')),
|
||||
url(r'^logs/', include('logs.urls')),
|
||||
url(r'^datasource/', include('datasource.urls')),
|
||||
|
||||
url(r'^compute/(?P<compute_id>[0-9]+)/storages/$',
|
||||
'storages.views.storages', name='storages'),
|
||||
|
|
Loading…
Reference in a new issue