diff --git a/README.md b/README.md index 8ec15f5..ae6d2c8 100644 --- a/README.md +++ b/README.md @@ -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 +### 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 diff --git a/datasource/__init__.py b/datasource/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/datasource/admin.py b/datasource/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/datasource/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/datasource/migrations/__init__.py b/datasource/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/datasource/models.py b/datasource/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/datasource/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/datasource/templates/user_data b/datasource/templates/user_data new file mode 100644 index 0000000..4a94483 --- /dev/null +++ b/datasource/templates/user_data @@ -0,0 +1,6 @@ +#cloud-config +{% if instance_keys %} +ssh_authorized_keys: +{% for key in instance_keys %} - {{ key }}{% endfor %} +{% endif %} + diff --git a/datasource/tests.py b/datasource/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/datasource/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/datasource/urls.py b/datasource/urls.py new file mode 100644 index 0000000..9183d41 --- /dev/null +++ b/datasource/urls.py @@ -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[\w\-\.]+)/meta_data.json$', + views.os_metadata_json, name='ds_openstack_metadata'), + url(r'^openstack/(?P[\w\-\.]+)/user_data$', + views.os_userdata, name='ds_openstack_userdata'), +] diff --git a/datasource/views.py b/datasource/views.py new file mode 100644 index 0000000..9264717 --- /dev/null +++ b/datasource/views.py @@ -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] diff --git a/webvirtcloud/urls.py b/webvirtcloud/urls.py index 8cfa9d9..e5ab3fc 100644 --- a/webvirtcloud/urls.py +++ b/webvirtcloud/urls.py @@ -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[0-9]+)/storages/$', 'storages.views.storages', name='storages'),