From d61a7e670f562af0ce0d858715b4bf14997c05e3 Mon Sep 17 00:00:00 2001 From: meow Date: Fri, 21 Feb 2020 20:33:37 +0500 Subject: [PATCH] opennebula vm sync/query application added --- nicohack202002/uncloud/opennebula/__init__.py | 0 nicohack202002/uncloud/opennebula/admin.py | 3 ++ nicohack202002/uncloud/opennebula/apps.py | 5 +++ .../opennebula/management/commands/syncvm.py | 40 +++++++++++++++++++ .../opennebula/migrations/0001_initial.py | 23 +++++++++++ .../migrations/0002_auto_20200221_1024.py | 23 +++++++++++ .../migrations/0003_auto_20200221_1113.py | 21 ++++++++++ .../uncloud/opennebula/migrations/__init__.py | 0 nicohack202002/uncloud/opennebula/models.py | 8 ++++ .../uncloud/opennebula/serializers.py | 8 ++++ nicohack202002/uncloud/opennebula/tests.py | 3 ++ nicohack202002/uncloud/opennebula/views.py | 14 +++++++ nicohack202002/uncloud/uncloud/settings.py | 11 ++--- nicohack202002/uncloud/uncloud/urls.py | 11 +++-- nicohack202002/uncloud/uncloud_auth/models.py | 1 + requirements.txt | 3 ++ 16 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 nicohack202002/uncloud/opennebula/__init__.py create mode 100644 nicohack202002/uncloud/opennebula/admin.py create mode 100644 nicohack202002/uncloud/opennebula/apps.py create mode 100644 nicohack202002/uncloud/opennebula/management/commands/syncvm.py create mode 100644 nicohack202002/uncloud/opennebula/migrations/0001_initial.py create mode 100644 nicohack202002/uncloud/opennebula/migrations/0002_auto_20200221_1024.py create mode 100644 nicohack202002/uncloud/opennebula/migrations/0003_auto_20200221_1113.py create mode 100644 nicohack202002/uncloud/opennebula/migrations/__init__.py create mode 100644 nicohack202002/uncloud/opennebula/models.py create mode 100644 nicohack202002/uncloud/opennebula/serializers.py create mode 100644 nicohack202002/uncloud/opennebula/tests.py create mode 100644 nicohack202002/uncloud/opennebula/views.py diff --git a/nicohack202002/uncloud/opennebula/__init__.py b/nicohack202002/uncloud/opennebula/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nicohack202002/uncloud/opennebula/admin.py b/nicohack202002/uncloud/opennebula/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/nicohack202002/uncloud/opennebula/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/nicohack202002/uncloud/opennebula/apps.py b/nicohack202002/uncloud/opennebula/apps.py new file mode 100644 index 0000000..0750576 --- /dev/null +++ b/nicohack202002/uncloud/opennebula/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class OpennebulaConfig(AppConfig): + name = 'opennebula' diff --git a/nicohack202002/uncloud/opennebula/management/commands/syncvm.py b/nicohack202002/uncloud/opennebula/management/commands/syncvm.py new file mode 100644 index 0000000..5ea451d --- /dev/null +++ b/nicohack202002/uncloud/opennebula/management/commands/syncvm.py @@ -0,0 +1,40 @@ +import os +import json + +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model +from xmlrpc.client import ServerProxy as RPCClient + +from xmltodict import parse + +from opennebula.models import VM as VMModel + +OCA_SESSION_STRING = os.environ.get('OCASECRETS', '') + + +class Command(BaseCommand): + help = 'Syncronize VM information from OpenNebula' + + def add_arguments(self, parser): + pass + + def handle(self, *args, **options): + with RPCClient('https://opennebula.ungleich.ch:2634/RPC2') as rpc_client: + success, response, *_ = rpc_client.one.vmpool.infoextended( + OCA_SESSION_STRING, -2, -1, -1, -1 + ) + if success: + vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM'] + for i, vm in enumerate(vms): + vm_id = vm['ID'] + vm_owner = vm['UNAME'] + try: + user = get_user_model().objects.get(username=vm_owner) + except get_user_model().DoesNotExist: + user = get_user_model().objects.create_user(username=vm_owner) + + vm_object = VMModel.objects.create(vmid=vm_id, owner=user, data=vm) + vm_object.save() + else: + print(response) + diff --git a/nicohack202002/uncloud/opennebula/migrations/0001_initial.py b/nicohack202002/uncloud/opennebula/migrations/0001_initial.py new file mode 100644 index 0000000..e2c6a1f --- /dev/null +++ b/nicohack202002/uncloud/opennebula/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.3 on 2020-02-21 10:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='VM', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('vmid', models.IntegerField()), + ('owner', models.CharField(max_length=128)), + ('data', models.CharField(max_length=65536)), + ], + ), + ] diff --git a/nicohack202002/uncloud/opennebula/migrations/0002_auto_20200221_1024.py b/nicohack202002/uncloud/opennebula/migrations/0002_auto_20200221_1024.py new file mode 100644 index 0000000..43b7442 --- /dev/null +++ b/nicohack202002/uncloud/opennebula/migrations/0002_auto_20200221_1024.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.3 on 2020-02-21 10:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('opennebula', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='vm', + name='data', + field=models.CharField(max_length=65536, null=True), + ), + migrations.AlterField( + model_name='vm', + name='owner', + field=models.CharField(max_length=128, null=True), + ), + ] diff --git a/nicohack202002/uncloud/opennebula/migrations/0003_auto_20200221_1113.py b/nicohack202002/uncloud/opennebula/migrations/0003_auto_20200221_1113.py new file mode 100644 index 0000000..9ccc22e --- /dev/null +++ b/nicohack202002/uncloud/opennebula/migrations/0003_auto_20200221_1113.py @@ -0,0 +1,21 @@ +# Generated by Django 3.0.3 on 2020-02-21 11:13 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('opennebula', '0002_auto_20200221_1024'), + ] + + operations = [ + migrations.AlterField( + model_name='vm', + name='owner', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/nicohack202002/uncloud/opennebula/migrations/__init__.py b/nicohack202002/uncloud/opennebula/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nicohack202002/uncloud/opennebula/models.py b/nicohack202002/uncloud/opennebula/models.py new file mode 100644 index 0000000..cd1a044 --- /dev/null +++ b/nicohack202002/uncloud/opennebula/models.py @@ -0,0 +1,8 @@ +from django.db import models +from django.contrib.auth import get_user_model + + +class VM(models.Model): + vmid = models.IntegerField() + owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) + data = models.CharField(max_length=65536, null=True) diff --git a/nicohack202002/uncloud/opennebula/serializers.py b/nicohack202002/uncloud/opennebula/serializers.py new file mode 100644 index 0000000..c84f2ab --- /dev/null +++ b/nicohack202002/uncloud/opennebula/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from opennebula.models import VM + + +class VMSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = VM + fields = ['vmid', 'owner', 'data'] diff --git a/nicohack202002/uncloud/opennebula/tests.py b/nicohack202002/uncloud/opennebula/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/nicohack202002/uncloud/opennebula/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/nicohack202002/uncloud/opennebula/views.py b/nicohack202002/uncloud/opennebula/views.py new file mode 100644 index 0000000..f706815 --- /dev/null +++ b/nicohack202002/uncloud/opennebula/views.py @@ -0,0 +1,14 @@ +from rest_framework import viewsets, generics +from .models import VM +from .serializers import VMSerializer + + +class VMList(generics.ListAPIView): + queryset = VM.objects.all() + serializer_class = VMSerializer + + +class VMDetail(generics.RetrieveAPIView): + lookup_field = 'vmid' + queryset = VM.objects.all() + serializer_class = VMSerializer diff --git a/nicohack202002/uncloud/uncloud/settings.py b/nicohack202002/uncloud/uncloud/settings.py index be38f8f..1e8f358 100644 --- a/nicohack202002/uncloud/uncloud/settings.py +++ b/nicohack202002/uncloud/uncloud/settings.py @@ -39,7 +39,8 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'rest_framework', 'uncloud_api', - 'uncloud_auth' + 'uncloud_auth', + 'opennebula' ] MIDDLEWARE = [ @@ -159,7 +160,7 @@ STATIC_URL = '/static/' # Uncommitted file -import uncloud.secrets - -import stripe -stripe.api_key = uncloud.secrets.STRIPE_KEY +# import uncloud.secrets +# +# import stripe +# stripe.api_key = uncloud.secrets.STRIPE_KEY diff --git a/nicohack202002/uncloud/uncloud/urls.py b/nicohack202002/uncloud/uncloud/urls.py index cb50432..f5804c9 100644 --- a/nicohack202002/uncloud/uncloud/urls.py +++ b/nicohack202002/uncloud/uncloud/urls.py @@ -19,6 +19,8 @@ from django.urls import path, include from rest_framework import routers from uncloud_api import views +from opennebula import views as oneviews + router = routers.DefaultRouter() router.register(r'users', views.UserViewSet) router.register(r'groups', views.GroupViewSet) @@ -28,10 +30,7 @@ router.register(r'groups', views.GroupViewSet) urlpatterns = [ path('', include(router.urls)), path('admin/', admin.site.urls), - path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), + path('vm/list/', oneviews.VMList.as_view(), name='vm_list'), + path('vm/detail//', oneviews.VMDetail.as_view(), name='vm_detail'), ] - -#urlpatterns = [ -# path('admin/', admin.site.urls), -# path('api/', include('api.urls')), -#] diff --git a/nicohack202002/uncloud/uncloud_auth/models.py b/nicohack202002/uncloud/uncloud_auth/models.py index 4c9c171..3d30525 100644 --- a/nicohack202002/uncloud/uncloud_auth/models.py +++ b/nicohack202002/uncloud/uncloud_auth/models.py @@ -1,4 +1,5 @@ from django.contrib.auth.models import AbstractUser + class User(AbstractUser): pass diff --git a/requirements.txt b/requirements.txt index 29c21b4..1abfbed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,6 @@ +xmltodict +djangorestframework +django done stripe flask