cleanup migrations

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-08-01 18:38:38 +02:00
parent ed40b21d16
commit f7b14bf507
22 changed files with 148 additions and 198 deletions

View File

@ -46,6 +46,12 @@
old order. The old order stops one second before the new order
starts.
If a order has been replaced can be seen by its replaced_by count:
#+BEGIN_SRC sh
>>> Order.objects.get(id=1).replaced_by.count()
1
#+END_SRC
** Product and Product Children
- A product describes something a user can buy
- A product inherits from the uncloud_pay.models.Product model to

View File

@ -1,12 +1,9 @@
import json
import uncloud.secrets as secrets
from xmlrpc.client import ServerProxy as RPCClient
from django_auth_ldap.backend import LDAPBackend
from django.core.management.base import BaseCommand
from django.conf import settings
from xmltodict import parse
from opennebula.models import VM as VMModel
@ -19,9 +16,9 @@ class Command(BaseCommand):
pass
def handle(self, *args, **options):
with RPCClient(secrets.OPENNEBULA_URL) as rpc_client:
with RPCClient(settings.OPENNEBULA_URL) as rpc_client:
success, response, *_ = rpc_client.one.vmpool.infoextended(
secrets.OPENNEBULA_USER_PASS, -2, -1, -1, -1
settings.OPENNEBULA_USER_PASS, -2, -1, -1, -1
)
if success:
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']

View File

@ -1,4 +1,4 @@
# Generated by Django 3.0.6 on 2020-06-21 12:34
# Generated by Django 3.0.6 on 2020-08-01 16:38
from django.conf import settings
import django.contrib.postgres.fields.jsonb
@ -12,15 +12,22 @@ class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('uncloud_pay', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='VM',
fields=[
('extra_data', django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True)),
('status', 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)),
('vmid', models.IntegerField(primary_key=True, serialize=False)),
('data', django.contrib.postgres.fields.jsonb.JSONField()),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('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)),
],
options={
'abstract': False,
},
),
]

View File

@ -1,38 +0,0 @@
# Generated by Django 3.0.6 on 2020-08-01 12:03
from django.conf import settings
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0003_auto_20200621_1442'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('opennebula', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='vm',
name='extra_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name='vm',
name='order',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order'),
),
migrations.AddField(
model_name='vm',
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='vm',
name='owner',
field=models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View File

@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{% block title %}Welcome to uncloud{% endblock %}</title>
{% block header %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>

View File

@ -1,4 +1,4 @@
# Generated by Django 3.0.6 on 2020-06-21 12:34
# Generated by Django 3.0.6 on 2020-08-01 16:38
import django.contrib.auth.models
import django.contrib.auth.validators

View File

@ -1,4 +1,4 @@
# Generated by Django 3.0.6 on 2020-06-21 12:34
# Generated by Django 3.0.6 on 2020-08-01 16:38
from django.conf import settings
import django.contrib.postgres.fields.jsonb

View File

@ -0,0 +1,77 @@
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.utils import timezone
from datetime import datetime, timedelta
from uncloud_pay.models import *
#import opennebula.models as one
from uncloud_vm.models import *
def vm_price_2020(cpu=1, ram=2, v6only=False):
if v6only:
discount = 9
else:
discount = 0
return cpu*3 + ram*4 - discount
def disk_price_2020(size_in_gb, disk_type):
if disk_type == VMDiskType.CEPH_SSD:
price = 3.5/10
elif disk_type == VMDiskType.CEPH_HDD:
price = 1.5/100
else:
raise Exception("not yet defined price")
return size_in_gb * price
class Command(BaseCommand):
help = 'Adding VMs / creating orders for user'
def add_arguments(self, parser):
parser.add_argument('--username', type=str, required=True)
def handle(self, *args, **options):
user = get_user_model().objects.get(username=options['username'])
addr, created = BillingAddress.objects.get_or_create(
owner=user,
active=True,
defaults={'organization': 'Undefined organisation',
'name': 'Undefined name',
'street': 'Undefined Street',
'city': 'Undefined city',
'postal_code': '8750',
'country': 'CH',
'active': True
}
)
orders = []
# 25206
vm25206 = VMProduct.objects.create(name="OpenNebula 25206",
cores=1,
ram_in_gb=4,
owner=user)
vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206,
owner=user,
size_in_gb=30)
order_vm_25206 = Order.objects.create(owner=user,
billing_address=addr,
starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
recurring_period=RecurringPeriod.PER_30D,
recurring_price = vm_price_2020(cpu=1, ram=4) / 2,
description = "VM %s" % vm25206
)
order_vm_25206_ssd = Order.objects.create(owner=user,
billing_address=addr,
starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
recurring_period=RecurringPeriod.PER_30D,
recurring_price = disk_price_2020(30, VMDiskType.CEPH_SSD) / 2,
description = vm25206_ssd
)

View File

@ -1,4 +1,4 @@
# Generated by Django 3.0.6 on 2020-06-21 12:34
# Generated by Django 3.0.6 on 2020-08-01 16:38
from django.conf import settings
import django.core.validators
@ -23,9 +23,9 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('creation_date', models.DateTimeField(auto_now_add=True)),
('starting_date', models.DateTimeField()),
('ending_date', models.DateTimeField()),
('due_date', models.DateField()),
('starting_date', models.DateTimeField(default=uncloud_pay.models.start_of_this_month)),
('ending_date', models.DateTimeField(default=uncloud_pay.models.end_of_this_month)),
('due_date', models.DateField(default=uncloud_pay.models.default_payment_delay)),
('valid', models.BooleanField(default=True)),
],
),
@ -53,10 +53,12 @@ class Migration(migrations.Migration):
('starting_date', models.DateTimeField(default=django.utils.timezone.now)),
('ending_date', models.DateTimeField(blank=True, null=True)),
('recurring_period', models.IntegerField(choices=[(31536000, 'Per 365 days'), (2592000, 'Per 30 days'), (604800, 'Per Week'), (86400, 'Per Day'), (3600, 'Per Hour'), (60, 'Per Minute'), (1, 'Per Second'), (0, 'Onetime')], default=2592000)),
('one_time_price', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
('recurring_price', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
('billing_address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.BillingAddress')),
('depends_on', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='parent_of', to='uncloud_pay.Order')),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('replaced_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='supersede', to='uncloud_pay.Order')),
('replaces', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='replaced_by', to='uncloud_pay.Order')),
],
),
migrations.CreateModel(
@ -115,7 +117,7 @@ class Migration(migrations.Migration):
name='BillRecord',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('usage_count', models.IntegerField(default=1)),
('quantity', models.IntegerField(default=1)),
('creation_date', models.DateTimeField(auto_now_add=True)),
('starting_date', models.DateTimeField()),
('ending_date', models.DateTimeField()),
@ -137,4 +139,8 @@ class Migration(migrations.Migration):
model_name='billingaddress',
constraint=models.UniqueConstraint(condition=models.Q(active=True), fields=('owner',), name='one_active_billing_address_per_user'),
),
migrations.AddConstraint(
model_name='bill',
constraint=models.UniqueConstraint(fields=('owner', 'starting_date', 'ending_date'), name='one_bill_per_month_per_user'),
),
]

View File

@ -1,33 +0,0 @@
# Generated by Django 3.0.6 on 2020-06-21 13:35
from django.db import migrations, models
import uncloud_pay.models
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='bill',
name='due_date',
field=models.DateField(default=uncloud_pay.models.default_payment_delay),
),
migrations.AlterField(
model_name='bill',
name='ending_date',
field=models.DateTimeField(default=uncloud_pay.models.end_of_this_month),
),
migrations.AlterField(
model_name='bill',
name='starting_date',
field=models.DateTimeField(default=uncloud_pay.models.start_of_this_month),
),
migrations.AddConstraint(
model_name='bill',
constraint=models.UniqueConstraint(fields=('owner', 'starting_date', 'ending_date'), name='one_bill_per_month_per_user'),
),
]

View File

@ -1,18 +0,0 @@
# Generated by Django 3.0.6 on 2020-06-21 14:42
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0002_auto_20200621_1335'),
]
operations = [
migrations.RenameField(
model_name='billrecord',
old_name='usage_count',
new_name='quantity',
),
]

View File

@ -1,23 +0,0 @@
# Generated by Django 3.0.6 on 2020-08-01 16:04
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0003_auto_20200621_1442'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='replaced_by',
),
migrations.AddField(
model_name='order',
name='replaces',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='replaced_by', to='uncloud_pay.Order'),
),
]

View File

@ -1,24 +0,0 @@
# Generated by Django 3.0.6 on 2020-08-01 16:26
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0004_auto_20200801_1604'),
]
operations = [
migrations.AddField(
model_name='order',
name='one_time_price',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AddField(
model_name='order',
name='recurring_price',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
]

View File

@ -398,7 +398,7 @@ class Order(models.Model):
def records(self):
return OrderRecord.objects.filter(order=self)
# these are reald fields!!!
# these are real fields!!!
# @property
# def one_time_price(self):
# return reduce(lambda acc, record: acc + record.one_time_price, self.records, 0)

View File

@ -1,4 +1,4 @@
# Generated by Django 3.0.6 on 2020-06-21 12:34
# Generated by Django 3.0.6 on 2020-08-01 16:38
from django.conf import settings
import django.contrib.postgres.fields.jsonb
@ -12,9 +12,9 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('uncloud_pay', '0001_initial'),
('uncloud_vm', '__first__'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('uncloud_pay', '0001_initial'),
]
operations = [

View File

@ -1,5 +1,3 @@
import uuid
from django.db import models
from uncloud_pay.models import Product, RecurringPeriod, AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS
from uncloud_vm.models import VMProduct, VMDiskImageProduct
@ -23,7 +21,8 @@ class MatrixServiceProduct(Product):
@staticmethod
def base_image():
# TODO: find a way to safely reference debian 10 image.
return VMDiskImageProduct.objects.get(uuid="93e564c5-adb3-4741-941f-718f76075f02")
#e return VMDiskImageProduct.objects.get(uuid="93e564c5-adb3-4741-941f-718f76075f02")
return False
@staticmethod
def allowed_recurring_periods():

View File

@ -12,9 +12,9 @@ class MatrixServiceProductSerializer(serializers.ModelSerializer):
class Meta:
model = MatrixServiceProduct
fields = ['uuid', 'order', 'owner', 'status', 'vm', 'domain',
fields = ['order', 'owner', 'status', 'vm', 'domain',
'recurring_period']
read_only_fields = ['uuid', 'order', 'owner', 'status']
read_only_fields = ['order', 'owner', 'status']
class OrderMatrixServiceProductSerializer(MatrixServiceProductSerializer):
recurring_period = serializers.ChoiceField(
@ -37,9 +37,9 @@ class OrderMatrixServiceProductSerializer(MatrixServiceProductSerializer):
class GenericServiceProductSerializer(serializers.ModelSerializer):
class Meta:
model = GenericServiceProduct
fields = ['uuid', 'order', 'owner', 'status', 'custom_recurring_price',
fields = ['order', 'owner', 'status', 'custom_recurring_price',
'custom_description', 'custom_one_time_price']
read_only_fields = ['uuid', 'order', 'owner', 'status']
read_only_fields = [ 'owner', 'status']
class OrderGenericServiceProductSerializer(GenericServiceProductSerializer):
recurring_period = serializers.ChoiceField(

View File

@ -1,3 +1,7 @@
from django.contrib import admin
# Register your models here.
from uncloud_vm.models import *
admin.site.register(VMProduct)
admin.site.register(VMDiskProduct)

View File

@ -1,10 +1,9 @@
# Generated by Django 3.0.6 on 2020-06-21 12:34
# Generated by Django 3.0.6 on 2020-08-01 16:38
from django.conf import settings
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
@ -12,16 +11,16 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('uncloud_pay', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('uncloud_pay', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='VMCluster',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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={
@ -31,8 +30,8 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='VMDiskImageProduct',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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=256)),
('is_os_image', models.BooleanField(default=False)),
('is_public', models.BooleanField(default=False, editable=False)),
@ -51,8 +50,8 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='VMHost',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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)),
('hostname', models.CharField(max_length=253, unique=True)),
('physical_cores', models.IntegerField(default=0)),
('usable_cores', models.IntegerField(default=0)),
@ -114,7 +113,8 @@ class Migration(migrations.Migration):
('extra_data', django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True)),
('status', 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)),
('size_in_gb', models.FloatField(blank=True)),
('image', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMDiskImageProduct')),
('disk_type', models.CharField(choices=[('ceph/ssd', 'Ceph Ssd'), ('ceph/hdd', 'Ceph Hdd'), ('local/ssd', 'Local Ssd'), ('local/hdd', 'Local Hdd')], default='ceph/ssd', max_length=20)),
('image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMDiskImageProduct')),
('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')),

View File

@ -1,18 +0,0 @@
# Generated by Django 3.0.6 on 2020-08-01 14:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='vmdiskproduct',
name='disk_type',
field=models.CharField(choices=[('ceph/ssd', 'Ceph Ssd'), ('ceph/hdd', 'Ceph Hdd'), ('local/ssd', 'Local Ssd'), ('local/hdd', 'Local Hdd')], default='ceph/ssd', max_length=20),
),
]

View File

@ -1,5 +1,3 @@
import uuid
from django.db import models
from django.contrib.auth import get_user_model
@ -10,13 +8,9 @@ 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)
@ -132,7 +126,7 @@ class VMDiskImageProduct(UncloudModel):
)
def __str__(self):
return "VMDiskImage {} ({}): {} gb".format(self.uuid,
return "VMDiskImage {} ({}): {} gb".format(self.id,
self.name,
self.size_in_gb)

View File

@ -67,7 +67,7 @@ class VMSnapshotProductSerializer(serializers.ModelSerializer):
disks = VMDiskProduct.objects.filter(vm=value)
if len(disks) == 0:
raise serializers.ValidationError("VM {} does not have any disks, cannot snapshot".format(value.uuid))
raise serializers.ValidationError("VM {} does not have any disks, cannot snapshot".format(value.id))
return value
@ -96,9 +96,9 @@ class VMProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMWithOSProduct
fields = ['uuid', 'order', 'owner', 'status', 'name', 'cores',
fields = ['order', 'owner', 'status', 'name', 'cores',
'ram_in_gb', 'primary_disk', 'snapshots', 'disks', 'extra_data']
read_only_fields = ['uuid', 'order', 'owner', 'status']
read_only_fields = ['order', 'owner', 'status']
class OrderVMProductSerializer(VMProductSerializer):
recurring_period = serializers.ChoiceField(
@ -119,7 +119,7 @@ class NicoVMProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMProduct
read_only_fields = ['uuid', 'order', 'owner', 'status',
read_only_fields = ['order', 'owner', 'status',
'vmhost', 'vmcluster', 'snapshots',
'extra_data' ]
fields = read_only_fields + [ 'name',