From 5d840de55c04920ea269992c0402090048642b11 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 17 Mar 2020 15:39:24 +0100 Subject: [PATCH] [opennebula] refresh formula, cleanup vm import/migration to uncloud --- .../commands/opennebula-to-uncloud.py | 34 +++++++++---------- uncloud/opennebula/models.py | 6 ++-- uncloud/opennebula/serializers.py | 6 ---- uncloud/opennebula/views.py | 29 +++++----------- 4 files changed, 29 insertions(+), 46 deletions(-) diff --git a/uncloud/opennebula/management/commands/opennebula-to-uncloud.py b/uncloud/opennebula/management/commands/opennebula-to-uncloud.py index 2f91f83..7b4b864 100644 --- a/uncloud/opennebula/management/commands/opennebula-to-uncloud.py +++ b/uncloud/opennebula/management/commands/opennebula-to-uncloud.py @@ -21,9 +21,8 @@ def convert_mac_to_int(mac_address: str): return mac_address -def get_vm_price(core, ram, storage, n_of_ipv4, n_of_ipv6): - storage = storage / 10 # Division by 10 because our base storage unit is 10 GB - total = 3 * core + 4 * ram + 3.5 * storage + 8 * n_of_ipv4 + 0 * n_of_ipv6 +def get_vm_price(core, ram, ssd_size, hdd_size, n_of_ipv4, n_of_ipv6): + total = 3 * core + 4 * ram + (3.5 * ssd_size/10.) + (1.5 * hdd_size/100.) + 8 * n_of_ipv4 + 0 * n_of_ipv6 # TODO: Find some reason about the following magical subtraction. total -= 8 @@ -82,17 +81,18 @@ class Command(BaseCommand): def handle(self, *args, **options): for one_vm in VMModel.objects.all(): - # Host on which the VM is currently residing - #host = VMHost.objects.filter(vms__icontains=one_vm.vmid).first() - # VCPU, RAM, Owner, Status - # TODO: Set actual status instead of hard coded 'active' - vm_id, cores, ram_in_gb = one_vm.vmid, one_vm.cores, one_vm.ram_in_gb - owner, status = one_vm.owner, 'active' + vmhost = VMHost.objects.get(hostname=one_vm.last_host) + cores = one_vm.cores + ram_in_gb = one_vm.ram_in_gb + owner = one_vm.owner + status = 'active' # Total Amount of SSD Storage # TODO: What would happen if the attached storage is not SSD but HDD? - total_storage_in_gb = sum([disk['size_in_gb'] for disk in one_vm.disks]) + + ssd_size = sum([ disk['size_in_gb'] for disk in one.disks if disk['pool_name'] in ['ssd', 'one'] ]) + hdd_size = sum([ disk['size_in_gb'] for disk in one.disks if disk['pool_name'] in ['hdd'] ]) # List of IPv4 addresses and Global IPv6 addresses ipv4, ipv6 = one_vm.ips @@ -101,18 +101,18 @@ class Command(BaseCommand): # instead of pseudo one we are putting currently creation_date = starting_date = ending_date = datetime.now(tz=timezone.utc) - # Price calculation - - # TODO: Make the following non-hardcoded + # Price calculation based on datacenterlight.ch one_time_price = 0 recurring_period = 'per_month' + recurring_price = get_vm_price(cores, ram_in_gb, + ssd_size, hdd_size, + len(ipv4), len(ipv6)) - recurring_price = get_vm_price(cores, ram_in_gb, total_storage_in_gb, len(ipv4), len(ipv6)) try: - vm_product = VMProduct.objects.get(vmid=vm_id) + vm_product = VMProduct.objects.get(name=one_vm.uncloud_name) except VMProduct.DoesNotExist: order = Order.objects.create( - owner=one_vm.owner, + owner=owner, creation_date=creation_date, starting_date=starting_date, ending_date=ending_date, @@ -121,7 +121,7 @@ class Command(BaseCommand): recurring_period=recurring_period ) vm_product, _ = VMProduct.objects.update_or_create( - vmid=vm_id, + name= defaults={ 'cores': cores, 'ram_in_gb': ram_in_gb, diff --git a/uncloud/opennebula/models.py b/uncloud/opennebula/models.py index 0748ff5..f5faeb5 100644 --- a/uncloud/opennebula/models.py +++ b/uncloud/opennebula/models.py @@ -9,9 +9,9 @@ class VM(models.Model): owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) data = JSONField() - def save(self, *args, **kwargs): - self.id = 'opennebula' + str(self.data.get("ID")) - super().save(*args, **kwargs) + @property + def uncloud_name(self): + return "opennebula-{}".format(self.vmid) @property def cores(self): diff --git a/uncloud/opennebula/serializers.py b/uncloud/opennebula/serializers.py index 64fe005..8e0c513 100644 --- a/uncloud/opennebula/serializers.py +++ b/uncloud/opennebula/serializers.py @@ -2,12 +2,6 @@ from rest_framework import serializers from opennebula.models import VM -class VMSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = VM - fields = '__all__' - - class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = VM diff --git a/uncloud/opennebula/views.py b/uncloud/opennebula/views.py index 61ed5a4..89b1a52 100644 --- a/uncloud/opennebula/views.py +++ b/uncloud/opennebula/views.py @@ -1,27 +1,16 @@ from rest_framework import viewsets, permissions -from rest_framework.response import Response -from django.shortcuts import get_object_or_404 from .models import VM -from .serializers import VMSerializer, OpenNebulaVMSerializer +from .serializers import OpenNebulaVMSerializer - -class RawVMViewSet(viewsets.ModelViewSet): - queryset = VM.objects.all() - serializer_class = VMSerializer - permission_classes = [permissions.IsAdminUser] - - -class VMViewSet(viewsets.ViewSet): +class VMViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] + serializer_class = OpenNebulaVMSerializer - def list(self, request): - queryset = VM.objects.filter(owner=request.user) - serializer = OpenNebulaVMSerializer(queryset, many=True, context={'request': request}) - return Response(serializer.data) + def get_queryset(self): + if self.request.user.is_superuser: + obj = VM.objects.all() + else: + obj = VM.objects.filter(owner=self.request.user) - def retrieve(self, request, pk=None): - queryset = VM.objects.filter(owner=request.user) - vm = get_object_or_404(queryset, pk=pk) - serializer = OpenNebulaVMSerializer(vm, context={'request': request}) - return Response(serializer.data) + return obj