Move django-based uncloud to top-level

This commit is contained in:
fnux 2020-05-07 12:12:35 +02:00
commit 95d43f002f
265 changed files with 0 additions and 0 deletions

0
uncloud_vm/__init__.py Normal file
View file

3
uncloud_vm/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
uncloud_vm/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class UncloudVmConfig(AppConfig):
name = 'uncloud_vm'

View file

@ -0,0 +1,119 @@
import json
import uncloud.secrets as secrets
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from uncloud_vm.models import VMSnapshotProduct, VMProduct, VMHost
from datetime import datetime
class Command(BaseCommand):
help = 'Select VM Host for VMs'
def add_arguments(self, parser):
parser.add_argument('--this-hostname', required=True)
parser.add_argument('--this-cluster', required=True)
parser.add_argument('--create-vm-snapshots', action='store_true')
parser.add_argument('--schedule-vms', action='store_true')
parser.add_argument('--start-vms', action='store_true')
def handle(self, *args, **options):
for cmd in [ 'create_vm_snapshots', 'schedule_vms', 'start_vms' ]:
if options[cmd]:
f = getattr(self, cmd)
f(args, options)
def schedule_vms(self, *args, **options):
for pending_vm in VMProduct.objects.filter(status='PENDING'):
cores_needed = pending_vm.cores
ram_needed = pending_vm.ram_in_gb
# Database filtering
possible_vmhosts = VMHost.objects.filter(physical_cores__gte=cores_needed)
# Logical filtering
possible_vmhosts = [ vmhost for vmhost in possible_vmhosts
if vmhost.available_cores >=cores_needed
and vmhost.available_ram_in_gb >= ram_needed ]
if not possible_vmhosts:
log.error("No suitable Host found - cannot schedule VM {}".format(pending_vm))
continue
vmhost = possible_vmhosts[0]
pending_vm.vmhost = vmhost
pending_vm.status = 'SCHEDULED'
pending_vm.save()
print("Scheduled VM {} on VMHOST {}".format(pending_vm, pending_vm.vmhost))
print(self)
def start_vms(self, *args, **options):
vmhost = VMHost.objects.get(hostname=options['this_hostname'])
if not vmhost:
raise Exception("No vmhost {} exists".format(options['vmhostname']))
# not active? done here
if not vmhost.status = 'ACTIVE':
return
vms_to_start = VMProduct.objects.filter(vmhost=vmhost,
status='SCHEDULED')
for vm in vms_to_start:
""" run qemu:
check if VM is not already active / qemu running
prepare / create the Qemu arguments
"""
print("Starting VM {}".format(VM))
def check_vms(self, *args, **options):
"""
Check if all VMs that are supposed to run are running
"""
def modify_vms(self, *args, **options):
"""
Check all VMs that are requested to be modified and restart them
"""
def create_vm_snapshots(self, *args, **options):
this_cluster = VMCluster(option['this_cluster'])
for snapshot in VMSnapshotProduct.objects.filter(status='PENDING',
cluster=this_cluster):
if not snapshot.extra_data:
snapshot.extra_data = {}
# TODO: implement locking here
if 'creating_hostname' in snapshot.extra_data:
pass
snapshot.extra_data['creating_hostname'] = options['this_hostname']
snapshot.extra_data['creating_start'] = str(datetime.now())
snapshot.save()
# something on the line of:
# for disk im vm.disks:
# rbd snap create pool/image-name@snapshot name
# snapshot.extra_data['snapshots']
# register the snapshot names in extra_data (?)
print(snapshot)
def check_health(self, *args, **options):
pending_vms = VMProduct.objects.filter(status='PENDING')
vmhosts = VMHost.objects.filter(status='active')
# 1. Check that all active hosts reported back N seconds ago
# 2. Check that no VM is running on a dead host
# 3. Migrate VMs if necessary
# 4. Check that no VMs have been pending for longer than Y seconds
# If VM snapshots exist without a VM -> notify user (?)
print("Nothing is good, you should implement me")

View file

@ -0,0 +1,108 @@
# Generated by Django 3.0.3 on 2020-03-05 10:34
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = [
('uncloud_pay', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='VMDiskImageProduct',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('name', models.CharField(max_length=256)),
('is_os_image', models.BooleanField(default=False)),
('is_public', models.BooleanField(default=False)),
('size_in_gb', models.FloatField(blank=True, null=True)),
('import_url', models.URLField(blank=True, null=True)),
('image_source', models.CharField(max_length=128, null=True)),
('image_source_type', models.CharField(max_length=128, null=True)),
('storage_class', models.CharField(choices=[('hdd', 'HDD'), ('ssd', 'SSD')], default='ssd', max_length=32)),
('status', models.CharField(choices=[('pending', 'Pending'), ('creating', 'Creating'), ('active', 'Active'), ('disabled', 'Disabled'), ('unusable', 'Unusable'), ('deleted', 'Deleted')], default='pending', max_length=32)),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='VMHost',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('hostname', models.CharField(max_length=253, unique=True)),
('physical_cores', models.IntegerField(default=0)),
('usable_cores', models.IntegerField(default=0)),
('usable_ram_in_gb', models.FloatField(default=0)),
('status', models.CharField(choices=[('pending', 'Pending'), ('creating', 'Creating'), ('active', 'Active'), ('disabled', 'Disabled'), ('unusable', 'Unusable'), ('deleted', 'Deleted')], default='pending', max_length=32)),
('vms', models.TextField(default='')),
],
),
migrations.CreateModel(
name='VMProduct',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('status', models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted')], default='PENDING', max_length=32)),
('name', models.CharField(max_length=32)),
('cores', models.IntegerField()),
('ram_in_gb', models.FloatField()),
('vmid', models.IntegerField(null=True)),
('order', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order')),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('vmhost', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMHost')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='VMWithOSProduct',
fields=[
('vmproduct_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_vm.VMProduct')),
],
options={
'abstract': False,
},
bases=('uncloud_vm.vmproduct',),
),
migrations.CreateModel(
name='VMSnapshotProduct',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('status', models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted')], default='PENDING', max_length=32)),
('gb_ssd', models.FloatField(editable=False)),
('gb_hdd', models.FloatField(editable=False)),
('order', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order')),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='VMNetworkCard',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('mac_address', models.BigIntegerField()),
('ip_address', models.GenericIPAddressField(blank=True, null=True)),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct')),
],
),
migrations.CreateModel(
name='VMDiskProduct',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('size_in_gb', models.FloatField(blank=True)),
('image', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMDiskImageProduct')),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct')),
],
),
]

View file

@ -0,0 +1,23 @@
# Generated by Django 3.0.3 on 2020-03-05 13:21
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='vmdiskimageproduct',
name='storage_class',
field=models.CharField(choices=[('HDD', 'HDD'), ('SSD', 'SSD')], default='SSD', max_length=32),
),
migrations.AlterField(
model_name='vmproduct',
name='name',
field=models.CharField(blank=True, max_length=32, null=True),
),
]

View file

@ -0,0 +1,17 @@
# Generated by Django 3.0.3 on 2020-03-05 13:58
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0002_auto_20200305_1321'),
]
operations = [
migrations.RemoveField(
model_name='vmhost',
name='vms',
),
]

View file

@ -0,0 +1,17 @@
# Generated by Django 3.0.3 on 2020-03-17 14:40
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0003_remove_vmhost_vms'),
]
operations = [
migrations.RemoveField(
model_name='vmproduct',
name='vmid',
),
]

View file

@ -0,0 +1,19 @@
# Generated by Django 3.0.3 on 2020-03-09 12:43
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0004_remove_vmproduct_vmid'),
]
operations = [
migrations.AddField(
model_name='vmproduct',
name='primary_disk',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMDiskProduct'),
),
]

View file

@ -0,0 +1,25 @@
# Generated by Django 3.0.3 on 2020-03-09 12:58
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0001_initial'),
('uncloud_vm', '0004_vmproduct_primary_disk'),
]
operations = [
migrations.AddField(
model_name='vmdiskproduct',
name='order',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order'),
),
migrations.AddField(
model_name='vmdiskproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted')], default='PENDING', max_length=32),
),
]

View file

@ -0,0 +1,50 @@
# Generated by Django 3.0.3 on 2020-03-21 10:58
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0005_auto_20200309_1258'),
]
operations = [
migrations.AddField(
model_name='vmdiskimageproduct',
name='extra_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name='vmdiskproduct',
name='extra_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name='vmhost',
name='extra_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name='vmproduct',
name='extra_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name='vmsnapshotproduct',
name='extra_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True),
),
migrations.AlterField(
model_name='vmdiskproduct',
name='vm',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='disks', to='uncloud_vm.VMProduct'),
),
migrations.AlterField(
model_name='vmsnapshotproduct',
name='vm',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='snapshots', to='uncloud_vm.VMProduct'),
),
]

View file

@ -0,0 +1,57 @@
# Generated by Django 3.0.3 on 2020-03-22 17:58
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0005_auto_20200321_1058'),
]
operations = [
migrations.CreateModel(
name='VMCluster',
fields=[
('extra_data', django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True)),
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('name', models.CharField(max_length=128, unique=True)),
],
options={
'abstract': False,
},
),
migrations.AlterField(
model_name='vmdiskimageproduct',
name='is_public',
field=models.BooleanField(default=False, editable=False),
),
migrations.AlterField(
model_name='vmdiskimageproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AlterField(
model_name='vmhost',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AlterField(
model_name='vmproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AlterField(
model_name='vmsnapshotproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('ACTIVE', 'Active'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AddField(
model_name='vmproduct',
name='vmcluster',
field=models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMCluster'),
),
]

View file

@ -0,0 +1,19 @@
# Generated by Django 3.0.3 on 2020-03-22 18:09
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0006_auto_20200322_1758'),
]
operations = [
migrations.AddField(
model_name='vmhost',
name='vmcluster',
field=models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMCluster'),
),
]

View file

@ -0,0 +1,33 @@
# Generated by Django 3.0.5 on 2020-04-03 17:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0007_vmhost_vmcluster'),
]
operations = [
migrations.AlterField(
model_name='vmdiskimageproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AlterField(
model_name='vmhost',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AlterField(
model_name='vmproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AlterField(
model_name='vmsnapshotproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
]

View file

@ -0,0 +1,23 @@
# Generated by Django 3.0.5 on 2020-04-17 05:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0008_auto_20200403_1727'),
]
operations = [
migrations.AlterField(
model_name='vmproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='AWAITING_PAYMENT', max_length=32),
),
migrations.AlterField(
model_name='vmsnapshotproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='AWAITING_PAYMENT', max_length=32),
),
]

View file

@ -0,0 +1,14 @@
# Generated by Django 3.0.5 on 2020-04-13 08:57
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0004_remove_vmproduct_vmid'),
('uncloud_vm', '0008_auto_20200403_1727'),
]
operations = [
]

View file

@ -0,0 +1,24 @@
# Generated by Django 3.0.5 on 2020-04-13 09:24
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0009_merge_20200413_0857'),
]
operations = [
migrations.AlterField(
model_name='vmdiskproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32),
),
migrations.AlterField(
model_name='vmdiskproduct',
name='vm',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct'),
),
]

View file

@ -0,0 +1,14 @@
# Generated by Django 3.0.5 on 2020-04-18 06:41
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0009_auto_20200417_0551'),
('uncloud_vm', '0010_auto_20200413_0924'),
]
operations = [
]

View file

@ -0,0 +1,18 @@
# Generated by Django 3.0.5 on 2020-04-18 06:41
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0011_merge_20200418_0641'),
]
operations = [
migrations.AlterField(
model_name='vmdiskproduct',
name='status',
field=models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='AWAITING_PAYMENT', max_length=32),
),
]

View file

@ -0,0 +1,17 @@
# Generated by Django 3.0.5 on 2020-05-02 19:21
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0012_auto_20200418_0641'),
]
operations = [
migrations.RemoveField(
model_name='vmproduct',
name='primary_disk',
),
]

View file

198
uncloud_vm/models.py Normal file
View file

@ -0,0 +1,198 @@
import uuid
from django.db import models
from django.contrib.auth import get_user_model
from uncloud_pay.models import Product, RecurringPeriod
from uncloud.models import UncloudModel, UncloudStatus
import uncloud_pay.models as pay_models
import uncloud_storage.models
class VMCluster(UncloudModel):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=128, unique=True)
class VMHost(UncloudModel):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
# 253 is the maximum DNS name length
hostname = models.CharField(max_length=253, unique=True)
vmcluster = models.ForeignKey(
VMCluster, on_delete=models.CASCADE, editable=False, blank=True, null=True
)
# indirectly gives a maximum number of cores / VM - f.i. 32
physical_cores = models.IntegerField(default=0)
# determines the maximum usable cores - f.i. 320 if you overbook by a factor of 10
usable_cores = models.IntegerField(default=0)
# ram that can be used of the server
usable_ram_in_gb = models.FloatField(default=0)
status = models.CharField(
max_length=32, choices=UncloudStatus.choices, default=UncloudStatus.PENDING
)
@property
def vms(self):
return VMProduct.objects.filter(vmhost=self)
@property
def used_ram_in_gb(self):
return sum([vm.ram_in_gb for vm in VMProduct.objects.filter(vmhost=self)])
@property
def available_ram_in_gb(self):
return self.usable_ram_in_gb - self.used_ram_in_gb
@property
def available_cores(self):
return self.usable_cores - sum([vm.cores for vm in self.vms ])
class VMProduct(Product):
vmhost = models.ForeignKey(
VMHost, on_delete=models.CASCADE, editable=False, blank=True, null=True
)
vmcluster = models.ForeignKey(
VMCluster, on_delete=models.CASCADE, editable=False, blank=True, null=True
)
# VM-specific. The name is only intended for customers: it's a pain to
# remember IDs (speaking from experience as ungleich customer)!
name = models.CharField(max_length=32, blank=True, null=True)
cores = models.IntegerField()
ram_in_gb = models.FloatField()
@property
def recurring_price(self):
return self.cores * 3 + self.ram_in_gb * 4
def __str__(self):
return "VM {} ({}): {} cores {} gb ram".format(self.uuid,
self.name,
self.cores,
self.ram_in_gb)
@property
def description(self):
return "Virtual machine '{}': {} core(s), {}GB memory".format(
self.name, self.cores, self.ram_in_gb)
@staticmethod
def allowed_recurring_periods():
return list(filter(
lambda pair: pair[0] in [RecurringPeriod.PER_YEAR,
RecurringPeriod.PER_MONTH, RecurringPeriod.PER_HOUR],
RecurringPeriod.choices))
def __str__(self):
return "VM {} ({} Cores/{} GB RAM) running on {} in cluster {}".format(
self.uuid, self.cores, self.ram_in_gb,
self.vmhost, self.vmcluster)
class VMWithOSProduct(VMProduct):
pass
class VMDiskImageProduct(UncloudModel):
"""
Images are used for cloning/linking.
They are the base for images.
"""
uuid = models.UUIDField(
primary_key=True, default=uuid.uuid4, editable=False
)
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, editable=False
)
name = models.CharField(max_length=256)
is_os_image = models.BooleanField(default=False)
is_public = models.BooleanField(default=False, editable=False) # only allow admins to set this
size_in_gb = models.FloatField(null=True, blank=True)
import_url = models.URLField(null=True, blank=True)
image_source = models.CharField(max_length=128, null=True)
image_source_type = models.CharField(max_length=128, null=True)
storage_class = models.CharField(max_length=32,
choices = uncloud_storage.models.StorageClass.choices,
default = uncloud_storage.models.StorageClass.SSD)
status = models.CharField(
max_length=32, choices=UncloudStatus.choices, default=UncloudStatus.PENDING
)
def __str__(self):
return "VMDiskImage {} ({}): {} gb".format(self.uuid,
self.name,
self.size_in_gb)
class VMDiskProduct(Product):
"""
The VMDiskProduct is attached to a VM.
It is based on a VMDiskImageProduct that will be used as a basis.
It can be enlarged, but not shrinked compared to the VMDiskImageProduct.
"""
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE)
size_in_gb = models.FloatField(blank=True)
@property
def description(self):
return "Disk for VM '{}': {}GB".format(self.vm.name, self.size_in_gb)
@property
def recurring_price(self):
return (self.size_in_gb / 10) * 3.5
# Sample code for clean method
# Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct
# that is in status 'active'
# def clean(self):
# if self.image.status != 'active':
# raise ValidationError({
# 'image': 'VM Disk must be created from an active disk image.'
# })
def save(self, *args, **kwargs):
self.full_clean()
super().save(*args, **kwargs)
class VMNetworkCard(models.Model):
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
mac_address = models.BigIntegerField()
ip_address = models.GenericIPAddressField(blank=True,
null=True)
class VMSnapshotProduct(Product):
gb_ssd = models.FloatField(editable=False)
gb_hdd = models.FloatField(editable=False)
vm = models.ForeignKey(VMProduct,
related_name='snapshots',
on_delete=models.CASCADE)

151
uncloud_vm/serializers.py Normal file
View file

@ -0,0 +1,151 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct, VMCluster
from uncloud_pay.models import RecurringPeriod, BillingAddress
# XXX: does not seem to be used?
GB_SSD_PER_DAY=0.012
GB_HDD_PER_DAY=0.0006
GB_SSD_PER_DAY=0.012
GB_HDD_PER_DAY=0.0006
###
# Admin views.
class VMHostSerializer(serializers.HyperlinkedModelSerializer):
vms = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = VMHost
fields = '__all__'
read_only_fields = [ 'vms' ]
class VMClusterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = VMCluster
fields = '__all__'
###
# Disks.
class VMDiskProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMDiskProduct
fields = '__all__'
class CreateVMDiskProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMDiskProduct
fields = ['size_in_gb', 'image']
class CreateManagedVMDiskProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMDiskProduct
fields = ['size_in_gb']
class VMDiskImageProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMDiskImageProduct
fields = '__all__'
class VMSnapshotProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMSnapshotProduct
fields = '__all__'
# verify that vm.owner == user.request
def validate_vm(self, value):
if not value.owner == self.context['request'].user:
raise serializers.ValidationError("VM {} not found for owner {}.".format(value,
self.context['request'].user))
disks = VMDiskProduct.objects.filter(vm=value)
if len(disks) == 0:
raise serializers.ValidationError("VM {} does not have any disks, cannot snapshot".format(value.uuid))
return value
pricing = {}
pricing['per_gb_ssd'] = 0.012
pricing['per_gb_hdd'] = 0.0006
pricing['recurring_period'] = 'per_day'
###
# VMs
# Helper used in uncloud_service for services allocating VM.
class ManagedVMProductSerializer(serializers.ModelSerializer):
"""
Managed VM serializer used in ungleich_service app.
"""
primary_disk = CreateManagedVMDiskProductSerializer()
class Meta:
model = VMProduct
fields = [ 'cores', 'ram_in_gb', 'primary_disk']
class VMProductSerializer(serializers.HyperlinkedModelSerializer):
primary_disk = CreateVMDiskProductSerializer()
snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
disks = VMDiskProductSerializer(many=True, read_only=True)
class Meta:
model = VMProduct
fields = ['uuid', 'order', 'owner', 'status', 'name', 'cores',
'ram_in_gb', 'primary_disk', 'snapshots', 'disks', 'extra_data']
read_only_fields = ['uuid', 'order', 'owner', 'status']
class OrderVMProductSerializer(VMProductSerializer):
recurring_period = serializers.ChoiceField(
choices=VMProduct.allowed_recurring_periods())
def __init__(self, *args, **kwargs):
super(VMProductSerializer, self).__init__(*args, **kwargs)
self.fields['billing_address'] = serializers.ChoiceField(
choices=BillingAddress.get_addresses_for(
self.context['request'].user)
)
class Meta:
model = VMProductSerializer.Meta.model
fields = VMProductSerializer.Meta.fields + [
'recurring_period', 'billing_address'
]
read_only_fields = VMProductSerializer.Meta.read_only_fields
# Nico's playground.
class NicoVMProductSerializer(serializers.ModelSerializer):
snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
order = serializers.StringRelatedField()
class Meta:
model = VMProduct
read_only_fields = ['uuid', 'order', 'owner', 'status',
'vmhost', 'vmcluster', 'snapshots',
'extra_data' ]
fields = read_only_fields + [ 'name',
'cores',
'ram_in_gb'
]
class DCLVMProductSerializer(serializers.HyperlinkedModelSerializer):
"""
Create an interface similar to standard DCL
"""
# Custom field used at creation (= ordering) only.
recurring_period = serializers.ChoiceField(
choices=VMProduct.allowed_recurring_periods())
os_disk_uuid = serializers.UUIDField()
# os_disk_size =
class Meta:
model = VMProduct

114
uncloud_vm/tests.py Normal file
View file

@ -0,0 +1,114 @@
import datetime
import parsedatetime
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.utils import timezone
from django.core.exceptions import ValidationError
from uncloud_vm.models import VMDiskImageProduct, VMDiskProduct, VMProduct, VMHost
from uncloud_pay.models import Order, RecurringPeriod
User = get_user_model()
cal = parsedatetime.Calendar()
# If you want to check the test database using some GUI/cli tool
# then use the following connecting parameters
# host: localhost
# database: test_uncloud
# user: root
# password:
# port: 5432
class VMTestCase(TestCase):
@classmethod
def setUpClass(cls):
# Setup vm host
cls.vm_host, created = VMHost.objects.get_or_create(
hostname='serverx.placey.ungleich.ch', physical_cores=32, usable_cores=320,
usable_ram_in_gb=512.0, status='active'
)
super().setUpClass()
def setUp(self) -> None:
# Setup two users as it is common to test with different user
self.user = User.objects.create_user(
username='testuser', email='test@test.com', first_name='Test', last_name='User'
)
self.user2 = User.objects.create_user(
username='Meow', email='meow123@test.com', first_name='Meow', last_name='Cat'
)
super().setUp()
def create_sample_vm(self, owner):
one_month_later, parse_status = cal.parse("1 month later")
return VMProduct.objects.create(
vmhost=self.vm_host, cores=2, ram_in_gb=4, owner=owner,
order=Order.objects.create(
owner=owner,
creation_date=datetime.datetime.now(tz=timezone.utc),
starting_date=datetime.datetime.now(tz=timezone.utc),
ending_date=datetime.datetime(*one_month_later[:6], tzinfo=timezone.utc),
recurring_period=RecurringPeriod.PER_MONTH
)
)
# TODO: the logic tested by this test is not implemented yet.
# def test_disk_product(self):
# """Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct
# that is in status 'active'"""
#
# vm = self.create_sample_vm(owner=self.user)
#
# pending_disk_image = VMDiskImageProduct.objects.create(
# owner=self.user, name='pending_disk_image', is_os_image=True, is_public=True, size_in_gb=10,
# status='pending'
# )
# try:
# vm_disk_product = VMDiskProduct.objects.create(
# owner=self.user, vm=vm, image=pending_disk_image, size_in_gb=10
# )
# except ValidationError:
# vm_disk_product = None
#
# self.assertIsNone(
# vm_disk_product,
# msg='VMDiskProduct created with disk image whose status is not active.'
# )
def test_vm_disk_product_creation(self):
"""Ensure that a user can only create a VMDiskProduct for an existing VM"""
disk_image = VMDiskImageProduct.objects.create(
owner=self.user, name='disk_image', is_os_image=True, is_public=True, size_in_gb=10,
status='active'
)
with self.assertRaises(ValidationError, msg='User created a VMDiskProduct for non-existing VM'):
# Create VMProduct object but don't save it in database
vm = VMProduct()
vm_disk_product = VMDiskProduct.objects.create(
owner=self.user, vm=vm, image=disk_image, size_in_gb=10
)
# TODO: the logic tested by this test is not implemented yet.
# def test_vm_disk_product_creation_for_someone_else(self):
# """Ensure that a user can only create a VMDiskProduct for his/her own VM"""
#
# # Create a VM which is ownership of self.user2
# someone_else_vm = self.create_sample_vm(owner=self.user2)
#
# # 'self.user' would try to create a VMDiskProduct for 'user2's VM
# with self.assertRaises(ValidationError, msg='User created a VMDiskProduct for someone else VM.'):
# vm_disk_product = VMDiskProduct.objects.create(
# owner=self.user, vm=someone_else_vm,
# size_in_gb=10,
# image=VMDiskImageProduct.objects.create(
# owner=self.user, name='disk_image', is_os_image=True, is_public=True, size_in_gb=10,
# status='active'
# )
# )

249
uncloud_vm/views.py Normal file
View file

@ -0,0 +1,249 @@
from django.db import transaction
from django.shortcuts import render
from django.utils import timezone
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from rest_framework import viewsets, permissions
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct, VMCluster
from uncloud_pay.models import Order
from .serializers import *
from uncloud_pay.helpers import ProductViewSet
import datetime
###
# Generic disk image views. Do not require orders / billing.
class VMDiskImageProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskImageProductSerializer
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMDiskImageProduct.objects.all()
else:
obj = VMDiskImageProduct.objects.filter(owner=self.request.user) | VMDiskImageProduct.objects.filter(is_public=True)
return obj
def create(self, request):
serializer = VMDiskImageProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
# did not specify size NOR import url?
if not serializer.validated_data['size_in_gb']:
if not serializer.validated_data['import_url']:
raise ValidationError(detail={ 'error_mesage': 'Specify either import_url or size_in_gb' })
serializer.save(owner=request.user)
return Response(serializer.data)
class VMDiskImageProductPublicViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskImageProductSerializer
def get_queryset(self):
return VMDiskImageProduct.objects.filter(is_public=True)
###
# User VM disk and snapshots.
class VMDiskProductViewSet(viewsets.ModelViewSet):
"""
Let a user modify their own VMDisks
"""
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskProductSerializer
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMDiskProduct.objects.all()
else:
obj = VMDiskProduct.objects.filter(owner=self.request.user)
return obj
def create(self, request):
serializer = VMDiskProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
# get disk size from image, if not specified
if not 'size_in_gb' in serializer.validated_data:
size_in_gb = serializer.validated_data['image'].size_in_gb
else:
size_in_gb = serializer.validated_data['size_in_gb']
if size_in_gb < serializer.validated_data['image'].size_in_gb:
raise ValidationError(detail={ 'error_mesage': 'Size is smaller than original image' })
serializer.save(owner=request.user, size_in_gb=size_in_gb)
return Response(serializer.data)
class VMSnapshotProductViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMSnapshotProductSerializer
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMSnapshotProduct.objects.all()
else:
obj = VMSnapshotProduct.objects.filter(owner=self.request.user)
return obj
def create(self, request):
serializer = VMSnapshotProductSerializer(data=request.data, context={'request': request})
# This verifies that the VM belongs to the request user
serializer.is_valid(raise_exception=True)
vm = vm=serializer.validated_data['vm']
disks = VMDiskProduct.objects.filter(vm=vm)
ssds_size = sum([d.size_in_gb for d in disks if d.image.storage_class == 'ssd'])
hdds_size = sum([d.size_in_gb for d in disks if d.image.storage_class == 'hdd'])
recurring_price = serializer.pricing['per_gb_ssd'] * ssds_size + serializer.pricing['per_gb_hdd'] * hdds_size
recurring_period = serializer.pricing['recurring_period']
# Create order
now = datetime.datetime.now()
order = Order(owner=request.user,
recurring_period=recurring_period)
order.save()
order.add_record(one_time_price=0,
recurring_price=recurring_price,
description="Snapshot of VM {} from {}".format(vm, now))
serializer.save(owner=request.user,
order=order,
gb_ssd=ssds_size,
gb_hdd=hdds_size)
return Response(serializer.data)
###
# User VMs.
class VMProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMProduct.objects.all()
else:
obj = VMProduct.objects.filter(owner=self.request.user)
return obj
def get_serializer_class(self):
if self.action == 'create':
return OrderVMProductSerializer
else:
return VMProductSerializer
# Use a database transaction so that we do not get half-created structure
# if something goes wrong.
@transaction.atomic
def create(self, request):
# Extract serializer data.
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
order_recurring_period = serializer.validated_data.pop("recurring_period")
order_billing_address = serializer.validated_data.pop("billing_address")
# Create base order.
order = Order(
recurring_period=order_recurring_period,
billing_address=order_billing_address,
owner=request.user,
starting_date=timezone.now()
)
order.save()
# Create disk image.
disk = VMDiskProduct(owner=request.user, order=order,
**serializer.validated_data.pop("primary_disk"))
# Create VM.
vm = serializer.save(owner=request.user, order=order, primary_disk=disk)
disk.vm = vm
disk.save()
return Response(VMProductSerializer(vm, context={'request': request}).data)
class NicoVMProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = NicoVMProductSerializer
def get_queryset(self):
obj = VMProduct.objects.filter(owner=self.request.user)
return obj
def create(self, request):
serializer = self.serializer_class(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
vm = serializer.save(owner=request.user)
return Response(serializer.data)
###
# Admin stuff.
class VMHostViewSet(viewsets.ModelViewSet):
serializer_class = VMHostSerializer
queryset = VMHost.objects.all()
permission_classes = [permissions.IsAdminUser]
class VMClusterViewSet(viewsets.ModelViewSet):
serializer_class = VMClusterSerializer
queryset = VMCluster.objects.all()
permission_classes = [permissions.IsAdminUser]
##
# Nico's playground.
# Also create:
# - /dcl/available_os
# Basically a view of public and my disk images
# -
class DCLCreateVMProductViewSet(ProductViewSet):
"""
This view resembles the way how DCL VMs are created by default.
The user chooses an OS, os disk size, ram, cpu and whether or not to have a mapped IPv4 address
"""
permission_classes = [permissions.IsAuthenticated]
serializer_class = DCLVMProductSerializer
def get_queryset(self):
return VMProduct.objects.filter(owner=self.request.user)
# Use a database transaction so that we do not get half-created structure
# if something goes wrong.
@transaction.atomic
def create(self, request):
# Extract serializer data.
serializer = VMProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
order_recurring_period = serializer.validated_data.pop("recurring_period")
# Create base order.
order = Order.objects.create(
recurring_period=order_recurring_period,
owner=request.user
)
order.save()
# Create VM.
vm = serializer.save(owner=request.user, order=order)
return Response(serializer.data)