opennebula vm sync/query application added
This commit is contained in:
parent
5de973b204
commit
d61a7e670f
16 changed files with 163 additions and 11 deletions
0
nicohack202002/uncloud/opennebula/__init__.py
Normal file
0
nicohack202002/uncloud/opennebula/__init__.py
Normal file
3
nicohack202002/uncloud/opennebula/admin.py
Normal file
3
nicohack202002/uncloud/opennebula/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
5
nicohack202002/uncloud/opennebula/apps.py
Normal file
5
nicohack202002/uncloud/opennebula/apps.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class OpennebulaConfig(AppConfig):
|
||||
name = 'opennebula'
|
|
@ -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)
|
||||
|
23
nicohack202002/uncloud/opennebula/migrations/0001_initial.py
Normal file
23
nicohack202002/uncloud/opennebula/migrations/0001_initial.py
Normal file
|
@ -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)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
0
nicohack202002/uncloud/opennebula/migrations/__init__.py
Normal file
0
nicohack202002/uncloud/opennebula/migrations/__init__.py
Normal file
8
nicohack202002/uncloud/opennebula/models.py
Normal file
8
nicohack202002/uncloud/opennebula/models.py
Normal file
|
@ -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)
|
8
nicohack202002/uncloud/opennebula/serializers.py
Normal file
8
nicohack202002/uncloud/opennebula/serializers.py
Normal file
|
@ -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']
|
3
nicohack202002/uncloud/opennebula/tests.py
Normal file
3
nicohack202002/uncloud/opennebula/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
14
nicohack202002/uncloud/opennebula/views.py
Normal file
14
nicohack202002/uncloud/opennebula/views.py
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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/<int:vmid>/', oneviews.VMDetail.as_view(), name='vm_detail'),
|
||||
]
|
||||
|
||||
#urlpatterns = [
|
||||
# path('admin/', admin.site.urls),
|
||||
# path('api/', include('api.urls')),
|
||||
#]
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.contrib.auth.models import AbstractUser
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
pass
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
xmltodict
|
||||
djangorestframework
|
||||
django
|
||||
done
|
||||
stripe
|
||||
flask
|
||||
|
|
Loading…
Reference in a new issue