[opennebula] refresh formula, cleanup vm import/migration to uncloud
This commit is contained in:
parent
9f4b927c74
commit
5d840de55c
4 changed files with 29 additions and 46 deletions
|
@ -21,9 +21,8 @@ def convert_mac_to_int(mac_address: str):
|
||||||
return mac_address
|
return mac_address
|
||||||
|
|
||||||
|
|
||||||
def get_vm_price(core, ram, storage, n_of_ipv4, n_of_ipv6):
|
def get_vm_price(core, ram, ssd_size, hdd_size, 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 * ssd_size/10.) + (1.5 * hdd_size/100.) + 8 * n_of_ipv4 + 0 * n_of_ipv6
|
||||||
total = 3 * core + 4 * ram + 3.5 * storage + 8 * n_of_ipv4 + 0 * n_of_ipv6
|
|
||||||
|
|
||||||
# TODO: Find some reason about the following magical subtraction.
|
# TODO: Find some reason about the following magical subtraction.
|
||||||
total -= 8
|
total -= 8
|
||||||
|
@ -82,17 +81,18 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
for one_vm in VMModel.objects.all():
|
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
|
vmhost = VMHost.objects.get(hostname=one_vm.last_host)
|
||||||
# TODO: Set actual status instead of hard coded 'active'
|
cores = one_vm.cores
|
||||||
vm_id, cores, ram_in_gb = one_vm.vmid, one_vm.cores, one_vm.ram_in_gb
|
ram_in_gb = one_vm.ram_in_gb
|
||||||
owner, status = one_vm.owner, 'active'
|
owner = one_vm.owner
|
||||||
|
status = 'active'
|
||||||
|
|
||||||
# Total Amount of SSD Storage
|
# Total Amount of SSD Storage
|
||||||
# TODO: What would happen if the attached storage is not SSD but HDD?
|
# 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
|
# List of IPv4 addresses and Global IPv6 addresses
|
||||||
ipv4, ipv6 = one_vm.ips
|
ipv4, ipv6 = one_vm.ips
|
||||||
|
@ -101,18 +101,18 @@ class Command(BaseCommand):
|
||||||
# instead of pseudo one we are putting currently
|
# instead of pseudo one we are putting currently
|
||||||
creation_date = starting_date = ending_date = datetime.now(tz=timezone.utc)
|
creation_date = starting_date = ending_date = datetime.now(tz=timezone.utc)
|
||||||
|
|
||||||
# Price calculation
|
# Price calculation based on datacenterlight.ch
|
||||||
|
|
||||||
# TODO: Make the following non-hardcoded
|
|
||||||
one_time_price = 0
|
one_time_price = 0
|
||||||
recurring_period = 'per_month'
|
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:
|
try:
|
||||||
vm_product = VMProduct.objects.get(vmid=vm_id)
|
vm_product = VMProduct.objects.get(name=one_vm.uncloud_name)
|
||||||
except VMProduct.DoesNotExist:
|
except VMProduct.DoesNotExist:
|
||||||
order = Order.objects.create(
|
order = Order.objects.create(
|
||||||
owner=one_vm.owner,
|
owner=owner,
|
||||||
creation_date=creation_date,
|
creation_date=creation_date,
|
||||||
starting_date=starting_date,
|
starting_date=starting_date,
|
||||||
ending_date=ending_date,
|
ending_date=ending_date,
|
||||||
|
@ -121,7 +121,7 @@ class Command(BaseCommand):
|
||||||
recurring_period=recurring_period
|
recurring_period=recurring_period
|
||||||
)
|
)
|
||||||
vm_product, _ = VMProduct.objects.update_or_create(
|
vm_product, _ = VMProduct.objects.update_or_create(
|
||||||
vmid=vm_id,
|
name=
|
||||||
defaults={
|
defaults={
|
||||||
'cores': cores,
|
'cores': cores,
|
||||||
'ram_in_gb': ram_in_gb,
|
'ram_in_gb': ram_in_gb,
|
||||||
|
|
|
@ -9,9 +9,9 @@ class VM(models.Model):
|
||||||
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
||||||
data = JSONField()
|
data = JSONField()
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
@property
|
||||||
self.id = 'opennebula' + str(self.data.get("ID"))
|
def uncloud_name(self):
|
||||||
super().save(*args, **kwargs)
|
return "opennebula-{}".format(self.vmid)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cores(self):
|
def cores(self):
|
||||||
|
|
|
@ -2,12 +2,6 @@ from rest_framework import serializers
|
||||||
from opennebula.models import VM
|
from opennebula.models import VM
|
||||||
|
|
||||||
|
|
||||||
class VMSerializer(serializers.HyperlinkedModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = VM
|
|
||||||
fields = '__all__'
|
|
||||||
|
|
||||||
|
|
||||||
class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer):
|
class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = VM
|
model = VM
|
||||||
|
|
|
@ -1,27 +1,16 @@
|
||||||
from rest_framework import viewsets, permissions
|
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 .models import VM
|
||||||
from .serializers import VMSerializer, OpenNebulaVMSerializer
|
from .serializers import OpenNebulaVMSerializer
|
||||||
|
|
||||||
|
class VMViewSet(viewsets.ModelViewSet):
|
||||||
class RawVMViewSet(viewsets.ModelViewSet):
|
|
||||||
queryset = VM.objects.all()
|
|
||||||
serializer_class = VMSerializer
|
|
||||||
permission_classes = [permissions.IsAdminUser]
|
|
||||||
|
|
||||||
|
|
||||||
class VMViewSet(viewsets.ViewSet):
|
|
||||||
permission_classes = [permissions.IsAuthenticated]
|
permission_classes = [permissions.IsAuthenticated]
|
||||||
|
serializer_class = OpenNebulaVMSerializer
|
||||||
|
|
||||||
def list(self, request):
|
def get_queryset(self):
|
||||||
queryset = VM.objects.filter(owner=request.user)
|
if self.request.user.is_superuser:
|
||||||
serializer = OpenNebulaVMSerializer(queryset, many=True, context={'request': request})
|
obj = VM.objects.all()
|
||||||
return Response(serializer.data)
|
else:
|
||||||
|
obj = VM.objects.filter(owner=self.request.user)
|
||||||
|
|
||||||
def retrieve(self, request, pk=None):
|
return obj
|
||||||
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)
|
|
||||||
|
|
Loading…
Reference in a new issue