-
-
-
-
-
- {% trans "Are you sure that you want to cancel this subscription?."%}
-
- {% blocktrans %} The instance will be active till the end date of the last bill and will be deleted
- after that. {% endblocktrans %}
-
-
-
-
-
-
-
-{% endblock %}
-
-{% block js_extra %}
-
-{% endblock %}
diff --git a/nextcloud/templates/nextcloud/pricing.html b/nextcloud/templates/nextcloud/pricing.html
deleted file mode 100644
index 3dbc367..0000000
--- a/nextcloud/templates/nextcloud/pricing.html
+++ /dev/null
@@ -1,7 +0,0 @@
-{% extends "matrixhosting/base.html" %}
-
-{% load static i18n %}
-
-{% block main %}
-
-{% endblock %}
\ No newline at end of file
diff --git a/nextcloud/templates/uncloud_pay/bills.html b/nextcloud/templates/uncloud_pay/bills.html
deleted file mode 100644
index faae5f1..0000000
--- a/nextcloud/templates/uncloud_pay/bills.html
+++ /dev/null
@@ -1,190 +0,0 @@
-{% extends "nextcloud/base.html" %}
-
-{% load static i18n compress mathfilters %}
-
-{% block title %} Bills {% endblock %}
-
-{% block content %}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
{% trans "Bill No."%}
-
{% trans "Creation Date"%}
-
{% trans "Amount"%}
-
{% trans "Due Date"%}
-
{% trans "Status"%}
-
-
-
-
-
-
-
-
- {% for bill in object_list %}
-
-
-
-
-
Bill Lines:
-
-
-
-
- | Order |
- Description |
- Start Date |
- End Date |
- Subtotal |
- Total |
-
-
-
- {% for record in bill.bill_records.all %}
-
- | #{{record.order.id}} |
- {{record.description}} |
- {{record.starting_date|date:"Y-m-d"}} |
- {{record.ending_date|date:"Y-m-d"}} |
- {{record.subtotal}} |
- {{record.sum}} |
-
- {% endfor %}
-
-
-
-
-
-
-
-
-
{% trans "Subtotal" %}
-
-
-
-
-
-
{{bill.billing_address.get_country_display}} VAT {{ bill.vat_rate|mul:100 }}%
-
-
-
-
-
-
-
-
-
-
-
-
{{bill.sum|floatformat:2}} CHF
-
-
-
-
-
-
-
- {%endfor%}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-{% endblock %}
-{% block js_extra %}
-
-
-{% endblock js_extra %}
\ No newline at end of file
diff --git a/nextcloud/templates/uncloud_pay/cards.html b/nextcloud/templates/uncloud_pay/cards.html
deleted file mode 100644
index 7963d06..0000000
--- a/nextcloud/templates/uncloud_pay/cards.html
+++ /dev/null
@@ -1,210 +0,0 @@
-{% extends "nextcloud/base.html" %}
-
-{% load static i18n compress %}
-
-{% block title %} Payment Methods {% endblock %}
-
-{% block content %}
-
-
-
-
-
-
-
-
-
-
-
{% trans "Credit or Debit Cards"%} ({% trans "for payments"%})
-
- {% for card in object_list %}
-
-
-
XXXX-XXXX-XXXX-{{card.last4}}
-
Valid
- thru
- {{card.expiry_date|date:"m"}}/{{card.expiry_date|date:"y"}} {% if card.active %}{% trans "Primary"%}{%endif%}
-
{{card.card_name}}
-
-
-
- {% endfor %}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-{% endblock %}
-{% block js_extra %}
-
-
-
-{% endblock js_extra %}
\ No newline at end of file
diff --git a/nextcloud/templates/uncloud_pay/order_success.html b/nextcloud/templates/uncloud_pay/order_success.html
deleted file mode 100644
index 474e5c6..0000000
--- a/nextcloud/templates/uncloud_pay/order_success.html
+++ /dev/null
@@ -1,51 +0,0 @@
-{% extends "nextcloud/base.html" %}
-
-{% load static i18n %}
-
-{% block title %} Request Details {% endblock %}
-
-{% block content %}
-
-
-
-
-
-
-
{%trans "Success!" %}
-
{%trans "Order has been successfully added. Your VM will be up and running in a few moments. " %}
-
-
{%trans "We will send you a confirmation email as soon as it is ready." %} {%trans "Go to your dashboard" %} {%trans "Billing" %}.
-
{%trans "Download Invoice" %}
-
-
-
-{% endblock %}
-
-{% block js_extra %}
-{% endblock js_extra %}
\ No newline at end of file
diff --git a/nextcloud/templates/uncloud_pay/payments.html b/nextcloud/templates/uncloud_pay/payments.html
deleted file mode 100644
index c00faf5..0000000
--- a/nextcloud/templates/uncloud_pay/payments.html
+++ /dev/null
@@ -1,124 +0,0 @@
-{% extends "nextcloud/base.html" %}
-
-{% load static i18n compress %}
-
-{% block title %} Payments {% endblock %}
-
-{% block content %}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
{% trans "Payment Moves"%}
-
-
-
-
{% trans "ID"%}
-
{% trans "Date"%}
-
{% trans "Description" %}
-
{% trans "Type"%}
-
{% trans "Amount"%}
-
-
-
-
-
-
- {% for payment in object_list %}
-
-
-
#{{payment.id}}
-
{{payment.timestamp|date:"Y-m-d"}} {{payment.timestamp|date:"H:i"}}
-
{{payment.notes}}
-
{{payment.type}}
-
{% if payment.type == 'withdraw' %}- {%else%}+ {%endif%}{{payment.amount}} ({{payment.currency}})
-
-
- {%endfor%}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-{% endblock %}
-
-{% block js_extra %}
- {% compress js %}
-
- {% endcompress %}
-{% endblock js_extra %}
\ No newline at end of file
diff --git a/nextcloud/tests.py b/nextcloud/tests.py
deleted file mode 100644
index 78cac27..0000000
--- a/nextcloud/tests.py
+++ /dev/null
@@ -1,102 +0,0 @@
-import datetime
-import json
-import logging
-from django import utils
-
-from django.test import TestCase
-from django.contrib.auth import get_user_model
-from django.utils import timezone
-
-from .models import VMInstance
-from .utils import *
-from uncloud_pay.models import Order, PricingPlan, BillingAddress, Product, RecurringPeriod, Bill, BillRecord
-
-import oca
-
-logger = logging.getLogger()
-
-vm_product_config = {
- 'features': {
- 'cores':
- { 'min': 1,
- 'max': 48
- },
- 'ram_gb':
- { 'min': 2,
- 'max': 200
- },
- },
-}
-
-class VMInstanceTestCase(TestCase):
-
- def setUp(self):
- RecurringPeriod.populate_db_defaults()
- self.user = get_user_model().objects.create(
- username='random_user',
- email='jane.random@domain.tld')
- self.config = json.dumps({
- 'cores': 1,
- 'memory': 2,
- 'storage': 100,
- 'homeserver_domain': '',
- 'webclient_domain': '',
- 'matrix_domain': '',
- })
- self.pricing_plan = PricingPlan.objects.create(name="PricingSample", set_up_fees=35, cores_unit_price=3,
- ram_unit_price=4, storage_ssd_unit_price=0.35, storage_hd_unit_price=0.02)
- self.ba = BillingAddress.objects.create(
- owner=self.user,
- organization = 'Test org',
- street="unknown",
- city="unknown",
- postal_code="somewhere else",
- active=True)
-
- self.product = Product.objects.create(name="Testproduct",
- description="Only for testing",
- config=vm_product_config)
- self.default_recurring_period = RecurringPeriod.objects.get(name="Per 30 days")
- self.product.recurring_periods.add(self.default_recurring_period,
- through_defaults= { 'is_default': True })
-
- def test_delete_matrix_vm_related_to_bill(self):
- order = Order.objects.create(owner=self.user,
- recurring_period=self.default_recurring_period,
- billing_address=self.ba,
- pricing_plan = self.pricing_plan,
- product=self.product,
- config=self.config)
- VMInstance.create_instance(order)
- instances = VMInstance.objects.filter(order=order)
- self.assertEqual(len(instances), 1)
- bill = Bill.create_next_bill_for_order(order, ending_date=order.next_cancel_or_downgrade_date(order.starting_date))
- bill_records = BillRecord.objects.filter(bill=bill)
- self.assertEqual(len(bill_records), 1)
- self.assertEqual(bill_records[0].order, order)
-
- VMInstance.delete_for_bill(bill)
- instances = VMInstance.objects.filter(order=order)
- self.assertEqual(len(instances), 0)
-
-
- def test_create_matrix_vm(self):
- order = Order.objects.create(owner=self.user,
- recurring_period=self.default_recurring_period,
- billing_address=self.ba,
- pricing_plan = self.pricing_plan,
- product=self.product,
- config=self.config)
- VMInstance.create_instance(order)
- instances = VMInstance.objects.filter(order=order)
- self.assertEqual(len(instances), 1)
-
- def test_update_dns(self):
- #add_to_dns_files("test12", "192.168.1.32", "2001:db8::44", settings.MATRIX_DNS_MAIN_DOMAIN)
- remove_from_dns_files("test1", settings.MATRIX_DNS_MAIN_DOMAIN)
-
-
-
-
-
-
diff --git a/nextcloud/urls.py b/nextcloud/urls.py
deleted file mode 100644
index 1d8eb7d..0000000
--- a/nextcloud/urls.py
+++ /dev/null
@@ -1,15 +0,0 @@
-from django.urls import path, include
-from django.conf import settings
-from django.conf.urls.static import static
-from .views import *
-
-app_name = 'nextcloud'
-
-urlpatterns = [
- path('order/new/', OrderPaymentView.as_view(), name='payment'),
- path('order/confirm/', OrderConfirmationView.as_view(), name='order_confirmation'),
- path('instances/', InstancesView.as_view(), name='instances'),
- path('orders/', OrdersView.as_view(), name='orders'),
- path('pricing/', PricingView.as_view(), name='pricing'),
- path('', IndexView.as_view(), name='index'),
-]
diff --git a/nextcloud/utils.py b/nextcloud/utils.py
deleted file mode 100644
index a2ea75d..0000000
--- a/nextcloud/utils.py
+++ /dev/null
@@ -1,89 +0,0 @@
-import json
-import datetime
-import gitlab
-import logging
-from uncloud_pay.models import Product, Order, ProductToRecurringPeriod, Bill, Payment
-from .models import VMMachine
-import dns.zone
-from dns.exception import DNSException
-from django.conf import settings
-
-log = logging.getLogger()
-
-def finalize_order(request, customer, billing_address,
- one_time_price, pricing_plan,
- specs):
- product = Product.objects.first()
- recurring_period_product = ProductToRecurringPeriod.objects.filter(product=product, is_default=True).first()
- order = Order.objects.create(
- owner=request.user,
- customer=customer,
- billing_address=billing_address,
- one_time_price=one_time_price,
- pricing_plan=pricing_plan,
- recurring_period= recurring_period_product.recurring_period,
- product = product,
- config=json.dumps(specs)
- )
- if order:
- end_date = order.starting_date + datetime.timedelta(seconds=order.recurring_period.duration_seconds)
- bill = Bill.create_next_bill_for_order(order, ending_date=end_date)
- payment= Payment.withdraw(owner=request.user, amount=one_time_price, notes=f"BILL #{bill.id}")
- if payment:
- #Close the bill as the payment has been added
- VMMachine.create_instance(order)
- bill.close(status="paid")
-
- return order, bill
-
-def add_to_dns_files(sub_domain, ipv4, ipv6, domain):
- gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_DNS_OAUTH_TOKEN)
- project = gl.projects.get(settings.GITLAB_DNS_PROJECT_ID)
- f_path = f'zones/{domain}'
- file = project.files.get(file_path=f_path, ref='master')
- if file:
- try:
- zone_file = file.decode()
- zone = dns.zone.from_file(zone_file, domain)
-
- log.info(f"Adding record on {zone.origin} of type A: {sub_domain}")
-
- rdataset = zone.find_rdataset(sub_domain, rdtype=dns.rdatatype.A, create=True)
- rdata = dns.rdtypes.IN.A.A(dns.rdataclass.IN, dns.rdatatype.A, address=ipv4)
- rdataset.add(rdata, ttl=600)
-
- log.info(f"Adding record on {zone.origin} of type AAAA: {sub_domain}")
-
- rdataset_ipv6 = zone.find_rdataset(sub_domain, rdtype=dns.rdatatype.AAAA, create=True)
- rdata_ipv6 = dns.rdtypes.IN.AAAA.AAAA(dns.rdataclass.IN, dns.rdatatype.AAAA, address=ipv6)
- rdataset_ipv6.add(rdata_ipv6, ttl=600)
-
- file.content = zone.to_text()
-
- # Write it back to gitlab
- file.save(branch='master', commit_message=f'Update ungleich-dns-zones {domain}')
-
- except DNSException as e:
- log.error(e.__class__, e)
- raise e
-
-def remove_from_dns_files(sub_domain, domain):
- gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_DNS_OAUTH_TOKEN)
- project = gl.projects.get(settings.GITLAB_DNS_PROJECT_ID)
- f_path = f'zones/{domain}'
- file = project.files.get(file_path=f_path, ref='master')
- if file:
- try:
- zone_file = file.decode()
- zone = dns.zone.from_file(zone_file, domain)
-
- log.info(f"Removing record on {zone.origin}: {sub_domain}")
- zone.delete_node(sub_domain)
-
- file.content = zone.to_text()
- # Write it back to gitlab
- file.save(branch='master', commit_message=f'Removing {sub_domain} from {domain}')
- except DNSException as e:
- log.error(e.__class__, e)
- raise e
-
diff --git a/nextcloud/views.py b/nextcloud/views.py
deleted file mode 100644
index 47e2553..0000000
--- a/nextcloud/views.py
+++ /dev/null
@@ -1,289 +0,0 @@
-import logging
-from django.views.generic.base import TemplateView
-from stripe.error import CardError
-from django.shortcuts import redirect, render
-from django.contrib import messages
-from django.utils.translation import get_language, ugettext_lazy as _
-from django.contrib.auth.decorators import login_required
-from django.views.decorators.cache import cache_control
-from django.utils.decorators import method_decorator
-from django.views import View
-from django.views.generic import FormView, DetailView
-from django.views.generic.list import ListView
-from django.views.decorators.csrf import csrf_exempt
-from django.views.decorators.http import require_POST
-from .forms import InitialRequestForm, RequestDomainsNamesForm
-from uncloud_pay.forms import BillingAddressForm
-from django.urls import reverse
-from django.conf import settings
-from django.utils import timezone
-from django_q.models import Schedule
-from django_q.tasks import schedule
-from django.http import (
- HttpResponseRedirect, JsonResponse, HttpResponse
-)
-from rest_framework import viewsets, permissions
-
-from uncloud_pay.models import PricingPlan
-from uncloud_pay.utils import get_order_total_with_vat
-from uncloud_pay.models import *
-from uncloud_pay.utils import validate_vat_number
-from uncloud_pay.selectors import get_billing_address_for_user, get_balance_for_user
-import uncloud_pay.stripe as uncloud_stripe
-from .models import VMMachine
-from .serializers import *
-from .utils import *
-from ldap.ldapobject import LDAPObject
-
-logger = logging.getLogger(__name__)
-
-
-class PricingView(TemplateView):
- template_name = "nextcloud/pricing.html"
-
-class IndexView(FormView):
- template_name = "nextcloud/index.html"
- form_class = InitialRequestForm
- success_url = "/nextcloud#requestform"
- success_message = "Thank you, we will contact you as soon as possible"
-
-
- def get_context_data(self, **kwargs):
- context = super().get_context_data(**kwargs)
- context['vm_pricing'] = PricingPlan.get_default_pricing()
- return context
-
- def form_valid(self, form):
- self.request.session['order'] = form.cleaned_data
- pricing = get_order_total_with_vat(
- form.cleaned_data['cores'],
- form.cleaned_data['memory'],
- form.cleaned_data['storage'],
- form.cleaned_data['pricing_name'],
- False
- )
- self.request.session['pricing'] = pricing
- return HttpResponseRedirect(reverse('nextcloud:payment'))
-
-
-class OrderPaymentView(FormView):
- template_name = 'nextcloud/order_details.html'
- success_url = 'nextcloud:order_confirmation'
- form_class = BillingAddressForm
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_context_data(self, **kwargs):
- context = super(OrderPaymentView, self).get_context_data(**kwargs)
- default_pricing = PricingPlan.get_default_pricing()
- if 'billing_address_data' in self.request.session:
- billing_address_form = BillingAddressForm(
- initial=self.request.session['billing_address_data']
- )
- else:
- old_active = get_billing_address_for_user(self.request.user)
- billing_address_form = BillingAddressForm(
- instance=old_active
- ) if old_active else BillingAddressForm(
- initial={'active': True, 'owner': self.request.user.id}
- )
- if self.request.GET.get('product', False):
- matched_prod = Product.objects.filter(name=self.request.GET.get('product')).first()
- if matched_prod:
- self.request.session['order'] = matched_prod.config
-
- details_form = InitialRequestForm(
- initial=self.request.session.get('order', {'pricing_name': default_pricing.name})
- )
- balance = get_balance_for_user(self.request.user)
- customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
- #TODO optimize this part for better performance
- uncloud_stripe.sync_cards_for_user(self.request.user)
- cards = uncloud_stripe.get_customer_cards(customer_id)
- pricing = self.request.session.get('pricing', {'name': default_pricing.name, 'total': 0})
- context.update({
- 'vm_pricing': PricingPlan.get_by_name(pricing['name']),
- 'billing_address_form': billing_address_form,
- 'details_form': details_form,
- 'balance': balance,
- 'cards': cards,
- 'stripe_key': settings.STRIPE_PUBLIC_KEY,
- 'show_cards': True if balance < pricing['total'] else False,
- })
- return context
-
- @cache_control(no_cache=True, must_revalidate=True, no_store=True)
- def get(self, request, *args, **kwargs):
- for k in ['vat_validation_status', 'token', 'id_payment_method', 'total']:
- if request.session.get(k):
- request.session.pop(k)
- return self.render_to_response(self.get_context_data())
-
- def post(self, request, *args, **kwargs):
- details_form = InitialRequestForm(request.POST)
- billing_address_form = BillingAddressForm(request.POST)
- context = self.get_context_data()
- if not details_form.is_valid() or not billing_address_form.is_valid():
- context.update({'details_form': details_form,
- 'billing_address_form': billing_address_form})
- return self.render_to_response(context)
- address = get_billing_address_for_user(self.request.user)
- if address:
- form = BillingAddressForm(self.request.POST, instance=address)
- else:
- form = BillingAddressForm(self.request.POST)
- if form.is_valid:
- billing_address_ins = form.save()
- self.request.session["billing_address_id"] = billing_address_ins.id
- self.request.session['billing_address_data'] = billing_address_form.cleaned_data
- self.request.session['billing_address_data']['owner'] = self.request.user.id
- id_payment_method = self.request.POST.get('id_payment_method', False)
- selected_card = False
- if id_payment_method and id_payment_method != 'undefined':
- uncloud_stripe.attach_payment_method(id_payment_method, self.request.user)
- selected_card = StripeCreditCard.objects.filter(card_id=id_payment_method).first()
- selected_card.activate()
- vat_number = billing_address_form.cleaned_data.get('vat_number').strip()
- if vat_number:
- customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
- validate_result = validate_vat_number(
- stripe_customer_id=customer_id,
- billing_address_id=billing_address_ins.id
- )
- if 'error' in validate_result and validate_result['error']:
- messages.add_message(
- self.request, messages.ERROR, validate_result["error"],
- extra_tags='error'
- )
- return HttpResponseRedirect(
- reverse('nextcloud:payment')
- )
- self.request.session["vat_validation_status"] = validate_result["status"]
- specs = details_form.cleaned_data
- vat_rate = VATRate.get_vat_rate(billing_address_ins)
- vat_validation_status = "verified" if billing_address_ins.vat_number_validated_on and billing_address_ins.vat_number_verified else False
- pricing = get_order_total_with_vat(
- specs['cores'], specs['memory'], specs['storage'], context['vm_pricing'].name,
- vat_rate=vat_rate * 100, vat_validation_status = vat_validation_status
- )
- self.request.session['pricing'] = pricing
- self.request.session['order'] = specs
- self.request.session['vat_validation_status'] = vat_validation_status
- amount = get_balance_for_user(self.request.user) - pricing["total"]
- if (amount < 0 and not selected_card):
- messages.add_message(
- self.request, messages.ERROR, "You haven't enough balance please select credit card to continue",
- extra_tags='error'
- )
- return HttpResponseRedirect(
- reverse('nextcloud:payment')
- )
- return HttpResponseRedirect(reverse('nextcloud:order_confirmation'))
-
-class OrderConfirmationView(DetailView):
- template_name = "nextcloud/order_confirmation.html"
- context_object_name = "order"
- model = Order
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_context_data(self, **kwargs):
- context = {
- 'order': self.request.session.get('order'),
- 'pricing': self.request.session.get('pricing'),
- 'balance': get_balance_for_user(self.request.user)
- }
- return context
-
- @cache_control(no_cache=True, must_revalidate=True, no_store=True)
- def get(self, request, *args, **kwargs):
- context = self.get_context_data()
- context['domains_form'] = RequestDomainsNamesForm(initial={})
- if ('order' not in request.session):
- return HttpResponseRedirect(reverse('nextcloud:index'))
- elif 'pricing' not in self.request.session or 'vat_validation_status' not in self.request.session:
- return HttpResponseRedirect(reverse('nextcloud:payment'))
-
- total = self.request.session['pricing']['total']
- amount = get_balance_for_user(self.request.user) - total
- if (amount < 0):
- context['stripe_deposit_amount'] = max(amount, settings.MIN_PER_TRANSACTION)
- return render(request, self.template_name, context)
-
- def post(self, request, *args, **kwargs):
- domains_form = RequestDomainsNamesForm(self.request.POST)
- if domains_form.is_valid():
- customer = StripeCustomer.objects.get(owner=self.request.user)
- billing_address = BillingAddress.objects.get(id=request.session.get('billing_address_id'))
- total = self.request.session['pricing']['total']
- self.request.session['order']['domain'] = domains_form.cleaned_data.get('subdomain') + "." + domains_form.cleaned_data.get('main_domain')
- try:
- amount = get_balance_for_user(self.request.user) - total
- if (amount < 0):
- Payment.deposit(request.user, max(abs(amount), settings.MIN_PER_TRANSACTION), source='stripe')
- amount = get_balance_for_user(self.request.user) - total
- if (amount < 0):
- messages.add_message(
- self.request, messages.ERROR, "Please make sure that you have enough balance in your wallet and try again later.",
- extra_tags='error'
- )
- return HttpResponseRedirect(
- reverse('nextcloud:order_confirmation')
- )
- order, bill = finalize_order(request, customer,
- billing_address,
- total,
- PricingPlan.get_by_name(self.request.session['pricing']['name']),
- request.session.get('order'))
- if order and bill:
- self.request.session['bill_id'] = bill.id
- return HttpResponseRedirect(reverse('uncloud_pay:order_success'))
- except CardError as e:
- messages.add_message(
- self.request, messages.ERROR, e.user_message,
- extra_tags='error'
- )
- return HttpResponseRedirect(
- reverse('nextcloud:order_confirmation')
- )
- context = self.get_context_data()
- context['domains_form'] = domains_form
- return self.render_to_response(context)
-
-class OrdersView(ListView):
- template_name = "nextcloud/orders.html"
- model = Order
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_queryset(self):
- return Order.objects.filter(owner=self.request.user).order_by('-creation_date')
-
- def post(self, request, *args, **kwargs):
- order = Order.objects.get(id=request.POST.get('order_id', 0))
- order.cancel()
- if hasattr(order, 'instance_id'):
- last_bill_record = BillRecord.objects.filter(order=order).order_by('id').last()
- schedule('nextcloud.tasks.delete_instance',
- order.instance_id,
- schedule_type=Schedule.ONCE,
- next_run=last_bill_record.ending_date or (timezone.now() + datetime.timedelta(hours=1)))
- return JsonResponse({'message': 'Successfully Cancelled'})
-
-class InstancesView(ListView):
- template_name = "nextcloud/instances.html"
- model = VMMachine
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_queryset(self):
- return VMMachine.objects.filter(owner=self.request.user).order_by('-creation_date')
-
diff --git a/opennebula/__init__.py b/opennebula/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/opennebula/admin.py b/opennebula/admin.py
deleted file mode 100644
index 8c38f3f..0000000
--- a/opennebula/admin.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.contrib import admin
-
-# Register your models here.
diff --git a/opennebula/apps.py b/opennebula/apps.py
deleted file mode 100644
index 0750576..0000000
--- a/opennebula/apps.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class OpennebulaConfig(AppConfig):
- name = 'opennebula'
diff --git a/opennebula/management/commands/opennebula-synchosts.py b/opennebula/management/commands/opennebula-synchosts.py
deleted file mode 100644
index 29f9ac1..0000000
--- a/opennebula/management/commands/opennebula-synchosts.py
+++ /dev/null
@@ -1,74 +0,0 @@
-import json
-
-import uncloud.secrets as secrets
-
-from xmlrpc.client import ServerProxy as RPCClient
-
-from django.core.management.base import BaseCommand
-from xmltodict import parse
-from enum import IntEnum
-from opennebula.models import VM as VMModel
-from uncloud_vm.models import VMHost
-from django_auth_ldap.backend import LDAPBackend
-
-
-class HostStates(IntEnum):
- """
- The following flags are copied from
- https://docs.opennebula.org/5.8/integration/system_interfaces/api.html#schemas-for-host
- """
- INIT = 0 # Initial state for enabled hosts
- MONITORING_MONITORED = 1 # Monitoring the host (from monitored)
- MONITORED = 2 # The host has been successfully monitored
- ERROR = 3 # An error ocurrer while monitoring the host
- DISABLED = 4 # The host is disabled
- MONITORING_ERROR = 5 # Monitoring the host (from error)
- MONITORING_INIT = 6 # Monitoring the host (from init)
- MONITORING_DISABLED = 7 # Monitoring the host (from disabled)
- OFFLINE = 8 # The host is totally offline
-
-
-class Command(BaseCommand):
- help = 'Syncronize Host information from OpenNebula'
-
- def add_arguments(self, parser):
- pass
-
- def handle(self, *args, **options):
- with RPCClient(secrets.OPENNEBULA_URL) as rpc_client:
- success, response, *_ = rpc_client.one.hostpool.info(secrets.OPENNEBULA_USER_PASS)
- if success:
- response = json.loads(json.dumps(parse(response)))
- host_pool = response.get('HOST_POOL', {}).get('HOST', {})
- for host in host_pool:
- host_share = host.get('HOST_SHARE', {})
-
- host_name = host.get('NAME')
- state = int(host.get('STATE', HostStates.OFFLINE.value))
-
- if state == HostStates.MONITORED:
- status = 'active'
- elif state == HostStates.DISABLED:
- status = 'disabled'
- else:
- status = 'unusable'
-
- usable_cores = host_share.get('TOTAL_CPU')
- usable_ram_in_kb = int(host_share.get('TOTAL_MEM', 0))
- usable_ram_in_gb = int(usable_ram_in_kb / 2 ** 20)
-
- # vms cannot be created like this -- Nico, 2020-03-17
- # vms = host.get('VMS', {}) or {}
- # vms = vms.get('ID', []) or []
- # vms = ','.join(vms)
-
- VMHost.objects.update_or_create(
- hostname=host_name,
- defaults={
- 'usable_cores': usable_cores,
- 'usable_ram_in_gb': usable_ram_in_gb,
- 'status': status
- }
- )
- else:
- print(response)
diff --git a/opennebula/management/commands/opennebula-syncvms.py b/opennebula/management/commands/opennebula-syncvms.py
deleted file mode 100644
index 3c12fa9..0000000
--- a/opennebula/management/commands/opennebula-syncvms.py
+++ /dev/null
@@ -1,44 +0,0 @@
-import json
-
-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
-
-
-class Command(BaseCommand):
- help = 'Syncronize VM information from OpenNebula'
-
- def add_arguments(self, parser):
- pass
-
- def handle(self, *args, **options):
- with RPCClient(settings.OPENNEBULA_URL) as rpc_client:
- success, response, *_ = rpc_client.one.vmpool.infoextended(
- settings.OPENNEBULA_USER_PASS, -2, -1, -1, -1
- )
- if success:
- vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
- unknown_user = set()
-
- backend = LDAPBackend()
-
- for vm in vms:
- vm_id = vm['ID']
- vm_owner = vm['UNAME']
-
- user = backend.populate_user(username=vm_owner)
-
- if not user:
- unknown_user.add(vm_owner)
- else:
- VMModel.objects.update_or_create(
- vmid=vm_id,
- defaults={'data': vm, 'owner': user}
- )
- print('User not found in ldap:', unknown_user)
- else:
- print(response)
diff --git a/opennebula/management/commands/opennebula-to-uncloud.py b/opennebula/management/commands/opennebula-to-uncloud.py
deleted file mode 100644
index 230159a..0000000
--- a/opennebula/management/commands/opennebula-to-uncloud.py
+++ /dev/null
@@ -1,193 +0,0 @@
-import sys
-from datetime import datetime
-
-from django.core.management.base import BaseCommand
-from django.utils import timezone
-from django.contrib.auth import get_user_model
-
-from opennebula.models import VM as VMModel
-from uncloud_vm.models import VMHost, VMProduct, VMNetworkCard, VMDiskImageProduct, VMDiskProduct
-
-from uncloud_pay.models import Order
-
-import logging
-
-log = logging.getLogger(__name__)
-
-def convert_mac_to_int(mac_address: str):
- # Remove octet connecting characters
- mac_address = mac_address.replace(':', '')
- mac_address = mac_address.replace('.', '')
- mac_address = mac_address.replace('-', '')
- mac_address = mac_address.replace(' ', '')
-
- # Parse the resulting number as hexadecimal
- mac_address = int(mac_address, base=16)
-
- return mac_address
-
-
-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
-
- return total
-
-
-def create_nics(one_vm, vm_product):
- for nic in one_vm.nics:
- mac_address = convert_mac_to_int(nic.get('MAC'))
- ip_address = nic.get('IP', None) or nic.get('IP6_GLOBAL', None)
-
- VMNetworkCard.objects.update_or_create(
- mac_address=mac_address, vm=vm_product, defaults={'ip_address': ip_address}
- )
-
-
-def sync_disk_and_image(one_vm, vm_product, disk_owner):
- """
- a) Check all opennebula disk if they are in the uncloud VM, if not add
- b) Check all uncloud disks and remove them if they are not in the opennebula VM
- """
-
- vmdisknum = 0
-
- one_disks_extra_data = []
-
- for disk in one_vm.disks:
- vmowner = one_vm.owner
- name = disk.get('image')
- vmdisknum += 1
-
- log.info("Checking disk {} for VM {}".format(name, one_vm))
-
- is_os_image, is_public, status = True, False, 'active'
-
- image_size_in_gb = disk.get('image_size_in_gb')
- disk_size_in_gb = disk.get('size_in_gb')
- storage_class = disk.get('storage_class')
- image_source = disk.get('source')
- image_source_type = disk.get('source_type')
-
- image, _ = VMDiskImageProduct.objects.update_or_create(
- name=name,
- defaults={
- 'owner': disk_owner,
- 'is_os_image': is_os_image,
- 'is_public': is_public,
- 'size_in_gb': image_size_in_gb,
- 'storage_class': storage_class,
- 'image_source': image_source,
- 'image_source_type': image_source_type,
- 'status': status
- }
- )
-
- # identify vmdisk from opennebula - primary mapping key
- extra_data = {
- 'opennebula_vm': one_vm.vmid,
- 'opennebula_size_in_gb': disk_size_in_gb,
- 'opennebula_source': disk.get('opennebula_source'),
- 'opennebula_disk_num': vmdisknum
- }
- # Save for comparing later
- one_disks_extra_data.append(extra_data)
-
- try:
- vm_disk = VMDiskProduct.objects.get(extra_data=extra_data)
- except VMDiskProduct.DoesNotExist:
- vm_disk = VMDiskProduct.objects.create(
- owner=vmowner,
- vm=vm_product,
- image=image,
- size_in_gb=disk_size_in_gb,
- extra_data=extra_data
- )
-
- # Now remove all disks that are not in above extra_data list
- for disk in VMDiskProduct.objects.filter(vm=vm_product):
- extra_data = disk.extra_data
- if not extra_data in one_disks_extra_data:
- log.info("Removing disk {} from VM {}".format(disk, vm_product))
- disk.delete()
-
- disks = [ disk.extra_data for disk in VMDiskProduct.objects.filter(vm=vm_product) ]
- log.info("VM {} has disks: {}".format(vm_product, disks))
-
-class Command(BaseCommand):
- help = 'Migrate Opennebula VM to regular (uncloud) vm'
-
- def add_arguments(self, parser):
- parser.add_argument('--disk-owner', required=True, help="The user who owns the the opennebula disks")
-
- def handle(self, *args, **options):
- log.debug("{} {}".format(args, options))
-
- disk_owner = get_user_model().objects.get(username=options['disk_owner'])
-
- for one_vm in VMModel.objects.all():
-
- if not one_vm.last_host:
- log.warning("No VMHost for VM {} - VM might be on hold - skipping".format(one_vm.vmid))
- continue
-
- try:
- vmhost = VMHost.objects.get(hostname=one_vm.last_host)
- except VMHost.DoesNotExist:
- log.error("VMHost {} does not exist, aborting".format(one_vm.last_host))
- raise
-
- cores = one_vm.cores
- ram_in_gb = one_vm.ram_in_gb
- owner = one_vm.owner
- status = 'active'
-
- ssd_size = sum([ disk['size_in_gb'] for disk in one_vm.disks if disk['pool_name'] in ['ssd', 'one'] ])
- hdd_size = sum([ disk['size_in_gb'] for disk in one_vm.disks if disk['pool_name'] in ['hdd'] ])
-
- # List of IPv4 addresses and Global IPv6 addresses
- ipv4, ipv6 = one_vm.ips
-
- # TODO: Insert actual/real creation_date, starting_date, ending_date
- # instead of pseudo one we are putting currently
- creation_date = starting_date = datetime.now(tz=timezone.utc)
-
- # 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))
-
- try:
- vm_product = VMProduct.objects.get(extra_data__opennebula_id=one_vm.vmid)
- except VMProduct.DoesNotExist:
- order = Order.objects.create(
- owner=owner,
- creation_date=creation_date,
- starting_date=starting_date
- )
- vm_product = VMProduct(
- extra_data={ 'opennebula_id': one_vm.vmid },
- name=one_vm.uncloud_name,
- order=order
- )
-
- # we don't use update_or_create, as filtering by json AND setting json
- # at the same time does not work
-
- vm_product.vmhost = vmhost
- vm_product.owner = owner
- vm_product.cores = cores
- vm_product.ram_in_gb = ram_in_gb
- vm_product.status = status
-
- vm_product.save()
-
- # Create VMNetworkCards
- create_nics(one_vm, vm_product)
-
- # Create VMDiskImageProduct and VMDiskProduct
- sync_disk_and_image(one_vm, vm_product, disk_owner=disk_owner)
diff --git a/opennebula/migrations/0001_initial.py b/opennebula/migrations/0001_initial.py
deleted file mode 100644
index 9a135c6..0000000
--- a/opennebula/migrations/0001_initial.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 10:38
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- ]
-
- operations = [
- migrations.CreateModel(
- name='VM',
- fields=[
- ('vmid', models.IntegerField(primary_key=True, serialize=False)),
- ('data', models.JSONField()),
- ],
- ),
- ]
diff --git a/opennebula/migrations/__init__.py b/opennebula/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/opennebula/models.py b/opennebula/models.py
deleted file mode 100644
index f15b845..0000000
--- a/opennebula/models.py
+++ /dev/null
@@ -1,90 +0,0 @@
-import uuid
-from django.db import models
-from django.contrib.auth import get_user_model
-from uncloud_pay.models import Product
-
-# ungleich specific
-storage_class_mapping = {
- 'one': 'ssd',
- 'ssd': 'ssd',
- 'hdd': 'hdd'
-}
-
-class VM(models.Model):
- vmid = models.IntegerField(primary_key=True)
- data = models.JSONField()
-
- @property
- def uncloud_name(self):
- return "opennebula-{}".format(self.vmid)
-
- @property
- def cores(self):
- return int(self.data['TEMPLATE']['VCPU'])
-
- @property
- def ram_in_gb(self):
- return int(self.data['TEMPLATE']['MEMORY'])/1024
-
- @property
- def disks(self):
- """
- If there is no disk then the key DISK does not exist.
-
- If there is only one disk, we have a dictionary in the database.
-
- If there are multiple disks, we have a list of dictionaries in the database.
- """
-
- disks = []
-
- if 'DISK' in self.data['TEMPLATE']:
- if type(self.data['TEMPLATE']['DISK']) is dict:
- disks = [self.data['TEMPLATE']['DISK']]
- else:
- disks = self.data['TEMPLATE']['DISK']
-
- disks = [
- {
- 'size_in_gb': int(d['SIZE'])/1024,
- 'opennebula_source': d['SOURCE'],
- 'opennebula_name': d['IMAGE'],
- 'image_size_in_gb': int(d['ORIGINAL_SIZE'])/1024,
- 'pool_name': d['POOL_NAME'],
- 'image': d['IMAGE'],
- 'source': d['SOURCE'],
- 'source_type': d['TM_MAD'],
- 'storage_class': storage_class_mapping[d['POOL_NAME']]
-
- }
- for d in disks
- ]
-
- return disks
-
- @property
- def last_host(self):
- return ((self.data.get('HISTORY_RECORDS', {}) or {}).get('HISTORY', {}) or {}).get('HOSTNAME', None)
-
- @property
- def graphics(self):
- return self.data.get('TEMPLATE', {}).get('GRAPHICS', {})
-
- @property
- def nics(self):
- _nics = self.data.get('TEMPLATE', {}).get('NIC', {})
- if isinstance(_nics, dict):
- _nics = [_nics]
- return _nics
-
- @property
- def ips(self):
- ipv4, ipv6 = [], []
- for nic in self.nics:
- ip = nic.get('IP')
- ip6 = nic.get('IP6_GLOBAL')
- if ip:
- ipv4.append(ip)
- if ip6:
- ipv6.append(ip6)
- return ipv4, ipv6
diff --git a/opennebula/serializers.py b/opennebula/serializers.py
deleted file mode 100644
index cd00622..0000000
--- a/opennebula/serializers.py
+++ /dev/null
@@ -1,10 +0,0 @@
-from rest_framework import serializers
-from opennebula.models import VM
-
-
-class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer):
- class Meta:
- model = VM
- fields = [ 'vmid', 'owner', 'data',
- 'uncloud_name', 'cores', 'ram_in_gb',
- 'disks', 'nics', 'ips' ]
diff --git a/opennebula/tests.py b/opennebula/tests.py
deleted file mode 100644
index 7ce503c..0000000
--- a/opennebula/tests.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.test import TestCase
-
-# Create your tests here.
diff --git a/opennebula/views.py b/opennebula/views.py
deleted file mode 100644
index 688f0b4..0000000
--- a/opennebula/views.py
+++ /dev/null
@@ -1,16 +0,0 @@
-from rest_framework import viewsets, permissions
-
-#from .models import VM
-# from .serializers import OpenNebulaVMSerializer
-
-# class VMViewSet(viewsets.ModelViewSet):
-# permission_classes = [permissions.IsAuthenticated]
-# serializer_class = OpenNebulaVMSerializer
-
-# def get_queryset(self):
-# if self.request.user.is_superuser:
-# obj = VM.objects.all()
-# else:
-# obj = VM.objects.filter(owner=self.request.user)
-
-# return obj
diff --git a/requirements.txt b/requirements.txt
deleted file mode 100644
index 71b1305..0000000
--- a/requirements.txt
+++ /dev/null
@@ -1,45 +0,0 @@
-# Django basics
-Django==4.0
-djangorestframework
-django-auth-ldap
-psycopg2
-
-django-bootstrap-v5
-
-#fontawesome-free
-#django-mathfilters
-
-
-ldap3
-django-allauth
-django-compressor
-xmltodict
-django-wkhtmltopdf
-parsedatetime
-# Follow are for creating graph models
-pyparsing
-pydot
-django-extensions
-django-notifications-hq
-django-environ
-
-# PDF creating
-#django-hardcopy
-
-# schema support
-pyyaml
-uritemplate
-tldextract
-# Payment & VAT
-vat-validator
-stripe
-
-#Jobs
-django-q
-redis
-
-jinja2
-#python-gitlab
-dnspython
-
-git+https://github.com/ungleich/python-oca.git#egg=oca
diff --git a/resources/ci/.lock b/resources/ci/.lock
deleted file mode 100644
index e69de29..0000000
diff --git a/resources/ci/Dockerfile b/resources/ci/Dockerfile
deleted file mode 100644
index 020b66e..0000000
--- a/resources/ci/Dockerfile
+++ /dev/null
@@ -1,3 +0,0 @@
-FROM fedora:latest
-
-RUN dnf install -y python3-devel python3-pip python3-coverage libpq-devel openldap-devel gcc chromium
diff --git a/resources/vat-rates.csv b/resources/vat-rates.csv
deleted file mode 100644
index 17bdb99..0000000
--- a/resources/vat-rates.csv
+++ /dev/null
@@ -1,325 +0,0 @@
-start_date,stop_date,territory_codes,currency_code,rate,rate_type,description
-2011-01-04,,AI,XCD,0,standard,Anguilla (British overseas territory) is exempted of VAT.
-1984-01-01,,AT,EUR,0.2,standard,Austria (member state) standard VAT rate.
-1976-01-01,1984-01-01,AT,EUR,0.18,standard,
-1973-01-01,1976-01-01,AT,EUR,0.16,standard,
-1984-01-01,,"AT-6691
-DE-87491",EUR,0.19,standard,Jungholz (Austrian town) special VAT rate.
-1984-01-01,,"AT-6991
-AT-6992
-AT-6993
-DE-87567
-DE-87568
-DE-87569",EUR,0.19,standard,Mittelberg (Austrian town) special VAT rate.
-1996-01-01,,BE,EUR,0.21,standard,Belgium (member state) standard VAT rate.
-1994-01-01,1996-01-01,BE,EUR,0.205,standard,
-1992-04-01,1994-01-01,BE,EUR,0.195,standard,
-1983-01-01,1992-04-01,BE,EUR,0.19,standard,
-1981-07-01,1983-01-01,BE,EUR,0.17,standard,
-1978-07-01,1981-07-01,BE,EUR,0.16,standard,
-1971-07-01,1978-07-01,BE,EUR,0.18,standard,
-1999-01-01,,BG,BGN,0.2,standard,Bulgaria (member state) standard VAT rate.
-1996-07-01,1999-01-01,BG,BGN,0.22,standard,
-1994-04-01,1996-07-01,BG,BGN,0.18,standard,
-2011-01-04,,BM,BMD,0,standard,Bermuda (British overseas territory) is exempted of VAT.
-2014-01-13,,"CY
-GB-BFPO 57
-GB-BFPO 58
-GB-BFPO 59
-UK-BFPO 57
-UK-BFPO 58
-UK-BFPO 59",EUR,0.19,standard,"Cyprus (member state) standard VAT rate.
-Akrotiri and Dhekelia (British overseas territory) is subjected to Cyprus' standard VAT rate."
-2013-01-14,2014-01-13,CY,EUR,0.18,standard,
-2012-03-01,2013-01-14,CY,EUR,0.17,standard,
-2003-01-01,2012-03-01,CY,EUR,0.15,standard,
-2002-07-01,2003-01-01,CY,EUR,0.13,standard,
-2000-07-01,2002-07-01,CY,EUR,0.1,standard,
-1993-10-01,2000-07-01,CY,EUR,0.08,standard,
-1992-07-01,1993-10-01,CY,EUR,0.05,standard,
-2013-01-01,,CZ,CZK,0.21,standard,Czech Republic (member state) standard VAT rate.
-2010-01-01,2013-01-01,CZ,CZK,0.2,standard,
-2004-05-01,2010-01-01,CZ,CZK,0.19,standard,
-1995-01-01,2004-05-01,CZ,CZK,0.22,standard,
-1993-01-01,1995-01-01,CZ,CZK,0.23,standard,
-2007-01-01,,DE,EUR,0.19,standard,Germany (member state) standard VAT rate.
-1998-04-01,2007-01-01,DE,EUR,0.16,standard,
-1993-01-01,1998-04-01,DE,EUR,0.15,standard,
-1983-07-01,1993-01-01,DE,EUR,0.14,standard,
-1979-07-01,1983-07-01,DE,EUR,0.13,standard,
-1978-01-01,1979-07-01,DE,EUR,0.12,standard,
-1968-07-01,1978-01-01,DE,EUR,0.11,standard,
-1968-01-01,1968-07-01,DE,EUR,0.1,standard,
-2007-01-01,,DE-27498,EUR,0,standard,Heligoland (German island) is exempted of VAT.
-2007-01-01,,"DE-78266
-CH-8238",EUR,0,standard,Busingen am Hochrhein (German territory) is exempted of VAT.
-1992-01-01,,DK,DKK,0.25,standard,Denmark (member state) standard VAT rate.
-1980-06-30,1992-01-01,DK,DKK,0.22,standard,
-1978-10-30,1980-06-30,DK,DKK,0.2025,standard,
-1977-10-03,1978-10-30,DK,DKK,0.18,standard,
-1970-06-29,1977-10-03,DK,DKK,0.15,standard,
-1968-04-01,1970-06-29,DK,DKK,0.125,standard,
-1967-07-03,1968-04-01,DK,DKK,0.1,standard,
-2009-07-01,,EE,EUR,0.2,standard,Estonia (member state) standard VAT rate.
-1993-01-01,2009-07-01,EE,EUR,0.18,standard,
-1991-01-01,1993-01-01,EE,EUR,0.1,standard,
-2016-06-01,,"GR
-EL",EUR,0.24,standard,Greece (member state) standard VAT rate.
-2010-07-01,2016-06-01,"GR
-EL",EUR,0.23,standard,
-2010-03-15,2010-07-01,"GR
-EL",EUR,0.21,standard,
-2005-04-01,2010-03-15,"GR
-EL",EUR,0.19,standard,
-1990-04-28,2005-04-01,"GR
-EL",EUR,0.18,standard,
-1988-01-01,1990-04-28,"GR
-EL",EUR,0.16,standard,
-1987-01-01,1988-01-01,"GR
-EL",EUR,0.18,standard,
-2012-09-01,,ES,EUR,0.21,standard,Spain (member state) standard VAT rate.
-2010-07-01,2012-09-01,ES,EUR,0.18,standard,
-1995-01-01,2010-07-01,ES,EUR,0.16,standard,
-1992-08-01,1995-01-01,ES,EUR,0.15,standard,
-1992-01-01,1992-08-01,ES,EUR,0.13,standard,
-1986-01-01,1992-01-01,ES,EUR,0.12,standard,
-2012-09-01,,"ES-CN
-ES-GC
-ES-TF
-IC",EUR,0,standard,Canary Islands (Spanish autonomous community) is exempted of VAT.
-2012-09-01,,"ES-ML
-ES-CE
-EA",EUR,0,standard,Ceuta and Melilla (Spanish autonomous cities) is exempted of VAT.
-2013-01-01,,FI,EUR,0.24,standard,Finland (member state) standard VAT rate.
-2010-07-01,2013-01-01,FI,EUR,0.23,standard,
-1994-06-01,2010-07-01,FI,EUR,0.22,standard,
-2013-01-01,,"FI-01
-AX",EUR,0,standard,Aland Islands (Finish autonomous region) is exempted of VAT.
-2011-01-04,,FK,FKP,0,standard,Falkland Islands (British overseas territory) is exempted of VAT.
-1992-01-01,,FO,DKK,0,standard,Faroe Islands (Danish autonomous country) is exempted of VAT.
-2014-01-01,,"FR
-MC",EUR,0.2,standard,"France (member state) standard VAT rate.
-Monaco (sovereign city-state) is member of the EU VAT area and subjected to France's standard VAT rate."
-2000-04-01,2014-01-01,"FR
-MC",EUR,0.196,standard,
-1995-08-01,2000-04-01,"FR
-MC",EUR,0.206,standard,
-1982-07-01,1995-08-01,"FR
-MC",EUR,0.186,standard,
-1977-01-01,1982-07-01,"FR
-MC",EUR,0.176,standard,
-1973-01-01,1977-01-01,"FR
-MC",EUR,0.2,standard,
-1970-01-01,1973-01-01,"FR
-MC",EUR,0.23,standard,
-1968-12-01,1970-01-01,"FR
-MC",EUR,0.19,standard,
-1968-01-01,1968-12-01,"FR
-MC",EUR,0.1666,standard,
-2014-01-01,,"FR-BL
-BL",EUR,0,standard,Saint Barthelemy (French overseas collectivity) is exempted of VAT.
-2014-01-01,,"FR-GF
-GF",EUR,0,standard,Guiana (French overseas department) is exempted of VAT.
-2014-01-01,,"FR-GP
-GP",EUR,0.085,standard,Guadeloupe (French overseas department) special VAT rate.
-2014-01-01,,"FR-MF
-MF",EUR,0,standard,Saint Martin (French overseas collectivity) is subjected to France's standard VAT rate.
-2014-01-01,,"FR-MQ
-MQ",EUR,0.085,standard,Martinique (French overseas department) special VAT rate.
-2014-01-01,,"FR-NC
-NC",XPF,0,standard,New Caledonia (French special collectivity) is exempted of VAT.
-2014-01-01,,"FR-PF
-PF",XPF,0,standard,French Polynesia (French overseas collectivity) is exempted of VAT.
-2014-01-01,,"FR-PM
-PM",EUR,0,standard,Saint Pierre and Miquelon (French overseas collectivity) is exempted of VAT.
-2014-01-01,,"FR-RE
-RE",EUR,0.085,standard,Reunion (French overseas department) special VAT rate.
-2014-01-01,,"FR-TF
-TF",EUR,0,standard,French Southern and Antarctic Lands (French overseas territory) is exempted of VAT.
-2014-01-01,,"FR-WF
-WF",XPF,0,standard,Wallis and Futuna (French overseas collectivity) is exempted of VAT.
-2014-01-01,,"FR-YT
-YT",EUR,0,standard,Mayotte (French overseas department) is exempted of VAT.
-2011-01-04,,GG,GBP,0,standard,Guernsey (British Crown dependency) is exempted of VAT.
-2011-01-04,,GI,GIP,0,standard,Gibraltar (British overseas territory) is exempted of VAT.
-1992-01-01,,GL,DKK,0,standard,Greenland (Danish autonomous country) is exempted of VAT.
-2010-07-01,2016-06-01,"GR-34007
-EL-34007",EUR,0.16,standard,Skyros (Greek island) special VAT rate.
-2010-07-01,2016-06-01,"GR-37002
-GR-37003
-GR-37005
-EL-37002
-EL-37003
-EL-37005",EUR,0.16,standard,Northern Sporades (Greek islands) special VAT rate.
-2010-07-01,2016-06-01,"GR-64004
-EL-64004",EUR,0.16,standard,Thasos (Greek island) special VAT rate.
-2010-07-01,2016-06-01,"GR-68002
-EL-68002",EUR,0.16,standard,Samothrace (Greek island) special VAT rate.
-2010-07-01,,"GR-69
-EL-69",EUR,0,standard,Mount Athos (Greek self-governed part) is exempted of VAT.
-2010-07-01,2016-06-01,"GR-81
-EL-81",EUR,0.16,standard,Dodecanese (Greek department) special VAT rate.
-2010-07-01,2016-06-01,"GR-82
-EL-82",EUR,0.16,standard,Cyclades (Greek department) special VAT rate.
-2010-07-01,2016-06-01,"GR-83
-EL-83",EUR,0.16,standard,Lesbos (Greek department) special VAT rate.
-2010-07-01,2016-06-01,"GR-84
-EL-84",EUR,0.16,standard,Samos (Greek department) special VAT rate.
-2010-07-01,2016-06-01,"GR-85
-EL-85",EUR,0.16,standard,Chios (Greek department) special VAT rate.
-2011-01-04,,GS,GBP,0,standard,South Georgia and the South Sandwich Islands (British overseas territory) is exempted of VAT.
-2012-03-01,,HR,HRK,0.25,standard,Croatia (member state) standard VAT rate.
-2009-08-01,2012-03-01,HR,HRK,0.23,standard,
-1998-08-01,2009-08-01,HR,HRK,0.22,standard,
-2012-01-01,,HU,HUF,0.27,standard,Hungary (member state) standard VAT rate.
-2009-07-01,2012-01-01,HU,HUF,0.25,standard,
-2006-01-01,2009-07-01,HU,HUF,0.2,standard,
-1988-01-01,2006-01-01,HU,HUF,0.25,standard,
-2012-01-01,,IE,EUR,0.23,standard,Republic of Ireland (member state) standard VAT rate.
-2010-01-01,2012-01-01,IE,EUR,0.21,standard,
-2008-12-01,2010-01-01,IE,EUR,0.215,standard,
-2002-03-01,2008-12-01,IE,EUR,0.21,standard,
-2001-01-01,2002-03-01,IE,EUR,0.2,standard,
-1991-03-01,2001-01-01,IE,EUR,0.21,standard,
-1990-03-01,1991-03-01,IE,EUR,0.23,standard,
-1986-03-01,1990-03-01,IE,EUR,0.25,standard,
-1983-05-01,1986-03-01,IE,EUR,0.23,standard,
-1983-03-01,1983-05-01,IE,EUR,0.35,standard,
-1982-05-01,1983-03-01,IE,EUR,0.3,standard,
-1980-05-01,1982-05-01,IE,EUR,0.25,standard,
-1976-03-01,1980-05-01,IE,EUR,0.2,standard,
-1973-09-03,1976-03-01,IE,EUR,0.195,standard,
-1972-11-01,1973-09-03,IE,EUR,0.1637,standard,
-2011-01-04,,IO,GBP,0,standard,British Indian Ocean Territory (British overseas territory) is exempted of VAT.
-2013-10-01,,IT,EUR,0.22,standard,Italy (member state) standard VAT rate.
-2011-09-17,2013-10-01,IT,EUR,0.21,standard,
-1997-10-01,2011-09-17,IT,EUR,0.2,standard,
-1988-08-01,1997-10-01,IT,EUR,0.19,standard,
-1982-08-05,1988-08-01,IT,EUR,0.18,standard,
-1981-01-01,1982-08-05,IT,EUR,0.15,standard,
-1980-11-01,1981-01-01,IT,EUR,0.14,standard,
-1980-07-03,1980-11-01,IT,EUR,0.15,standard,
-1977-02-08,1980-07-03,IT,EUR,0.14,standard,
-1973-01-01,1977-02-08,IT,EUR,0.12,standard,
-2013-10-01,,"IT-22060
-CH-6911",CHF,0,standard,Campione (Italian town) is exempted of VAT.
-2013-10-01,,IT-23030,EUR,0,standard,Livigno (Italian town) is exempted of VAT.
-2011-01-04,,JE,GBP,0,standard,Jersey (British Crown dependency) is exempted of VAT.
-2011-01-04,,KY,KYD,0,standard,Cayman Islands (British overseas territory) is exempted of VAT.
-2009-09-01,,LT,EUR,0.21,standard,Lithuania (member state) standard VAT rate.
-2009-01-01,2009-09-01,LT,EUR,0.19,standard,
-1994-05-01,2009-01-01,LT,EUR,0.18,standard,
-2015-01-01,,LU,EUR,0.17,standard,Luxembourg (member state) standard VAT rate.
-1992-01-01,2015-01-01,LU,EUR,0.15,standard,
-1983-07-01,1992-01-01,LU,EUR,0.12,standard,
-1971-01-01,1983-07-01,LU,EUR,0.1,standard,
-1970-01-01,1971-01-01,LU,EUR,0.8,standard,
-2012-07-01,,LV,EUR,0.21,standard,Latvia (member state) standard VAT rate.
-2011-01-01,2012-07-01,LV,EUR,0.22,standard,
-2009-01-01,2011-01-01,LV,EUR,0.21,standard,
-1995-05-01,2009-01-01,LV,EUR,0.18,standard,
-2011-01-04,,MS,XCD,0,standard,Montserrat (British overseas territory) is exempted of VAT.
-2004-01-01,,MT,EUR,0.18,standard,Malta (member state) standard VAT rate.
-1995-01-01,2004-01-01,MT,EUR,0.15,standard,
-2012-10-01,,NL,EUR,0.21,standard,Netherlands (member state) standard VAT rate.
-2001-01-01,2012-10-01,NL,EUR,0.19,standard,
-1992-10-01,2001-01-01,NL,EUR,0.175,standard,
-1989-01-01,1992-10-01,NL,EUR,0.185,standard,
-1986-10-01,1989-01-01,NL,EUR,0.2,standard,
-1984-01-01,1986-10-01,NL,EUR,0.19,standard,
-1976-01-01,1984-01-01,NL,EUR,0.18,standard,
-1973-01-01,1976-01-01,NL,EUR,0.16,standard,
-1971-01-01,1973-01-01,NL,EUR,0.14,standard,
-1969-01-01,1971-01-01,NL,EUR,0.12,standard,
-2012-10-01,,"NL-AW
-AW",AWG,0,standard,Aruba (Dutch country) are exempted of VAT.
-2012-10-01,,"NL-CW
-NL-SX
-CW
-SX",ANG,0,standard,Curacao and Sint Maarten (Dutch countries) are exempted of VAT.
-2012-10-01,,"NL-BQ1
-NL-BQ2
-NL-BQ3
-BQ
-BQ-BO
-BQ-SA
-BQ-SE",USD,0,standard,"Bonaire, Saba and Sint Eustatius (Dutch special municipalities) are exempted of VAT."
-2011-01-01,,PL,PLN,0.23,standard,Poland (member state) standard VAT rate.
-1993-01-08,2011-01-01,PL,PLN,0.22,standard,
-2011-01-04,,PN,NZD,0,standard,Pitcairn Islands (British overseas territory) is exempted of VAT.
-2011-01-01,,PT,EUR,0.23,standard,Portugal (member state) standard VAT rate.
-2010-07-01,2011-01-01,PT,EUR,0.21,standard,
-2008-07-01,2010-07-01,PT,EUR,0.2,standard,
-2005-07-01,2008-07-01,PT,EUR,0.21,standard,
-2002-06-05,2005-07-01,PT,EUR,0.19,standard,
-1995-01-01,2002-06-05,PT,EUR,0.17,standard,
-1992-03-24,1995-01-01,PT,EUR,0.16,standard,
-1988-02-01,1992-03-24,PT,EUR,0.17,standard,
-1986-01-01,1988-02-01,PT,EUR,0.16,standard,
-2011-01-01,,PT-20,EUR,0.18,standard,Azores (Portuguese autonomous region) special VAT rate.
-2011-01-01,,PT-30,EUR,0.22,standard,Madeira (Portuguese autonomous region) special VAT rate.
-2017-01-01,,RO,RON,0.19,standard,Romania (member state) standard VAT rate.
-2016-01-01,2017-01-01,RO,RON,0.2,standard,Romania (member state) standard VAT rate.
-2010-07-01,2016-01-01,RO,RON,0.24,standard,
-2000-01-01,2010-07-01,RO,RON,0.19,standard,
-1998-02-01,2000-01-01,RO,RON,0.22,standard,
-1993-07-01,1998-02-01,RO,RON,0.18,standard,
-1990-07-01,,SE,SEK,0.25,standard,Sweden (member state) standard VAT rate.
-1983-01-01,1990-07-01,SE,SEK,0.2346,standard,
-1981-11-16,1983-01-01,SE,SEK,0.2151,standard,
-1980-09-08,1981-11-16,SE,SEK,0.2346,standard,
-1977-06-01,1980-09-08,SE,SEK,0.2063,standard,
-1971-01-01,1977-06-01,SE,SEK,0.1765,standard,
-1969-01-01,1971-01-01,SE,SEK,0.1111,standard,
-2011-01-04,,"AC
-SH
-SH-AC
-SH-HL",SHP,0,standard,Ascension and Saint Helena (British overseas territory) is exempted of VAT.
-2011-01-04,,"TA
-SH-TA",GBP,0,standard,Tristan da Cunha (British oversea territory) is exempted of VAT.
-2013-07-01,,SI,EUR,0.22,standard,Slovenia (member state) standard VAT rate.
-2002-01-01,2013-07-01,SI,EUR,0.2,standard,
-1999-07-01,2002-01-01,SI,EUR,0.19,standard,
-2011-01-01,,SK,EUR,0.2,standard,Slovakia (member state) standard VAT rate.
-2004-01-01,2011-01-01,SK,EUR,0.19,standard,
-2003-01-01,2004-01-01,SK,EUR,0.2,standard,
-1996-01-01,2003-01-01,SK,EUR,0.23,standard,
-1993-08-01,1996-01-01,SK,EUR,0.25,standard,
-1993-01-01,1993-08-01,SK,EUR,0.23,standard,
-2011-01-04,,TC,USD,0,standard,Turks and Caicos Islands (British overseas territory) is exempted of VAT.
-2011-01-04,,"GB
-UK
-IM",GBP,0.2,standard,"United Kingdom (member state) standard VAT rate.
-Isle of Man (British self-governing dependency) is member of the EU VAT area and subjected to UK's standard VAT rate."
-2010-01-01,2011-01-04,"GB
-UK
-IM",GBP,0.175,standard,
-2008-12-01,2010-01-01,"GB
-UK
-IM",GBP,0.15,standard,
-1991-04-01,2008-12-01,"GB
-UK
-IM",GBP,0.175,standard,
-1979-06-18,1991-04-01,"GB
-UK
-IM",GBP,0.15,standard,
-1974-07-29,1979-06-18,"GB
-UK
-IM",GBP,0.08,standard,
-1973-04-01,1974-07-29,"GB
-UK
-IM",GBP,0.1,standard,
-2011-01-04,,VG,USD,0,standard,British Virgin Islands (British overseas territory) is exempted of VAT.
-2014-01-01,,CP,EUR,0,standard,Clipperton Island (French overseas possession) is exempted of VAT.
-2019-11-15,,CH,CHF,0.077,standard,Switzerland standard VAT (added manually)
-2019-11-15,,MC,EUR,0.196,standard,Monaco standard VAT (added manually)
-2019-11-15,,FR,EUR,0.2,standard,France standard VAT (added manually)
-2019-11-15,,GR,EUR,0.24,standard,Greece standard VAT (added manually)
-2019-11-15,,GB,EUR,0.2,standard,UK standard VAT (added manually)
-2019-12-17,,AD,EUR,0.045,standard,Andorra standard VAT (added manually)
-2019-12-17,,TK,EUR,0.18,standard,Turkey standard VAT (added manually)
-2019-12-17,,IS,EUR,0.24,standard,Iceland standard VAT (added manually)
-2019-12-17,,FX,EUR,0.20,standard,France metropolitan standard VAT (added manually)
-2020-01-04,,CY,EUR,0.19,standard,Cyprus standard VAT (added manually)
-2019-01-04,,IL,EUR,0.23,standard,Ireland standard VAT (added manually)
-2019-01-04,,LI,EUR,0.077,standard,Liechtenstein standard VAT (added manually)
diff --git a/scripts/ucloud b/scripts/ucloud
new file mode 100755
index 0000000..7be6b24
--- /dev/null
+++ b/scripts/ucloud
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+import argparse
+import multiprocessing as mp
+import logging
+
+from os.path import join as join_path
+from ucloud.sanity_checks import check
+
+if __name__ == "__main__":
+ arg_parser = argparse.ArgumentParser(prog='ucloud',
+ description='Open Source Cloud Management Software')
+ arg_parser.add_argument('component',
+ choices=['api', 'scheduler', 'host',
+ 'filescanner', 'imagescanner',
+ 'metadata'])
+ arg_parser.add_argument('component_args', nargs='*')
+ args = arg_parser.parse_args()
+
+ logging.basicConfig(
+ level=logging.DEBUG,
+ filename=join_path("/", "etc", "ucloud", "log.txt"),
+ filemode="a",
+ format="%(name)s %(asctime)s: %(levelname)s - %(message)s",
+ datefmt="%d-%b-%y %H:%M:%S",
+ )
+ try:
+ check()
+
+ if args.component == 'api':
+ from ucloud.api.main import main
+
+ main()
+ elif args.component == 'host':
+ from ucloud.host.main import main
+
+ hostname = args.component_args
+ mp.set_start_method('spawn')
+ main(*hostname)
+ elif args.component == 'scheduler':
+ from ucloud.scheduler.main import main
+
+ main()
+ elif args.component == 'filescanner':
+ from ucloud.filescanner.main import main
+
+ main()
+ elif args.component == 'imagescanner':
+ from ucloud.imagescanner.main import main
+
+ main()
+ elif args.component == 'metadata':
+ from ucloud.metadata.main import main
+
+ main()
+
+ except Exception as e:
+ logging.exception(e)
+ print(e)
\ No newline at end of file
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..9a35f27
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,35 @@
+from setuptools import setup, find_packages
+
+with open("README.md", "r") as fh:
+ long_description = fh.read()
+
+setup(name='ucloud',
+ version='0.0.1',
+ description='All ucloud server components.',
+ url='https://code.ungleich.ch/ucloud/ucloud',
+ long_description=long_description,
+ long_description_content_type='text/markdown',
+ classifiers=[
+ 'Development Status :: 3 - Alpha',
+ 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
+ 'Programming Language :: Python :: 3'
+ ],
+ author='ungleich',
+ author_email='technik@ungleich.ch',
+ packages=find_packages(),
+ install_requires=[
+ 'requests',
+ 'python-decouple',
+ 'flask',
+ 'flask-restful',
+ 'bitmath',
+ 'pyotp',
+ 'sshtunnel',
+ 'sphinx',
+ 'pynetbox',
+ 'sphinx-rtd-theme',
+ 'etcd3_wrapper @ https://code.ungleich.ch/ungleich-public/etcd3_wrapper/repository/master/archive.tar.gz#egg=etcd3_wrapper',
+ 'etcd3 @ https://github.com/kragniz/python-etcd3/tarball/master#egg=etcd3',
+ ],
+ scripts=['scripts/ucloud'],
+ zip_safe=False)
diff --git a/archive/uncloud_etcd_based/docs/__init__.py b/ucloud/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/docs/__init__.py
rename to ucloud/__init__.py
diff --git a/archive/uncloud_etcd_based/uncloud/api/README.md b/ucloud/api/README.md
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/api/README.md
rename to ucloud/api/README.md
diff --git a/archive/uncloud_etcd_based/uncloud/api/__init__.py b/ucloud/api/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/api/__init__.py
rename to ucloud/api/__init__.py
diff --git a/archive/uncloud_etcd_based/uncloud/api/common_fields.py b/ucloud/api/common_fields.py
similarity index 72%
rename from archive/uncloud_etcd_based/uncloud/api/common_fields.py
rename to ucloud/api/common_fields.py
index ba9fb37..e9903ac 100755
--- a/archive/uncloud_etcd_based/uncloud/api/common_fields.py
+++ b/ucloud/api/common_fields.py
@@ -1,6 +1,6 @@
import os
-from uncloud.common.shared import shared
+from ucloud.config import etcd_client, env_vars
class Optional:
@@ -19,16 +19,12 @@ class Field:
def is_valid(self):
if self.value == KeyError:
- self.add_error(
- "'{}' field is a required field".format(self.name)
- )
+ self.add_error("'{}' field is a required field".format(self.name))
else:
if isinstance(self.value, Optional):
pass
elif not isinstance(self.value, self.type):
- self.add_error(
- "Incorrect Type for '{}' field".format(self.name)
- )
+ self.add_error("Incorrect Type for '{}' field".format(self.name))
else:
self.validation()
@@ -52,8 +48,6 @@ class VmUUIDField(Field):
self.validation = self.vm_uuid_validation
def vm_uuid_validation(self):
- r = shared.etcd_client.get(
- os.path.join(shared.settings["etcd"]["vm_prefix"], self.uuid)
- )
+ r = etcd_client.get(os.path.join(env_vars.get('VM_PREFIX'), self.uuid))
if not r:
self.add_error("VM with uuid {} does not exists".format(self.uuid))
diff --git a/ucloud/api/create_image_store.py b/ucloud/api/create_image_store.py
new file mode 100755
index 0000000..17fa63c
--- /dev/null
+++ b/ucloud/api/create_image_store.py
@@ -0,0 +1,16 @@
+import json
+import os
+
+from uuid import uuid4
+
+from ucloud.config import etcd_client, env_vars
+
+data = {
+ "is_public": True,
+ "type": "ceph",
+ "name": "images",
+ "description": "first ever public image-store",
+ "attributes": {"list": [], "key": [], "pool": "images"},
+}
+
+etcd_client.put(os.path.join(env_vars.get('IMAGE_STORE_PREFIX'), uuid4().hex), json.dumps(data))
diff --git a/archive/uncloud_etcd_based/uncloud/api/helper.py b/ucloud/api/helper.py
similarity index 55%
rename from archive/uncloud_etcd_based/uncloud/api/helper.py
rename to ucloud/api/helper.py
index 8ceb3a6..63d2f90 100755
--- a/archive/uncloud_etcd_based/uncloud/api/helper.py
+++ b/ucloud/api/helper.py
@@ -1,51 +1,48 @@
import binascii
import ipaddress
import random
-import logging
+import subprocess as sp
+
import requests
from pyotp import TOTP
-from uncloud.common.shared import shared
-
-logger = logging.getLogger(__name__)
+from ucloud.config import vm_pool, env_vars
def check_otp(name, realm, token):
try:
data = {
- "auth_name": shared.settings["otp"]["auth_name"],
- "auth_token": TOTP(shared.settings["otp"]["auth_seed"]).now(),
- "auth_realm": shared.settings["otp"]["auth_realm"],
+ "auth_name": env_vars.get("AUTH_NAME"),
+ "auth_token": TOTP(env_vars.get("AUTH_SEED")).now(),
+ "auth_realm": env_vars.get("AUTH_REALM"),
"name": name,
"realm": realm,
"token": token,
}
- except binascii.Error as err:
- logger.error(
- "Cannot compute OTP for seed: {}".format(
- shared.settings["otp"]["auth_seed"]
- )
- )
+ except binascii.Error:
return 400
response = requests.post(
- shared.settings["otp"]["verification_controller_url"], json=data
+ "{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
+ OTP_SERVER=env_vars.get("OTP_SERVER", ""),
+ OTP_VERIFY_ENDPOINT=env_vars.get("OTP_VERIFY_ENDPOINT", "verify/"),
+ ),
+ json=data,
)
return response.status_code
def resolve_vm_name(name, owner):
"""Return UUID of Virtual Machine of name == name and owner == owner
-
+
Input: name of vm, owner of vm.
Output: uuid of vm if found otherwise None
"""
result = next(
filter(
- lambda vm: vm.value["owner"] == owner
- and vm.value["name"] == name,
- shared.vm_pool.vms,
+ lambda vm: vm.value["owner"] == owner and vm.value["name"] == name,
+ vm_pool.vms,
),
None,
)
@@ -57,7 +54,7 @@ def resolve_vm_name(name, owner):
def resolve_image_name(name, etcd_client):
"""Return image uuid given its name and its store
-
+
* If the provided name is not in correct format
i.e {store_name}:{image_name} return ValueError
* If no such image found then return KeyError
@@ -73,35 +70,26 @@ def resolve_image_name(name, etcd_client):
"""
Examples, where it would work and where it would raise exception
"images:alpine" --> ["images", "alpine"]
-
+
"images" --> ["images"] it would raise Exception as non enough value to unpack
-
+
"images:alpine:meow" --> ["images", "alpine", "meow"] it would raise Exception
as too many values to unpack
"""
store_name, image_name = store_name_and_image_name
except Exception:
- raise ValueError(
- "Image name not in correct format i.e {store_name}:{image_name}"
- )
+ raise ValueError("Image name not in correct format i.e {store_name}:{image_name}")
- images = etcd_client.get_prefix(
- shared.settings["etcd"]["image_prefix"], value_in_json=True
- )
+ images = etcd_client.get_prefix(env_vars.get('IMAGE_PREFIX'), value_in_json=True)
# Try to find image with name == image_name and store_name == store_name
try:
- image = next(
- filter(
- lambda im: im.value["name"] == image_name
- and im.value["store_name"] == store_name,
- images,
- )
- )
+ image = next(filter(lambda im: im.value['name'] == image_name
+ and im.value['store_name'] == store_name, images))
except StopIteration:
raise KeyError("No image with name {} found.".format(name))
else:
- image_uuid = image.key.split("/")[-1]
+ image_uuid = image.key.split('/')[-1]
return image_uuid
@@ -110,7 +98,7 @@ def random_bytes(num=6):
return [random.randrange(256) for _ in range(num)]
-def generate_mac(uaa=False, multicast=False, oui=None, separator=":", byte_fmt="%02x"):
+def generate_mac(uaa=False, multicast=False, oui=None, separator=':', byte_fmt='%02x'):
mac = random_bytes()
if oui:
if type(oui) == str:
@@ -128,6 +116,36 @@ def generate_mac(uaa=False, multicast=False, oui=None, separator=":", byte_fmt="
return separator.join(byte_fmt % b for b in mac)
+def get_ip_addr(mac_address, device):
+ """Return IP address of a device provided its mac address / link local address
+ and the device with which it is connected.
+
+ For Example, if we call get_ip_addr(mac_address="52:54:00:12:34:56", device="br0")
+ the following two scenarios can happen
+ 1. It would return None if we can't be able to find device whose mac_address is equal
+ to the arg:mac_address or the mentioned arg:device does not exists or the ip address
+ we found is local.
+ 2. It would return ip_address of device whose mac_address is equal to arg:mac_address
+ and is connected/neighbor of arg:device
+ """
+ try:
+ output = sp.check_output(['ip', '-6', 'neigh', 'show', 'dev', device], stderr=sp.PIPE)
+ except sp.CalledProcessError:
+ return None
+ else:
+ result = []
+ output = output.strip().decode("utf-8")
+ output = output.split("\n")
+ for entry in output:
+ entry = entry.split()
+ if entry:
+ ip = ipaddress.ip_address(entry[0])
+ mac = entry[2]
+ if ip.is_global and mac_address == mac:
+ result.append(ip)
+ return result
+
+
def mac2ipv6(mac, prefix):
# only accept MACs separated by a colon
parts = mac.split(":")
@@ -140,9 +158,8 @@ def mac2ipv6(mac, prefix):
# format output
ipv6_parts = [str(0)] * 4
for i in range(0, len(parts), 2):
- ipv6_parts.append("".join(parts[i : i + 2]))
+ ipv6_parts.append("".join(parts[i:i + 2]))
lower_part = ipaddress.IPv6Address(":".join(ipv6_parts))
prefix = ipaddress.IPv6Address(prefix)
return str(prefix + int(lower_part))
-
diff --git a/ucloud/api/main.py b/ucloud/api/main.py
new file mode 100644
index 0000000..1475fb0
--- /dev/null
+++ b/ucloud/api/main.py
@@ -0,0 +1,517 @@
+import json
+import pynetbox
+
+from uuid import uuid4
+from os.path import join as join_path
+
+from flask import Flask, request
+from flask_restful import Resource, Api
+
+from ucloud.common import counters
+from ucloud.common.vm import VMStatus
+from ucloud.common.request import RequestEntry, RequestType
+from ucloud.config import (etcd_client, request_pool, vm_pool, host_pool, env_vars, image_storage_handler)
+from . import schemas
+from .helper import generate_mac, mac2ipv6
+from . import logger
+
+app = Flask(__name__)
+api = Api(app)
+
+
+class CreateVM(Resource):
+ """API Request to Handle Creation of VM"""
+
+ @staticmethod
+ def post():
+ data = request.json
+ validator = schemas.CreateVMSchema(data)
+ if validator.is_valid():
+ vm_uuid = uuid4().hex
+ vm_key = join_path(env_vars.get("VM_PREFIX"), vm_uuid)
+ specs = {
+ "cpu": validator.specs["cpu"],
+ "ram": validator.specs["ram"],
+ "os-ssd": validator.specs["os-ssd"],
+ "hdd": validator.specs["hdd"],
+ }
+ macs = [generate_mac() for _ in range(len(data["network"]))]
+ tap_ids = [counters.increment_etcd_counter(etcd_client, "/v1/counter/tap")
+ for _ in range(len(data["network"]))]
+ vm_entry = {
+ "name": data["vm_name"],
+ "owner": data["name"],
+ "owner_realm": data["realm"],
+ "specs": specs,
+ "hostname": "",
+ "status": VMStatus.stopped,
+ "image_uuid": validator.image_uuid,
+ "log": [],
+ "vnc_socket": "",
+ "network": list(zip(data["network"], macs, tap_ids)),
+ "metadata": {"ssh-keys": []},
+ }
+ etcd_client.put(vm_key, vm_entry, value_in_json=True)
+
+ # Create ScheduleVM Request
+ r = RequestEntry.from_scratch(
+ type=RequestType.ScheduleVM, uuid=vm_uuid,
+ request_prefix=env_vars.get("REQUEST_PREFIX")
+ )
+ request_pool.put(r)
+
+ return {"message": "VM Creation Queued"}, 200
+ return validator.get_errors(), 400
+
+
+class VmStatus(Resource):
+ @staticmethod
+ def get():
+ data = request.json
+ validator = schemas.VMStatusSchema(data)
+ if validator.is_valid():
+ vm = vm_pool.get(
+ join_path(env_vars.get("VM_PREFIX"), data["uuid"])
+ )
+ vm_value = vm.value.copy()
+ vm_value["ip"] = []
+ for network_mac_and_tap in vm.network:
+ network_name, mac, tap = network_mac_and_tap
+ network = etcd_client.get(
+ join_path(
+ env_vars.get("NETWORK_PREFIX"),
+ data["name"],
+ network_name,
+ ),
+ value_in_json=True,
+ )
+ ipv6_addr = network.value.get("ipv6").split("::")[0] + "::"
+ vm_value["ip"].append(mac2ipv6(mac, ipv6_addr))
+ vm.value = vm_value
+ return vm.value
+ else:
+ return validator.get_errors(), 400
+
+
+class CreateImage(Resource):
+ @staticmethod
+ def post():
+ data = request.json
+ validator = schemas.CreateImageSchema(data)
+ if validator.is_valid():
+ file_entry = etcd_client.get(
+ join_path(env_vars.get("FILE_PREFIX"), data["uuid"])
+ )
+ file_entry_value = json.loads(file_entry.value)
+
+ image_entry_json = {
+ "status": "TO_BE_CREATED",
+ "owner": file_entry_value["owner"],
+ "filename": file_entry_value["filename"],
+ "name": data["name"],
+ "store_name": data["image_store"],
+ "visibility": "public",
+ }
+ etcd_client.put(
+ join_path(env_vars.get("IMAGE_PREFIX"), data["uuid"]),
+ json.dumps(image_entry_json),
+ )
+
+ return {"message": "Image queued for creation."}
+ return validator.get_errors(), 400
+
+
+class ListPublicImages(Resource):
+ @staticmethod
+ def get():
+ images = etcd_client.get_prefix(
+ env_vars.get("IMAGE_PREFIX"), value_in_json=True
+ )
+ r = {
+ "images": []
+ }
+ for image in images:
+ image_key = "{}:{}".format(
+ image.value["store_name"], image.value["name"]
+ )
+ r["images"].append(
+ {"name": image_key, "status": image.value["status"]}
+ )
+ return r, 200
+
+
+class VMAction(Resource):
+ @staticmethod
+ def post():
+ data = request.json
+ validator = schemas.VmActionSchema(data)
+
+ if validator.is_valid():
+ vm_entry = vm_pool.get(
+ join_path(env_vars.get("VM_PREFIX"), data["uuid"])
+ )
+ action = data["action"]
+
+ if action == "start":
+ action = "schedule"
+
+ if action == "delete" and vm_entry.hostname == "":
+ if image_storage_handler.is_vm_image_exists(vm_entry.uuid):
+ r_status = image_storage_handler.delete_vm_image(vm_entry.uuid)
+ if r_status:
+ etcd_client.client.delete(vm_entry.key)
+ return {"message": "VM successfully deleted"}
+ else:
+ logger.error("Some Error Occurred while deleting VM")
+ return {"message": "VM deletion unsuccessfull"}
+ else:
+ etcd_client.client.delete(vm_entry.key)
+ return {"message": "VM successfully deleted"}
+
+ r = RequestEntry.from_scratch(
+ type="{}VM".format(action.title()),
+ uuid=data["uuid"],
+ hostname=vm_entry.hostname,
+ request_prefix=env_vars.get("REQUEST_PREFIX")
+ )
+ request_pool.put(r)
+ return {"message": "VM {} Queued".format(action.title())}, 200
+ else:
+ return validator.get_errors(), 400
+
+
+class VMMigration(Resource):
+ @staticmethod
+ def post():
+ data = request.json
+ validator = schemas.VmMigrationSchema(data)
+
+ if validator.is_valid():
+ vm = vm_pool.get(data["uuid"])
+
+ r = RequestEntry.from_scratch(
+ type=RequestType.ScheduleVM,
+ uuid=vm.uuid,
+ destination=join_path(
+ env_vars.get("HOST_PREFIX"), validator.destination.value
+ ),
+ migration=True,
+ request_prefix=env_vars.get("REQUEST_PREFIX")
+ )
+ request_pool.put(r)
+ return {"message": "VM Migration Initialization Queued"}, 200
+ else:
+ return validator.get_errors(), 400
+
+
+class ListUserVM(Resource):
+ @staticmethod
+ def get():
+ data = request.json
+ validator = schemas.OTPSchema(data)
+
+ if validator.is_valid():
+ vms = etcd_client.get_prefix(
+ env_vars.get("VM_PREFIX"), value_in_json=True
+ )
+ return_vms = []
+ user_vms = filter(lambda v: v.value["owner"] == data["name"], vms)
+ for vm in user_vms:
+ return_vms.append(
+ {
+ "name": vm.value["name"],
+ "vm_uuid": vm.key.split("/")[-1],
+ "specs": vm.value["specs"],
+ "status": vm.value["status"],
+ "hostname": vm.value["hostname"],
+ # "mac": vm.value["mac"],
+ "vnc_socket": None
+ if vm.value.get("vnc_socket", None) is None
+ else vm.value["vnc_socket"],
+ }
+ )
+ if return_vms:
+ return {"message": return_vms}, 200
+ return {"message": "No VM found"}, 404
+
+ else:
+ return validator.get_errors(), 400
+
+
+class ListUserFiles(Resource):
+ @staticmethod
+ def get():
+ data = request.json
+ validator = schemas.OTPSchema(data)
+
+ if validator.is_valid():
+ files = etcd_client.get_prefix(
+ env_vars.get("FILE_PREFIX"), value_in_json=True
+ )
+ return_files = []
+ user_files = list(
+ filter(lambda f: f.value["owner"] == data["name"], files)
+ )
+ for file in user_files:
+ return_files.append(
+ {
+ "filename": file.value["filename"],
+ "uuid": file.key.split("/")[-1],
+ }
+ )
+ return {"message": return_files}, 200
+ else:
+ return validator.get_errors(), 400
+
+
+class CreateHost(Resource):
+ @staticmethod
+ def post():
+ data = request.json
+ validator = schemas.CreateHostSchema(data)
+ if validator.is_valid():
+ host_key = join_path(env_vars.get("HOST_PREFIX"), uuid4().hex)
+ host_entry = {
+ "specs": data["specs"],
+ "hostname": data["hostname"],
+ "status": "DEAD",
+ "last_heartbeat": "",
+ }
+ etcd_client.put(host_key, host_entry, value_in_json=True)
+
+ return {"message": "Host Created"}, 200
+
+ return validator.get_errors(), 400
+
+
+class ListHost(Resource):
+ @staticmethod
+ def get():
+ hosts = host_pool.hosts
+ r = {
+ host.key: {
+ "status": host.status,
+ "specs": host.specs,
+ "hostname": host.hostname,
+ }
+ for host in hosts
+ }
+ return r, 200
+
+
+class GetSSHKeys(Resource):
+ @staticmethod
+ def get():
+ data = request.json
+ validator = schemas.GetSSHSchema(data)
+ if validator.is_valid():
+ if not validator.key_name.value:
+
+ # {user_prefix}/{realm}/{name}/key/
+ etcd_key = join_path(
+ env_vars.get('USER_PREFIX'),
+ data["realm"],
+ data["name"],
+ "key",
+ )
+ etcd_entry = etcd_client.get_prefix(
+ etcd_key, value_in_json=True
+ )
+
+ keys = {
+ key.key.split("/")[-1]: key.value for key in etcd_entry
+ }
+ return {"keys": keys}
+ else:
+
+ # {user_prefix}/{realm}/{name}/key/{key_name}
+ etcd_key = join_path(
+ env_vars.get('USER_PREFIX'),
+ data["realm"],
+ data["name"],
+ "key",
+ data["key_name"],
+ )
+ etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
+
+ if etcd_entry:
+ return {
+ "keys": {
+ etcd_entry.key.split("/")[-1]: etcd_entry.value
+ }
+ }
+ else:
+ return {"keys": {}}
+ else:
+ return validator.get_errors(), 400
+
+
+class AddSSHKey(Resource):
+ @staticmethod
+ def post():
+ data = request.json
+ validator = schemas.AddSSHSchema(data)
+ if validator.is_valid():
+
+ # {user_prefix}/{realm}/{name}/key/{key_name}
+ etcd_key = join_path(
+ env_vars.get("USER_PREFIX"),
+ data["realm"],
+ data["name"],
+ "key",
+ data["key_name"],
+ )
+ etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
+ if etcd_entry:
+ return {
+ "message": "Key with name '{}' already exists".format(
+ data["key_name"]
+ )
+ }
+ else:
+ # Key Not Found. It implies user' haven't added any key yet.
+ etcd_client.put(etcd_key, data["key"], value_in_json=True)
+ return {"message": "Key added successfully"}
+ else:
+ return validator.get_errors(), 400
+
+
+class RemoveSSHKey(Resource):
+ @staticmethod
+ def get():
+ data = request.json
+ validator = schemas.RemoveSSHSchema(data)
+ if validator.is_valid():
+
+ # {user_prefix}/{realm}/{name}/key/{key_name}
+ etcd_key = join_path(
+ env_vars.get("USER_PREFIX"),
+ data["realm"],
+ data["name"],
+ "key",
+ data["key_name"],
+ )
+ etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
+ if etcd_entry:
+ etcd_client.client.delete(etcd_key)
+ return {"message": "Key successfully removed."}
+ else:
+ return {
+ "message": "No Key with name '{}' Exists at all.".format(
+ data["key_name"]
+ )
+ }
+ else:
+ return validator.get_errors(), 400
+
+
+class CreateNetwork(Resource):
+ @staticmethod
+ def post():
+ data = request.json
+ validator = schemas.CreateNetwork(data)
+
+ if validator.is_valid():
+
+ network_entry = {
+ "id": counters.increment_etcd_counter(
+ etcd_client, "/v1/counter/vxlan"
+ ),
+ "type": data["type"],
+ }
+ if validator.user.value:
+ nb = pynetbox.api(
+ url=env_vars.get("NETBOX_URL"),
+ token=env_vars.get("NETBOX_TOKEN"),
+ )
+ nb_prefix = nb.ipam.prefixes.get(
+ prefix=env_vars.get("PREFIX")
+ )
+
+ prefix = nb_prefix.available_prefixes.create(
+ data={
+ "prefix_length": env_vars.get(
+ "PREFIX_LENGTH", cast=int
+ ),
+ "description": '{}\'s network "{}"'.format(
+ data["name"], data["network_name"]
+ ),
+ "is_pool": True,
+ }
+ )
+ network_entry["ipv6"] = prefix["prefix"]
+ else:
+ network_entry["ipv6"] = "fd00::/64"
+
+ network_key = join_path(
+ env_vars.get("NETWORK_PREFIX"),
+ data["name"],
+ data["network_name"],
+ )
+ etcd_client.put(network_key, network_entry, value_in_json=True)
+ return {"message": "Network successfully added."}
+ else:
+ return validator.get_errors(), 400
+
+
+class ListUserNetwork(Resource):
+ @staticmethod
+ def get():
+ data = request.json
+ validator = schemas.OTPSchema(data)
+
+ if validator.is_valid():
+ prefix = join_path(
+ env_vars.get("NETWORK_PREFIX"), data["name"]
+ )
+ networks = etcd_client.get_prefix(prefix, value_in_json=True)
+ user_networks = []
+ for net in networks:
+ net.value["name"] = net.key.split("/")[-1]
+ user_networks.append(net.value)
+ return {"networks": user_networks}, 200
+ else:
+ return validator.get_errors(), 400
+
+
+api.add_resource(CreateVM, "/vm/create")
+api.add_resource(VmStatus, "/vm/status")
+
+api.add_resource(VMAction, "/vm/action")
+api.add_resource(VMMigration, "/vm/migrate")
+
+api.add_resource(CreateImage, "/image/create")
+api.add_resource(ListPublicImages, "/image/list-public")
+
+api.add_resource(ListUserVM, "/user/vms")
+api.add_resource(ListUserFiles, "/user/files")
+api.add_resource(ListUserNetwork, "/user/networks")
+
+api.add_resource(AddSSHKey, "/user/add-ssh")
+api.add_resource(RemoveSSHKey, "/user/remove-ssh")
+api.add_resource(GetSSHKeys, "/user/get-ssh")
+
+api.add_resource(CreateHost, "/host/create")
+api.add_resource(ListHost, "/host/list")
+
+api.add_resource(CreateNetwork, "/network/create")
+
+
+def main():
+ image_stores = list(etcd_client.get_prefix(env_vars.get('IMAGE_STORE_PREFIX'), value_in_json=True))
+ if len(image_stores) == 0:
+ data = {
+ "is_public": True,
+ "type": "ceph",
+ "name": "images",
+ "description": "first ever public image-store",
+ "attributes": {"list": [], "key": [], "pool": "images"},
+ }
+
+ etcd_client.put(join_path(env_vars.get('IMAGE_STORE_PREFIX'), uuid4().hex), json.dumps(data))
+
+ app.run(host="::", debug=True)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/archive/uncloud_etcd_based/uncloud/api/schemas.py b/ucloud/api/schemas.py
similarity index 58%
rename from archive/uncloud_etcd_based/uncloud/api/schemas.py
rename to ucloud/api/schemas.py
index 87f20c9..c4f60ca 100755
--- a/archive/uncloud_etcd_based/uncloud/api/schemas.py
+++ b/ucloud/api/schemas.py
@@ -1,6 +1,6 @@
"""
This module contain classes thats validates and intercept/modify
-data coming from uncloud-cli (user)
+data coming from ucloud-cli (user)
It was primarily developed as an alternative to argument parser
of Flask_Restful which is going to be deprecated. I also tried
@@ -19,10 +19,10 @@ import os
import bitmath
-from uncloud.common.host import HostStatus
-from uncloud.common.vm import VMStatus
-from uncloud.common.shared import shared
-from . import helper, logger
+from ucloud.common.host import HostStatus
+from ucloud.common.vm import VMStatus
+from ucloud.config import etcd_client, env_vars, vm_pool, host_pool
+from . import helper
from .common_fields import Field, VmUUIDField
from .helper import check_otp, resolve_vm_name
@@ -79,12 +79,7 @@ class OTPSchema(BaseSchema):
super().__init__(data=data, fields=_fields)
def validation(self):
- if (
- check_otp(
- self.name.value, self.realm.value, self.token.value
- )
- != 200
- ):
+ if check_otp(self.name.value, self.realm.value, self.token.value) != 200:
self.add_error("Wrong Credentials")
@@ -96,9 +91,7 @@ class CreateImageSchema(BaseSchema):
# Fields
self.uuid = Field("uuid", str, data.get("uuid", KeyError))
self.name = Field("name", str, data.get("name", KeyError))
- self.image_store = Field(
- "image_store", str, data.get("image_store", KeyError)
- )
+ self.image_store = Field("image_store", str, data.get("image_store", KeyError))
# Validations
self.uuid.validation = self.file_uuid_validation
@@ -109,51 +102,34 @@ class CreateImageSchema(BaseSchema):
super().__init__(data, fields)
def file_uuid_validation(self):
- file_entry = shared.etcd_client.get(
- os.path.join(
- shared.shared.shared.shared.shared.settings["etcd"]["file_prefix"], self.uuid.value
- )
- )
+ file_entry = etcd_client.get(os.path.join(env_vars.get('FILE_PREFIX'), self.uuid.value))
if file_entry is None:
self.add_error(
- "Image File with uuid '{}' Not Found".format(
- self.uuid.value
- )
+ "Image File with uuid '{}' Not Found".format(self.uuid.value)
)
def image_store_name_validation(self):
- image_stores = list(
- shared.etcd_client.get_prefix(
- shared.shared.shared.shared.shared.settings["etcd"]["image_store_prefix"]
- )
- )
+ image_stores = list(etcd_client.get_prefix(env_vars.get('IMAGE_STORE_PREFIX')))
image_store = next(
filter(
- lambda s: json.loads(s.value)["name"]
- == self.image_store.value,
+ lambda s: json.loads(s.value)["name"] == self.image_store.value,
image_stores,
),
None,
)
if not image_store:
- self.add_error(
- "Store '{}' does not exists".format(
- self.image_store.value
- )
- )
+ self.add_error("Store '{}' does not exists".format(self.image_store.value))
# Host Operations
-
class CreateHostSchema(OTPSchema):
def __init__(self, data):
+ self.parsed_specs = {}
# Fields
self.specs = Field("specs", dict, data.get("specs", KeyError))
- self.hostname = Field(
- "hostname", str, data.get("hostname", KeyError)
- )
+ self.hostname = Field("hostname", str, data.get("hostname", KeyError))
# Validation
self.specs.validation = self.specs_validation
@@ -165,28 +141,22 @@ class CreateHostSchema(OTPSchema):
def specs_validation(self):
ALLOWED_BASE = 10
- _cpu = self.specs.value.get("cpu", KeyError)
- _ram = self.specs.value.get("ram", KeyError)
- _os_ssd = self.specs.value.get("os-ssd", KeyError)
- _hdd = self.specs.value.get("hdd", KeyError)
+ _cpu = self.specs.value.get('cpu', KeyError)
+ _ram = self.specs.value.get('ram', KeyError)
+ _os_ssd = self.specs.value.get('os-ssd', KeyError)
+ _hdd = self.specs.value.get('hdd', KeyError)
if KeyError in [_cpu, _ram, _os_ssd, _hdd]:
- self.add_error(
- "You must specify CPU, RAM and OS-SSD in your specs"
- )
+ self.add_error("You must specify CPU, RAM and OS-SSD in your specs")
return None
try:
parsed_ram = bitmath.parse_string_unsafe(_ram)
parsed_os_ssd = bitmath.parse_string_unsafe(_os_ssd)
if parsed_ram.base != ALLOWED_BASE:
- self.add_error(
- "Your specified RAM is not in correct units"
- )
+ self.add_error("Your specified RAM is not in correct units")
if parsed_os_ssd.base != ALLOWED_BASE:
- self.add_error(
- "Your specified OS-SSD is not in correct units"
- )
+ self.add_error("Your specified OS-SSD is not in correct units")
if _cpu < 1:
self.add_error("CPU must be atleast 1")
@@ -201,9 +171,7 @@ class CreateHostSchema(OTPSchema):
for hdd in _hdd:
_parsed_hdd = bitmath.parse_string_unsafe(hdd)
if _parsed_hdd.base != ALLOWED_BASE:
- self.add_error(
- "Your specified HDD is not in correct units"
- )
+ self.add_error("Your specified HDD is not in correct units")
break
else:
parsed_hdd.append(str(_parsed_hdd))
@@ -214,17 +182,15 @@ class CreateHostSchema(OTPSchema):
else:
if self.get_errors():
self.specs = {
- "cpu": _cpu,
- "ram": str(parsed_ram),
- "os-ssd": str(parsed_os_ssd),
- "hdd": parsed_hdd,
+ 'cpu': _cpu,
+ 'ram': str(parsed_ram),
+ 'os-ssd': str(parsed_os_ssd),
+ 'hdd': parsed_hdd
}
def validation(self):
if self.realm.value != "ungleich-admin":
- self.add_error(
- "Invalid Credentials/Insufficient Permission"
- )
+ self.add_error("Invalid Credentials/Insufficient Permission")
# VM Operations
@@ -232,15 +198,13 @@ class CreateHostSchema(OTPSchema):
class CreateVMSchema(OTPSchema):
def __init__(self, data):
+ self.parsed_specs = {}
+
# Fields
self.specs = Field("specs", dict, data.get("specs", KeyError))
- self.vm_name = Field(
- "vm_name", str, data.get("vm_name", KeyError)
- )
+ self.vm_name = Field("vm_name", str, data.get("vm_name", KeyError))
self.image = Field("image", str, data.get("image", KeyError))
- self.network = Field(
- "network", list, data.get("network", KeyError)
- )
+ self.network = Field("network", list, data.get("network", KeyError))
# Validation
self.image.validation = self.image_validation
@@ -254,25 +218,16 @@ class CreateVMSchema(OTPSchema):
def image_validation(self):
try:
- image_uuid = helper.resolve_image_name(
- self.image.value, shared.etcd_client
- )
+ image_uuid = helper.resolve_image_name(self.image.value, etcd_client)
except Exception as e:
- logger.exception(
- "Cannot resolve image name = %s", self.image.value
- )
self.add_error(str(e))
else:
self.image_uuid = image_uuid
def vm_name_validation(self):
- if resolve_vm_name(
- name=self.vm_name.value, owner=self.name.value
- ):
+ if resolve_vm_name(name=self.vm_name.value, owner=self.name.value):
self.add_error(
- 'VM with same name "{}" already exists'.format(
- self.vm_name.value
- )
+ 'VM with same name "{}" already exists'.format(self.vm_name.value)
)
def network_validation(self):
@@ -280,48 +235,34 @@ class CreateVMSchema(OTPSchema):
if _network:
for net in _network:
- network = shared.etcd_client.get(
- os.path.join(
- shared.shared.shared.shared.shared.settings["etcd"]["network_prefix"],
- self.name.value,
- net,
- ),
- value_in_json=True,
- )
+ network = etcd_client.get(os.path.join(env_vars.get('NETWORK_PREFIX'),
+ self.name.value,
+ net), value_in_json=True)
if not network:
- self.add_error(
- "Network with name {} does not exists".format(
- net
- )
- )
+ self.add_error("Network with name {} does not exists" \
+ .format(net))
def specs_validation(self):
ALLOWED_BASE = 10
- _cpu = self.specs.value.get("cpu", KeyError)
- _ram = self.specs.value.get("ram", KeyError)
- _os_ssd = self.specs.value.get("os-ssd", KeyError)
- _hdd = self.specs.value.get("hdd", KeyError)
+ _cpu = self.specs.value.get('cpu', KeyError)
+ _ram = self.specs.value.get('ram', KeyError)
+ _os_ssd = self.specs.value.get('os-ssd', KeyError)
+ _hdd = self.specs.value.get('hdd', KeyError)
if KeyError in [_cpu, _ram, _os_ssd, _hdd]:
- self.add_error(
- "You must specify CPU, RAM and OS-SSD in your specs"
- )
+ self.add_error("You must specify CPU, RAM and OS-SSD in your specs")
return None
try:
parsed_ram = bitmath.parse_string_unsafe(_ram)
parsed_os_ssd = bitmath.parse_string_unsafe(_os_ssd)
if parsed_ram.base != ALLOWED_BASE:
- self.add_error(
- "Your specified RAM is not in correct units"
- )
+ self.add_error("Your specified RAM is not in correct units")
if parsed_os_ssd.base != ALLOWED_BASE:
- self.add_error(
- "Your specified OS-SSD is not in correct units"
- )
+ self.add_error("Your specified OS-SSD is not in correct units")
- if int(_cpu) < 1:
+ if _cpu < 1:
self.add_error("CPU must be atleast 1")
if parsed_ram < bitmath.GB(1):
@@ -334,9 +275,7 @@ class CreateVMSchema(OTPSchema):
for hdd in _hdd:
_parsed_hdd = bitmath.parse_string_unsafe(hdd)
if _parsed_hdd.base != ALLOWED_BASE:
- self.add_error(
- "Your specified HDD is not in correct units"
- )
+ self.add_error("Your specified HDD is not in correct units")
break
else:
parsed_hdd.append(str(_parsed_hdd))
@@ -347,24 +286,21 @@ class CreateVMSchema(OTPSchema):
else:
if self.get_errors():
self.specs = {
- "cpu": _cpu,
- "ram": str(parsed_ram),
- "os-ssd": str(parsed_os_ssd),
- "hdd": parsed_hdd,
+ 'cpu': _cpu,
+ 'ram': str(parsed_ram),
+ 'os-ssd': str(parsed_os_ssd),
+ 'hdd': parsed_hdd
}
class VMStatusSchema(OTPSchema):
def __init__(self, data):
data["uuid"] = (
- resolve_vm_name(
- name=data.get("vm_name", None),
- owner=(
- data.get("in_support_of", None)
- or data.get("name", None)
- ),
- )
- or KeyError
+ resolve_vm_name(
+ name=data.get("vm_name", None),
+ owner=(data.get("in_support_of", None) or data.get("name", None)),
+ )
+ or KeyError
)
self.uuid = VmUUIDField(data)
@@ -373,10 +309,9 @@ class VMStatusSchema(OTPSchema):
super().__init__(data, fields)
def validation(self):
- vm = shared.vm_pool.get(self.uuid.value)
+ vm = vm_pool.get(self.uuid.value)
if not (
- vm.value["owner"] == self.name.value
- or self.realm.value == "ungleich-admin"
+ vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
):
self.add_error("Invalid User")
@@ -384,14 +319,11 @@ class VMStatusSchema(OTPSchema):
class VmActionSchema(OTPSchema):
def __init__(self, data):
data["uuid"] = (
- resolve_vm_name(
- name=data.get("vm_name", None),
- owner=(
- data.get("in_support_of", None)
- or data.get("name", None)
- ),
- )
- or KeyError
+ resolve_vm_name(
+ name=data.get("vm_name", None),
+ owner=(data.get("in_support_of", None) or data.get("name", None)),
+ )
+ or KeyError
)
self.uuid = VmUUIDField(data)
self.action = Field("action", str, data.get("action", KeyError))
@@ -406,23 +338,20 @@ class VmActionSchema(OTPSchema):
allowed_actions = ["start", "stop", "delete"]
if self.action.value not in allowed_actions:
self.add_error(
- "Invalid Action. Allowed Actions are {}".format(
- allowed_actions
- )
+ "Invalid Action. Allowed Actions are {}".format(allowed_actions)
)
def validation(self):
- vm = shared.vm_pool.get(self.uuid.value)
+ vm = vm_pool.get(self.uuid.value)
if not (
- vm.value["owner"] == self.name.value
- or self.realm.value == "ungleich-admin"
+ vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
):
self.add_error("Invalid User")
if (
- self.action.value == "start"
- and vm.status == VMStatus.running
- and vm.hostname != ""
+ self.action.value == "start"
+ and vm.status == VMStatus.running
+ and vm.hostname != ""
):
self.add_error("VM Already Running")
@@ -436,20 +365,15 @@ class VmActionSchema(OTPSchema):
class VmMigrationSchema(OTPSchema):
def __init__(self, data):
data["uuid"] = (
- resolve_vm_name(
- name=data.get("vm_name", None),
- owner=(
- data.get("in_support_of", None)
- or data.get("name", None)
- ),
- )
- or KeyError
+ resolve_vm_name(
+ name=data.get("vm_name", None),
+ owner=(data.get("in_support_of", None) or data.get("name", None)),
+ )
+ or KeyError
)
self.uuid = VmUUIDField(data)
- self.destination = Field(
- "destination", str, data.get("destination", KeyError)
- )
+ self.destination = Field("destination", str, data.get("destination", KeyError))
self.destination.validation = self.destination_validation
@@ -458,47 +382,31 @@ class VmMigrationSchema(OTPSchema):
def destination_validation(self):
hostname = self.destination.value
- host = next(
- filter(
- lambda h: h.hostname == hostname, shared.host_pool.hosts
- ),
- None,
- )
+ host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
if not host:
- self.add_error(
- "No Such Host ({}) exists".format(
- self.destination.value
- )
- )
+ self.add_error("No Such Host ({}) exists".format(self.destination.value))
elif host.status != HostStatus.alive:
self.add_error("Destination Host is dead")
else:
self.destination.value = host.key
def validation(self):
- vm = shared.vm_pool.get(self.uuid.value)
+ vm = vm_pool.get(self.uuid.value)
if not (
- vm.value["owner"] == self.name.value
- or self.realm.value == "ungleich-admin"
+ vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
):
self.add_error("Invalid User")
if vm.status != VMStatus.running:
self.add_error("Can't migrate non-running VM")
- if vm.hostname == os.path.join(
- shared.shared.shared.shared.shared.settings["etcd"]["host_prefix"], self.destination.value
- ):
- self.add_error(
- "Destination host couldn't be same as Source Host"
- )
+ if vm.hostname == os.path.join(env_vars.get('HOST_PREFIX'), self.destination.value):
+ self.add_error("Destination host couldn't be same as Source Host")
class AddSSHSchema(OTPSchema):
def __init__(self, data):
- self.key_name = Field(
- "key_name", str, data.get("key_name", KeyError)
- )
+ self.key_name = Field("key_name", str, data.get("key_name", KeyError))
self.key = Field("key", str, data.get("key_name", KeyError))
fields = [self.key_name, self.key]
@@ -507,9 +415,7 @@ class AddSSHSchema(OTPSchema):
class RemoveSSHSchema(OTPSchema):
def __init__(self, data):
- self.key_name = Field(
- "key_name", str, data.get("key_name", KeyError)
- )
+ self.key_name = Field("key_name", str, data.get("key_name", KeyError))
fields = [self.key_name]
super().__init__(data=data, fields=fields)
@@ -517,9 +423,7 @@ class RemoveSSHSchema(OTPSchema):
class GetSSHSchema(OTPSchema):
def __init__(self, data):
- self.key_name = Field(
- "key_name", str, data.get("key_name", None)
- )
+ self.key_name = Field("key_name", str, data.get("key_name", None))
fields = [self.key_name]
super().__init__(data=data, fields=fields)
@@ -538,20 +442,15 @@ class CreateNetwork(OTPSchema):
super().__init__(data, fields=fields)
def network_name_validation(self):
- key = os.path.join(shared.shared.shared.shared.shared.settings["etcd"]["network_prefix"], self.name.value, self.network_name.value)
- network = shared.etcd_client.get(key, value_in_json=True)
+ network = etcd_client.get(os.path.join(env_vars.get('NETWORK_PREFIX'),
+ self.name.value,
+ self.network_name.value),
+ value_in_json=True)
if network:
- self.add_error(
- "Network with name {} already exists".format(
- self.network_name.value
- )
- )
+ self.add_error("Network with name {} already exists" \
+ .format(self.network_name.value))
def network_type_validation(self):
supported_network_types = ["vxlan"]
if self.type.value not in supported_network_types:
- self.add_error(
- "Unsupported Network Type. Supported network types are {}".format(
- supported_network_types
- )
- )
+ self.add_error("Unsupported Network Type. Supported network types are {}".format(supported_network_types))
diff --git a/archive/uncloud_etcd_based/uncloud/common/__init__.py b/ucloud/common/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/common/__init__.py
rename to ucloud/common/__init__.py
diff --git a/archive/uncloud_etcd_based/uncloud/common/classes.py b/ucloud/common/classes.py
similarity index 93%
rename from archive/uncloud_etcd_based/uncloud/common/classes.py
rename to ucloud/common/classes.py
index 29dffd4..2eae809 100644
--- a/archive/uncloud_etcd_based/uncloud/common/classes.py
+++ b/ucloud/common/classes.py
@@ -1,4 +1,4 @@
-from .etcd_wrapper import EtcdEntry
+from etcd3_wrapper import EtcdEntry
class SpecificEtcdEntryBase:
diff --git a/archive/uncloud_etcd_based/uncloud/common/counters.py b/ucloud/common/counters.py
similarity index 91%
rename from archive/uncloud_etcd_based/uncloud/common/counters.py
rename to ucloud/common/counters.py
index 2d4a8e9..066a870 100644
--- a/archive/uncloud_etcd_based/uncloud/common/counters.py
+++ b/ucloud/common/counters.py
@@ -1,4 +1,4 @@
-from .etcd_wrapper import Etcd3Wrapper
+from etcd3_wrapper import Etcd3Wrapper
def increment_etcd_counter(etcd_client: Etcd3Wrapper, key):
diff --git a/ucloud/common/helpers.py b/ucloud/common/helpers.py
new file mode 100644
index 0000000..1bdf0b4
--- /dev/null
+++ b/ucloud/common/helpers.py
@@ -0,0 +1,54 @@
+import logging
+import socket
+import requests
+import json
+
+from ipaddress import ip_address
+
+from os.path import join as join_path
+
+
+def create_package_loggers(packages, base_path, mode="a"):
+ loggers = {}
+ for pkg in packages:
+ logger = logging.getLogger(pkg)
+ logger_handler = logging.FileHandler(
+ join_path(base_path, "{}.txt".format(pkg)),
+ mode=mode
+ )
+ logger.setLevel(logging.DEBUG)
+ logger_handler.setFormatter(logging.Formatter(fmt="%(asctime)s: %(levelname)s - %(message)s",
+ datefmt="%d-%b-%y %H:%M:%S"))
+ logger.addHandler(logger_handler)
+ loggers[pkg] = logger
+
+
+# TODO: Should be removed as soon as migration
+# mechanism is finalized inside ucloud
+def get_ipv4_address():
+ # If host is connected to internet
+ # Return IPv4 address of machine
+ # Otherwise, return 127.0.0.1
+ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
+ try:
+ s.connect(("8.8.8.8", 80))
+ except socket.timeout:
+ address = "127.0.0.1"
+ except Exception as e:
+ logging.getLogger().exception(e)
+ address = "127.0.0.1"
+ else:
+ address = s.getsockname()[0]
+
+ return address
+
+
+def get_ipv6_address():
+ try:
+ r = requests.get("https://api6.ipify.org?format=json")
+ content = json.loads(r.content.decode("utf-8"))
+ ip = ip_address(content["ip"]).exploded
+ except Exception as e:
+ logging.exception(e)
+ else:
+ return ip
diff --git a/archive/uncloud_etcd_based/uncloud/common/host.py b/ucloud/common/host.py
similarity index 85%
rename from archive/uncloud_etcd_based/uncloud/common/host.py
rename to ucloud/common/host.py
index f7bb7d5..ccbf7a8 100644
--- a/archive/uncloud_etcd_based/uncloud/common/host.py
+++ b/ucloud/common/host.py
@@ -7,7 +7,7 @@ from .classes import SpecificEtcdEntryBase
class HostStatus:
- """Possible Statuses of uncloud host."""
+ """Possible Statuses of ucloud host."""
alive = "ALIVE"
dead = "DEAD"
@@ -26,13 +26,11 @@ class HostEntry(SpecificEtcdEntryBase):
def update_heartbeat(self):
self.status = HostStatus.alive
- self.last_heartbeat = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
+ self.last_heartbeat = time.strftime("%Y-%m-%d %H:%M:%S")
def is_alive(self):
- last_heartbeat = datetime.strptime(
- self.last_heartbeat, "%Y-%m-%d %H:%M:%S"
- )
- delta = datetime.utcnow() - last_heartbeat
+ last_heartbeat = datetime.strptime(self.last_heartbeat, "%Y-%m-%d %H:%M:%S")
+ delta = datetime.now() - last_heartbeat
if delta.total_seconds() > 60:
return False
return True
diff --git a/archive/uncloud_etcd_based/uncloud/common/request.py b/ucloud/common/request.py
similarity index 74%
rename from archive/uncloud_etcd_based/uncloud/common/request.py
rename to ucloud/common/request.py
index cb0add5..cadac80 100644
--- a/archive/uncloud_etcd_based/uncloud/common/request.py
+++ b/ucloud/common/request.py
@@ -2,8 +2,9 @@ import json
from os.path import join
from uuid import uuid4
-from uncloud.common.etcd_wrapper import EtcdEntry
-from uncloud.common.classes import SpecificEtcdEntryBase
+from etcd3_wrapper.etcd3_wrapper import PsuedoEtcdEntry
+
+from .classes import SpecificEtcdEntryBase
class RequestType:
@@ -17,9 +18,8 @@ class RequestType:
class RequestEntry(SpecificEtcdEntryBase):
+
def __init__(self, e):
- self.destination_sock_path = None
- self.destination_host_key = None
self.type = None # type: str
self.migration = None # type: bool
self.destination = None # type: str
@@ -29,8 +29,8 @@ class RequestEntry(SpecificEtcdEntryBase):
@classmethod
def from_scratch(cls, request_prefix, **kwargs):
- e = EtcdEntry(meta_or_key=join(request_prefix, uuid4().hex),
- value=json.dumps(kwargs).encode('utf-8'), value_in_json=True)
+ e = PsuedoEtcdEntry(join(request_prefix, uuid4().hex),
+ value=json.dumps(kwargs).encode("utf-8"), value_in_json=True)
return cls(e)
diff --git a/archive/uncloud_etcd_based/uncloud/common/storage_handlers.py b/ucloud/common/storage_handlers.py
similarity index 63%
rename from archive/uncloud_etcd_based/uncloud/common/storage_handlers.py
rename to ucloud/common/storage_handlers.py
index 58c2dc2..8b1097a 100644
--- a/archive/uncloud_etcd_based/uncloud/common/storage_handlers.py
+++ b/ucloud/common/storage_handlers.py
@@ -6,20 +6,17 @@ import stat
from abc import ABC
from . import logger
from os.path import join as join_path
-import uncloud.common.shared as shared
class ImageStorageHandler(ABC):
- handler_name = "base"
-
def __init__(self, image_base, vm_base):
self.image_base = image_base
self.vm_base = vm_base
def import_image(self, image_src, image_dest, protect=False):
"""Put an image at the destination
- :param image_src: An Image file
- :param image_dest: A path where :param src: is to be put.
+ :param src: An Image file
+ :param dest: A path where :param src: is to be put.
:param protect: If protect is true then the dest is protect (readonly etc)
The obj must exist on filesystem.
"""
@@ -29,8 +26,8 @@ class ImageStorageHandler(ABC):
def make_vm_image(self, image_path, path):
"""Copy image from src to dest
- :param image_path: A path
- :param path: A path
+ :param src: A path
+ :param dest: A path
src and destination must be on same storage system i.e both on file system or both on CEPH etc.
"""
@@ -46,17 +43,14 @@ class ImageStorageHandler(ABC):
def delete_vm_image(self, path):
raise NotImplementedError()
- def execute_command(self, command, report=True, error_origin=None):
- if not error_origin:
- error_origin = self.handler_name
-
+ def execute_command(self, command, report=True):
command = list(map(str, command))
try:
- sp.check_output(command, stderr=sp.PIPE)
- except sp.CalledProcessError as e:
- _stderr = e.stderr.decode("utf-8").strip()
+ output = sp.check_output(command, stderr=sp.PIPE)
+ except Exception as e:
if report:
- logger.exception("%s:- %s", error_origin, _stderr)
+ print(e)
+ logger.exception(e)
return False
return True
@@ -71,16 +65,12 @@ class ImageStorageHandler(ABC):
class FileSystemBasedImageStorageHandler(ImageStorageHandler):
- handler_name = "Filesystem"
-
def import_image(self, src, dest, protect=False):
dest = join_path(self.image_base, dest)
try:
shutil.copy(src, dest)
if protect:
- os.chmod(
- dest, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
- )
+ os.chmod(dest, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
except Exception as e:
logger.exception(e)
return False
@@ -90,7 +80,7 @@ class FileSystemBasedImageStorageHandler(ImageStorageHandler):
src = join_path(self.image_base, src)
dest = join_path(self.vm_base, dest)
try:
- shutil.copyfile(src, dest)
+ shutil.copy(src, dest)
except Exception as e:
logger.exception(e)
return False
@@ -98,14 +88,7 @@ class FileSystemBasedImageStorageHandler(ImageStorageHandler):
def resize_vm_image(self, path, size):
path = join_path(self.vm_base, path)
- command = [
- "qemu-img",
- "resize",
- "-f",
- "raw",
- path,
- "{}M".format(size),
- ]
+ command = ["qemu-img", "resize", "-f", "raw", path, "{}M".format(size)]
if self.execute_command(command):
return True
else:
@@ -134,33 +117,17 @@ class FileSystemBasedImageStorageHandler(ImageStorageHandler):
class CEPHBasedImageStorageHandler(ImageStorageHandler):
- handler_name = "Ceph"
-
def import_image(self, src, dest, protect=False):
dest = join_path(self.image_base, dest)
- import_command = ["rbd", "import", src, dest]
- commands = [import_command]
+ command = ["rbd", "import", src, dest]
if protect:
- snap_create_command = [
- "rbd",
- "snap",
- "create",
- "{}@protected".format(dest),
- ]
- snap_protect_command = [
- "rbd",
- "snap",
- "protect",
- "{}@protected".format(dest),
- ]
- commands.append(snap_create_command)
- commands.append(snap_protect_command)
+ snap_create_command = ["rbd", "snap", "create", "{}@protected".format(dest)]
+ snap_protect_command = ["rbd", "snap", "protect", "{}@protected".format(dest)]
- result = True
- for command in commands:
- result = result and self.execute_command(command)
+ return self.execute_command(command) and self.execute_command(snap_create_command) and\
+ self.execute_command(snap_protect_command)
- return result
+ return self.execute_command(command)
def make_vm_image(self, src, dest):
src = join_path(self.image_base, src)
@@ -189,19 +156,3 @@ class CEPHBasedImageStorageHandler(ImageStorageHandler):
path = join_path(self.vm_base, path)
command = ["rbd", "info", path]
return self.execute_command(command, report=False)
-
-
-def get_storage_handler():
- __storage_backend = shared.shared.settings["storage"]["storage_backend"]
- if __storage_backend == "filesystem":
- return FileSystemBasedImageStorageHandler(
- vm_base=shared.shared.settings["storage"]["vm_dir"],
- image_base=shared.shared.settings["storage"]["image_dir"],
- )
- elif __storage_backend == "ceph":
- return CEPHBasedImageStorageHandler(
- vm_base=shared.shared.settings["storage"]["ceph_vm_pool"],
- image_base=shared.shared.settings["storage"]["ceph_image_pool"],
- )
- else:
- raise Exception("Unknown Image Storage Handler")
\ No newline at end of file
diff --git a/archive/uncloud_etcd_based/uncloud/common/vm.py b/ucloud/common/vm.py
similarity index 92%
rename from archive/uncloud_etcd_based/uncloud/common/vm.py
rename to ucloud/common/vm.py
index d11046d..0fb5cea 100644
--- a/archive/uncloud_etcd_based/uncloud/common/vm.py
+++ b/ucloud/common/vm.py
@@ -12,13 +12,8 @@ class VMStatus:
error = "ERROR" # An error occurred that cannot be resolved automatically
-def declare_stopped(vm):
- vm["hostname"] = ""
- vm["in_migration"] = False
- vm["status"] = VMStatus.stopped
-
-
class VMEntry(SpecificEtcdEntryBase):
+
def __init__(self, e):
self.owner = None # type: str
self.specs = None # type: dict
@@ -47,9 +42,7 @@ class VMEntry(SpecificEtcdEntryBase):
def add_log(self, msg):
self.log = self.log[:5]
- self.log.append(
- "{} - {}".format(datetime.now().isoformat(), msg)
- )
+ self.log.append("{} - {}".format(datetime.now().isoformat(), msg))
class VmPool:
diff --git a/ucloud/config.py b/ucloud/config.py
new file mode 100644
index 0000000..7c141a3
--- /dev/null
+++ b/ucloud/config.py
@@ -0,0 +1,41 @@
+from etcd3_wrapper import Etcd3Wrapper
+
+from ucloud.common.host import HostPool
+from ucloud.common.request import RequestPool
+from ucloud.common.vm import VmPool
+from ucloud.common.storage_handlers import FileSystemBasedImageStorageHandler, CEPHBasedImageStorageHandler
+from decouple import Config, RepositoryEnv, RepositoryEmpty
+
+
+# Try importing config, but don't fail if it does not exist
+try:
+ env_vars = Config(RepositoryEnv('/etc/ucloud/ucloud.conf'))
+except FileNotFoundError:
+ env_vars = Config(RepositoryEmpty())
+
+
+etcd_wrapper_args = ()
+etcd_wrapper_kwargs = {
+ 'host': env_vars.get('ETCD_URL', 'localhost'),
+ 'port': env_vars.get('ETCD_PORT', 2379),
+ 'ca_cert': env_vars.get('CA_CERT', None),
+ 'cert_cert': env_vars.get('CERT_CERT', None),
+ 'cert_key': env_vars.get('CERT_KEY', None)
+}
+
+etcd_client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
+
+host_pool = HostPool(etcd_client, env_vars.get('HOST_PREFIX'))
+vm_pool = VmPool(etcd_client, env_vars.get('VM_PREFIX'))
+request_pool = RequestPool(etcd_client, env_vars.get('REQUEST_PREFIX'))
+
+running_vms = []
+
+__storage_backend = env_vars.get("STORAGE_BACKEND")
+if __storage_backend == "filesystem":
+ image_storage_handler = FileSystemBasedImageStorageHandler(vm_base=env_vars.get("VM_DIR"),
+ image_base=env_vars.get("IMAGE_DIR"))
+elif __storage_backend == "ceph":
+ image_storage_handler = CEPHBasedImageStorageHandler(vm_base="ssd", image_base="ssd")
+else:
+ raise Exception("Unknown Image Storage Handler")
diff --git a/archive/uncloud_etcd_based/docs/Makefile b/ucloud/docs/Makefile
similarity index 93%
rename from archive/uncloud_etcd_based/docs/Makefile
rename to ucloud/docs/Makefile
index 246b56c..5e7ea85 100644
--- a/archive/uncloud_etcd_based/docs/Makefile
+++ b/ucloud/docs/Makefile
@@ -7,7 +7,7 @@ SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source/
BUILDDIR = build/
-DESTINATION=root@staticweb.ungleich.ch:/home/services/www/ungleichstatic/staticcms.ungleich.ch/www/uncloud/
+DESTINATION=root@staticweb.ungleich.ch:/home/services/www/ungleichstatic/staticcms.ungleich.ch/www/ucloud/
.PHONY: all build clean
diff --git a/archive/uncloud_etcd_based/docs/source/__init__.py b/ucloud/docs/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/__init__.py
rename to ucloud/docs/__init__.py
diff --git a/archive/uncloud_etcd_based/test/__init__.py b/ucloud/docs/source/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/test/__init__.py
rename to ucloud/docs/source/__init__.py
diff --git a/archive/uncloud_etcd_based/docs/source/admin-guide.rst b/ucloud/docs/source/admin-guide
similarity index 72%
rename from archive/uncloud_etcd_based/docs/source/admin-guide.rst
rename to ucloud/docs/source/admin-guide
index b62808d..ec6597d 100644
--- a/archive/uncloud_etcd_based/docs/source/admin-guide.rst
+++ b/ucloud/docs/source/admin-guide
@@ -56,13 +56,40 @@ To start host we created earlier, execute the following command
ucloud host ungleich.ch
-File & image scanners
---------------------------
+Create OS Image
+---------------
-Let's assume we have uploaded an *alpine-uploaded.qcow2* disk images to our
-uncloud server. Currently, our *alpine-untouched.qcow2* is not tracked by
-ucloud. We can only make images from tracked files. So, we need to track the
-file by running File Scanner
+Create ucloud-init ready OS image (Optional)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+This step is optional if you just want to test ucloud. However, sooner or later
+you want to create OS images with ucloud-init to properly
+contexualize VMs.
+
+1. Start a VM with OS image on which you want to install ucloud-init
+2. Execute the following command on the started VM
+
+ .. code-block:: sh
+
+ apk add git
+ git clone https://code.ungleich.ch/ucloud/ucloud-init.git
+ cd ucloud-init
+ sh ./install.sh
+3. Congratulations. Your image is now ucloud-init ready.
+
+
+Upload Sample OS Image
+~~~~~~~~~~~~~~~~~~~~~~
+Execute the following to get the sample OS image file.
+
+.. code-block:: sh
+
+ mkdir /var/www/admin
+ (cd /var/www/admin && wget https://cloud.ungleich.ch/s/qTb5dFYW5ii8KsD/download)
+
+Run File Scanner and Image Scanner
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Currently, our uploaded file *alpine-untouched.qcow2* is not tracked by ucloud. We can only make
+images from tracked files. So, we need to track the file by running File Scanner
.. code-block:: sh
diff --git a/archive/uncloud_etcd_based/docs/source/conf.py b/ucloud/docs/source/conf.py
similarity index 90%
rename from archive/uncloud_etcd_based/docs/source/conf.py
rename to ucloud/docs/source/conf.py
index c8138a7..9b133f9 100644
--- a/archive/uncloud_etcd_based/docs/source/conf.py
+++ b/ucloud/docs/source/conf.py
@@ -17,9 +17,9 @@
# -- Project information -----------------------------------------------------
-project = "uncloud"
-copyright = "2019, ungleich"
-author = "ungleich"
+project = 'ucloud'
+copyright = '2019, ungleich'
+author = 'ungleich'
# -- General configuration ---------------------------------------------------
@@ -27,12 +27,12 @@ author = "ungleich"
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
- "sphinx.ext.autodoc",
- "sphinx_rtd_theme",
+ 'sphinx.ext.autodoc',
+ 'sphinx_rtd_theme',
]
# Add any paths that contain templates here, relative to this directory.
-templates_path = ["_templates"]
+templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
@@ -50,4 +50,4 @@ html_theme = "sphinx_rtd_theme"
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
-html_static_path = ["_static"]
+html_static_path = ['_static']
diff --git a/archive/uncloud_etcd_based/docs/source/diagram-code/ucloud b/ucloud/docs/source/diagram-code/ucloud
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/diagram-code/ucloud
rename to ucloud/docs/source/diagram-code/ucloud
diff --git a/archive/uncloud_etcd_based/docs/source/images/ucloud.svg b/ucloud/docs/source/images/ucloud.svg
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/images/ucloud.svg
rename to ucloud/docs/source/images/ucloud.svg
diff --git a/archive/uncloud_etcd_based/docs/source/index.rst b/ucloud/docs/source/index.rst
similarity index 90%
rename from archive/uncloud_etcd_based/docs/source/index.rst
rename to ucloud/docs/source/index.rst
index fad1f88..879ac32 100644
--- a/archive/uncloud_etcd_based/docs/source/index.rst
+++ b/ucloud/docs/source/index.rst
@@ -11,12 +11,12 @@ Welcome to ucloud's documentation!
:caption: Contents:
introduction
- setup-install
- vm-images
user-guide
+ setup-install
admin-guide
+ user-guide/how-to-create-an-os-image-for-ucloud
troubleshooting
- hacking
+
Indices and tables
==================
diff --git a/archive/uncloud_etcd_based/docs/source/introduction.rst b/ucloud/docs/source/introduction.rst
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/introduction.rst
rename to ucloud/docs/source/introduction.rst
diff --git a/archive/uncloud_etcd_based/docs/source/misc/todo.rst b/ucloud/docs/source/misc/todo.rst
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/misc/todo.rst
rename to ucloud/docs/source/misc/todo.rst
diff --git a/archive/uncloud_etcd_based/docs/source/setup-install.rst b/ucloud/docs/source/setup-install.rst
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/setup-install.rst
rename to ucloud/docs/source/setup-install.rst
diff --git a/archive/uncloud_etcd_based/docs/source/theory/summary.rst b/ucloud/docs/source/theory/summary.rst
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/theory/summary.rst
rename to ucloud/docs/source/theory/summary.rst
diff --git a/archive/uncloud_etcd_based/docs/source/troubleshooting.rst b/ucloud/docs/source/troubleshooting.rst
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/troubleshooting.rst
rename to ucloud/docs/source/troubleshooting.rst
diff --git a/archive/uncloud_etcd_based/docs/source/user-guide.rst b/ucloud/docs/source/user-guide.rst
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/user-guide.rst
rename to ucloud/docs/source/user-guide.rst
diff --git a/archive/uncloud_etcd_based/docs/source/user-guide/how-to-create-an-os-image-for-ucloud.rst b/ucloud/docs/source/user-guide/how-to-create-an-os-image-for-ucloud.rst
similarity index 100%
rename from archive/uncloud_etcd_based/docs/source/user-guide/how-to-create-an-os-image-for-ucloud.rst
rename to ucloud/docs/source/user-guide/how-to-create-an-os-image-for-ucloud.rst
diff --git a/archive/uncloud_etcd_based/uncloud/filescanner/__init__.py b/ucloud/filescanner/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/filescanner/__init__.py
rename to ucloud/filescanner/__init__.py
diff --git a/ucloud/filescanner/main.py b/ucloud/filescanner/main.py
new file mode 100755
index 0000000..b70cb5b
--- /dev/null
+++ b/ucloud/filescanner/main.py
@@ -0,0 +1,126 @@
+import glob
+import os
+import pathlib
+import subprocess as sp
+import time
+from uuid import uuid4
+
+from . import logger
+from ucloud.config import env_vars, etcd_client
+
+
+def getxattr(file, attr):
+ """Get specified user extended attribute (arg:attr) of a file (arg:file)"""
+ try:
+ attr = "user." + attr
+ value = sp.check_output(['getfattr', file,
+ '--name', attr,
+ '--only-values',
+ '--absolute-names'], stderr=sp.DEVNULL)
+ value = value.decode("utf-8")
+ except sp.CalledProcessError as e:
+ logger.exception(e)
+ value = None
+
+ return value
+
+
+def setxattr(file, attr, value):
+ """Set specified user extended attribute (arg:attr) equal to (arg:value)
+ of a file (arg:file)"""
+
+ attr = "user." + attr
+ sp.check_output(['setfattr', file,
+ '--name', attr,
+ '--value', str(value)])
+
+
+def sha512sum(file: str):
+ """Use sha512sum utility to compute sha512 sum of arg:file
+
+ IF arg:file does not exists:
+ raise FileNotFoundError exception
+ ELSE IF sum successfully computer:
+ return computed sha512 sum
+ ELSE:
+ return None
+ """
+ if not isinstance(file, str): raise TypeError
+ try:
+ output = sp.check_output(["sha512sum", file], stderr=sp.PIPE)
+ except sp.CalledProcessError as e:
+ error = e.stderr.decode("utf-8")
+ if "No such file or directory" in error:
+ raise FileNotFoundError from None
+ else:
+ output = output.decode("utf-8").strip()
+ output = output.split(" ")
+ return output[0]
+ return None
+
+
+try:
+ sp.check_output(['which', 'getfattr'])
+ sp.check_output(['which', 'setfattr'])
+except Exception as e:
+ logger.exception(e)
+ print('Make sure you have getfattr and setfattr available')
+ exit(1)
+
+
+def main():
+ BASE_DIR = env_vars.get("BASE_DIR")
+
+ FILE_PREFIX = env_vars.get("FILE_PREFIX")
+
+ # Recursively Get All Files and Folder below BASE_DIR
+ files = glob.glob("{}/**".format(BASE_DIR), recursive=True)
+
+ # Retain only Files
+ files = list(filter(os.path.isfile, files))
+
+ untracked_files = list(
+ filter(lambda f: not bool(getxattr(f, "user.utracked")), files)
+ )
+
+ tracked_files = list(
+ filter(lambda f: f not in untracked_files, files)
+ )
+ for file in untracked_files:
+ file_id = uuid4()
+
+ # Get Username
+ owner = pathlib.Path(file).parts[3]
+ # Get Creation Date of File
+ # Here, we are assuming that ctime is creation time
+ # which is mostly not true.
+ creation_date = time.ctime(os.stat(file).st_ctime)
+
+ # Get File Size
+ size = os.path.getsize(file)
+
+ # Compute sha512 sum
+ sha_sum = sha512sum(file)
+
+ # File Path excluding base and username
+ file_path = pathlib.Path(file).parts[4:]
+ file_path = os.path.join(*file_path)
+
+ # Create Entry
+ entry_key = os.path.join(FILE_PREFIX, str(file_id))
+ entry_value = {
+ "filename": file_path,
+ "owner": owner,
+ "sha512sum": sha_sum,
+ "creation_date": creation_date,
+ "size": size
+ }
+
+ print("Tracking {}".format(file))
+ # Insert Entry
+ etcd_client.put(entry_key, entry_value, value_in_json=True)
+ setxattr(file, "user.utracked", True)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/archive/uncloud_etcd_based/uncloud/hack/README.org b/ucloud/hack/README.org
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/hack/README.org
rename to ucloud/hack/README.org
diff --git a/archive/uncloud_etcd_based/uncloud/hack/conf.d/ucloud-host b/ucloud/hack/conf.d/ucloud-host
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/hack/conf.d/ucloud-host
rename to ucloud/hack/conf.d/ucloud-host
diff --git a/archive/uncloud_etcd_based/uncloud/hack/nftables.conf b/ucloud/hack/nftables.conf
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/hack/nftables.conf
rename to ucloud/hack/nftables.conf
diff --git a/archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-api b/ucloud/hack/rc-scripts/ucloud-api
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-api
rename to ucloud/hack/rc-scripts/ucloud-api
diff --git a/archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-host b/ucloud/hack/rc-scripts/ucloud-host
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-host
rename to ucloud/hack/rc-scripts/ucloud-host
diff --git a/archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-metadata b/ucloud/hack/rc-scripts/ucloud-metadata
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-metadata
rename to ucloud/hack/rc-scripts/ucloud-metadata
diff --git a/archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-scheduler b/ucloud/hack/rc-scripts/ucloud-scheduler
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/hack/rc-scripts/ucloud-scheduler
rename to ucloud/hack/rc-scripts/ucloud-scheduler
diff --git a/archive/uncloud_etcd_based/uncloud/host/__init__.py b/ucloud/host/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/host/__init__.py
rename to ucloud/host/__init__.py
diff --git a/ucloud/host/helper.py b/ucloud/host/helper.py
new file mode 100644
index 0000000..edcb82d
--- /dev/null
+++ b/ucloud/host/helper.py
@@ -0,0 +1,13 @@
+import socket
+from contextlib import closing
+
+
+def find_free_port():
+ with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
+ try:
+ s.bind(('', 0))
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ except Exception:
+ return None
+ else:
+ return s.getsockname()[1]
diff --git a/ucloud/host/main.py b/ucloud/host/main.py
new file mode 100755
index 0000000..ccf0a8d
--- /dev/null
+++ b/ucloud/host/main.py
@@ -0,0 +1,143 @@
+import argparse
+import multiprocessing as mp
+import time
+
+from etcd3_wrapper import Etcd3Wrapper
+
+from ucloud.common.request import RequestEntry, RequestType
+from ucloud.config import (vm_pool, request_pool,
+ etcd_client, running_vms,
+ etcd_wrapper_args, etcd_wrapper_kwargs,
+ HostPool, env_vars)
+
+from .helper import find_free_port
+from . import virtualmachine
+from ucloud.host import logger
+
+
+def update_heartbeat(hostname):
+ """Update Last HeartBeat Time for :param hostname: in etcd"""
+ client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
+ host_pool = HostPool(client, env_vars.get('HOST_PREFIX'))
+ this_host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
+
+ while True:
+ this_host.update_heartbeat()
+ host_pool.put(this_host)
+ time.sleep(10)
+
+
+def maintenance(host):
+ # To capture vm running according to running_vms list
+
+ # This is to capture successful migration of a VM.
+ # Suppose, this host is running "vm1" and user initiated
+ # request to migrate this "vm1" to some other host. On,
+ # successful migration the destination host would set
+ # the vm hostname to itself. Thus, we are checking
+ # whether this host vm is successfully migrated. If yes
+ # then we shutdown "vm1" on this host.
+
+ to_be_removed = []
+ for running_vm in running_vms:
+ with vm_pool.get_put(running_vm.key) as vm_entry:
+ if vm_entry.hostname != host.key and not vm_entry.in_migration:
+ running_vm.handle.shutdown()
+ logger.info("VM migration not completed successfully.")
+ to_be_removed.append(running_vm)
+
+ for r in to_be_removed:
+ running_vms.remove(r)
+
+ # To check vm running according to etcd entries
+ alleged_running_vms = vm_pool.by_status("RUNNING", vm_pool.by_host(host.key))
+
+ for vm_entry in alleged_running_vms:
+ _vm = virtualmachine.get_vm(running_vms, vm_entry.key)
+ # Whether, the allegedly running vm is in our
+ # running_vms list or not if it is said to be
+ # running on this host but it is not then we
+ # need to shut it down
+
+ # This is to capture poweroff/shutdown of a VM
+ # initiated by user inside VM. OR crash of VM by some
+ # user running process
+ if (_vm and not _vm.handle.is_running()) or not _vm:
+ logger.debug("_vm = %s, is_running() = %s" % (_vm, _vm.handle.is_running()))
+ vm_entry.add_log("""{} is not running but is said to be running.
+ So, shutting it down and declare it killed""".format(vm_entry.key))
+ vm_entry.declare_killed()
+ vm_pool.put(vm_entry)
+ if _vm:
+ running_vms.remove(_vm)
+
+
+def main(hostname):
+ heartbeat_updating_process = mp.Process(target=update_heartbeat, args=(hostname,))
+
+ host_pool = HostPool(etcd_client, env_vars.get('HOST_PREFIX'))
+ host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
+ assert host is not None, "No such host with name = {}".format(hostname)
+
+ try:
+ heartbeat_updating_process.start()
+ except Exception as e:
+ logger.info("No Need To Go Further. Our heartbeat updating mechanism is not working")
+ logger.exception(e)
+ exit(-1)
+
+ logger.info("%s Session Started %s", '*' * 5, '*' * 5)
+
+ # It is seen that under heavy load, timeout event doesn't come
+ # in a predictive manner (which is intentional because we give
+ # higher priority to customer's requests) which delays heart
+ # beat update which in turn misunderstood by scheduler that the
+ # host is dead when it is actually alive. So, to ensure that we
+ # update the heart beat in a predictive manner we start Heart
+ # beat updating mechanism in separated thread
+
+ for events_iterator in [
+ etcd_client.get_prefix(env_vars.get('REQUEST_PREFIX'), value_in_json=True),
+ etcd_client.watch_prefix(env_vars.get('REQUEST_PREFIX'), timeout=10, value_in_json=True),
+ ]:
+ for request_event in events_iterator:
+ request_event = RequestEntry(request_event)
+
+ if request_event.type == "TIMEOUT":
+ maintenance(host)
+ continue
+
+ # If the event is directed toward me OR I am destination of a InitVMMigration
+ if request_event.hostname == host.key or request_event.destination == host.key:
+ logger.debug("VM Request: %s", request_event)
+
+ request_pool.client.client.delete(request_event.key)
+ vm_entry = vm_pool.get(request_event.uuid)
+
+ if vm_entry:
+ if request_event.type == RequestType.StartVM:
+ virtualmachine.start(vm_entry)
+
+ elif request_event.type == RequestType.StopVM:
+ virtualmachine.stop(vm_entry)
+
+ elif request_event.type == RequestType.DeleteVM:
+ virtualmachine.delete(vm_entry)
+
+ elif request_event.type == RequestType.InitVMMigration:
+ virtualmachine.start(vm_entry, host.key, find_free_port())
+
+ elif request_event.type == RequestType.TransferVM:
+ virtualmachine.transfer(request_event)
+ else:
+ logger.info("VM Entry missing")
+
+ logger.info("Running VMs %s", running_vms)
+
+
+if __name__ == "__main__":
+ argparser = argparse.ArgumentParser()
+ argparser.add_argument("hostname", help="Name of this host. e.g /v1/host/1")
+ args = argparser.parse_args()
+ mp.set_start_method('spawn')
+ main(args.hostname)
diff --git a/ucloud/host/qmp/__init__.py b/ucloud/host/qmp/__init__.py
new file mode 100755
index 0000000..775b397
--- /dev/null
+++ b/ucloud/host/qmp/__init__.py
@@ -0,0 +1,537 @@
+# QEMU library
+#
+# Copyright (C) 2015-2016 Red Hat Inc.
+# Copyright (C) 2012 IBM Corp.
+#
+# Authors:
+# Fam Zheng
+#
+# This work is licensed under the terms of the GNU GPL, version 2. See
+# the COPYING file in the top-level directory.
+#
+# Based on qmp.py.
+#
+
+import errno
+import logging
+import os
+import shutil
+import socket
+import subprocess
+import tempfile
+
+from . import qmp
+
+LOG = logging.getLogger(__name__)
+
+# Mapping host architecture to any additional architectures it can
+# support which often includes its 32 bit cousin.
+ADDITIONAL_ARCHES = {
+ "x86_64": "i386",
+ "aarch64": "armhf"
+}
+
+
+def kvm_available(target_arch=None):
+ host_arch = os.uname()[4]
+ if target_arch and target_arch != host_arch:
+ if target_arch != ADDITIONAL_ARCHES.get(host_arch):
+ return False
+ return os.access("/dev/kvm", os.R_OK | os.W_OK)
+
+
+class QEMUMachineError(Exception):
+ """
+ Exception called when an error in QEMUMachine happens.
+ """
+
+
+class QEMUMachineAddDeviceError(QEMUMachineError):
+ """
+ Exception raised when a request to add a device can not be fulfilled
+
+ The failures are caused by limitations, lack of information or conflicting
+ requests on the QEMUMachine methods. This exception does not represent
+ failures reported by the QEMU binary itself.
+ """
+
+
+class MonitorResponseError(qmp.QMPError):
+ """
+ Represents erroneous QMP monitor reply
+ """
+
+ def __init__(self, reply):
+ try:
+ desc = reply["error"]["desc"]
+ except KeyError:
+ desc = reply
+ super(MonitorResponseError, self).__init__(desc)
+ self.reply = reply
+
+
+class QEMUMachine(object):
+ """
+ A QEMU VM
+
+ Use this object as a context manager to ensure the QEMU process terminates::
+
+ with VM(binary) as vm:
+ ...
+ # vm is guaranteed to be shut down here
+ """
+
+ def __init__(self, binary, args=None, wrapper=None, name=None,
+ test_dir="/var/tmp", monitor_address=None,
+ socket_scm_helper=None):
+ '''
+ Initialize a QEMUMachine
+
+ @param binary: path to the qemu binary
+ @param args: list of extra arguments
+ @param wrapper: list of arguments used as prefix to qemu binary
+ @param name: prefix for socket and log file names (default: qemu-PID)
+ @param test_dir: where to create socket and log file
+ @param monitor_address: address for QMP monitor
+ @param socket_scm_helper: helper program, required for send_fd_scm()
+ @note: Qemu process is not started until launch() is used.
+ '''
+ if args is None:
+ args = []
+ if wrapper is None:
+ wrapper = []
+ if name is None:
+ name = "qemu-%d" % os.getpid()
+ self._name = name
+ self._monitor_address = monitor_address
+ self._vm_monitor = None
+ self._qemu_log_path = None
+ self._qemu_log_file = None
+ self._popen = None
+ self._binary = binary
+ self._args = list(args) # Force copy args in case we modify them
+ self._wrapper = wrapper
+ self._events = []
+ self._iolog = None
+ self._socket_scm_helper = socket_scm_helper
+ self._qmp = None
+ self._qemu_full_args = None
+ self._test_dir = test_dir
+ self._temp_dir = None
+ self._launched = False
+ self._machine = None
+ self._console_set = False
+ self._console_device_type = None
+ self._console_address = None
+ self._console_socket = None
+
+ # just in case logging wasn't configured by the main script:
+ logging.basicConfig(level=logging.DEBUG)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.shutdown()
+ return False
+
+ # This can be used to add an unused monitor instance.
+ def add_monitor_null(self):
+ self._args.append('-monitor')
+ self._args.append('null')
+
+ def add_fd(self, fd, fdset, opaque, opts=''):
+ """
+ Pass a file descriptor to the VM
+ """
+ options = ['fd=%d' % fd,
+ 'set=%d' % fdset,
+ 'opaque=%s' % opaque]
+ if opts:
+ options.append(opts)
+
+ # This did not exist before 3.4, but since then it is
+ # mandatory for our purpose
+ if hasattr(os, 'set_inheritable'):
+ os.set_inheritable(fd, True)
+
+ self._args.append('-add-fd')
+ self._args.append(','.join(options))
+ return self
+
+ # Exactly one of fd and file_path must be given.
+ # (If it is file_path, the helper will open that file and pass its
+ # own fd)
+ def send_fd_scm(self, fd=None, file_path=None):
+ # In iotest.py, the qmp should always use unix socket.
+ assert self._qmp.is_scm_available()
+ if self._socket_scm_helper is None:
+ raise QEMUMachineError("No path to socket_scm_helper set")
+ if not os.path.exists(self._socket_scm_helper):
+ raise QEMUMachineError("%s does not exist" %
+ self._socket_scm_helper)
+
+ # This did not exist before 3.4, but since then it is
+ # mandatory for our purpose
+ if hasattr(os, 'set_inheritable'):
+ os.set_inheritable(self._qmp.get_sock_fd(), True)
+ if fd is not None:
+ os.set_inheritable(fd, True)
+
+ fd_param = ["%s" % self._socket_scm_helper,
+ "%d" % self._qmp.get_sock_fd()]
+
+ if file_path is not None:
+ assert fd is None
+ fd_param.append(file_path)
+ else:
+ assert fd is not None
+ fd_param.append(str(fd))
+
+ devnull = open(os.path.devnull, 'rb')
+ proc = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT, close_fds=False)
+ output = proc.communicate()[0]
+ if output:
+ LOG.debug(output)
+
+ return proc.returncode
+
+ @staticmethod
+ def _remove_if_exists(path):
+ """
+ Remove file object at path if it exists
+ """
+ try:
+ os.remove(path)
+ except OSError as exception:
+ if exception.errno == errno.ENOENT:
+ return
+ raise
+
+ def is_running(self):
+ return self._popen is not None and self._popen.poll() is None
+
+ def exitcode(self):
+ if self._popen is None:
+ return None
+ return self._popen.poll()
+
+ def get_pid(self):
+ if not self.is_running():
+ return None
+ return self._popen.pid
+
+ def _load_io_log(self):
+ if self._qemu_log_path is not None:
+ with open(self._qemu_log_path, "r") as iolog:
+ self._iolog = iolog.read()
+
+ def _base_args(self):
+ if isinstance(self._monitor_address, tuple):
+ moncdev = "socket,id=mon,host=%s,port=%s" % (
+ self._monitor_address[0],
+ self._monitor_address[1])
+ else:
+ moncdev = 'socket,id=mon,path=%s' % self._vm_monitor
+ args = ['-chardev', moncdev,
+ '-mon', 'chardev=mon,mode=control']
+ if self._machine is not None:
+ args.extend(['-machine', self._machine])
+ if self._console_set:
+ self._console_address = os.path.join(self._temp_dir,
+ self._name + "-console.sock")
+ chardev = ('socket,id=console,path=%s,server,nowait' %
+ self._console_address)
+ args.extend(['-chardev', chardev])
+ if self._console_device_type is None:
+ args.extend(['-serial', 'chardev:console'])
+ else:
+ device = '%s,chardev=console' % self._console_device_type
+ args.extend(['-device', device])
+ return args
+
+ def _pre_launch(self):
+ self._temp_dir = tempfile.mkdtemp(dir=self._test_dir)
+ if self._monitor_address is not None:
+ self._vm_monitor = self._monitor_address
+ else:
+ self._vm_monitor = os.path.join(self._temp_dir,
+ self._name + "-monitor.sock")
+ self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log")
+ self._qemu_log_file = open(self._qemu_log_path, 'wb')
+
+ self._qmp = qmp.QEMUMonitorProtocol(self._vm_monitor,
+ server=True)
+
+ def _post_launch(self):
+ self._qmp.accept()
+
+ def _post_shutdown(self):
+ if self._qemu_log_file is not None:
+ self._qemu_log_file.close()
+ self._qemu_log_file = None
+
+ self._qemu_log_path = None
+
+ if self._console_socket is not None:
+ self._console_socket.close()
+ self._console_socket = None
+
+ if self._temp_dir is not None:
+ shutil.rmtree(self._temp_dir)
+ self._temp_dir = None
+
+ def launch(self):
+ """
+ Launch the VM and make sure we cleanup and expose the
+ command line/output in case of exception
+ """
+
+ if self._launched:
+ raise QEMUMachineError('VM already launched')
+
+ self._iolog = None
+ self._qemu_full_args = None
+ try:
+ self._launch()
+ self._launched = True
+ except:
+ self.shutdown()
+
+ LOG.debug('Error launching VM')
+ if self._qemu_full_args:
+ LOG.debug('Command: %r', ' '.join(self._qemu_full_args))
+ if self._iolog:
+ LOG.debug('Output: %r', self._iolog)
+ raise Exception(self._iolog)
+ raise
+
+ def _launch(self):
+ """
+ Launch the VM and establish a QMP connection
+ """
+ devnull = open(os.path.devnull, 'rb')
+ self._pre_launch()
+ self._qemu_full_args = (self._wrapper + [self._binary] +
+ self._base_args() + self._args)
+ LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args))
+ self._popen = subprocess.Popen(self._qemu_full_args,
+ stdin=devnull,
+ stdout=self._qemu_log_file,
+ stderr=subprocess.STDOUT,
+ shell=False,
+ close_fds=False)
+ self._post_launch()
+
+ def wait(self):
+ """
+ Wait for the VM to power off
+ """
+ self._popen.wait()
+ self._qmp.close()
+ self._load_io_log()
+ self._post_shutdown()
+
+ def shutdown(self):
+ """
+ Terminate the VM and clean up
+ """
+ if self.is_running():
+ try:
+ self._qmp.cmd('quit')
+ self._qmp.close()
+ except:
+ self._popen.kill()
+ self._popen.wait()
+
+ self._load_io_log()
+ self._post_shutdown()
+
+ exitcode = self.exitcode()
+ if exitcode is not None and exitcode < 0:
+ msg = 'qemu received signal %i: %s'
+ if self._qemu_full_args:
+ command = ' '.join(self._qemu_full_args)
+ else:
+ command = ''
+ LOG.warn(msg, -exitcode, command)
+
+ self._launched = False
+
+ def qmp(self, cmd, conv_keys=True, **args):
+ """
+ Invoke a QMP command and return the response dict
+ """
+ qmp_args = dict()
+ for key, value in args.items():
+ if conv_keys:
+ qmp_args[key.replace('_', '-')] = value
+ else:
+ qmp_args[key] = value
+
+ return self._qmp.cmd(cmd, args=qmp_args)
+
+ def command(self, cmd, conv_keys=True, **args):
+ """
+ Invoke a QMP command.
+ On success return the response dict.
+ On failure raise an exception.
+ """
+ reply = self.qmp(cmd, conv_keys, **args)
+ if reply is None:
+ raise qmp.QMPError("Monitor is closed")
+ if "error" in reply:
+ raise MonitorResponseError(reply)
+ return reply["return"]
+
+ def get_qmp_event(self, wait=False):
+ """
+ Poll for one queued QMP events and return it
+ """
+ if len(self._events) > 0:
+ return self._events.pop(0)
+ return self._qmp.pull_event(wait=wait)
+
+ def get_qmp_events(self, wait=False):
+ """
+ Poll for queued QMP events and return a list of dicts
+ """
+ events = self._qmp.get_events(wait=wait)
+ events.extend(self._events)
+ del self._events[:]
+ self._qmp.clear_events()
+ return events
+
+ @staticmethod
+ def event_match(event, match=None):
+ """
+ Check if an event matches optional match criteria.
+
+ The match criteria takes the form of a matching subdict. The event is
+ checked to be a superset of the subdict, recursively, with matching
+ values whenever the subdict values are not None.
+
+ This has a limitation that you cannot explicitly check for None values.
+
+ Examples, with the subdict queries on the left:
+ - None matches any object.
+ - {"foo": None} matches {"foo": {"bar": 1}}
+ - {"foo": None} matches {"foo": 5}
+ - {"foo": {"abc": None}} does not match {"foo": {"bar": 1}}
+ - {"foo": {"rab": 2}} matches {"foo": {"bar": 1, "rab": 2}}
+ """
+ if match is None:
+ return True
+
+ try:
+ for key in match:
+ if key in event:
+ if not QEMUMachine.event_match(event[key], match[key]):
+ return False
+ else:
+ return False
+ return True
+ except TypeError:
+ # either match or event wasn't iterable (not a dict)
+ return match == event
+
+ def event_wait(self, name, timeout=60.0, match=None):
+ """
+ event_wait waits for and returns a named event from QMP with a timeout.
+
+ name: The event to wait for.
+ timeout: QEMUMonitorProtocol.pull_event timeout parameter.
+ match: Optional match criteria. See event_match for details.
+ """
+ return self.events_wait([(name, match)], timeout)
+
+ def events_wait(self, events, timeout=60.0):
+ """
+ events_wait waits for and returns a named event from QMP with a timeout.
+
+ events: a sequence of (name, match_criteria) tuples.
+ The match criteria are optional and may be None.
+ See event_match for details.
+ timeout: QEMUMonitorProtocol.pull_event timeout parameter.
+ """
+
+ def _match(event):
+ for name, match in events:
+ if (event['event'] == name and
+ self.event_match(event, match)):
+ return True
+ return False
+
+ # Search cached events
+ for event in self._events:
+ if _match(event):
+ self._events.remove(event)
+ return event
+
+ # Poll for new events
+ while True:
+ event = self._qmp.pull_event(wait=timeout)
+ if _match(event):
+ return event
+ self._events.append(event)
+
+ return None
+
+ def get_log(self):
+ """
+ After self.shutdown or failed qemu execution, this returns the output
+ of the qemu process.
+ """
+ return self._iolog
+
+ def add_args(self, *args):
+ """
+ Adds to the list of extra arguments to be given to the QEMU binary
+ """
+ self._args.extend(args)
+
+ def set_machine(self, machine_type):
+ """
+ Sets the machine type
+
+ If set, the machine type will be added to the base arguments
+ of the resulting QEMU command line.
+ """
+ self._machine = machine_type
+
+ def set_console(self, device_type=None):
+ """
+ Sets the device type for a console device
+
+ If set, the console device and a backing character device will
+ be added to the base arguments of the resulting QEMU command
+ line.
+
+ This is a convenience method that will either use the provided
+ device type, or default to a "-serial chardev:console" command
+ line argument.
+
+ The actual setting of command line arguments will be be done at
+ machine launch time, as it depends on the temporary directory
+ to be created.
+
+ @param device_type: the device type, such as "isa-serial". If
+ None is given (the default value) a "-serial
+ chardev:console" command line argument will
+ be used instead, resorting to the machine's
+ default device type.
+ """
+ self._console_set = True
+ self._console_device_type = device_type
+
+ @property
+ def console_socket(self):
+ """
+ Returns a socket connected to the console
+ """
+ if self._console_socket is None:
+ self._console_socket = socket.socket(socket.AF_UNIX,
+ socket.SOCK_STREAM)
+ self._console_socket.connect(self._console_address)
+ return self._console_socket
diff --git a/ucloud/host/qmp/qmp.py b/ucloud/host/qmp/qmp.py
new file mode 100755
index 0000000..bf35d71
--- /dev/null
+++ b/ucloud/host/qmp/qmp.py
@@ -0,0 +1,255 @@
+# QEMU Monitor Protocol Python class
+#
+# Copyright (C) 2009, 2010 Red Hat Inc.
+#
+# Authors:
+# Luiz Capitulino
+#
+# This work is licensed under the terms of the GNU GPL, version 2. See
+# the COPYING file in the top-level directory.
+
+import errno
+import json
+import logging
+import socket
+
+
+class QMPError(Exception):
+ pass
+
+
+class QMPConnectError(QMPError):
+ pass
+
+
+class QMPCapabilitiesError(QMPError):
+ pass
+
+
+class QMPTimeoutError(QMPError):
+ pass
+
+
+class QEMUMonitorProtocol(object):
+ #: Logger object for debugging messages
+ logger = logging.getLogger('QMP')
+ #: Socket's error class
+ error = socket.error
+ #: Socket's timeout
+ timeout = socket.timeout
+
+ def __init__(self, address, server=False):
+ """
+ Create a QEMUMonitorProtocol class.
+
+ @param address: QEMU address, can be either a unix socket path (string)
+ or a tuple in the form ( address, port ) for a TCP
+ connection
+ @param server: server mode listens on the socket (bool)
+ @raise socket.error on socket connection errors
+ @note No connection is established, this is done by the connect() or
+ accept() methods
+ """
+ self.__events = []
+ self.__address = address
+ self.__sock = self.__get_sock()
+ self.__sockfile = None
+ if server:
+ self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ self.__sock.bind(self.__address)
+ self.__sock.listen(1)
+
+ def __get_sock(self):
+ if isinstance(self.__address, tuple):
+ family = socket.AF_INET
+ else:
+ family = socket.AF_UNIX
+ return socket.socket(family, socket.SOCK_STREAM)
+
+ def __negotiate_capabilities(self):
+ greeting = self.__json_read()
+ if greeting is None or "QMP" not in greeting:
+ raise QMPConnectError
+ # Greeting seems ok, negotiate capabilities
+ resp = self.cmd('qmp_capabilities')
+ if "return" in resp:
+ return greeting
+ raise QMPCapabilitiesError
+
+ def __json_read(self, only_event=False):
+ while True:
+ data = self.__sockfile.readline()
+ if not data:
+ return
+ resp = json.loads(data)
+ if 'event' in resp:
+ self.logger.debug("<<< %s", resp)
+ self.__events.append(resp)
+ if not only_event:
+ continue
+ return resp
+
+ def __get_events(self, wait=False):
+ """
+ Check for new events in the stream and cache them in __events.
+
+ @param wait (bool): block until an event is available.
+ @param wait (float): If wait is a float, treat it as a timeout value.
+
+ @raise QMPTimeoutError: If a timeout float is provided and the timeout
+ period elapses.
+ @raise QMPConnectError: If wait is True but no events could be
+ retrieved or if some other error occurred.
+ """
+
+ # Check for new events regardless and pull them into the cache:
+ self.__sock.setblocking(0)
+ try:
+ self.__json_read()
+ except socket.error as err:
+ if err[0] == errno.EAGAIN:
+ # No data available
+ pass
+ self.__sock.setblocking(1)
+
+ # Wait for new events, if needed.
+ # if wait is 0.0, this means "no wait" and is also implicitly false.
+ if not self.__events and wait:
+ if isinstance(wait, float):
+ self.__sock.settimeout(wait)
+ try:
+ ret = self.__json_read(only_event=True)
+ except socket.timeout:
+ raise QMPTimeoutError("Timeout waiting for event")
+ except:
+ raise QMPConnectError("Error while reading from socket")
+ if ret is None:
+ raise QMPConnectError("Error while reading from socket")
+ self.__sock.settimeout(None)
+
+ def connect(self, negotiate=True):
+ """
+ Connect to the QMP Monitor and perform capabilities negotiation.
+
+ @return QMP greeting dict
+ @raise socket.error on socket connection errors
+ @raise QMPConnectError if the greeting is not received
+ @raise QMPCapabilitiesError if fails to negotiate capabilities
+ """
+ self.__sock.connect(self.__address)
+ self.__sockfile = self.__sock.makefile()
+ if negotiate:
+ return self.__negotiate_capabilities()
+
+ def accept(self):
+ """
+ Await connection from QMP Monitor and perform capabilities negotiation.
+
+ @return QMP greeting dict
+ @raise socket.error on socket connection errors
+ @raise QMPConnectError if the greeting is not received
+ @raise QMPCapabilitiesError if fails to negotiate capabilities
+ """
+ self.__sock.settimeout(15)
+ self.__sock, _ = self.__sock.accept()
+ self.__sockfile = self.__sock.makefile()
+ return self.__negotiate_capabilities()
+
+ def cmd_obj(self, qmp_cmd):
+ """
+ Send a QMP command to the QMP Monitor.
+
+ @param qmp_cmd: QMP command to be sent as a Python dict
+ @return QMP response as a Python dict or None if the connection has
+ been closed
+ """
+ self.logger.debug(">>> %s", qmp_cmd)
+ try:
+ self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
+ except socket.error as err:
+ if err[0] == errno.EPIPE:
+ return
+ raise socket.error(err)
+ resp = self.__json_read()
+ self.logger.debug("<<< %s", resp)
+ return resp
+
+ def cmd(self, name, args=None, cmd_id=None):
+ """
+ Build a QMP command and send it to the QMP Monitor.
+
+ @param name: command name (string)
+ @param args: command arguments (dict)
+ @param cmd_id: command id (dict, list, string or int)
+ """
+ qmp_cmd = {'execute': name}
+ if args:
+ qmp_cmd['arguments'] = args
+ if cmd_id:
+ qmp_cmd['id'] = cmd_id
+ return self.cmd_obj(qmp_cmd)
+
+ def command(self, cmd, **kwds):
+ """
+ Build and send a QMP command to the monitor, report errors if any
+ """
+ ret = self.cmd(cmd, kwds)
+ if "error" in ret:
+ raise Exception(ret['error']['desc'])
+ return ret['return']
+
+ def pull_event(self, wait=False):
+ """
+ Pulls a single event.
+
+ @param wait (bool): block until an event is available.
+ @param wait (float): If wait is a float, treat it as a timeout value.
+
+ @raise QMPTimeoutError: If a timeout float is provided and the timeout
+ period elapses.
+ @raise QMPConnectError: If wait is True but no events could be
+ retrieved or if some other error occurred.
+
+ @return The first available QMP event, or None.
+ """
+ self.__get_events(wait)
+
+ if self.__events:
+ return self.__events.pop(0)
+ return None
+
+ def get_events(self, wait=False):
+ """
+ Get a list of available QMP events.
+
+ @param wait (bool): block until an event is available.
+ @param wait (float): If wait is a float, treat it as a timeout value.
+
+ @raise QMPTimeoutError: If a timeout float is provided and the timeout
+ period elapses.
+ @raise QMPConnectError: If wait is True but no events could be
+ retrieved or if some other error occurred.
+
+ @return The list of available QMP events.
+ """
+ self.__get_events(wait)
+ return self.__events
+
+ def clear_events(self):
+ """
+ Clear current list of pending events.
+ """
+ self.__events = []
+
+ def close(self):
+ self.__sock.close()
+ self.__sockfile.close()
+
+ def settimeout(self, timeout):
+ self.__sock.settimeout(timeout)
+
+ def get_sock_fd(self):
+ return self.__sock.fileno()
+
+ def is_scm_available(self):
+ return self.__sock.family == socket.AF_UNIX
diff --git a/ucloud/host/virtualmachine.py b/ucloud/host/virtualmachine.py
new file mode 100755
index 0000000..7524083
--- /dev/null
+++ b/ucloud/host/virtualmachine.py
@@ -0,0 +1,384 @@
+# QEMU Manual
+# https://qemu.weilnetz.de/doc/qemu-doc.html
+
+# For QEMU Monitor Protocol Commands Information, See
+# https://qemu.weilnetz.de/doc/qemu-doc.html#pcsys_005fmonitor
+
+import os
+import random
+import subprocess as sp
+import tempfile
+import time
+
+from functools import wraps
+from string import Template
+from typing import Union
+from os.path import join as join_path
+
+import bitmath
+import sshtunnel
+
+from ucloud.common.helpers import get_ipv6_address
+from ucloud.common.request import RequestEntry, RequestType
+from ucloud.common.vm import VMEntry, VMStatus
+from ucloud.config import (etcd_client, request_pool,
+ running_vms, vm_pool, env_vars,
+ image_storage_handler)
+from . import qmp
+from ucloud.host import logger
+
+
+class VM:
+ def __init__(self, key, handle, vnc_socket_file):
+ self.key = key # type: str
+ self.handle = handle # type: qmp.QEMUMachine
+ self.vnc_socket_file = vnc_socket_file # type: tempfile.NamedTemporaryFile
+
+ def __repr__(self):
+ return "VM({})".format(self.key)
+
+
+def delete_network_interface(iface):
+ try:
+ sp.check_output(['ip', 'link', 'del', iface])
+ except Exception:
+ pass
+
+
+def resolve_network(network_name, network_owner):
+ network = etcd_client.get(join_path(env_vars.get("NETWORK_PREFIX"),
+ network_owner,
+ network_name),
+ value_in_json=True)
+ return network
+
+
+def delete_vm_network(vm_entry):
+ try:
+ for network in vm_entry.network:
+ network_name = network[0]
+ tap_mac = network[1]
+ tap_id = network[2]
+
+ delete_network_interface('tap{}'.format(tap_id))
+
+ owners_vms = vm_pool.by_owner(vm_entry.owner)
+ owners_running_vms = vm_pool.by_status(VMStatus.running,
+ _vms=owners_vms)
+
+ networks = map(lambda n: n[0],
+ map(lambda vm: vm.network, owners_running_vms)
+ )
+ networks_in_use_by_user_vms = [vm[0] for vm in networks]
+ if network_name not in networks_in_use_by_user_vms:
+ network_entry = resolve_network(network[0], vm_entry.owner)
+ if network_entry:
+ network_type = network_entry.value["type"]
+ network_id = network_entry.value["id"]
+ if network_type == "vxlan":
+ delete_network_interface('br{}'.format(network_id))
+ delete_network_interface('vxlan{}'.format(network_id))
+ except Exception:
+ logger.exception("Exception in network interface deletion")
+
+
+def create_dev(script, _id, dev, ip=None):
+ command = [script, _id, dev]
+ if ip:
+ command.append(ip)
+ try:
+ output = sp.check_output(command, stderr=sp.PIPE)
+ except Exception as e:
+ print(e.stderr)
+ return None
+ else:
+ return output.decode("utf-8").strip()
+
+
+def create_vxlan_br_tap(_id, _dev, tap_id, ip=None):
+ network_script_base = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'network')
+ vxlan = create_dev(script=os.path.join(network_script_base, 'create-vxlan.sh'),
+ _id=_id, dev=_dev)
+ if vxlan:
+ bridge = create_dev(script=os.path.join(network_script_base, 'create-bridge.sh'),
+ _id=_id, dev=vxlan, ip=ip)
+ if bridge:
+ tap = create_dev(script=os.path.join(network_script_base, 'create-tap.sh'),
+ _id=str(tap_id), dev=bridge)
+ if tap:
+ return tap
+
+
+def random_bytes(num=6):
+ return [random.randrange(256) for _ in range(num)]
+
+
+def generate_mac(uaa=False, multicast=False, oui=None, separator=':', byte_fmt='%02x'):
+ mac = random_bytes()
+ if oui:
+ if type(oui) == str:
+ oui = [int(chunk) for chunk in oui.split(separator)]
+ mac = oui + random_bytes(num=6 - len(oui))
+ else:
+ if multicast:
+ mac[0] |= 1 # set bit 0
+ else:
+ mac[0] &= ~1 # clear bit 0
+ if uaa:
+ mac[0] &= ~(1 << 1) # clear bit 1
+ else:
+ mac[0] |= 1 << 1 # set bit 1
+ return separator.join(byte_fmt % b for b in mac)
+
+
+def update_radvd_conf(etcd_client):
+ network_script_base = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'network')
+
+ networks = {
+ net.value['ipv6']: net.value['id']
+ for net in etcd_client.get_prefix('/v1/network/', value_in_json=True)
+ if net.value.get('ipv6')
+ }
+ radvd_template = open(os.path.join(network_script_base,
+ 'radvd-template.conf'), 'r').read()
+ radvd_template = Template(radvd_template)
+
+ content = [radvd_template.safe_substitute(bridge='br{}'.format(networks[net]),
+ prefix=net)
+ for net in networks if networks.get(net)]
+
+ with open('/etc/radvd.conf', 'w') as radvd_conf:
+ radvd_conf.writelines(content)
+ try:
+ sp.check_output(['systemctl', 'restart', 'radvd'])
+ except Exception:
+ sp.check_output(['service', 'radvd', 'restart'])
+
+
+def get_start_command_args(vm_entry, vnc_sock_filename: str, migration=False, migration_port=None):
+ threads_per_core = 1
+ vm_memory = int(bitmath.parse_string_unsafe(vm_entry.specs["ram"]).to_MB())
+ vm_cpus = int(vm_entry.specs["cpu"])
+ vm_uuid = vm_entry.uuid
+ vm_networks = vm_entry.network
+
+ command = "-name {}_{}".format(vm_entry.owner, vm_entry.name)
+
+ command += " -drive file={},format=raw,if=virtio,cache=none".format(
+ image_storage_handler.qemu_path_string(vm_uuid)
+ )
+ command += " -device virtio-rng-pci -vnc unix:{}".format(vnc_sock_filename)
+ command += " -m {} -smp cores={},threads={}".format(
+ vm_memory, vm_cpus, threads_per_core
+ )
+
+ if migration:
+ command += " -incoming tcp:[::]:{}".format(migration_port)
+
+ tap = None
+ for network_mac_and_tap in vm_networks:
+ network_name, mac, tap = network_mac_and_tap
+
+ _key = os.path.join(env_vars.get('NETWORK_PREFIX'), vm_entry.owner, network_name)
+ network = etcd_client.get(_key, value_in_json=True)
+ network_type = network.value["type"]
+ network_id = str(network.value["id"])
+ network_ipv6 = network.value["ipv6"]
+
+ if network_type == "vxlan":
+ tap = create_vxlan_br_tap(_id=network_id,
+ _dev=env_vars.get("VXLAN_PHY_DEV"),
+ tap_id=tap,
+ ip=network_ipv6)
+ update_radvd_conf(etcd_client)
+
+ command += " -netdev tap,id=vmnet{net_id},ifname={tap},script=no,downscript=no" \
+ " -device virtio-net-pci,netdev=vmnet{net_id},mac={mac}" \
+ .format(tap=tap, net_id=network_id, mac=mac)
+
+ return command.split(" ")
+
+
+def create_vm_object(vm_entry, migration=False, migration_port=None):
+ # NOTE: If migration suddenly stop working, having different
+ # VNC unix filename on source and destination host can
+ # be a possible cause of it.
+
+ # REQUIREMENT: Use Unix Socket instead of TCP Port for VNC
+ vnc_sock_file = tempfile.NamedTemporaryFile()
+
+ qemu_args = get_start_command_args(
+ vm_entry=vm_entry,
+ vnc_sock_filename=vnc_sock_file.name,
+ migration=migration,
+ migration_port=migration_port,
+ )
+ qemu_machine = qmp.QEMUMachine("/usr/bin/qemu-system-x86_64", args=qemu_args)
+ return VM(vm_entry.key, qemu_machine, vnc_sock_file)
+
+
+def get_vm(vm_list: list, vm_key) -> Union[VM, None]:
+ return next((vm for vm in vm_list if vm.key == vm_key), None)
+
+
+def need_running_vm(func):
+ @wraps(func)
+ def wrapper(e):
+ vm = get_vm(running_vms, e.key)
+ if vm:
+ try:
+ status = vm.handle.command("query-status")
+ logger.debug("VM Status Check - %s", status)
+ except Exception as exception:
+ logger.info("%s failed - VM %s %s", func.__name__, e, exception)
+ else:
+ return func(e)
+
+ return None
+ else:
+ logger.info("%s failed because VM %s is not running", func.__name__, e.key)
+ return None
+
+ return wrapper
+
+
+def create(vm_entry: VMEntry):
+ if image_storage_handler.is_vm_image_exists(vm_entry.uuid):
+ # File Already exists. No Problem Continue
+ logger.debug("Image for vm %s exists", vm_entry.uuid)
+ else:
+ vm_hdd = int(bitmath.parse_string_unsafe(vm_entry.specs["os-ssd"]).to_MB())
+ if image_storage_handler.make_vm_image(src=vm_entry.image_uuid, dest=vm_entry.uuid):
+ if not image_storage_handler.resize_vm_image(path=vm_entry.uuid, size=vm_hdd):
+ vm_entry.status = VMStatus.error
+ else:
+ logger.info("New VM Created")
+
+
+def start(vm_entry: VMEntry, destination_host_key=None, migration_port=None):
+ _vm = get_vm(running_vms, vm_entry.key)
+
+ # VM already running. No need to proceed further.
+ if _vm:
+ logger.info("VM %s already running" % vm_entry.uuid)
+ return
+ else:
+ logger.info("Trying to start %s" % vm_entry.uuid)
+ if destination_host_key:
+ launch_vm(vm_entry, migration=True, migration_port=migration_port,
+ destination_host_key=destination_host_key)
+ else:
+ create(vm_entry)
+ launch_vm(vm_entry)
+
+
+@need_running_vm
+def stop(vm_entry):
+ vm = get_vm(running_vms, vm_entry.key)
+ vm.handle.shutdown()
+ if not vm.handle.is_running():
+ vm_entry.add_log("Shutdown successfully")
+ vm_entry.declare_stopped()
+ vm_pool.put(vm_entry)
+ running_vms.remove(vm)
+ delete_vm_network(vm_entry)
+
+
+def delete(vm_entry):
+ logger.info("Deleting VM | %s", vm_entry)
+ stop(vm_entry)
+
+ if image_storage_handler.is_vm_image_exists(vm_entry.uuid):
+ r_status = image_storage_handler.delete_vm_image(vm_entry.uuid)
+ if r_status:
+ etcd_client.client.delete(vm_entry.key)
+ else:
+ etcd_client.client.delete(vm_entry.key)
+
+def transfer(request_event):
+ # This function would run on source host i.e host on which the vm
+ # is running initially. This host would be responsible for transferring
+ # vm state to destination host.
+
+ _host, _port = request_event.parameters["host"], request_event.parameters["port"]
+ _uuid = request_event.uuid
+ _destination = request_event.destination_host_key
+ vm = get_vm(running_vms, join_path(env_vars.get('VM_PREFIX'), _uuid))
+
+ if vm:
+ tunnel = sshtunnel.SSHTunnelForwarder(
+ _host,
+ ssh_username=env_vars.get("ssh_username"),
+ ssh_pkey=env_vars.get("ssh_pkey"),
+ remote_bind_address=("127.0.0.1", _port),
+ ssh_proxy_enabled=True,
+ ssh_proxy=(_host, 22)
+ )
+ try:
+ tunnel.start()
+ except sshtunnel.BaseSSHTunnelForwarderError:
+ logger.exception("Couldn't establish connection to (%s, 22)", _host)
+ else:
+ vm.handle.command(
+ "migrate", uri="tcp:0.0.0.0:{}".format(tunnel.local_bind_port)
+ )
+
+ status = vm.handle.command("query-migrate")["status"]
+ while status not in ["failed", "completed"]:
+ time.sleep(2)
+ status = vm.handle.command("query-migrate")["status"]
+
+ with vm_pool.get_put(request_event.uuid) as source_vm:
+ if status == "failed":
+ source_vm.add_log("Migration Failed")
+ elif status == "completed":
+ # If VM is successfully migrated then shutdown the VM
+ # on this host and update hostname to destination host key
+ source_vm.add_log("Successfully migrated")
+ source_vm.hostname = _destination
+ running_vms.remove(vm)
+ vm.handle.shutdown()
+ source_vm.in_migration = False # VM transfer finished
+ finally:
+ tunnel.close()
+
+
+def launch_vm(vm_entry, migration=False, migration_port=None, destination_host_key=None):
+ logger.info("Starting %s" % vm_entry.key)
+
+ vm = create_vm_object(vm_entry, migration=migration, migration_port=migration_port)
+ try:
+ vm.handle.launch()
+ except Exception:
+ logger.exception("Error Occured while starting VM")
+ vm.handle.shutdown()
+
+ if migration:
+ # We don't care whether MachineError or any other error occurred
+ pass
+ else:
+ # Error during typical launch of a vm
+ vm.handle.shutdown()
+ vm_entry.declare_killed()
+ vm_pool.put(vm_entry)
+ else:
+ vm_entry.vnc_socket = vm.vnc_socket_file.name
+ running_vms.append(vm)
+
+ if migration:
+ vm_entry.in_migration = True
+ r = RequestEntry.from_scratch(
+ type=RequestType.TransferVM,
+ hostname=vm_entry.hostname,
+ parameters={"host": get_ipv6_address(), "port": migration_port},
+ uuid=vm_entry.uuid,
+ destination_host_key=destination_host_key,
+ request_prefix=env_vars.get("REQUEST_PREFIX")
+ )
+ request_pool.put(r)
+ else:
+ # Typical launching of a vm
+ vm_entry.status = VMStatus.running
+ vm_entry.add_log("Started successfully")
+
+ vm_pool.put(vm_entry)
diff --git a/archive/uncloud_etcd_based/uncloud/imagescanner/__init__.py b/ucloud/imagescanner/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/imagescanner/__init__.py
rename to ucloud/imagescanner/__init__.py
diff --git a/ucloud/imagescanner/main.py b/ucloud/imagescanner/main.py
new file mode 100755
index 0000000..20ce9d5
--- /dev/null
+++ b/ucloud/imagescanner/main.py
@@ -0,0 +1,78 @@
+import json
+import os
+import subprocess
+
+from os.path import join as join_path
+from ucloud.config import etcd_client, env_vars, image_storage_handler
+from ucloud.imagescanner import logger
+
+
+def qemu_img_type(path):
+ qemu_img_info_command = ["qemu-img", "info", "--output", "json", path]
+ try:
+ qemu_img_info = subprocess.check_output(qemu_img_info_command)
+ except Exception as e:
+ logger.exception(e)
+ return None
+ else:
+ qemu_img_info = json.loads(qemu_img_info.decode("utf-8"))
+ return qemu_img_info["format"]
+
+
+def main():
+ # We want to get images entries that requests images to be created
+ images = etcd_client.get_prefix(env_vars.get('IMAGE_PREFIX'), value_in_json=True)
+ images_to_be_created = list(filter(lambda im: im.value['status'] == 'TO_BE_CREATED', images))
+
+ for image in images_to_be_created:
+ try:
+ image_uuid = image.key.split('/')[-1]
+ image_owner = image.value['owner']
+ image_filename = image.value['filename']
+ image_store_name = image.value['store_name']
+ image_full_path = join_path(env_vars.get('BASE_DIR'), image_owner, image_filename)
+
+ image_stores = etcd_client.get_prefix(env_vars.get('IMAGE_STORE_PREFIX'), value_in_json=True)
+ user_image_store = next(filter(
+ lambda s, store_name=image_store_name: s.value["name"] == store_name,
+ image_stores
+ ))
+
+ image_store_pool = user_image_store.value['attributes']['pool']
+
+ except Exception as e:
+ logger.exception(e)
+ else:
+ # At least our basic data is available
+ qemu_img_convert_command = ["qemu-img", "convert", "-f", "qcow2",
+ "-O", "raw", image_full_path, "image.raw"]
+
+ if qemu_img_type(image_full_path) == "qcow2":
+ try:
+ # Convert .qcow2 to .raw
+ subprocess.check_output(qemu_img_convert_command)
+ except Exception as e:
+ logger.exception(e)
+ else:
+ # Import and Protect
+ r_status = image_storage_handler.import_image(src="image.raw",
+ dest=image_uuid,
+ protect=True)
+ if r_status:
+ # Everything is successfully done
+ image.value["status"] = "CREATED"
+ etcd_client.put(image.key, json.dumps(image.value))
+
+ else:
+ # The user provided image is either not found or of invalid format
+ image.value["status"] = "INVALID_IMAGE"
+ etcd_client.put(image.key, json.dumps(image.value))
+
+ try:
+ os.remove("image.raw")
+ except Exception:
+ pass
+
+
+if __name__ == "__main__":
+ main()
diff --git a/archive/uncloud_etcd_based/uncloud/cli/__init__.py b/ucloud/metadata/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/cli/__init__.py
rename to ucloud/metadata/__init__.py
diff --git a/ucloud/metadata/main.py b/ucloud/metadata/main.py
new file mode 100644
index 0000000..e7cb33b
--- /dev/null
+++ b/ucloud/metadata/main.py
@@ -0,0 +1,91 @@
+import os
+
+from flask import Flask, request
+from flask_restful import Resource, Api
+
+from ucloud.config import etcd_client, env_vars, vm_pool
+
+app = Flask(__name__)
+api = Api(app)
+
+
+def get_vm_entry(mac_addr):
+ return next(filter(lambda vm: mac_addr in list(zip(*vm.network))[1], vm_pool.vms), None)
+
+
+# https://stackoverflow.com/questions/37140846/how-to-convert-ipv6-link-local-address-to-mac-address-in-python
+def ipv62mac(ipv6):
+ # remove subnet info if given
+ subnet_index = ipv6.find('/')
+ if subnet_index != -1:
+ ipv6 = ipv6[:subnet_index]
+
+ ipv6_parts = ipv6.split(':')
+ mac_parts = list()
+ for ipv6_part in ipv6_parts[-4:]:
+ while len(ipv6_part) < 4:
+ ipv6_part = '0' + ipv6_part
+ mac_parts.append(ipv6_part[:2])
+ mac_parts.append(ipv6_part[-2:])
+
+ # modify parts to match MAC value
+ mac_parts[0] = '%02x' % (int(mac_parts[0], 16) ^ 2)
+ del mac_parts[4]
+ del mac_parts[3]
+ return ':'.join(mac_parts)
+
+
+class Root(Resource):
+ @staticmethod
+ def get():
+ data = get_vm_entry(ipv62mac(request.remote_addr))
+
+ if not data:
+ return {'message': 'Metadata for such VM does not exists.'}, 404
+ else:
+
+ # {env_vars.get('USER_PREFIX')}/{realm}/{name}/key
+ etcd_key = os.path.join(env_vars.get('USER_PREFIX'), data.value['owner_realm'],
+ data.value['owner'], 'key')
+ etcd_entry = etcd_client.get_prefix(etcd_key, value_in_json=True)
+ user_personal_ssh_keys = [key.value for key in etcd_entry]
+ data.value['metadata']['ssh-keys'] += user_personal_ssh_keys
+ return data.value['metadata'], 200
+
+ @staticmethod
+ def post():
+ return {'message': 'Previous Implementation is deprecated.'}
+ # data = etcd_client.get("/v1/metadata/{}".format(request.remote_addr), value_in_json=True)
+ # print(data)
+ # if data:
+ # for k in request.json:
+ # if k not in data.value:
+ # data.value[k] = request.json[k]
+ # if k.endswith("-list"):
+ # data.value[k] = [request.json[k]]
+ # else:
+ # if k.endswith("-list"):
+ # data.value[k].append(request.json[k])
+ # else:
+ # data.value[k] = request.json[k]
+ # etcd_client.put("/v1/metadata/{}".format(request.remote_addr),
+ # data.value, value_in_json=True)
+ # else:
+ # data = {}
+ # for k in request.json:
+ # data[k] = request.json[k]
+ # if k.endswith("-list"):
+ # data[k] = [request.json[k]]
+ # etcd_client.put("/v1/metadata/{}".format(request.remote_addr),
+ # data, value_in_json=True)
+
+
+api.add_resource(Root, '/')
+
+
+def main():
+ app.run(debug=True, host="::", port="80")
+
+
+if __name__ == '__main__':
+ main()
diff --git a/archive/uncloud_etcd_based/uncloud/network/README b/ucloud/network/README
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/network/README
rename to ucloud/network/README
diff --git a/archive/uncloud_etcd_based/uncloud/client/__init__.py b/ucloud/network/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/client/__init__.py
rename to ucloud/network/__init__.py
diff --git a/archive/uncloud_etcd_based/uncloud/network/create-bridge.sh b/ucloud/network/create-bridge.sh
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/network/create-bridge.sh
rename to ucloud/network/create-bridge.sh
diff --git a/archive/uncloud_etcd_based/uncloud/network/create-tap.sh b/ucloud/network/create-tap.sh
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/network/create-tap.sh
rename to ucloud/network/create-tap.sh
diff --git a/archive/uncloud_etcd_based/uncloud/network/create-vxlan.sh b/ucloud/network/create-vxlan.sh
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/network/create-vxlan.sh
rename to ucloud/network/create-vxlan.sh
diff --git a/archive/uncloud_etcd_based/uncloud/network/radvd-template.conf b/ucloud/network/radvd-template.conf
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/network/radvd-template.conf
rename to ucloud/network/radvd-template.conf
diff --git a/ucloud/sanity_checks.py b/ucloud/sanity_checks.py
new file mode 100644
index 0000000..143f767
--- /dev/null
+++ b/ucloud/sanity_checks.py
@@ -0,0 +1,33 @@
+import sys
+import subprocess as sp
+
+from os.path import isdir
+from ucloud.config import env_vars
+
+
+def check():
+ #########################
+ # ucloud-image-scanner #
+ #########################
+ if env_vars.get('STORAGE_BACKEND') == 'filesystem' and not isdir(env_vars.get('IMAGE_DIR')):
+ print("You have set STORAGE_BACKEND to filesystem. So,"
+ "the {} must exists. But, it don't".format(env_vars.get('IMAGE_DIR')))
+ sys.exit(1)
+
+ try:
+ sp.check_output(['which', 'qemu-img'])
+ except Exception:
+ print("qemu-img missing")
+ sys.exit(1)
+
+ ###############
+ # ucloud-host #
+ ###############
+
+ if env_vars.get('STORAGE_BACKEND') == 'filesystem' and not isdir(env_vars.get('VM_DIR')):
+ print("You have set STORAGE_BACKEND to filesystem. So, the vm directory mentioned"
+ " in .env file must exists. But, it don't.")
+ sys.exit(1)
+
+if __name__ == "__main__":
+ check()
\ No newline at end of file
diff --git a/ucloud/scheduler/__init__.py b/ucloud/scheduler/__init__.py
new file mode 100644
index 0000000..95e1be0
--- /dev/null
+++ b/ucloud/scheduler/__init__.py
@@ -0,0 +1,3 @@
+import logging
+
+logger = logging.getLogger(__name__)
\ No newline at end of file
diff --git a/archive/uncloud_etcd_based/uncloud/scheduler/helper.py b/ucloud/scheduler/helper.py
similarity index 50%
rename from archive/uncloud_etcd_based/uncloud/scheduler/helper.py
rename to ucloud/scheduler/helper.py
index 79db322..ba577d6 100755
--- a/archive/uncloud_etcd_based/uncloud/scheduler/helper.py
+++ b/ucloud/scheduler/helper.py
@@ -3,10 +3,10 @@ from functools import reduce
import bitmath
-from uncloud.common.host import HostStatus
-from uncloud.common.request import RequestEntry, RequestType
-from uncloud.common.vm import VMStatus
-from uncloud.common.shared import shared
+from ucloud.common.host import HostStatus
+from ucloud.common.request import RequestEntry, RequestType
+from ucloud.common.vm import VMStatus
+from ucloud.config import vm_pool, host_pool, request_pool, env_vars
def accumulated_specs(vms_specs):
@@ -23,35 +23,17 @@ def remaining_resources(host_specs, vms_specs):
for component in _vms_specs:
if isinstance(_vms_specs[component], str):
- _vms_specs[component] = int(
- bitmath.parse_string_unsafe(
- _vms_specs[component]
- ).to_MB()
- )
+ _vms_specs[component] = int(bitmath.parse_string_unsafe(_vms_specs[component]).to_MB())
elif isinstance(_vms_specs[component], list):
- _vms_specs[component] = map(
- lambda x: int(bitmath.parse_string_unsafe(x).to_MB()),
- _vms_specs[component],
- )
- _vms_specs[component] = reduce(
- lambda x, y: x + y, _vms_specs[component], 0
- )
+ _vms_specs[component] = map(lambda x: int(bitmath.parse_string_unsafe(x).to_MB()), _vms_specs[component])
+ _vms_specs[component] = reduce(lambda x, y: x + y, _vms_specs[component], 0)
for component in _remaining:
if isinstance(_remaining[component], str):
- _remaining[component] = int(
- bitmath.parse_string_unsafe(
- _remaining[component]
- ).to_MB()
- )
+ _remaining[component] = int(bitmath.parse_string_unsafe(_remaining[component]).to_MB())
elif isinstance(_remaining[component], list):
- _remaining[component] = map(
- lambda x: int(bitmath.parse_string_unsafe(x).to_MB()),
- _remaining[component],
- )
- _remaining[component] = reduce(
- lambda x, y: x + y, _remaining[component], 0
- )
+ _remaining[component] = map(lambda x: int(bitmath.parse_string_unsafe(x).to_MB()), _remaining[component])
+ _remaining[component] = reduce(lambda x, y: x + y, _remaining[component], 0)
_remaining.subtract(_vms_specs)
@@ -64,27 +46,23 @@ class NoSuitableHostFound(Exception):
def get_suitable_host(vm_specs, hosts=None):
if hosts is None:
- hosts = shared.host_pool.by_status(HostStatus.alive)
+ hosts = host_pool.by_status(HostStatus.alive)
for host in hosts:
# Filter them by host_name
- vms = shared.vm_pool.by_host(host.key)
+ vms = vm_pool.by_host(host.key)
# Filter them by status
- vms = shared.vm_pool.by_status(VMStatus.running, vms)
+ vms = vm_pool.by_status(VMStatus.running, vms)
running_vms_specs = [vm.specs for vm in vms]
# Accumulate all of their combined specs
- running_vms_accumulated_specs = accumulated_specs(
- running_vms_specs
- )
+ running_vms_accumulated_specs = accumulated_specs(running_vms_specs)
# Find out remaining resources after
# host_specs - already running vm_specs
- remaining = remaining_resources(
- host.specs, running_vms_accumulated_specs
- )
+ remaining = remaining_resources(host.specs, running_vms_accumulated_specs)
# Find out remaining - new_vm_specs
remaining = remaining_resources(remaining, vm_specs)
@@ -97,7 +75,7 @@ def get_suitable_host(vm_specs, hosts=None):
def dead_host_detection():
# Bring out your dead! - Monty Python and the Holy Grail
- hosts = shared.host_pool.by_status(HostStatus.alive)
+ hosts = host_pool.by_status(HostStatus.alive)
dead_hosts_keys = []
for host in hosts:
@@ -111,27 +89,25 @@ def dead_host_detection():
def dead_host_mitigation(dead_hosts_keys):
for host_key in dead_hosts_keys:
- host = shared.host_pool.get(host_key)
+ host = host_pool.get(host_key)
host.declare_dead()
- vms_hosted_on_dead_host = shared.vm_pool.by_host(host_key)
+ vms_hosted_on_dead_host = vm_pool.by_host(host_key)
for vm in vms_hosted_on_dead_host:
- vm.status = "UNKNOWN"
- shared.vm_pool.put(vm)
- shared.host_pool.put(host)
+ vm.declare_killed()
+ vm_pool.put(vm)
+ host_pool.put(host)
def assign_host(vm):
vm.hostname = get_suitable_host(vm.specs)
- shared.vm_pool.put(vm)
+ vm_pool.put(vm)
- r = RequestEntry.from_scratch(
- type=RequestType.StartVM,
- uuid=vm.uuid,
- hostname=vm.hostname,
- request_prefix=shared.settings["etcd"]["request_prefix"],
- )
- shared.request_pool.put(r)
+ r = RequestEntry.from_scratch(type=RequestType.StartVM,
+ uuid=vm.uuid,
+ hostname=vm.hostname,
+ request_prefix=env_vars.get("REQUEST_PREFIX"))
+ request_pool.put(r)
vm.log.append("VM scheduled for starting")
return vm.hostname
diff --git a/ucloud/scheduler/main.py b/ucloud/scheduler/main.py
new file mode 100755
index 0000000..e2c975a
--- /dev/null
+++ b/ucloud/scheduler/main.py
@@ -0,0 +1,93 @@
+# TODO
+# 1. send an email to an email address defined by env['admin-email']
+# if resources are finished
+# 2. Introduce a status endpoint of the scheduler -
+# maybe expose a prometheus compatible output
+
+from ucloud.common.request import RequestEntry, RequestType
+from ucloud.config import etcd_client
+from ucloud.config import host_pool, request_pool, vm_pool, env_vars
+from .helper import (get_suitable_host, dead_host_mitigation, dead_host_detection,
+ assign_host, NoSuitableHostFound)
+from . import logger
+
+
+def main():
+ logger.info("%s SESSION STARTED %s", '*' * 5, '*' * 5)
+
+ pending_vms = []
+
+ for request_iterator in [
+ etcd_client.get_prefix(env_vars.get('REQUEST_PREFIX'), value_in_json=True),
+ etcd_client.watch_prefix(env_vars.get('REQUEST_PREFIX'), timeout=5, value_in_json=True),
+ ]:
+ for request_event in request_iterator:
+ request_entry = RequestEntry(request_event)
+ # Never Run time critical mechanism inside timeout
+ # mechanism because timeout mechanism only comes
+ # when no other event is happening. It means under
+ # heavy load there would not be a timeout event.
+ if request_entry.type == "TIMEOUT":
+
+ # Detect hosts that are dead and set their status
+ # to "DEAD", and their VMs' status to "KILLED"
+ dead_hosts = dead_host_detection()
+ if dead_hosts:
+ logger.debug("Dead hosts: %s", dead_hosts)
+ dead_host_mitigation(dead_hosts)
+
+ # If there are VMs that weren't assigned a host
+ # because there wasn't a host available which
+ # meets requirement of that VM then we would
+ # create a new ScheduleVM request for that VM
+ # on our behalf.
+ while pending_vms:
+ pending_vm_entry = pending_vms.pop()
+ r = RequestEntry.from_scratch(type="ScheduleVM",
+ uuid=pending_vm_entry.uuid,
+ hostname=pending_vm_entry.hostname,
+ request_prefix=env_vars.get("REQUEST_PREFIX"))
+ request_pool.put(r)
+
+ elif request_entry.type == RequestType.ScheduleVM:
+ logger.debug("%s, %s", request_entry.key, request_entry.value)
+
+ vm_entry = vm_pool.get(request_entry.uuid)
+ if vm_entry is None:
+ logger.info("Trying to act on {} but it is deleted".format(request_entry.uuid))
+ continue
+ etcd_client.client.delete(request_entry.key) # consume Request
+
+ # If the Request is about a VM which is labelled as "migration"
+ # and has a destination
+ if hasattr(request_entry, "migration") and request_entry.migration \
+ and hasattr(request_entry, "destination") and request_entry.destination:
+ try:
+ get_suitable_host(vm_specs=vm_entry.specs,
+ hosts=[host_pool.get(request_entry.destination)])
+ except NoSuitableHostFound:
+ logger.info("Requested destination host doesn't have enough capacity"
+ "to hold %s" % vm_entry.uuid)
+ else:
+ r = RequestEntry.from_scratch(type=RequestType.InitVMMigration,
+ uuid=request_entry.uuid,
+ destination=request_entry.destination,
+ request_prefix=env_vars.get("REQUEST_PREFIX"))
+ request_pool.put(r)
+
+ # If the Request is about a VM that just want to get started/created
+ else:
+ # assign_host only returns None when we couldn't be able to assign
+ # a host to a VM because of resource constraints
+ try:
+ assign_host(vm_entry)
+ except NoSuitableHostFound:
+ vm_entry.add_log("Can't schedule VM. No Resource Left.")
+ vm_pool.put(vm_entry)
+
+ pending_vms.append(vm_entry)
+ logger.info("No Resource Left. Emailing admin....")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/archive/uncloud_etcd_based/uncloud/configure/__init__.py b/ucloud/scheduler/tests/__init__.py
similarity index 100%
rename from archive/uncloud_etcd_based/uncloud/configure/__init__.py
rename to ucloud/scheduler/tests/__init__.py
diff --git a/archive/uncloud_etcd_based/uncloud/scheduler/tests/test_basics.py b/ucloud/scheduler/tests/test_basics.py
similarity index 83%
rename from archive/uncloud_etcd_based/uncloud/scheduler/tests/test_basics.py
rename to ucloud/scheduler/tests/test_basics.py
index defeb23..92b3a83 100755
--- a/archive/uncloud_etcd_based/uncloud/scheduler/tests/test_basics.py
+++ b/ucloud/scheduler/tests/test_basics.py
@@ -15,7 +15,7 @@ from main import (
main,
)
-from uncloud.config import etcd_client
+from ucloud.config import etcd_client
class TestFunctions(unittest.TestCase):
@@ -70,15 +70,9 @@ class TestFunctions(unittest.TestCase):
"last_heartbeat": datetime.utcnow().isoformat(),
}
with self.client.client.lock("lock"):
- self.client.put(
- f"{self.host_prefix}/1", host1, value_in_json=True
- )
- self.client.put(
- f"{self.host_prefix}/2", host2, value_in_json=True
- )
- self.client.put(
- f"{self.host_prefix}/3", host3, value_in_json=True
- )
+ self.client.put(f"{self.host_prefix}/1", host1, value_in_json=True)
+ self.client.put(f"{self.host_prefix}/2", host2, value_in_json=True)
+ self.client.put(f"{self.host_prefix}/3", host3, value_in_json=True)
def create_vms(self):
vm1 = json.dumps(
@@ -152,17 +146,15 @@ class TestFunctions(unittest.TestCase):
{"cpu": 8, "ram": 32},
]
self.assertEqual(
- accumulated_specs(vms),
- {"ssd": 10, "cpu": 16, "ram": 48, "hdd": 10},
+ accumulated_specs(vms), {"ssd": 10, "cpu": 16, "ram": 48, "hdd": 10}
)
def test_remaining_resources(self):
host_specs = {"ssd": 10, "cpu": 16, "ram": 48, "hdd": 10}
vms_specs = {"ssd": 10, "cpu": 32, "ram": 12, "hdd": 0}
resultant_specs = {"ssd": 0, "cpu": -16, "ram": 36, "hdd": 10}
- self.assertEqual(
- remaining_resources(host_specs, vms_specs), resultant_specs
- )
+ self.assertEqual(remaining_resources(host_specs, vms_specs),
+ resultant_specs)
def test_vmpool(self):
self.p.join(1)
@@ -175,12 +167,7 @@ class TestFunctions(unittest.TestCase):
f"{self.vm_prefix}/1",
{
"owner": "meow",
- "specs": {
- "cpu": 4,
- "ram": 8,
- "hdd": 100,
- "sdd": 256,
- },
+ "specs": {"cpu": 4, "ram": 8, "hdd": 100, "sdd": 256},
"hostname": f"{self.host_prefix}/3",
"status": "SCHEDULED_DEPLOY",
},
@@ -195,12 +182,7 @@ class TestFunctions(unittest.TestCase):
f"{self.vm_prefix}/7",
{
"owner": "meow",
- "specs": {
- "cpu": 10,
- "ram": 22,
- "hdd": 146,
- "sdd": 0,
- },
+ "specs": {"cpu": 10, "ram": 22, "hdd": 146, "sdd": 0},
"hostname": "",
"status": "REQUESTED_NEW",
},
@@ -215,12 +197,7 @@ class TestFunctions(unittest.TestCase):
f"{self.vm_prefix}/7",
{
"owner": "meow",
- "specs": {
- "cpu": 10,
- "ram": 22,
- "hdd": 146,
- "sdd": 0,
- },
+ "specs": {"cpu": 10, "ram": 22, "hdd": 146, "sdd": 0},
"hostname": "",
"status": "REQUESTED_NEW",
},
diff --git a/archive/uncloud_etcd_based/uncloud/scheduler/tests/test_dead_host_mechanism.py b/ucloud/scheduler/tests/test_dead_host_mechanism.py
similarity index 70%
rename from archive/uncloud_etcd_based/uncloud/scheduler/tests/test_dead_host_mechanism.py
rename to ucloud/scheduler/tests/test_dead_host_mechanism.py
index 466b9ee..0b403ef 100755
--- a/archive/uncloud_etcd_based/uncloud/scheduler/tests/test_dead_host_mechanism.py
+++ b/ucloud/scheduler/tests/test_dead_host_mechanism.py
@@ -6,7 +6,11 @@ from os.path import dirname
BASE_DIR = dirname(dirname(__file__))
sys.path.insert(0, BASE_DIR)
-from main import dead_host_detection, dead_host_mitigation, config
+from main import (
+ dead_host_detection,
+ dead_host_mitigation,
+ config
+)
class TestDeadHostMechanism(unittest.TestCase):
@@ -48,23 +52,13 @@ class TestDeadHostMechanism(unittest.TestCase):
"last_heartbeat": datetime(2011, 1, 1).isoformat(),
}
with self.client.client.lock("lock"):
- self.client.put(
- f"{self.host_prefix}/1", host1, value_in_json=True
- )
- self.client.put(
- f"{self.host_prefix}/2", host2, value_in_json=True
- )
- self.client.put(
- f"{self.host_prefix}/3", host3, value_in_json=True
- )
- self.client.put(
- f"{self.host_prefix}/4", host4, value_in_json=True
- )
+ self.client.put(f"{self.host_prefix}/1", host1, value_in_json=True)
+ self.client.put(f"{self.host_prefix}/2", host2, value_in_json=True)
+ self.client.put(f"{self.host_prefix}/3", host3, value_in_json=True)
+ self.client.put(f"{self.host_prefix}/4", host4, value_in_json=True)
def test_dead_host_detection(self):
- hosts = self.client.get_prefix(
- self.host_prefix, value_in_json=True
- )
+ hosts = self.client.get_prefix(self.host_prefix, value_in_json=True)
deads = dead_host_detection(hosts)
self.assertEqual(deads, ["/test/host/2", "/test/host/3"])
return deads
@@ -72,9 +66,7 @@ class TestDeadHostMechanism(unittest.TestCase):
def test_dead_host_mitigation(self):
deads = self.test_dead_host_detection()
dead_host_mitigation(self.client, deads)
- hosts = self.client.get_prefix(
- self.host_prefix, value_in_json=True
- )
+ hosts = self.client.get_prefix(self.host_prefix, value_in_json=True)
deads = dead_host_detection(hosts)
self.assertEqual(deads, [])
diff --git a/uncloud/.dockerignore b/uncloud/.dockerignore
deleted file mode 100644
index 32eda49..0000000
--- a/uncloud/.dockerignore
+++ /dev/null
@@ -1,3 +0,0 @@
-local_settings.py
-ldap_max_uid_file
-.env
diff --git a/uncloud/.env-sample b/uncloud/.env-sample
deleted file mode 100644
index 974d307..0000000
--- a/uncloud/.env-sample
+++ /dev/null
@@ -1,31 +0,0 @@
-ACTIVE_APP=nextcloud
-ALLOWED_HOSTS='*'
-STRIPE_KEY=
-STRIPE_PUBLIC_KEY=
-DATABASE_NAME=uncloud
-DATABASE_HOST=localhost
-DATABASE_PORT=
-DATABASE_USER=
-DATABASE_PASSWORD=
-EMAIL_HOST=
-EMAIL_HOST_USER=
-EMAIL_HOST_PASSWORD=
-GITLAB_SERVER=
-GITLAB_YAML_DIR=
-GITLAB_PROJECT_ID=
-GITLAB_DNS_PROJECT_ID=
-MATRIX_DNS_MAIN_DOMAIN=
-GITLAB_DNS_OAUTH_TOKEN=
-GITLAB_OAUTH_TOKEN=
-GITLAB_AUTHOR_EMAIL=
-GITLAB_AUTHOR_NAME=
-WKHTMLTOPDF_CMD=/usr/bin/wkhtmltopdf
-LDAP_DEFAULT_START_UID=
-AUTH_LDAP_SERVER_HOST=
-AUTH_LDAP_SERVER_URI=
-AUTH_LDAP_BIND_DN=
-AUTH_LDAP_BIND_PASSWORD=
-LDAP_ADMIN_DN=
-LDAP_ADMIN_PASSWORD=
-LDAP_CUSTOMER_GROUP_ID=
-LDAP_CUSTOMER_DN=
diff --git a/uncloud/.gitignore b/uncloud/.gitignore
deleted file mode 100644
index 32eda49..0000000
--- a/uncloud/.gitignore
+++ /dev/null
@@ -1,3 +0,0 @@
-local_settings.py
-ldap_max_uid_file
-.env
diff --git a/uncloud/__init__.py b/uncloud/__init__.py
deleted file mode 100644
index 3ce2d95..0000000
--- a/uncloud/__init__.py
+++ /dev/null
@@ -1,253 +0,0 @@
-from django.utils.translation import gettext_lazy as _
-import decimal
-
-# Define DecimalField properties, used to represent amounts of money.
-AMOUNT_MAX_DIGITS=10
-AMOUNT_DECIMALS=2
-
-decimal.getcontext().prec = AMOUNT_DECIMALS
-
-# http://xml.coverpages.org/country3166.html
-COUNTRIES = (
- ('AD', _('Andorra')),
- ('AE', _('United Arab Emirates')),
- ('AF', _('Afghanistan')),
- ('AG', _('Antigua & Barbuda')),
- ('AI', _('Anguilla')),
- ('AL', _('Albania')),
- ('AM', _('Armenia')),
- ('AN', _('Netherlands Antilles')),
- ('AO', _('Angola')),
- ('AQ', _('Antarctica')),
- ('AR', _('Argentina')),
- ('AS', _('American Samoa')),
- ('AT', _('Austria')),
- ('AU', _('Australia')),
- ('AW', _('Aruba')),
- ('AZ', _('Azerbaijan')),
- ('BA', _('Bosnia and Herzegovina')),
- ('BB', _('Barbados')),
- ('BD', _('Bangladesh')),
- ('BE', _('Belgium')),
- ('BF', _('Burkina Faso')),
- ('BG', _('Bulgaria')),
- ('BH', _('Bahrain')),
- ('BI', _('Burundi')),
- ('BJ', _('Benin')),
- ('BM', _('Bermuda')),
- ('BN', _('Brunei Darussalam')),
- ('BO', _('Bolivia')),
- ('BR', _('Brazil')),
- ('BS', _('Bahama')),
- ('BT', _('Bhutan')),
- ('BV', _('Bouvet Island')),
- ('BW', _('Botswana')),
- ('BY', _('Belarus')),
- ('BZ', _('Belize')),
- ('CA', _('Canada')),
- ('CC', _('Cocos (Keeling) Islands')),
- ('CF', _('Central African Republic')),
- ('CG', _('Congo')),
- ('CH', _('Switzerland')),
- ('CI', _('Ivory Coast')),
- ('CK', _('Cook Iislands')),
- ('CL', _('Chile')),
- ('CM', _('Cameroon')),
- ('CN', _('China')),
- ('CO', _('Colombia')),
- ('CR', _('Costa Rica')),
- ('CU', _('Cuba')),
- ('CV', _('Cape Verde')),
- ('CX', _('Christmas Island')),
- ('CY', _('Cyprus')),
- ('CZ', _('Czech Republic')),
- ('DE', _('Germany')),
- ('DJ', _('Djibouti')),
- ('DK', _('Denmark')),
- ('DM', _('Dominica')),
- ('DO', _('Dominican Republic')),
- ('DZ', _('Algeria')),
- ('EC', _('Ecuador')),
- ('EE', _('Estonia')),
- ('EG', _('Egypt')),
- ('EH', _('Western Sahara')),
- ('ER', _('Eritrea')),
- ('ES', _('Spain')),
- ('ET', _('Ethiopia')),
- ('FI', _('Finland')),
- ('FJ', _('Fiji')),
- ('FK', _('Falkland Islands (Malvinas)')),
- ('FM', _('Micronesia')),
- ('FO', _('Faroe Islands')),
- ('FR', _('France')),
- ('FX', _('France, Metropolitan')),
- ('GA', _('Gabon')),
- ('GB', _('United Kingdom (Great Britain)')),
- ('GD', _('Grenada')),
- ('GE', _('Georgia')),
- ('GF', _('French Guiana')),
- ('GH', _('Ghana')),
- ('GI', _('Gibraltar')),
- ('GL', _('Greenland')),
- ('GM', _('Gambia')),
- ('GN', _('Guinea')),
- ('GP', _('Guadeloupe')),
- ('GQ', _('Equatorial Guinea')),
- ('GR', _('Greece')),
- ('GS', _('South Georgia and the South Sandwich Islands')),
- ('GT', _('Guatemala')),
- ('GU', _('Guam')),
- ('GW', _('Guinea-Bissau')),
- ('GY', _('Guyana')),
- ('HK', _('Hong Kong')),
- ('HM', _('Heard & McDonald Islands')),
- ('HN', _('Honduras')),
- ('HR', _('Croatia')),
- ('HT', _('Haiti')),
- ('HU', _('Hungary')),
- ('ID', _('Indonesia')),
- ('IE', _('Ireland')),
- ('IL', _('Israel')),
- ('IN', _('India')),
- ('IO', _('British Indian Ocean Territory')),
- ('IQ', _('Iraq')),
- ('IR', _('Islamic Republic of Iran')),
- ('IS', _('Iceland')),
- ('IT', _('Italy')),
- ('JM', _('Jamaica')),
- ('JO', _('Jordan')),
- ('JP', _('Japan')),
- ('KE', _('Kenya')),
- ('KG', _('Kyrgyzstan')),
- ('KH', _('Cambodia')),
- ('KI', _('Kiribati')),
- ('KM', _('Comoros')),
- ('KN', _('St. Kitts and Nevis')),
- ('KP', _('Korea, Democratic People\'s Republic of')),
- ('KR', _('Korea, Republic of')),
- ('KW', _('Kuwait')),
- ('KY', _('Cayman Islands')),
- ('KZ', _('Kazakhstan')),
- ('LA', _('Lao People\'s Democratic Republic')),
- ('LB', _('Lebanon')),
- ('LC', _('Saint Lucia')),
- ('LI', _('Liechtenstein')),
- ('LK', _('Sri Lanka')),
- ('LR', _('Liberia')),
- ('LS', _('Lesotho')),
- ('LT', _('Lithuania')),
- ('LU', _('Luxembourg')),
- ('LV', _('Latvia')),
- ('LY', _('Libyan Arab Jamahiriya')),
- ('MA', _('Morocco')),
- ('MC', _('Monaco')),
- ('MD', _('Moldova, Republic of')),
- ('MG', _('Madagascar')),
- ('MH', _('Marshall Islands')),
- ('ML', _('Mali')),
- ('MN', _('Mongolia')),
- ('MM', _('Myanmar')),
- ('MO', _('Macau')),
- ('MP', _('Northern Mariana Islands')),
- ('MQ', _('Martinique')),
- ('MR', _('Mauritania')),
- ('MS', _('Monserrat')),
- ('MT', _('Malta')),
- ('MU', _('Mauritius')),
- ('MV', _('Maldives')),
- ('MW', _('Malawi')),
- ('MX', _('Mexico')),
- ('MY', _('Malaysia')),
- ('MZ', _('Mozambique')),
- ('NA', _('Namibia')),
- ('NC', _('New Caledonia')),
- ('NE', _('Niger')),
- ('NF', _('Norfolk Island')),
- ('NG', _('Nigeria')),
- ('NI', _('Nicaragua')),
- ('NL', _('Netherlands')),
- ('NO', _('Norway')),
- ('NP', _('Nepal')),
- ('NR', _('Nauru')),
- ('NU', _('Niue')),
- ('NZ', _('New Zealand')),
- ('OM', _('Oman')),
- ('PA', _('Panama')),
- ('PE', _('Peru')),
- ('PF', _('French Polynesia')),
- ('PG', _('Papua New Guinea')),
- ('PH', _('Philippines')),
- ('PK', _('Pakistan')),
- ('PL', _('Poland')),
- ('PM', _('St. Pierre & Miquelon')),
- ('PN', _('Pitcairn')),
- ('PR', _('Puerto Rico')),
- ('PT', _('Portugal')),
- ('PW', _('Palau')),
- ('PY', _('Paraguay')),
- ('QA', _('Qatar')),
- ('RE', _('Reunion')),
- ('RO', _('Romania')),
- ('RU', _('Russian Federation')),
- ('RW', _('Rwanda')),
- ('SA', _('Saudi Arabia')),
- ('SB', _('Solomon Islands')),
- ('SC', _('Seychelles')),
- ('SD', _('Sudan')),
- ('SE', _('Sweden')),
- ('SG', _('Singapore')),
- ('SH', _('St. Helena')),
- ('SI', _('Slovenia')),
- ('SJ', _('Svalbard & Jan Mayen Islands')),
- ('SK', _('Slovakia')),
- ('SL', _('Sierra Leone')),
- ('SM', _('San Marino')),
- ('SN', _('Senegal')),
- ('SO', _('Somalia')),
- ('SR', _('Suriname')),
- ('ST', _('Sao Tome & Principe')),
- ('SV', _('El Salvador')),
- ('SY', _('Syrian Arab Republic')),
- ('SZ', _('Swaziland')),
- ('TC', _('Turks & Caicos Islands')),
- ('TD', _('Chad')),
- ('TF', _('French Southern Territories')),
- ('TG', _('Togo')),
- ('TH', _('Thailand')),
- ('TJ', _('Tajikistan')),
- ('TK', _('Tokelau')),
- ('TM', _('Turkmenistan')),
- ('TN', _('Tunisia')),
- ('TO', _('Tonga')),
- ('TP', _('East Timor')),
- ('TR', _('Turkey')),
- ('TT', _('Trinidad & Tobago')),
- ('TV', _('Tuvalu')),
- ('TW', _('Taiwan, Province of China')),
- ('TZ', _('Tanzania, United Republic of')),
- ('UA', _('Ukraine')),
- ('UG', _('Uganda')),
- ('UM', _('United States Minor Outlying Islands')),
- ('US', _('United States of America')),
- ('UY', _('Uruguay')),
- ('UZ', _('Uzbekistan')),
- ('VA', _('Vatican City State (Holy See)')),
- ('VC', _('St. Vincent & the Grenadines')),
- ('VE', _('Venezuela')),
- ('VG', _('British Virgin Islands')),
- ('VI', _('United States Virgin Islands')),
- ('VN', _('Viet Nam')),
- ('VU', _('Vanuatu')),
- ('WF', _('Wallis & Futuna Islands')),
- ('WS', _('Samoa')),
- ('YE', _('Yemen')),
- ('YT', _('Mayotte')),
- ('YU', _('Yugoslavia')),
- ('ZA', _('South Africa')),
- ('ZM', _('Zambia')),
- ('ZR', _('Zaire')),
- ('ZW', _('Zimbabwe')),
-)
-
-
-__all__ = ()
diff --git a/uncloud/admin.py b/uncloud/admin.py
deleted file mode 100644
index 38f8cce..0000000
--- a/uncloud/admin.py
+++ /dev/null
@@ -1,6 +0,0 @@
-from django.contrib import admin
-
-from .models import *
-
-for m in [ UncloudProvider, UncloudNetwork ]:
- admin.site.register(m)
diff --git a/uncloud/asgi.py b/uncloud/asgi.py
deleted file mode 100644
index 2b5a7a3..0000000
--- a/uncloud/asgi.py
+++ /dev/null
@@ -1,16 +0,0 @@
-"""
-ASGI config for uncloud project.
-
-It exposes the ASGI callable as a module-level variable named ``application``.
-
-For more information on this file, see
-https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
-"""
-
-import os
-
-from django.core.asgi import get_asgi_application
-
-os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'uncloud.settings')
-
-application = get_asgi_application()
diff --git a/uncloud/forms.py b/uncloud/forms.py
deleted file mode 100644
index da8c4ec..0000000
--- a/uncloud/forms.py
+++ /dev/null
@@ -1,35 +0,0 @@
-from django import forms
-from django.contrib.auth.models import User
-from django.forms import ModelForm
-from .validators import domain_name_validator
-
-
-class DomainNameField(forms.CharField):
- description = 'Domain name form field'
- default_validators = [domain_name_validator, ]
-
- def __init__(self, *args, **kwargs):
- super(DomainNameField, self).__init__(*args, **kwargs)
-
-class UserDeleteForm(forms.ModelForm):
- class Meta:
- model = User
- fields = []
-
-class MainForm(forms.Form):
- def __init__(self, *args, **kwargs):
- super(MainForm, self).__init__(*args, **kwargs)
- for visible in self.visible_fields():
- if isinstance(visible.field.widget, forms.TextInput):
- visible.field.widget.attrs['class'] = 'form-control'
- elif isinstance(visible.field.widget, forms.Select):
- visible.field.widget.attrs['class'] = 'form-select'
- elif (isinstance(visible.field.widget, forms.CheckboxInput)):
- visible.field.widget.attrs['class'] = 'custom-control-input'
-
-class MainModelForm(ModelForm):
- def __init__(self, *args, **kwargs):
- super(MainModelForm, self).__init__(*args, **kwargs)
- for visible in self.visible_fields():
- visible.field.widget.attrs['class'] = 'form-control'
-
diff --git a/uncloud/management/commands/db-add-defaults.py b/uncloud/management/commands/db-add-defaults.py
deleted file mode 100644
index 605c8f5..0000000
--- a/uncloud/management/commands/db-add-defaults.py
+++ /dev/null
@@ -1,43 +0,0 @@
-import random
-import string
-
-from django.core.management.base import BaseCommand
-from django.core.exceptions import ObjectDoesNotExist
-from django.contrib.auth import get_user_model
-from django.conf import settings
-
-from uncloud_pay.models import BillingAddress, RecurringPeriod, Product
-from uncloud.models import UncloudProvider, UncloudNetwork
-
-
-class Command(BaseCommand):
- help = 'Add standard uncloud values'
-
- def add_arguments(self, parser):
- pass
-
- def handle(self, *args, **options):
- # Order matters, objects can be dependent on each other
-
- admin_username="uncloud-admin"
- pw_length = 32
-
- # Only set password if the user did not exist before
- try:
- admin_user = get_user_model().objects.get(username=settings.UNCLOUD_ADMIN_NAME)
- except ObjectDoesNotExist:
- random_password = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(pw_length))
-
- admin_user = get_user_model().objects.create_user(username=settings.UNCLOUD_ADMIN_NAME, password=random_password)
- admin_user.is_superuser=True
- admin_user.is_staff=True
- admin_user.save()
-
- print(f"Created admin user '{admin_username}' with password '{random_password}'")
-
- BillingAddress.populate_db_defaults()
- RecurringPeriod.populate_db_defaults()
- Product.populate_db_defaults()
-
- UncloudNetwork.populate_db_defaults()
- UncloudProvider.populate_db_defaults()
diff --git a/uncloud/management/commands/uncloud.py b/uncloud/management/commands/uncloud.py
deleted file mode 100644
index bd47c6b..0000000
--- a/uncloud/management/commands/uncloud.py
+++ /dev/null
@@ -1,28 +0,0 @@
-import sys
-from datetime import datetime
-
-from django.core.management.base import BaseCommand
-
-from django.contrib.auth import get_user_model
-
-from opennebula.models import VM as VMModel
-from uncloud_vm.models import VMHost, VMProduct, VMNetworkCard, VMDiskImageProduct, VMDiskProduct, VMCluster
-
-import logging
-log = logging.getLogger(__name__)
-
-
-class Command(BaseCommand):
- help = 'General uncloud commands'
-
- def add_arguments(self, parser):
- parser.add_argument('--bootstrap', action='store_true', help='Bootstrap a typical uncloud installation')
-
- def handle(self, *args, **options):
-
- if options['bootstrap']:
- self.bootstrap()
-
- def bootstrap(self):
- default_cluster = VMCluster.objects.get_or_create(name="default")
-# local_host =
diff --git a/uncloud/migrations/0001_initial.py b/uncloud/migrations/0001_initial.py
deleted file mode 100644
index 10d1144..0000000
--- a/uncloud/migrations/0001_initial.py
+++ /dev/null
@@ -1,46 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 10:38
-
-import django.core.validators
-from django.db import migrations, models
-import django.db.models.deletion
-import uncloud.models
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- ]
-
- operations = [
- migrations.CreateModel(
- name='UncloudNetwork',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('network_address', models.GenericIPAddressField(unique=True)),
- ('network_mask', models.IntegerField(validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(128)])),
- ('description', models.CharField(max_length=256)),
- ],
- ),
- migrations.CreateModel(
- name='UncloudProvider',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('full_name', models.CharField(max_length=256)),
- ('organization', models.CharField(blank=True, max_length=256, null=True)),
- ('street', models.CharField(max_length=256)),
- ('city', models.CharField(max_length=256)),
- ('postal_code', models.CharField(max_length=64)),
- ('country', uncloud.models.CountryField(blank=True, choices=[('AD', 'Andorra'), ('AE', 'United Arab Emirates'), ('AF', 'Afghanistan'), ('AG', 'Antigua & Barbuda'), ('AI', 'Anguilla'), ('AL', 'Albania'), ('AM', 'Armenia'), ('AN', 'Netherlands Antilles'), ('AO', 'Angola'), ('AQ', 'Antarctica'), ('AR', 'Argentina'), ('AS', 'American Samoa'), ('AT', 'Austria'), ('AU', 'Australia'), ('AW', 'Aruba'), ('AZ', 'Azerbaijan'), ('BA', 'Bosnia and Herzegovina'), ('BB', 'Barbados'), ('BD', 'Bangladesh'), ('BE', 'Belgium'), ('BF', 'Burkina Faso'), ('BG', 'Bulgaria'), ('BH', 'Bahrain'), ('BI', 'Burundi'), ('BJ', 'Benin'), ('BM', 'Bermuda'), ('BN', 'Brunei Darussalam'), ('BO', 'Bolivia'), ('BR', 'Brazil'), ('BS', 'Bahama'), ('BT', 'Bhutan'), ('BV', 'Bouvet Island'), ('BW', 'Botswana'), ('BY', 'Belarus'), ('BZ', 'Belize'), ('CA', 'Canada'), ('CC', 'Cocos (Keeling) Islands'), ('CF', 'Central African Republic'), ('CG', 'Congo'), ('CH', 'Switzerland'), ('CI', 'Ivory Coast'), ('CK', 'Cook Iislands'), ('CL', 'Chile'), ('CM', 'Cameroon'), ('CN', 'China'), ('CO', 'Colombia'), ('CR', 'Costa Rica'), ('CU', 'Cuba'), ('CV', 'Cape Verde'), ('CX', 'Christmas Island'), ('CY', 'Cyprus'), ('CZ', 'Czech Republic'), ('DE', 'Germany'), ('DJ', 'Djibouti'), ('DK', 'Denmark'), ('DM', 'Dominica'), ('DO', 'Dominican Republic'), ('DZ', 'Algeria'), ('EC', 'Ecuador'), ('EE', 'Estonia'), ('EG', 'Egypt'), ('EH', 'Western Sahara'), ('ER', 'Eritrea'), ('ES', 'Spain'), ('ET', 'Ethiopia'), ('FI', 'Finland'), ('FJ', 'Fiji'), ('FK', 'Falkland Islands (Malvinas)'), ('FM', 'Micronesia'), ('FO', 'Faroe Islands'), ('FR', 'France'), ('FX', 'France, Metropolitan'), ('GA', 'Gabon'), ('GB', 'United Kingdom (Great Britain)'), ('GD', 'Grenada'), ('GE', 'Georgia'), ('GF', 'French Guiana'), ('GH', 'Ghana'), ('GI', 'Gibraltar'), ('GL', 'Greenland'), ('GM', 'Gambia'), ('GN', 'Guinea'), ('GP', 'Guadeloupe'), ('GQ', 'Equatorial Guinea'), ('GR', 'Greece'), ('GS', 'South Georgia and the South Sandwich Islands'), ('GT', 'Guatemala'), ('GU', 'Guam'), ('GW', 'Guinea-Bissau'), ('GY', 'Guyana'), ('HK', 'Hong Kong'), ('HM', 'Heard & McDonald Islands'), ('HN', 'Honduras'), ('HR', 'Croatia'), ('HT', 'Haiti'), ('HU', 'Hungary'), ('ID', 'Indonesia'), ('IE', 'Ireland'), ('IL', 'Israel'), ('IN', 'India'), ('IO', 'British Indian Ocean Territory'), ('IQ', 'Iraq'), ('IR', 'Islamic Republic of Iran'), ('IS', 'Iceland'), ('IT', 'Italy'), ('JM', 'Jamaica'), ('JO', 'Jordan'), ('JP', 'Japan'), ('KE', 'Kenya'), ('KG', 'Kyrgyzstan'), ('KH', 'Cambodia'), ('KI', 'Kiribati'), ('KM', 'Comoros'), ('KN', 'St. Kitts and Nevis'), ('KP', "Korea, Democratic People's Republic of"), ('KR', 'Korea, Republic of'), ('KW', 'Kuwait'), ('KY', 'Cayman Islands'), ('KZ', 'Kazakhstan'), ('LA', "Lao People's Democratic Republic"), ('LB', 'Lebanon'), ('LC', 'Saint Lucia'), ('LI', 'Liechtenstein'), ('LK', 'Sri Lanka'), ('LR', 'Liberia'), ('LS', 'Lesotho'), ('LT', 'Lithuania'), ('LU', 'Luxembourg'), ('LV', 'Latvia'), ('LY', 'Libyan Arab Jamahiriya'), ('MA', 'Morocco'), ('MC', 'Monaco'), ('MD', 'Moldova, Republic of'), ('MG', 'Madagascar'), ('MH', 'Marshall Islands'), ('ML', 'Mali'), ('MN', 'Mongolia'), ('MM', 'Myanmar'), ('MO', 'Macau'), ('MP', 'Northern Mariana Islands'), ('MQ', 'Martinique'), ('MR', 'Mauritania'), ('MS', 'Monserrat'), ('MT', 'Malta'), ('MU', 'Mauritius'), ('MV', 'Maldives'), ('MW', 'Malawi'), ('MX', 'Mexico'), ('MY', 'Malaysia'), ('MZ', 'Mozambique'), ('NA', 'Namibia'), ('NC', 'New Caledonia'), ('NE', 'Niger'), ('NF', 'Norfolk Island'), ('NG', 'Nigeria'), ('NI', 'Nicaragua'), ('NL', 'Netherlands'), ('NO', 'Norway'), ('NP', 'Nepal'), ('NR', 'Nauru'), ('NU', 'Niue'), ('NZ', 'New Zealand'), ('OM', 'Oman'), ('PA', 'Panama'), ('PE', 'Peru'), ('PF', 'French Polynesia'), ('PG', 'Papua New Guinea'), ('PH', 'Philippines'), ('PK', 'Pakistan'), ('PL', 'Poland'), ('PM', 'St. Pierre & Miquelon'), ('PN', 'Pitcairn'), ('PR', 'Puerto Rico'), ('PT', 'Portugal'), ('PW', 'Palau'), ('PY', 'Paraguay'), ('QA', 'Qatar'), ('RE', 'Reunion'), ('RO', 'Romania'), ('RU', 'Russian Federation'), ('RW', 'Rwanda'), ('SA', 'Saudi Arabia'), ('SB', 'Solomon Islands'), ('SC', 'Seychelles'), ('SD', 'Sudan'), ('SE', 'Sweden'), ('SG', 'Singapore'), ('SH', 'St. Helena'), ('SI', 'Slovenia'), ('SJ', 'Svalbard & Jan Mayen Islands'), ('SK', 'Slovakia'), ('SL', 'Sierra Leone'), ('SM', 'San Marino'), ('SN', 'Senegal'), ('SO', 'Somalia'), ('SR', 'Suriname'), ('ST', 'Sao Tome & Principe'), ('SV', 'El Salvador'), ('SY', 'Syrian Arab Republic'), ('SZ', 'Swaziland'), ('TC', 'Turks & Caicos Islands'), ('TD', 'Chad'), ('TF', 'French Southern Territories'), ('TG', 'Togo'), ('TH', 'Thailand'), ('TJ', 'Tajikistan'), ('TK', 'Tokelau'), ('TM', 'Turkmenistan'), ('TN', 'Tunisia'), ('TO', 'Tonga'), ('TP', 'East Timor'), ('TR', 'Turkey'), ('TT', 'Trinidad & Tobago'), ('TV', 'Tuvalu'), ('TW', 'Taiwan, Province of China'), ('TZ', 'Tanzania, United Republic of'), ('UA', 'Ukraine'), ('UG', 'Uganda'), ('UM', 'United States Minor Outlying Islands'), ('US', 'United States of America'), ('UY', 'Uruguay'), ('UZ', 'Uzbekistan'), ('VA', 'Vatican City State (Holy See)'), ('VC', 'St. Vincent & the Grenadines'), ('VE', 'Venezuela'), ('VG', 'British Virgin Islands'), ('VI', 'United States Virgin Islands'), ('VN', 'Viet Nam'), ('VU', 'Vanuatu'), ('WF', 'Wallis & Futuna Islands'), ('WS', 'Samoa'), ('YE', 'Yemen'), ('YT', 'Mayotte'), ('YU', 'Yugoslavia'), ('ZA', 'South Africa'), ('ZM', 'Zambia'), ('ZR', 'Zaire'), ('ZW', 'Zimbabwe')], default='CH', max_length=2)),
- ('starting_date', models.DateField()),
- ('ending_date', models.DateField(blank=True, null=True)),
- ('billing_network', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='uncloudproviderbill', to='uncloud.uncloudnetwork')),
- ('coupon_network', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='uncloudprovidercoupon', to='uncloud.uncloudnetwork')),
- ('referral_network', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='uncloudproviderreferral', to='uncloud.uncloudnetwork')),
- ],
- options={
- 'abstract': False,
- },
- ),
- ]
diff --git a/uncloud/migrations/0002_uncloudtasks.py b/uncloud/migrations/0002_uncloudtasks.py
deleted file mode 100644
index 9c69606..0000000
--- a/uncloud/migrations/0002_uncloudtasks.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Generated by Django 3.1 on 2020-12-20 17:16
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud', '0001_initial'),
- ]
-
- operations = [
- migrations.CreateModel(
- name='UncloudTasks',
- fields=[
- ('task_id', models.UUIDField(primary_key=True, serialize=False)),
- ],
- ),
- ]
diff --git a/uncloud/migrations/0003_auto_20201220_1728.py b/uncloud/migrations/0003_auto_20201220_1728.py
deleted file mode 100644
index 2ec0eec..0000000
--- a/uncloud/migrations/0003_auto_20201220_1728.py
+++ /dev/null
@@ -1,17 +0,0 @@
-# Generated by Django 3.1 on 2020-12-20 17:28
-
-from django.db import migrations
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud', '0002_uncloudtasks'),
- ]
-
- operations = [
- migrations.RenameModel(
- old_name='UncloudTasks',
- new_name='UncloudTask',
- ),
- ]
diff --git a/uncloud/migrations/0004_auto_20210101_1308.py b/uncloud/migrations/0004_auto_20210101_1308.py
deleted file mode 100644
index 8385b16..0000000
--- a/uncloud/migrations/0004_auto_20210101_1308.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Generated by Django 3.1 on 2021-01-01 13:08
-
-from django.db import migrations
-import uncloud.models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud', '0003_auto_20201220_1728'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='uncloudprovider',
- name='country',
- field=uncloud.models.CountryField(choices=[('AD', 'Andorra'), ('AE', 'United Arab Emirates'), ('AF', 'Afghanistan'), ('AG', 'Antigua & Barbuda'), ('AI', 'Anguilla'), ('AL', 'Albania'), ('AM', 'Armenia'), ('AN', 'Netherlands Antilles'), ('AO', 'Angola'), ('AQ', 'Antarctica'), ('AR', 'Argentina'), ('AS', 'American Samoa'), ('AT', 'Austria'), ('AU', 'Australia'), ('AW', 'Aruba'), ('AZ', 'Azerbaijan'), ('BA', 'Bosnia and Herzegovina'), ('BB', 'Barbados'), ('BD', 'Bangladesh'), ('BE', 'Belgium'), ('BF', 'Burkina Faso'), ('BG', 'Bulgaria'), ('BH', 'Bahrain'), ('BI', 'Burundi'), ('BJ', 'Benin'), ('BM', 'Bermuda'), ('BN', 'Brunei Darussalam'), ('BO', 'Bolivia'), ('BR', 'Brazil'), ('BS', 'Bahama'), ('BT', 'Bhutan'), ('BV', 'Bouvet Island'), ('BW', 'Botswana'), ('BY', 'Belarus'), ('BZ', 'Belize'), ('CA', 'Canada'), ('CC', 'Cocos (Keeling) Islands'), ('CF', 'Central African Republic'), ('CG', 'Congo'), ('CH', 'Switzerland'), ('CI', 'Ivory Coast'), ('CK', 'Cook Iislands'), ('CL', 'Chile'), ('CM', 'Cameroon'), ('CN', 'China'), ('CO', 'Colombia'), ('CR', 'Costa Rica'), ('CU', 'Cuba'), ('CV', 'Cape Verde'), ('CX', 'Christmas Island'), ('CY', 'Cyprus'), ('CZ', 'Czech Republic'), ('DE', 'Germany'), ('DJ', 'Djibouti'), ('DK', 'Denmark'), ('DM', 'Dominica'), ('DO', 'Dominican Republic'), ('DZ', 'Algeria'), ('EC', 'Ecuador'), ('EE', 'Estonia'), ('EG', 'Egypt'), ('EH', 'Western Sahara'), ('ER', 'Eritrea'), ('ES', 'Spain'), ('ET', 'Ethiopia'), ('FI', 'Finland'), ('FJ', 'Fiji'), ('FK', 'Falkland Islands (Malvinas)'), ('FM', 'Micronesia'), ('FO', 'Faroe Islands'), ('FR', 'France'), ('FX', 'France, Metropolitan'), ('GA', 'Gabon'), ('GB', 'United Kingdom (Great Britain)'), ('GD', 'Grenada'), ('GE', 'Georgia'), ('GF', 'French Guiana'), ('GH', 'Ghana'), ('GI', 'Gibraltar'), ('GL', 'Greenland'), ('GM', 'Gambia'), ('GN', 'Guinea'), ('GP', 'Guadeloupe'), ('GQ', 'Equatorial Guinea'), ('GR', 'Greece'), ('GS', 'South Georgia and the South Sandwich Islands'), ('GT', 'Guatemala'), ('GU', 'Guam'), ('GW', 'Guinea-Bissau'), ('GY', 'Guyana'), ('HK', 'Hong Kong'), ('HM', 'Heard & McDonald Islands'), ('HN', 'Honduras'), ('HR', 'Croatia'), ('HT', 'Haiti'), ('HU', 'Hungary'), ('ID', 'Indonesia'), ('IE', 'Ireland'), ('IL', 'Israel'), ('IN', 'India'), ('IO', 'British Indian Ocean Territory'), ('IQ', 'Iraq'), ('IR', 'Islamic Republic of Iran'), ('IS', 'Iceland'), ('IT', 'Italy'), ('JM', 'Jamaica'), ('JO', 'Jordan'), ('JP', 'Japan'), ('KE', 'Kenya'), ('KG', 'Kyrgyzstan'), ('KH', 'Cambodia'), ('KI', 'Kiribati'), ('KM', 'Comoros'), ('KN', 'St. Kitts and Nevis'), ('KP', "Korea, Democratic People's Republic of"), ('KR', 'Korea, Republic of'), ('KW', 'Kuwait'), ('KY', 'Cayman Islands'), ('KZ', 'Kazakhstan'), ('LA', "Lao People's Democratic Republic"), ('LB', 'Lebanon'), ('LC', 'Saint Lucia'), ('LI', 'Liechtenstein'), ('LK', 'Sri Lanka'), ('LR', 'Liberia'), ('LS', 'Lesotho'), ('LT', 'Lithuania'), ('LU', 'Luxembourg'), ('LV', 'Latvia'), ('LY', 'Libyan Arab Jamahiriya'), ('MA', 'Morocco'), ('MC', 'Monaco'), ('MD', 'Moldova, Republic of'), ('MG', 'Madagascar'), ('MH', 'Marshall Islands'), ('ML', 'Mali'), ('MN', 'Mongolia'), ('MM', 'Myanmar'), ('MO', 'Macau'), ('MP', 'Northern Mariana Islands'), ('MQ', 'Martinique'), ('MR', 'Mauritania'), ('MS', 'Monserrat'), ('MT', 'Malta'), ('MU', 'Mauritius'), ('MV', 'Maldives'), ('MW', 'Malawi'), ('MX', 'Mexico'), ('MY', 'Malaysia'), ('MZ', 'Mozambique'), ('NA', 'Namibia'), ('NC', 'New Caledonia'), ('NE', 'Niger'), ('NF', 'Norfolk Island'), ('NG', 'Nigeria'), ('NI', 'Nicaragua'), ('NL', 'Netherlands'), ('NO', 'Norway'), ('NP', 'Nepal'), ('NR', 'Nauru'), ('NU', 'Niue'), ('NZ', 'New Zealand'), ('OM', 'Oman'), ('PA', 'Panama'), ('PE', 'Peru'), ('PF', 'French Polynesia'), ('PG', 'Papua New Guinea'), ('PH', 'Philippines'), ('PK', 'Pakistan'), ('PL', 'Poland'), ('PM', 'St. Pierre & Miquelon'), ('PN', 'Pitcairn'), ('PR', 'Puerto Rico'), ('PT', 'Portugal'), ('PW', 'Palau'), ('PY', 'Paraguay'), ('QA', 'Qatar'), ('RE', 'Reunion'), ('RO', 'Romania'), ('RU', 'Russian Federation'), ('RW', 'Rwanda'), ('SA', 'Saudi Arabia'), ('SB', 'Solomon Islands'), ('SC', 'Seychelles'), ('SD', 'Sudan'), ('SE', 'Sweden'), ('SG', 'Singapore'), ('SH', 'St. Helena'), ('SI', 'Slovenia'), ('SJ', 'Svalbard & Jan Mayen Islands'), ('SK', 'Slovakia'), ('SL', 'Sierra Leone'), ('SM', 'San Marino'), ('SN', 'Senegal'), ('SO', 'Somalia'), ('SR', 'Suriname'), ('ST', 'Sao Tome & Principe'), ('SV', 'El Salvador'), ('SY', 'Syrian Arab Republic'), ('SZ', 'Swaziland'), ('TC', 'Turks & Caicos Islands'), ('TD', 'Chad'), ('TF', 'French Southern Territories'), ('TG', 'Togo'), ('TH', 'Thailand'), ('TJ', 'Tajikistan'), ('TK', 'Tokelau'), ('TM', 'Turkmenistan'), ('TN', 'Tunisia'), ('TO', 'Tonga'), ('TP', 'East Timor'), ('TR', 'Turkey'), ('TT', 'Trinidad & Tobago'), ('TV', 'Tuvalu'), ('TW', 'Taiwan, Province of China'), ('TZ', 'Tanzania, United Republic of'), ('UA', 'Ukraine'), ('UG', 'Uganda'), ('UM', 'United States Minor Outlying Islands'), ('US', 'United States of America'), ('UY', 'Uruguay'), ('UZ', 'Uzbekistan'), ('VA', 'Vatican City State (Holy See)'), ('VC', 'St. Vincent & the Grenadines'), ('VE', 'Venezuela'), ('VG', 'British Virgin Islands'), ('VI', 'United States Virgin Islands'), ('VN', 'Viet Nam'), ('VU', 'Vanuatu'), ('WF', 'Wallis & Futuna Islands'), ('WS', 'Samoa'), ('YE', 'Yemen'), ('YT', 'Mayotte'), ('YU', 'Yugoslavia'), ('ZA', 'South Africa'), ('ZM', 'Zambia'), ('ZR', 'Zaire'), ('ZW', 'Zimbabwe')], default='CH', max_length=2),
- ),
- ]
diff --git a/uncloud/migrations/0005_delete_uncloudtask.py b/uncloud/migrations/0005_delete_uncloudtask.py
deleted file mode 100644
index 6d9b095..0000000
--- a/uncloud/migrations/0005_delete_uncloudtask.py
+++ /dev/null
@@ -1,16 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-07 15:11
-
-from django.db import migrations
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud', '0004_auto_20210101_1308'),
- ]
-
- operations = [
- migrations.DeleteModel(
- name='UncloudTask',
- ),
- ]
diff --git a/uncloud/migrations/__init__.py b/uncloud/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud/models.py b/uncloud/models.py
deleted file mode 100644
index c2b3cf9..0000000
--- a/uncloud/models.py
+++ /dev/null
@@ -1,209 +0,0 @@
-from django.db import models
-from django.db.models import JSONField, Q
-from django.utils import timezone
-from django.utils.translation import gettext_lazy as _
-from django.core.validators import MinValueValidator, MaxValueValidator
-from django.core.exceptions import FieldError
-
-from uncloud import COUNTRIES
-from .selectors import filter_for_when
-
-class UncloudModel(models.Model):
- """
- This class extends the standard model with an
- extra_data field that can be used to include public,
- but internal information.
-
- For instance if you migrate from an existing virtualisation
- framework to uncloud.
-
- The extra_data attribute should be considered a hack and whenever
- data is necessary for running uncloud, it should **not** be stored
- in there.
-
- """
-
- extra_data = JSONField(editable=False, blank=True, null=True)
-
- class Meta:
- abstract = True
-
-# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
-class UncloudStatus(models.TextChoices):
- PENDING = 'PENDING', _('Pending')
- AWAITING_PAYMENT = 'AWAITING_PAYMENT', _('Awaiting payment')
- BEING_CREATED = 'BEING_CREATED', _('Being created')
- SCHEDULED = 'SCHEDULED', _('Scheduled') # resource selected, waiting for dispatching
- ACTIVE = 'ACTIVE', _('Active')
- MODIFYING = 'MODIFYING', _('Modifying') # Resource is being changed
- DELETED = 'DELETED', _('Deleted') # Resource has been deleted
- DISABLED = 'DISABLED', _('Disabled') # Is usable, but cannot be used for new things
- UNUSABLE = 'UNUSABLE', _('Unusable'), # Has some kind of error
-
-
-
-###
-# General address handling
-class CountryField(models.CharField):
- def __init__(self, *args, **kwargs):
- kwargs.setdefault('choices', COUNTRIES)
- kwargs.setdefault('default', 'CH')
- kwargs.setdefault('max_length', 2)
-
- super().__init__(*args, **kwargs)
-
- def get_internal_type(self):
- return "CharField"
-
-
-class UncloudAddress(models.Model):
- full_name = models.CharField(max_length=256, null=False)
- organization = models.CharField(max_length=256, blank=True, null=True)
- street = models.CharField(max_length=256, null=False)
- city = models.CharField(max_length=256, null=False)
- postal_code = models.CharField(max_length=64)
- country = CountryField(blank=False, null=False)
-
- class Meta:
- abstract = True
-
-
-class UncloudValidTimeFrame(models.Model):
- """
- A model that allows to limit validity of something to a certain
- time frame. Used for versioning basically.
-
- Logic:
-
- """
-
- class Meta:
- abstract = True
-
- constraints = [
- models.UniqueConstraint(fields=['owner'],
- condition=models.Q(active=True),
- name='one_active_card_per_user')
- ]
-
-
- valid_from = models.DateTimeField(default=timezone.now, null=True, blank=True)
- valid_to = models.DateTimeField(null=True, blank=True)
-
- @classmethod
- def get_current(cls, *args, **kwargs):
- now = timezone.now()
-
- # With both given
- cls.objects.filter(valid_from__lte=now,
- valid_to__gte=now)
-
- # With to missing
- cls.objects.filter(valid_from__lte=now,
- valid_to__isnull=true)
-
- # With from missing
- cls.objects.filter(valid_from__isnull=true,
- valid_to__gte=now)
-
- # Both missing
- cls.objects.filter(valid_from__isnull=true,
- valid_to__gte=now)
-
-
-
-
-
-###
-# UncloudNetworks are used as identifiers - such they are a base of uncloud
-
-class UncloudNetwork(models.Model):
- """
- Storing IP networks
- """
-
- network_address = models.GenericIPAddressField(null=False, unique=True)
- network_mask = models.IntegerField(null=False,
- validators=[MinValueValidator(0),
- MaxValueValidator(128)]
- )
-
- description = models.CharField(max_length=256)
-
- @classmethod
- def populate_db_defaults(cls):
- for net, desc in [
- ( "2a0a:e5c0:11::", "uncloud Billing" ),
- ( "2a0a:e5c0:11:1::", "uncloud Referral" ),
- ( "2a0a:e5c0:11:2::", "uncloud Coupon" )
- ]:
- obj, created = cls.objects.get_or_create(network_address=net,
- defaults= {
- 'network_mask': 64,
- 'description': desc
- }
- )
-
-
- def save(self, *args, **kwargs):
- if not ':' in self.network_address and self.network_mask > 32:
- raise FieldError("Mask cannot exceed 32 for IPv4")
-
- super().save(*args, **kwargs)
-
-
- def __str__(self):
- return f"{self.network_address}/{self.network_mask} {self.description}"
-
-###
-# Who is running / providing this instance of uncloud?
-
-class UncloudProvider(UncloudAddress):
- """
- A class resembling who is running this uncloud instance.
- This might change over time so we allow starting/ending dates
-
- This also defines the taxation rules.
-
- starting/ending date define from when to when this is valid. This way
- we can model address changes and have it correct in the bills.
- """
-
- # Meta:
- # FIXMe: only allow non overlapping time frames -- how to define this as a constraint?
- starting_date = models.DateField()
- ending_date = models.DateField(blank=True, null=True)
-
- billing_network = models.ForeignKey(UncloudNetwork, related_name="uncloudproviderbill", on_delete=models.CASCADE)
- referral_network = models.ForeignKey(UncloudNetwork, related_name="uncloudproviderreferral", on_delete=models.CASCADE)
- coupon_network = models.ForeignKey(UncloudNetwork, related_name="uncloudprovidercoupon", on_delete=models.CASCADE)
-
-
- @classmethod
- def get_provider(cls, when=None):
- """
- Find active provide at a certain time - if there was any
- """
-
-
- return cls.objects.get(Q(starting_date__gte=when, ending_date__lte=when) |
- Q(starting_date__gte=when, ending_date__isnull=True))
-
-
- @classmethod
- def populate_db_defaults(cls):
- obj, created = cls.objects.get_or_create(full_name="ungleich glarus ag",
- street="Bahnhofstrasse 1",
- postal_code="8783",
- city="Linthal",
- country="CH",
- starting_date=timezone.now(),
- billing_network=UncloudNetwork.objects.get(description="uncloud Billing"),
- referral_network=UncloudNetwork.objects.get(description="uncloud Referral"),
- coupon_network=UncloudNetwork.objects.get(description="uncloud Coupon")
- )
-
-
- def __str__(self):
- return f"{self.full_name} {self.country}"
-
diff --git a/uncloud/selectors.py b/uncloud/selectors.py
deleted file mode 100644
index 52b8548..0000000
--- a/uncloud/selectors.py
+++ /dev/null
@@ -1,23 +0,0 @@
-from django.db.models import Q
-from django.utils import timezone
-
-def filter_for_when(queryset, when=None):
- """
- Return a filtered queryset which is valid for the given date
-
- Logic:
-
- Look for entries that have a starting date before when
- and either
- - No ending date
- - Ending date after "when"
-
- Returns a queryset, you'll neet to apply .first() or similar on it
-
- """
-
- if not when:
- when = timezone.now()
-
- return queryset.filter(starting_date__lte=when).filter(Q(ending_date__gte=when) |
- Q(ending_date__isnull=True))
diff --git a/uncloud/settings.py b/uncloud/settings.py
deleted file mode 100644
index f7a16ce..0000000
--- a/uncloud/settings.py
+++ /dev/null
@@ -1,304 +0,0 @@
-"""
-Django settings for uncloud project.
-
-Generated by 'django-admin startproject' using Django 3.0.3.
-
-For more information on this file, see
-https://docs.djangoproject.com/en/3.0/topics/settings/
-
-For the full list of settings and their values, see
-https://docs.djangoproject.com/en/3.0/ref/settings/
-"""
-
-import os
-import re
-import ldap
-import sys
-import environ
-
-from django.core.management.utils import get_random_secret_key
-from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion
-
-LOGGING = {
- 'version': 1,
- 'disable_existing_loggers': False,
- 'handlers': {
- 'console': {
- 'class': 'logging.StreamHandler',
- },
- },
- 'root': {
- 'handlers': ['console'],
- 'level': 'DEBUG',
- },
-}
-
-# Initialise environment variables
-env = environ.Env()
-environ.Env.read_env()
-
-# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
-BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
-
-# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
-
-# Quick-start development settings - unsuitable for production
-# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
-
-# SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = True
-
-SITE_ID = 1
-
-# Application definition
-
-INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.sites',
- 'django.contrib.staticfiles',
- 'django_extensions',
- 'bootstrap5',
-# 'mathfilters',
-# 'compressor',
-# 'wkhtmltopdf',
- 'rest_framework',
- 'django_q',
-# 'notifications',
- 'uncloud',
- 'uncloud_auth',
- 'uncloud_net',
- 'uncloud_storage',
- 'uncloud_vm',
- 'uncloud_service',
- 'opennebula',
-# env('ACTIVE_APP'),
- 'uncloud_pay', # should be after the active app to load the templates from the active app first
-# 'allauth',
-# 'allauth.account',
-# 'allauth.socialaccount',
-]
-
-DEFAULT_AUTO_FIELD='django.db.models.AutoField'
-
-MIDDLEWARE = [
- 'django.middleware.security.SecurityMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'django.middleware.common.CommonMiddleware',
- 'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
- 'django.contrib.messages.middleware.MessageMiddleware',
- 'django.middleware.clickjacking.XFrameOptionsMiddleware',
-]
-
-ROOT_URLCONF = 'uncloud.urls'
-#WKHTMLTOPDF_CMD = env('WKHTMLTOPDF_CMD')
-
-TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [],
- 'APP_DIRS': True,
- 'OPTIONS': {
- 'context_processors': [
- 'django.template.context_processors.debug',
- 'django.template.context_processors.request',
- 'django.contrib.auth.context_processors.auth',
- 'django.contrib.messages.context_processors.messages',
- ],
- },
- },
-]
-
-WSGI_APPLICATION = 'uncloud.wsgi.application'
-DJANGO_NOTIFICATIONS_CONFIG = { 'USE_JSONFIELD': True}
-
-# Password validation
-# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
-
-AUTH_PASSWORD_VALIDATORS = [
- {
- 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
- },
-]
-###############################################################################
-# Authall Settings
-ACCOUNT_AUTHENTICATION_METHOD = "username"
-ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1
-ACCOUNT_EMAIL_REQUIRED = True
-ACCOUNT_UNIQUE_EMAIL = True
-MAX_EMAIL_ADDRESSES = 1
-################################################################################
-# AUTH/LDAP
-
-LDAP_ENABLED = True
-AUTH_LDAP_SERVER_HOST = env('AUTH_LDAP_SERVER_HOST')
-AUTH_LDAP_SERVER_URI = env('AUTH_LDAP_SERVER_URI')
-AUTH_LDAP_BIND_DN = env('AUTH_LDAP_BIND_DN')
-AUTH_LDAP_BIND_PASSWORD = env('AUTH_LDAP_BIND_PASSWORD')
-
-AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=customers,dc=ungleich,dc=ch"
-AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=customers,dc=ungleich,dc=ch",
- ldap.SCOPE_SUBTREE,
- "(uid=%(user)s)")
-# BIND_AS_AUTHENTICATING_USER = True
-START_TLS = True
-LDAP_ADMIN_DN = env("LDAP_ADMIN_DN")
-LDAP_ADMIN_PASSWORD = env("LDAP_ADMIN_PASSWORD")
-LDAP_CUSTOMER_GROUP_ID = env("LDAP_CUSTOMER_GROUP_ID")
-LDAP_CUSTOMER_DN=env("LDAP_CUSTOMER_DN")
-
-#AUTH_LDAP_USER_QUERY_FIELD = "email"
-AUTH_LDAP_USER_ATTR_MAP = {
- "first_name": "cn",
- "last_name": "sn",
- "email": "mail"
-}
-LDAP_DEFAULT_START_UID = int(env('LDAP_DEFAULT_START_UID'))
-
-LDAP_MAX_UID_FILE_PATH = os.environ.get('LDAP_MAX_UID_FILE_PATH',
- os.path.join(os.path.abspath(os.path.dirname(__file__)), 'ldap_max_uid_file')
-)
-################################################################################
-# AUTH/Django
-AUTHENTICATION_BACKENDS = [
- "django_auth_ldap.backend.LDAPBackend",
- "django.contrib.auth.backends.ModelBackend",
-# 'allauth.account.auth_backends.AuthenticationBackend',
-]
-
-
-
-AUTH_USER_MODEL = 'uncloud_auth.User'
-ACCOUNT_FORMS = {
- 'signup': 'uncloud_auth.forms.MySignupForm',
- 'change_password': 'uncloud_auth.forms.MyChangePasswordForm',
- 'set_password': 'uncloud_auth.forms.MySetPasswordForm',
- 'reset_password_from_key': 'uncloud_auth.forms.MyResetPasswordKeyForm',
- }
-
-################################################################################
-# AUTH/REST
-REST_FRAMEWORK = {
- 'DEFAULT_AUTHENTICATION_CLASSES': [
- 'rest_framework.authentication.BasicAuthentication',
- 'rest_framework.authentication.SessionAuthentication',
- ]
-}
-
-
-# Internationalization
-# https://docs.djangoproject.com/en/3.0/topics/i18n/
-
-LANGUAGE_CODE = 'en-us'
-
-TIME_ZONE = 'UTC'
-
-USE_I18N = True
-
-USE_L10N = True
-
-USE_TZ = True
-
-
-# Static files (CSS, JavaScript, Images)
-# https://docs.djangoproject.com/en/3.0/howto/static-files/
-STATIC_URL = '/static/'
-STATIC_ROOT = os.path.join(BASE_DIR, "static")
-STATICFILES_FINDERS = [
- 'django.contrib.staticfiles.finders.FileSystemFinder',
- 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
- 'compressor.finders.CompressorFinder',
-]
-COMPRESS_ENABLED = True
-
-#VM Deployment TEMPLATE
-GITLAB_SERVER = env('GITLAB_SERVER')
-GITLAB_OAUTH_TOKEN = env('GITLAB_OAUTH_TOKEN')
-GITLAB_PROJECT_ID = env('GITLAB_PROJECT_ID')
-GITLAB_AUTHOR_EMAIL = env('GITLAB_AUTHOR_EMAIL')
-GITLAB_AUTHOR_NAME = env('GITLAB_AUTHOR_NAME')
-GITLAB_YAML_DIR = env('GITLAB_YAML_DIR')
-GITLAB_DNS_PROJECT_ID = env('GITLAB_DNS_PROJECT_ID')
-MATRIX_DNS_MAIN_DOMAIN = env('MATRIX_DNS_MAIN_DOMAIN')
-GITLAB_DNS_OAUTH_TOKEN = env('GITLAB_DNS_OAUTH_TOKEN')
-
-# XML-RPC interface of opennebula
-OPENNEBULA_URL = 'https://opennebula.example.com:2634/RPC2'
-
-# user:pass for accessing opennebula
-OPENNEBULA_USER_PASS = 'user:password'
-
-# Stripe (Credit Card payments)
-STRIPE_KEY=env('STRIPE_KEY')
-STRIPE_PUBLIC_KEY=env('STRIPE_PUBLIC_KEY')
-BILL_PAYMENT_DELAY = 0
-MIN_PER_TRANSACTION = 5
-# The django secret key
-SECRET_KEY=get_random_secret_key()
-
-ALLOWED_HOSTS = ''
-
-# required for hardcopy / pdf rendering: https://github.com/loftylabs/django-hardcopy
-CHROME_PATH = '/usr/bin/chromium-browser'
-
-# Username that is created by default and owns the configuration objects
-UNCLOUD_ADMIN_NAME = "uncloud-admin"
-
-LOGIN_REDIRECT_URL = '/'
-LOGOUT_REDIRECT_URL = '/'
-
-EMAIL_USE_TLS = True
-EMAIL_HOST = env('EMAIL_HOST')
-
-EMAIL_PORT = 25
-EMAIL_HOST_USER = DEFAULT_FROM_EMAIL = env('EMAIL_HOST_USER')
-EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD')
-DEFAULT_FROM_EMAIL = 'support@ungleich.ch'
-RENEWAL_FROM_EMAIL = 'support@ungleich.ch'
-
-##############
-# Jobs
-Q_CLUSTER = {
- 'name': 'uncloud',
- 'workers': 1,
- 'recycle': 500,
- 'timeout': 60,
- 'compress': True,
- 'cpu_affinity': 1,
- 'save_limit': 250,
- 'queue_limit': 500,
- 'label': 'Django Q',
- 'redis': {
- 'host': '127.0.0.1',
- 'port': 6379,
- 'db': 0, }
-}
-
-REPORT_FORMAT = {
- 'page_height': 200,
- 'page_width':175,
- 'orientation': 'Portrait',
- 'header_spacing': 65,
- 'margin_bottom':25,
- 'header_line': False,
-}
-
-
-# Overwrite settings with local settings, if existing
-try:
- from uncloud.local_settings import *
-except (ModuleNotFoundError, ImportError):
- pass
diff --git a/uncloud/static/uncloud/uncloud.css b/uncloud/static/uncloud/uncloud.css
deleted file mode 100644
index 51d93ef..0000000
--- a/uncloud/static/uncloud/uncloud.css
+++ /dev/null
@@ -1,4 +0,0 @@
-#content {
- width: 400px;
- margin: auto;
-}
diff --git a/uncloud/templates/uncloud/base.html b/uncloud/templates/uncloud/base.html
deleted file mode 100644
index 3f1632d..0000000
--- a/uncloud/templates/uncloud/base.html
+++ /dev/null
@@ -1,61 +0,0 @@
-{% load static i18n %}
-{% load bootstrap5 %}
-
-{# Display django.contrib.messages as Bootstrap alerts #}
-{% bootstrap_messages %}
-
-{% get_current_language as LANGUAGE_CODE %}
-
-
-
-
-
-
-
-
-
-
- uncloud - {% block title %} made in Switzerland {% endblock %}
-
-
- {% block css_extra %} {% endblock css_extra %}
- {% block head_extra %} {% endblock head_extra %}
-
- {# Load CSS and JavaScript #}
- {% bootstrap_css %}
- {% bootstrap_javascript %}
-
-
-
-
-
-
- {% block content %} {% endblock %}
-
-
- {% block js_extra %} {% endblock js_extra %}
-
-
diff --git a/uncloud/templates/uncloud/index.html b/uncloud/templates/uncloud/index.html
deleted file mode 100644
index 5aeb112..0000000
--- a/uncloud/templates/uncloud/index.html
+++ /dev/null
@@ -1,168 +0,0 @@
-{% extends 'uncloud/base.html' %}
-{% block title %}Welcome to uncloud [beta]{% endblock %}
-{% block content %}
-
-
-
-
-
Welcome to uncloud [beta]
-
-
-
-
-
-
Getting started
-
-
uncloud is designed to be as easy as possible to use. However,
- there are some "real world" requirements that need to be met to
- start using uncloud:
-
-
- - First you need
- to register an
- account. If you already have one, you can
- login.
-
- If you have forgotten your password or other issues with
- logging in, you can contact the ungleich support
- via support at ungleich.ch.
-
-
- Secondy you will need to
- create a billing
- address. This is required for determining the correct
- tax.
-
- Next you will need to
- register a credit card
- from which payments can be made. Your credit card will not
- be charged without your consent.
-
-
-
-
-
Introduction to uncloud concepts
-
-
We plan to offer many services on uncloud ranging from
- for free, for a small amount or regular charges. As transfer
- fees are a major challenge for our business, we based uncloud
- on the pre-paid account model. Which means
- that you can charge your account and then use your balance to
- pay for product usage.
-
-
-
-
-
Credit cards
-
-
- Credit cards are registered with stripe. We only save a the
- last 4 digits and the expiry date of the card to make
- identification for you easier.
-
-
- - Register a credit card
- (this is required to be done via Javascript so that we never see
- your credit card, but it is sent directly to stripe)
-
- You can list your
- credit cards
- By default the first credit card is used for charging
- ("active: true") and later added cards will not be
- used. To change this, first disable the active flag and
- then set it on another credit card.
-
-
-
-
Billing Address, Payments and Balance
-
-
Billing addresses behave similar to credit cards: you can
- have many of them, but only one can be active. The active
- billing address is taken for creating new orders.
-
-
In uncloud we use the pre-paid model: you can add money to
- your account via payments. You can always check your
- balance. The products you use will automatically be charged from
- your existing balance.
-
-
-
In the future you will be able opt-in to automatically
- recharging your account at a certain time frame or whenever it
- is below a certain amount
-
-
-
-
-
-
-
-
Networking
-
-
- With uncloud you can use a variety of network related
- services.
-
-
-
-
-
-
-
-
Current limitations
-
-
- - Payments are only possible in CHF.
-
-
-
- {% if user.is_authenticated %}
-
-
Account Settings
-
-
-
-
-
- {% endif %}
-
-
-{% endblock %}
diff --git a/uncloud/urls.py b/uncloud/urls.py
deleted file mode 100644
index afb3a2e..0000000
--- a/uncloud/urls.py
+++ /dev/null
@@ -1,72 +0,0 @@
-"""uncloud URL Configuration
-
-The `urlpatterns` list routes URLs to views. For more information please see:
- https://docs.djangoproject.com/en/3.0/topics/http/urls/
-"""
-
-from django.contrib import admin
-from django.urls import path, include
-from django.conf import settings
-from django.conf.urls.static import static
-
-from rest_framework import routers
-from rest_framework.schemas import get_schema_view
-
-from uncloud_auth import views as authviews
-from uncloud_net import views as netviews
-from uncloud_pay import views as payviews
-from uncloud_vm import views as vmviews
-from . import views as uncloudviews
-#import notifications.urls
-
-router = routers.DefaultRouter()
-
-# Beta endpoints
-router.register(r'beta/vm', vmviews.NicoVMProductViewSet, basename='nicovmproduct')
-
-################################################################################
-# v2
-
-# Net
-router.register(r'v2/net/wireguardvpn', netviews.WireGuardVPNViewSet, basename='wireguardvpnnetwork')
-router.register(r'v2/net/wireguardvpnsizes', netviews.WireGuardVPNSizes, basename='wireguardvpnnetworksizes')
-
-# Payment related for a user
-router.register(r'v2/payment/credit-card', payviews.CreditCardViewSet, basename='stripecreditcard')
-router.register(r'v2/payment/payment', payviews.PaymentViewSet, basename='payment')
-router.register(r'v2/payment/balance', payviews.BalanceViewSet, basename='payment-balance')
-router.register(r'v2/payment/address', payviews.BillingAddressViewSet, basename='billingaddress')
-router.register(r'v2/orders', payviews.OrderViewSet, basename='orders')
-router.register(r'v2/bill', payviews.BillViewSet, basename='bills')
-
-# Generic helper views that are usually not needed
-router.register(r'v2/generic/vat-rate', payviews.VATRateViewSet, basename='vatrate')
-
-
-urlpatterns = [
- path(r'', uncloudviews.UncloudIndex.as_view(), name='uncloudindex'),
- path(r'^/login', authviews.LoginView.as_view(), name='login'),
- path(r'^/logout', authviews.LogoutView.as_view(), name='logout'),
-
- path(r'api/', include(router.urls), name='api'),
-
- path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), # for login to REST API
- path('openapi', get_schema_view(
- title="uncloud",
- description="uncloud API",
- version="2.0.0"
- ), name='openapi-schema'),
-
- path('admin/', admin.site.urls),
- #path('accounts/', include('allauth.urls')),
- path('pricing//calculate/', payviews.PricingView.as_view(), name='pricing_calculator'),
- path('cc/reg/', payviews.RegisterCard.as_view(), name="cc_register"),
-# path('inbox/notifications/', include(notifications.urls, namespace='notifications')),
- path('payments/', include('uncloud_pay.urls', namespace='payments')),
- #
-]
-
-# if env('ACTIVE_APP') == 'nextcloud':
-# urlpatterns.append(path('', include('nextcloud.urls', namespace='nextcloud')))
-# elif env('ACTIVE_APP') == 'matrixhosting':
-# urlpatterns.append(path('', include('matrixhosting.urls', namespace='matrix')))
diff --git a/uncloud/validators.py b/uncloud/validators.py
deleted file mode 100644
index 08cc818..0000000
--- a/uncloud/validators.py
+++ /dev/null
@@ -1,34 +0,0 @@
-from django.core.validators import RegexValidator
-
-
-def _validator():
-
- ul = '\u00a1-\uffff' # unicode letters range (must not be a raw string)
-
- # IP patterns
- ipv4_re = r'(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
- ipv6_re = r'\[[0-9a-f:\.]+\]' # (simple regex, validated later)
-
- # Host patterns
- hostname_re = r'[a-z' + ul + \
- r'0-9](?:[a-z' + ul + r'0-9-]{0,61}[a-z' + ul + r'0-9])?'
- # Max length for domain name labels is 63 characters per RFC 1034 sec. 3.1
- domain_re = r'(?:\.(?!-)[a-z' + ul + r'0-9-]{1,63}(?{% trans "E-mail Addresses" %}
-{% if user.emailaddress_set.all %}
-{% trans 'The following e-mail addresses are associated with your account:' %}
-
-
-
-{% else %}
-{% trans 'Warning:'%} {% trans "You currently do not have any e-mail address set up. You should really add an e-mail address so you can receive notifications, reset your password, etc." %}
-
-{% endif %}
-
-
-
-{% endblock %}
-
-
-{% block extra_body %}
-
-{% endblock %}
\ No newline at end of file
diff --git a/uncloud_auth/templates/account/email_confirm.html b/uncloud_auth/templates/account/email_confirm.html
deleted file mode 100644
index 0ba7d45..0000000
--- a/uncloud_auth/templates/account/email_confirm.html
+++ /dev/null
@@ -1,30 +0,0 @@
-{% extends "account/base.html" %}
-
-{% load i18n %}
-{% load account %}
-
-{% block head_title %}{% trans "Confirm E-mail Address" %}{% endblock %}
-
-{% block content %}
-{% trans "Confirm E-mail Address" %}
-
-{% if confirmation %}
-
-{% user_display confirmation.email_address.user as user_display %}
-
-{% blocktrans with confirmation.email_address.email as email %}Please confirm that {{ email }} is an e-mail address for user {{ user_display }}.{% endblocktrans %}
-
-
-
-{% else %}
-
-{% url 'account_email' as email_url %}
-
-{% blocktrans %}This e-mail confirmation link expired or is invalid. Please issue a new e-mail confirmation request.{% endblocktrans %}
-
-{% endif %}
-
-{% endblock %}
diff --git a/uncloud_auth/templates/account/login.html b/uncloud_auth/templates/account/login.html
deleted file mode 100644
index d8d92f7..0000000
--- a/uncloud_auth/templates/account/login.html
+++ /dev/null
@@ -1,43 +0,0 @@
-{% extends "account/base.html" %}
-
-{% load i18n %}
-{% load account socialaccount %}
-
-{% block head_title %}{% trans "Sign In" %}{% endblock %}
-
-{% block content %}
- Log In
-
- {% trans "Don't have an account?" %}{% trans "Sign Up" %}
-
-{% endblock %}
diff --git a/uncloud_auth/templates/account/logout.html b/uncloud_auth/templates/account/logout.html
deleted file mode 100644
index 8363f84..0000000
--- a/uncloud_auth/templates/account/logout.html
+++ /dev/null
@@ -1,18 +0,0 @@
-{% extends "account/base.html" %}
-
-{% load i18n %}
-
-{% block head_title %}{% trans "Sign Out" %}{% endblock %}
-
-{% block content %}
- {% trans "Sign Out" %}
- {% trans 'Are you sure you want to sign out?' %}
-
-{% endblock %}
-
diff --git a/uncloud_auth/templates/account/password_reset.html b/uncloud_auth/templates/account/password_reset.html
deleted file mode 100644
index 033f84e..0000000
--- a/uncloud_auth/templates/account/password_reset.html
+++ /dev/null
@@ -1,29 +0,0 @@
-{% extends "account/base.html" %}
-
-{% load i18n %}
-{% load account %}
-
-{% block head_title %}{% trans "Password Reset" %}{% endblock %}
-
-{% block content %}
- {% if user.is_authenticated %}
- {% include "account/snippets/already_logged_in.html" %}
- {% endif %}
- {% trans "Forgotten your password? Enter your e-mail address below, and we'll send you an e-mail allowing you to reset it." %}
-
-{% endblock %}
diff --git a/uncloud_auth/templates/account/password_reset_from_key.html b/uncloud_auth/templates/account/password_reset_from_key.html
deleted file mode 100644
index b98d174..0000000
--- a/uncloud_auth/templates/account/password_reset_from_key.html
+++ /dev/null
@@ -1,37 +0,0 @@
-{% extends "account/base.html" %}
-
-{% load i18n %}
-{% block head_title %}{% trans "Change Password" %}{% endblock %}
-
-{% block content %}
- {% if token_fail %}{% trans "Bad Token" %}{% else %}{% trans "Change Password" %}{% endif %}
-
- {% if token_fail %}
- {% url 'account_reset_password' as passwd_reset_url %}
- {% blocktrans %}The password reset link was invalid, possibly because it has already been used. Please request a new password reset.{% endblocktrans %}
- {% else %}
- {% if form %}
-
- {% else %}
- {% trans 'Your password is now changed.' %}
- {% endif %}
- {% endif %}
-{% endblock %}
diff --git a/uncloud_auth/templates/account/signup.html b/uncloud_auth/templates/account/signup.html
deleted file mode 100644
index ea1d625..0000000
--- a/uncloud_auth/templates/account/signup.html
+++ /dev/null
@@ -1,57 +0,0 @@
-{% extends "account/base.html" %}
-
-{% load i18n %}
-{% load account socialaccount %}
-
-{% block head_title %}{% trans "Sign Up" %}{% endblock %}
-
-{% block content %}
- {% trans "Sign Up" %}
-
- {% trans "Already have an account?" %}{% trans "Login" %}
-{% endblock %}
diff --git a/uncloud_auth/templates/uncloud_auth/login.html b/uncloud_auth/templates/uncloud_auth/login.html
deleted file mode 100644
index 7956687..0000000
--- a/uncloud_auth/templates/uncloud_auth/login.html
+++ /dev/null
@@ -1,21 +0,0 @@
-{% extends 'uncloud/base.html' %}
-{% load bootstrap5 %}
-{% block title %}Welcome to uncloud [beta]{% endblock %}
-
-{% block content %}
-
-
-
-
-
Login to uncloud
-
-
-
-
-{% endblock %}
diff --git a/uncloud_auth/ungleich_ldap.py b/uncloud_auth/ungleich_ldap.py
deleted file mode 100644
index 4e260d1..0000000
--- a/uncloud_auth/ungleich_ldap.py
+++ /dev/null
@@ -1,277 +0,0 @@
-import base64
-import hashlib
-import logging
-import random
-import unicodedata
-
-import ldap3
-from ldap3 import ALL
-from django.conf import settings
-from django.contrib.auth.hashers import make_password
-
-logger = logging.getLogger(__name__)
-
-
-class LdapManager:
- __instance = None
- def __new__(cls):
- if LdapManager.__instance is None:
- LdapManager.__instance = object.__new__(cls)
- return LdapManager.__instance
-
- def __init__(self):
- """
- Initialize the LDAP subsystem.
- """
- self.rng = random.SystemRandom()
- self.server = ldap3.Server(settings.AUTH_LDAP_SERVER_HOST, use_ssl=True, get_info=ALL)
-
-
- def get_admin_conn(self):
- """
- Return a bound :class:`ldap3.Connection` instance which has write
- permissions on the dn in which the user accounts reside.
- """
- conn = self.get_conn(user=settings.LDAP_ADMIN_DN,
- password=settings.LDAP_ADMIN_PASSWORD,
- raise_exceptions=True)
- conn.start_tls()
- conn.bind()
- return conn
-
-
- def get_conn(self, **kwargs):
- """
- Return an unbound :class:`ldap3.Connection` which talks to the configured
- LDAP server.
-
- The *kwargs* are passed to the constructor of :class:`ldap3.Connection` and
- can be used to set *user*, *password* and other useful arguments.
- """
- return ldap3.Connection(self.server, **kwargs)
-
-
- def _ssha_password(self, password):
- """
- Apply the SSHA password hashing scheme to the given *password*.
- *password* must be a :class:`bytes` object, containing the utf-8
- encoded password.
-
- Return a :class:`bytes` object containing ``ascii``-compatible data
- which can be used as LDAP value, e.g. after armoring it once more using
- base64 or decoding it to unicode from ``ascii``.
- """
- SALT_BYTES = 15
- sha1 = hashlib.sha1()
- salt = self.rng.getrandbits(SALT_BYTES * 8).to_bytes(SALT_BYTES,
- "little")
- sha1.update(password)
- sha1.update(salt)
-
- digest = sha1.digest()
- passwd = b"{SSHA}" + base64.b64encode(digest + salt)
-
- return passwd
-
-
- def create_user(self, username, password, firstname, lastname, email):
- conn = self.get_admin_conn()
- uidNumber = self._get_max_uid() + 1
- logger.debug("uidNumber={uidNumber}".format(uidNumber=uidNumber))
- user_exists = True
- while user_exists:
- user_exists, _ = self.check_user_exists(
- "",
- '(&(objectClass=inetOrgPerson)(objectClass=posixAccount)'
- '(objectClass=top)(uidNumber={uidNumber}))'.format(
- uidNumber=uidNumber
- )
- )
- if user_exists:
- logger.debug(
- "{uid} exists. Trying next.".format(uid=uidNumber)
- )
- uidNumber += 1
- logger.debug("{uid} does not exist. Using it".format(uid=uidNumber))
- self._set_max_uid(uidNumber)
- try:
- uid = username
- conn.add("uid={uid},{customer_dn}".format(
- uid=uid, customer_dn=settings.LDAP_CUSTOMER_DN
- ),
- ["inetOrgPerson", "posixAccount", "ldapPublickey"],
- {
- "uid": [uid],
- "sn": [lastname.encode("utf-8")],
- "givenName": [firstname.encode("utf-8")],
- "cn": [uid],
- "displayName": ["{} {}".format(firstname, lastname).encode("utf-8")],
- "uidNumber": [str(uidNumber)],
- "gidNumber": [str(settings.LDAP_CUSTOMER_GROUP_ID)],
- "loginShell": ["/bin/bash"],
- "homeDirectory": ["/home/{}".format(unicodedata.normalize('NFKD', username).encode('ascii','ignore'))],
- "mail": email.encode("utf-8"),
- "userPassword": [self._ssha_password(password.encode("utf-8"))]
- }
- )
- logger.debug('Created user %s %s' % (username.encode('utf-8'),
- uidNumber))
- except Exception as ex:
- logger.debug('Could not create user %s' % username.encode('utf-8'))
- logger.error("Exception: " + str(ex))
- raise
- finally:
- conn.unbind()
-
-
- def change_password(self, entry_dn, new_password):
- """
- Changes the password of the user identified by user_dn
-
- :param entry_dn: str The dn that identifies the user
- :param new_password: str The new password string
- :return: True if password was changed successfully False otherwise
- """
- conn = self.get_admin_conn()
- return_val = False
- try:
- return_val = conn.modify(
- entry_dn,
- {
- "userpassword": (
- ldap3.MODIFY_REPLACE,
- [self._ssha_password(new_password.encode("utf-8"))]
- )
- }
- )
- except Exception as ex:
- logger.error("Exception: " + str(ex))
-
- conn.unbind()
- return return_val
-
- def change_user_details(self, uid, details):
- """
- Updates the user details as per given values in kwargs of the user
- identified by user_dn.
-
- Assumes that all attributes passed in kwargs are valid.
-
- :param uid: str The uid that identifies the user
- :param details: dict A dictionary containing the new values
- :return: True if user details were updated successfully False otherwise
- """
- conn = self.get_admin_conn()
-
- # Make sure the user exists first to change his/her details
- user_exists, entries = self.check_user_exists(
- uid=uid,
- search_base=settings.LDAP_CUSTOMER_DN
- )
-
- return_val = False
- if user_exists:
- details_dict = {k: (ldap3.MODIFY_REPLACE, [v.encode("utf-8")]) for
- k, v in details.items()}
- try:
- return_val = conn.modify(entries[0].entry_dn, details_dict)
- msg = "success"
- except Exception as ex:
- msg = str(ex)
- logger.error("Exception: " + msg)
- finally:
- conn.unbind()
- else:
- msg = "User {} not found".format(uid)
- logger.error(msg)
- conn.unbind()
- return return_val, msg
-
- def check_user_exists(self, uid, search_filter="", attributes=None,
- search_base=settings.LDAP_CUSTOMER_DN):
- """
- Check if the user with the given uid exists in the customer group.
-
- :param uid: str representing the user
- :param search_filter: str representing the filter condition to find
- users. If its empty, the search finds the user with
- the given uid.
- :param attributes: list A list of str representing all the attributes
- to be obtained in the result entries
- :param search_base: str
- :return: tuple (bool, [ldap3.abstract.entry.Entry ..])
- A bool indicating if the user exists
- A list of all entries obtained in the search
- """
- conn = self.get_admin_conn()
- entries = []
- try:
- result = conn.search(
- search_base=search_base,
- search_filter=search_filter if len(search_filter)> 0 else
- '(uid={uid})'.format(uid=uid),
- attributes=attributes
- )
- entries = conn.entries
- finally:
- conn.unbind()
- return result, entries
-
- def delete_user(self, uid):
- """
- Deletes the user with the given uid from ldap
-
- :param uid: str representing the user
- :return: True if the delete was successful False otherwise
- """
- conn = self.get_admin_conn()
- try:
- return_val = conn.delete(
- ("uid={uid}," + settings.LDAP_CUSTOMER_DN).format(uid=uid),
- )
- msg = "success"
- except Exception as ex:
- msg = str(ex)
- logger.error("Exception: " + msg)
- return_val = False
- finally:
- conn.unbind()
- return return_val, msg
-
- def _set_max_uid(self, max_uid):
- """
- a utility function to save max_uid value to a file
-
- :param max_uid: an integer representing the max uid
- :return:
- """
- with open(settings.LDAP_MAX_UID_FILE_PATH, 'w+') as handler:
- handler.write(str(max_uid))
-
- def _get_max_uid(self):
- """
- A utility function to read the max uid value that was previously set
-
- :return: An integer representing the max uid value that was previously
- set
- """
- try:
- with open(settings.LDAP_MAX_UID_FILE_PATH, 'r+') as handler:
- try:
- return_value = int(handler.read())
- except ValueError as ve:
- logger.error(
- "Error reading int value from {}. {}"
- "Returning default value {} instead".format(
- settings.LDAP_MAX_UID_PATH,
- str(ve),
- settings.LDAP_DEFAULT_START_UID
- )
- )
- return_value = settings.LDAP_DEFAULT_START_UID
- return return_value
- except FileNotFoundError as fnfe:
- logger.error("File not found : " + str(fnfe))
- return_value = settings.LDAP_DEFAULT_START_UID
- logger.error("So, returning UID={}".format(return_value))
- return return_value
diff --git a/uncloud_auth/views.py b/uncloud_auth/views.py
deleted file mode 100644
index 6430be9..0000000
--- a/uncloud_auth/views.py
+++ /dev/null
@@ -1,75 +0,0 @@
-from django.contrib.auth import views as auth_views
-from django.contrib.auth import logout
-
-from django_auth_ldap.backend import LDAPBackend
-from rest_framework import mixins, permissions, status, viewsets
-from rest_framework.decorators import action
-from rest_framework.response import Response
-
-from .serializers import *
-
-class LoginView(auth_views.LoginView):
- template_name = 'uncloud_auth/login.html'
-
-class LogoutView(auth_views.LogoutView):
- pass
-
-
-class UserViewSet(viewsets.GenericViewSet):
- permission_classes = [permissions.IsAuthenticated]
- serializer_class = UserSerializer
-
- def get_queryset(self):
- return self.request.user
-
- def list(self, request, format=None):
- # This is a bit stupid: we have a user, we create a queryset by
- # matching on the username. But I don't know a "nicer" way.
- # Nico, 2020-03-18
- user = request.user
- serializer = self.get_serializer(user, context = {'request': request})
- return Response(serializer.data)
-
- @action(detail=False, methods=['post'])
- def change_email(self, request):
- serializer = self.get_serializer(
- request.user, data=request.data, context={'request': request}
- )
- serializer.is_valid(raise_exception=True)
- serializer.save()
- return Response(serializer.data)
-
-
-class AccountManagementViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
- serializer_class = UserRegistrationSerializer
-
- def create(self, request, *args, **kwargs):
- serializer = self.get_serializer(data=request.data)
- serializer.is_valid(raise_exception=True)
- self.perform_create(serializer)
- headers = self.get_success_headers(serializer.data)
- return Response(
- serializer.data, status=status.HTTP_201_CREATED, headers=headers
- )
-
-
-class AdminUserViewSet(viewsets.ReadOnlyModelViewSet):
- permission_classes = [permissions.IsAdminUser]
-
- def get_serializer_class(self):
- if self.action == 'import_from_ldap':
- return ImportUserSerializer
- else:
- return UserSerializer
-
- def get_queryset(self):
- return get_user_model().objects.all()
-
- @action(detail=False, methods=['post'], url_path='import_from_ldap')
- def import_from_ldap(self, request, pk=None):
- serializer = self.get_serializer(data=request.data)
- serializer.is_valid(raise_exception=True)
- ldap_username = serializer.validated_data.pop("username")
- user = LDAPBackend().populate_user(ldap_username)
-
- return Response(UserSerializer(user, context = {'request': request}).data)
diff --git a/uncloud_net/__init__.py b/uncloud_net/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_net/admin.py b/uncloud_net/admin.py
deleted file mode 100644
index ca6aaa1..0000000
--- a/uncloud_net/admin.py
+++ /dev/null
@@ -1,7 +0,0 @@
-from django.contrib import admin
-
-from .models import *
-
-
-for m in [ ReverseDNSEntry, WireGuardVPNPool, WireGuardVPN ]:
- admin.site.register(m)
diff --git a/uncloud_net/apps.py b/uncloud_net/apps.py
deleted file mode 100644
index 489beb1..0000000
--- a/uncloud_net/apps.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class UncloudNetConfig(AppConfig):
- name = 'uncloud_net'
diff --git a/uncloud_net/forms.py b/uncloud_net/forms.py
deleted file mode 100644
index ad4e013..0000000
--- a/uncloud_net/forms.py
+++ /dev/null
@@ -1,11 +0,0 @@
-from django import forms
-
-from .models import *
-from .selectors import *
-
-class WireGuardVPNForm(forms.ModelForm):
- network_size = forms.ChoiceField(choices=allowed_vpn_network_reservation_size)
-
- class Meta:
- model = WireGuardVPN
- fields = [ "wireguard_public_key" ]
diff --git a/uncloud_net/management/commands/vpn.py b/uncloud_net/management/commands/vpn.py
deleted file mode 100644
index 9fdc80d..0000000
--- a/uncloud_net/management/commands/vpn.py
+++ /dev/null
@@ -1,44 +0,0 @@
-import sys
-from datetime import datetime
-
-from django.core.management.base import BaseCommand
-
-from django.contrib.auth import get_user_model
-
-from opennebula.models import VM as VMModel
-from uncloud_vm.models import VMHost, VMProduct, VMNetworkCard, VMDiskImageProduct, VMDiskProduct, VMCluster
-
-import logging
-log = logging.getLogger(__name__)
-
-
-
-peer_template="""
-# {username}
-[Peer]
-PublicKey = {public_key}
-AllowedIPs = {vpnnetwork}
-"""
-
-class Command(BaseCommand):
- help = 'General uncloud commands'
-
- def add_arguments(self, parser):
- parser.add_argument('--hostname',
- action='store_true',
- help='Name of this VPN Host',
- required=True)
-
- def handle(self, *args, **options):
- if options['bootstrap']:
- self.bootstrap()
-
- self.create_vpn_config(options['hostname'])
-
- def create_vpn_config(self, hostname):
- configs = []
-
- for pool in VPNPool.objects.filter(vpn_hostname=hostname):
- configs.append(pool_config)
-
- print(configs)
diff --git a/uncloud_net/migrations/0001_initial.py b/uncloud_net/migrations/0001_initial.py
deleted file mode 100644
index 6794156..0000000
--- a/uncloud_net/migrations/0001_initial.py
+++ /dev/null
@@ -1,62 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 13:42
-
-from django.conf import settings
-import django.core.validators
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- migrations.swappable_dependency(settings.AUTH_USER_MODEL),
- ]
-
- operations = [
- migrations.CreateModel(
- name='MACAdress',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ],
- ),
- migrations.CreateModel(
- name='WireGuardVPNPool',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('network', models.GenericIPAddressField(unique=True)),
- ('network_mask', models.IntegerField(validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(128)])),
- ('subnetwork_mask', models.IntegerField(validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(128)])),
- ('vpn_server_hostname', models.CharField(max_length=256)),
- ('wireguard_private_key', models.CharField(max_length=48)),
- ],
- ),
- migrations.CreateModel(
- name='WireGuardVPNFreeLeases',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('pool_index', models.IntegerField(unique=True)),
- ('vpnpool', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_net.wireguardvpnpool')),
- ],
- ),
- migrations.CreateModel(
- name='WireGuardVPN',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('pool_index', models.IntegerField(unique=True)),
- ('wireguard_public_key', models.CharField(max_length=48)),
- ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ('vpnpool', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_net.wireguardvpnpool')),
- ],
- ),
- migrations.CreateModel(
- name='ReverseDNSEntry',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('ip_address', models.GenericIPAddressField(unique=True)),
- ('name', models.CharField(max_length=253)),
- ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- ),
- ]
diff --git a/uncloud_net/migrations/0002_wireguardvpnpool_wireguard_public_key.py b/uncloud_net/migrations/0002_wireguardvpnpool_wireguard_public_key.py
deleted file mode 100644
index 479aba1..0000000
--- a/uncloud_net/migrations/0002_wireguardvpnpool_wireguard_public_key.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 17:04
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_net', '0001_initial'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='wireguardvpnpool',
- name='wireguard_public_key',
- field=models.CharField(default='', max_length=48),
- preserve_default=False,
- ),
- ]
diff --git a/uncloud_net/migrations/0003_wireguardvpnpool_wg_name.py b/uncloud_net/migrations/0003_wireguardvpnpool_wg_name.py
deleted file mode 100644
index 9ecf52c..0000000
--- a/uncloud_net/migrations/0003_wireguardvpnpool_wg_name.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 17:31
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_net', '0002_wireguardvpnpool_wireguard_public_key'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='wireguardvpnpool',
- name='wg_name',
- field=models.CharField(default='wg0', max_length=15),
- preserve_default=False,
- ),
- ]
diff --git a/uncloud_net/migrations/0004_auto_20201213_1734.py b/uncloud_net/migrations/0004_auto_20201213_1734.py
deleted file mode 100644
index 24e46e7..0000000
--- a/uncloud_net/migrations/0004_auto_20201213_1734.py
+++ /dev/null
@@ -1,17 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 17:34
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_net', '0003_wireguardvpnpool_wg_name'),
- ]
-
- operations = [
- migrations.AddConstraint(
- model_name='wireguardvpnpool',
- constraint=models.UniqueConstraint(fields=('wg_name', 'vpn_server_hostname'), name='unique_interface_name_per_host'),
- ),
- ]
diff --git a/uncloud_net/migrations/0005_auto_20201220_1837.py b/uncloud_net/migrations/0005_auto_20201220_1837.py
deleted file mode 100644
index 1dbabe6..0000000
--- a/uncloud_net/migrations/0005_auto_20201220_1837.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.1 on 2020-12-20 18:37
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_net', '0004_auto_20201213_1734'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='wireguardvpn',
- name='wireguard_public_key',
- field=models.CharField(max_length=48, unique=True),
- ),
- ]
diff --git a/uncloud_net/migrations/0006_auto_20201224_1626.py b/uncloud_net/migrations/0006_auto_20201224_1626.py
deleted file mode 100644
index c0dd2ef..0000000
--- a/uncloud_net/migrations/0006_auto_20201224_1626.py
+++ /dev/null
@@ -1,17 +0,0 @@
-# Generated by Django 3.1 on 2020-12-24 16:26
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_net', '0005_auto_20201220_1837'),
- ]
-
- operations = [
- migrations.AddConstraint(
- model_name='wireguardvpn',
- constraint=models.UniqueConstraint(fields=('vpnpool', 'wireguard_public_key'), name='wg_key_unique_per_pool'),
- ),
- ]
diff --git a/uncloud_net/migrations/__init__.py b/uncloud_net/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_net/models.py b/uncloud_net/models.py
deleted file mode 100644
index 9865a08..0000000
--- a/uncloud_net/models.py
+++ /dev/null
@@ -1,208 +0,0 @@
-import uuid
-import ipaddress
-
-from django.db import models
-from django.contrib.auth import get_user_model
-from django.core.validators import MinValueValidator, MaxValueValidator
-from django.core.exceptions import FieldError, ValidationError
-
-from uncloud_pay.models import Order, Product
-
-class WireGuardVPNPool(models.Model):
- """
- Network address pools from which VPNs can be created
- """
-
- class Meta:
- constraints = [
- models.UniqueConstraint(fields=['wg_name', 'vpn_server_hostname' ],
- name='unique_interface_name_per_host')
- ]
-
-
- # Linux interface naming is restricing to max 15 characters
- wg_name = models.CharField(max_length=15)
-
- network = models.GenericIPAddressField(unique=True)
- network_mask = models.IntegerField(validators=[MinValueValidator(0),
- MaxValueValidator(128)])
-
- subnetwork_mask = models.IntegerField(validators=[
- MinValueValidator(0),
- MaxValueValidator(128)
- ])
-
- vpn_server_hostname = models.CharField(max_length=256)
- wireguard_private_key = models.CharField(max_length=48)
- wireguard_public_key = models.CharField(max_length=48)
-
- @property
- def max_pool_index(self):
- """
- Return the highest possible network / last network id
- """
-
- bits = self.subnetwork_mask - self.network_mask
-
- return (2**bits)-1
-
- @property
- def ip_network(self):
- """
- Return the IP network based on our address and mask
- """
- return ipaddress.ip_network(f"{self.network}/{self.network_mask}")
-
- def __str__(self):
- return f"{self.ip_network} (subnets: /{self.subnetwork_mask})"
-
- @property
- def wireguard_config(self):
- wireguard_config = [ f"[Interface]\nListenPort = 51820\nPrivateKey = {self.wireguard_private_key}\n" ]
-
- peers = []
-
- for vpn in self.wireguardvpn_set.all():
- public_key = vpn.wireguard_public_key
- peer_network = f"{vpn.address}/{self.subnetwork_mask}"
- owner = vpn.owner
-
- peers.append(f"# Owner: {owner}\n[Peer]\nPublicKey = {public_key}\nAllowedIPs = {peer_network}\n\n")
-
- wireguard_config.extend(peers)
-
- return "\n".join(wireguard_config)
-
-
-class WireGuardVPN(models.Model):
- """
- Created VPNNetworks
- """
- owner = models.ForeignKey(get_user_model(),
- on_delete=models.CASCADE)
- vpnpool = models.ForeignKey(WireGuardVPNPool,
- on_delete=models.CASCADE)
-
- pool_index = models.IntegerField(unique=True)
-
- wireguard_public_key = models.CharField(max_length=48, unique=True)
-
- class Meta:
- constraints = [
- models.UniqueConstraint(fields=['vpnpool', 'wireguard_public_key'],
- name='wg_key_unique_per_pool')
- ]
-
-
- @property
- def network_mask(self):
- return self.vpnpool.subnetwork_mask
-
- @property
- def vpn_server(self):
- return self.vpnpool.vpn_server_hostname
-
- @property
- def vpn_server_public_key(self):
- return self.vpnpool.wireguard_public_key
-
- @property
- def address(self):
- """
- Locate the correct subnet in the supernet
-
- First get the network itself
-
- """
-
- net = self.vpnpool.ip_network
- subnet = net[(2**(128-self.vpnpool.subnetwork_mask)) * self.pool_index]
-
- return str(subnet)
-
- def __str__(self):
- return f"{self.address} ({self.pool_index})"
-
- def create_product(self):
- """
- Ensure we have a product for the WireguardVPN
- """
-
- pass
-
- # Product.objects.get_or_create(
- # name="WireGuardVPN",
- # description="Wireguard VPN",
- # currency=Currency.CHF,
- # config=
-
-
-class WireGuardVPNFreeLeases(models.Model):
- """
- Previously used VPNNetworks
- """
- vpnpool = models.ForeignKey(WireGuardVPNPool,
- on_delete=models.CASCADE)
-
- pool_index = models.IntegerField(unique=True)
-
-################################################################################
-
-class MACAdress(models.Model):
- default_prefix = 0x420000000000
-
-
-class ReverseDNSEntry(models.Model):
- """
- A reverse DNS entry
- """
- owner = models.ForeignKey(get_user_model(),
- on_delete=models.CASCADE)
-
- ip_address = models.GenericIPAddressField(null=False, unique=True)
-
- name = models.CharField(max_length=253, null=False)
-
- @property
- def reverse_pointer(self):
- return ipaddress.ip_address(self.ip_address).reverse_pointer
-
- def implement(self):
- """
- The implement function implements the change
- """
-
- # Get all DNS entries (?) / update this DNS entry
- # convert to DNS name
- #
- pass
-
-
- def save(self, *args, **kwargs):
- # Product.objects.filter(config__parameters__contains='reverse_dns_network')
- # FIXME: check if order is still active / not replaced
-
- allowed = False
- product = None
-
- for order in Order.objects.filter(config__parameters__reverse_dns_network__isnull=False,
- owner=self.owner):
- network = order.config['parameters']['reverse_dns_network']
-
- net = ipaddress.ip_network(network)
- addr = ipaddress.ip_address(self.ip_address)
-
- if addr in net:
- allowed = True
- product = order.product
- break
-
-
- if not allowed:
- raise ValidationError(f"User {self.owner} does not have the right to create reverse DNS entry for {self.ip_address}")
-
- super().save(*args, **kwargs)
-
-
- def __str__(self):
- return f"{self.ip_address} - {self.name}"
diff --git a/uncloud_net/selectors.py b/uncloud_net/selectors.py
deleted file mode 100644
index 6e12e8b..0000000
--- a/uncloud_net/selectors.py
+++ /dev/null
@@ -1,43 +0,0 @@
-from django.db import transaction
-from django.db.models import Count, F
-from .models import *
-
-def get_suitable_pools(subnetwork_mask):
- """
- Find suitable pools for a certain network size.
-
- First, filter for all pools that offer the requested subnetwork_size.
-
- Then find those pools that are not fully exhausted:
-
- The number of available networks in a pool is 2^(subnetwork_size-network_size.
-
- The number of available networks in a pool is given by the number of VPNNetworkreservations.
-
- """
-
- return WireGuardVPNPool.objects.annotate(
- num_reservations=Count('wireguardvpn'),
- max_reservations=2**(F('subnetwork_mask')-F('network_mask'))).filter(
- num_reservations__lt=F('max_reservations'),
- subnetwork_mask=subnetwork_mask)
-
-
-def allowed_vpn_network_reservation_size():
- """
- Find all possible sizes of subnetworks that are available.
-
- Select all pools with free networks.
-
- Get their subnetwork sizes, reduce to a set
-
- """
-
- pools = WireGuardVPNPool.objects.annotate(num_reservations=Count('wireguardvpn'),
- max_reservations=2**(F('subnetwork_mask')-F('network_mask'))).filter(
- num_reservations__lt=F('max_reservations'))
-
- # Need to return set of tuples, see
- # https://docs.djangoproject.com/en/3.1/ref/models/fields/#field-choices
-# return set([ (pool.subnetwork_mask, pool.subnetwork_mask) for pool in pools ])
- return set([pool.subnetwork_mask for pool in pools ])
diff --git a/uncloud_net/serializers.py b/uncloud_net/serializers.py
deleted file mode 100644
index 09baa59..0000000
--- a/uncloud_net/serializers.py
+++ /dev/null
@@ -1,57 +0,0 @@
-import base64
-
-from django.contrib.auth import get_user_model
-from django.utils.translation import gettext_lazy as _
-from rest_framework import serializers
-
-from .models import *
-from .services import *
-from .selectors import *
-
-
-class WireGuardVPNSerializer(serializers.ModelSerializer):
- address = serializers.CharField(read_only=True)
- vpn_server = serializers.CharField(read_only=True)
- vpn_server_public_key = serializers.CharField(read_only=True)
- network_mask = serializers.IntegerField()
-
- class Meta:
- model = WireGuardVPN
- fields = [ 'wireguard_public_key', 'address', 'network_mask', 'vpn_server',
- 'vpn_server_public_key' ]
-
- extra_kwargs = {
- 'network_mask': {'write_only': True }
- }
-
-
- def validate_network_mask(self, value):
- msg = _(f"No pool for network size {value}")
- sizes = allowed_vpn_network_reservation_size()
-
- if not value in sizes:
- raise serializers.ValidationError(msg)
-
- return value
-
- def validate_wireguard_public_key(self, value):
- msg = _("Supplied key is not a valid wireguard public key")
-
- """
- Verify wireguard key.
- See https://lists.zx2c4.com/pipermail/wireguard/2020-December/006221.html
- """
-
- try:
- decoded_key = base64.standard_b64decode(value)
- except Exception as e:
- raise serializers.ValidationError(msg)
-
- if not len(decoded_key) == 32:
- raise serializers.ValidationError(msg)
-
- return value
-
-
-class WireGuardVPNSizesSerializer(serializers.Serializer):
- size = serializers.IntegerField(min_value=0, max_value=128)
diff --git a/uncloud_net/services.py b/uncloud_net/services.py
deleted file mode 100644
index 8449394..0000000
--- a/uncloud_net/services.py
+++ /dev/null
@@ -1,65 +0,0 @@
-from django.db import transaction
-
-from .models import *
-from .selectors import *
-from .tasks import *
-from django_q.tasks import async_task, result
-
-@transaction.atomic
-def create_wireguard_vpn(owner, public_key, network_mask):
- # Check if the user has a membership.
- #------------------------------------
- # If yes, user is eligible for API access and 2 VPNs
- # If user already has 2 VPNs, we deduct from the credit
- # If deduction is higher than the allowed credit, we fail
-
-
- #
- # Check if the user has suitable balance
- # Create order
- #
- return create_wireguard_vpn_tech(owner, public_key, network_mask)
-
-@transaction.atomic
-def create_wireguard_vpn_tech(owner, public_key, network_mask):
- pool = get_suitable_pools(network_mask)[0]
- count = pool.wireguardvpn_set.count()
-
- # Try re-using previously used networks first
- try:
- free_lease = WireGuardVPNFreeLeases.objects.get(vpnpool=pool)
-
- vpn = WireGuardVPN.objects.create(owner=owner,
- vpnpool=pool,
- pool_index=free_lease.pool_index,
- wireguard_public_key=public_key)
-
- free_lease.delete()
-
- except WireGuardVPNFreeLeases.DoesNotExist:
- # First object
- if count == 0:
- vpn = WireGuardVPN.objects.create(owner=owner,
- vpnpool=pool,
- pool_index=0,
- wireguard_public_key=public_key)
-
- else: # Select last network and try +1 it
- last_net = WireGuardVPN.objects.filter(vpnpool=pool).order_by('pool_index').last()
-
- next_index = last_net.pool_index + 1
-
- if next_index <= pool.max_pool_index:
- vpn = WireGuardVPN.objects.create(owner=owner,
- vpnpool=pool,
- pool_index=next_index,
- wireguard_public_key=public_key)
-
-
- config = pool.wireguard_config
- server = pool.vpn_server_hostname
- wg_name = pool.wg_name
-
- async_task(configure_wireguard_server_on_host, (wg_name, config), queue=server)
-
- return vpn
diff --git a/uncloud_net/tasks.py b/uncloud_net/tasks.py
deleted file mode 100644
index 5684871..0000000
--- a/uncloud_net/tasks.py
+++ /dev/null
@@ -1,74 +0,0 @@
-from .models import *
-
-import os
-import subprocess
-import logging
-import uuid
-from django_q.tasks import async_task, result
-
-log = logging.getLogger(__name__)
-
-
-def configure_wireguard_server_on_host(wg_name, config):
- """
- - Create wireguard config (DB query -> string)
- - Submit config to cdist worker
- - Change config locally on worker / commit / shared
- """
-
- # Write config
- fname = f"/etc/wireguard/{wg_name}.conf"
- with open(fname, "w") as fd:
- fd.write(config)
-
- # Ensure the device exists
- subprocess.run(f"ip link show {wg_name} >/dev/null || sudo ip link add {{wg_name}} type wireguard",
- shell=True, check=True)
-
- # Ensure the config is correct
- subprocess.run(f"sudo wg setconf {wg_name} {fname}",
- shell=True, check=True)
-
-
-
-def configure_wireguard_server_via_cdist(wireguardvpnpool):
- """
- - Create wireguard config (DB query -> string)
- - Submit config to cdist worker
- - Change config locally on worker / commit / shared
-
- """
-
- config = wireguardvpnpool.wireguard_config
- server = wireguardvpnpool.vpn_server_hostname
-
- log.info(f"Configuring VPN server {server} (async)")
-
- async_task(cdist_configure_wireguard_server,config, server).id
-
-
-def cdist_configure_wireguard_server(config, server):
- """
- Create config and configure server.
-
- To be executed on the cdist worker.
- """
-
- dirname= "/home/app/.cdist/type/__ungleich_wireguard/files/"
- fname = os.path.join(dirname,server)
-
- log.info(f"Configuring VPN server {server} (on cdist host)")
- with open(fname, "w") as fd:
- fd.write(config)
-
- log.debug("git committing wireguard changes")
- subprocess.run(f"cd {dirname} && git pull && git add {server} && git commit -m 'Updating config for {server}' && git push",
- shell=True, check=True)
-
- log.debug(f"Configuring VPN server {server} with cdist")
- subprocess.run(f"cdist config {server}", shell=True, check=True)
-
- # FIXME:
- # ensure logs are on the server
- # ensure exit codes are known
- return True
diff --git a/uncloud_net/templates/uncloud_net/wireguardvpn_form.html b/uncloud_net/templates/uncloud_net/wireguardvpn_form.html
deleted file mode 100644
index 1463f41..0000000
--- a/uncloud_net/templates/uncloud_net/wireguardvpn_form.html
+++ /dev/null
@@ -1,25 +0,0 @@
-{% extends 'uncloud/base.html' %}
-
-{% block body %}
-
-
-
-
- Create a VPN Network
-
- Create a new wireguard based VPN network.
-
-
-
-
-
-
-
-
-
-
-{% endblock %}
diff --git a/uncloud_net/tests.py b/uncloud_net/tests.py
deleted file mode 100644
index 75bdafa..0000000
--- a/uncloud_net/tests.py
+++ /dev/null
@@ -1,53 +0,0 @@
-from django.test import TestCase
-from rest_framework.test import APIRequestFactory, force_authenticate
-
-from rest_framework.reverse import reverse
-from django.contrib.auth import get_user_model
-from django.core.exceptions import ValidationError, FieldError
-
-from .views import *
-from .models import *
-
-from uncloud_pay.models import BillingAddress, Order
-from uncloud.models import UncloudNetwork
-
-class UncloudNetworkTests(TestCase):
- def test_invalid_IPv4_network(self):
- with self.assertRaises(FieldError):
- UncloudNetwork.objects.create(network_address="192.168.1.0",
- network_mask=33)
-
-class VPNTests(TestCase):
- def setUp(self):
- self.user = get_user_model().objects.create_user('django-test-user', 'noreply@ungleich.ch')
- self.admin_user = get_user_model().objects.create_user('django-test-adminuser',
- 'noreply-admin@ungleich.ch')
-
-
-
- self.admin_user.is_staff = True
- self.admin_user.save()
-
- self.pool_network = '2001:db8::'
- self.pool_network2 = '2001:db8:1::'
- self.pool_network_size = '48'
- self.pool_subnetwork_size = '64'
- self.pool_vpn_hostname = 'vpn.example.org'
- self.pool_wireguard_private_key = 'MOz8kk0m4jhNtAXlge0qzexZh1MipIhu4HJwtdvZ2EY='
-
- self.vpn_wireguard_public_key = 'B2b78eWBIXPMM1x4DDjkCDZepS0qDgcLN3T3PjcgXkY='
-
- self.vpnpool = WireGuardVPNPool.objects.get_or_create(network=self.pool_network,
- network_size=self.pool_network_size,
- subnetwork_size=self.pool_subnetwork_size,
- vpn_hostname=self.pool_vpn_hostname,
- wireguard_private_key=self.pool_wireguard_private_key
- )
-
- self.factory = APIRequestFactory()
-
-
-
- def tearDown(self):
- self.user.delete()
- self.admin_user.delete()
diff --git a/uncloud_net/views.py b/uncloud_net/views.py
deleted file mode 100644
index 8e7e81b..0000000
--- a/uncloud_net/views.py
+++ /dev/null
@@ -1,63 +0,0 @@
-from django.views.generic.edit import CreateView
-from django.contrib.auth.mixins import LoginRequiredMixin
-from django.contrib.messages.views import SuccessMessageMixin
-from rest_framework.response import Response
-
-from django.shortcuts import render
-
-from rest_framework import viewsets, permissions
-
-from .models import *
-from .serializers import *
-from .selectors import *
-from .services import *
-from .forms import *
-from .tasks import *
-
-class WireGuardVPNViewSet(viewsets.ModelViewSet):
- serializer_class = WireGuardVPNSerializer
- permission_classes = [permissions.IsAuthenticated]
-
- def get_queryset(self):
- if self.request.user.is_superuser:
- obj = WireGuardVPN.objects.all()
- else:
- obj = WireGuardVPN.objects.filter(owner=self.request.user)
-
- return obj
-
- def create(self, request):
- serializer = self.get_serializer(data=request.data)
- serializer.is_valid(raise_exception=True)
-
- vpn = create_wireguard_vpn(
- owner=self.request.user,
- public_key=serializer.validated_data['wireguard_public_key'],
- network_mask=serializer.validated_data['network_mask']
- )
-
- return Response(WireGuardVPNSerializer(vpn).data)
-
-
-class WireGuardVPNCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
- model = WireGuardVPN
-
- login_url = '/login/'
- success_url = '/'
- success_message = "%(network) was created successfully"
-
- form_class = WireGuardVPNForm
-
- def get_success_message(self, cleaned_data):
- return self.success_message % dict(cleaned_data,
- the_prefix = self.object.prefix)
-
-class WireGuardVPNSizes(viewsets.ViewSet):
- def list(self, request):
- sizes = allowed_vpn_network_reservation_size()
- print(sizes)
-
- sizes = [ { 'size': size } for size in sizes ]
- print(sizes)
-
- return Response(WireGuardVPNSizesSerializer(sizes, many=True).data)
diff --git a/uncloud_pay/__init__.py b/uncloud_pay/__init__.py
deleted file mode 100644
index 8b13789..0000000
--- a/uncloud_pay/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/uncloud_pay/admin.py b/uncloud_pay/admin.py
deleted file mode 100644
index 2b07400..0000000
--- a/uncloud_pay/admin.py
+++ /dev/null
@@ -1,102 +0,0 @@
-from django.contrib import admin
-from django.template.response import TemplateResponse
-from django.urls import path
-from django.shortcuts import render
-
-from hardcopy import bytestring_to_pdf
-from django.core.files.temp import NamedTemporaryFile
-from django.http import FileResponse
-from django.template.loader import render_to_string
-
-from uncloud_pay.models import *
-
-class BillRecordInline(admin.TabularInline):
- model = BillRecord
-
-class RecurringPeriodInline(admin.TabularInline):
- model = ProductToRecurringPeriod
-
-class ProductAdmin(admin.ModelAdmin):
- inlines = [ RecurringPeriodInline ]
-
-# class BillAdmin(admin.ModelAdmin):
-# inlines = [ BillRecordInline ]
-
-# def get_urls(self):
-# """
-# Create URLs for PDF view
-# """
-
-# info = "%s_%s" % (self.model._meta.app_label, self.model._meta.model_name)
-# pat = lambda regex, fn: url(regex, self.admin_site.admin_view(fn), name='%s_%s' % (info, fn.__name__))
-
-# url_patterns = [
-# pat(r'^([0-9]+)/as_pdf/$', self.as_pdf),
-# pat(r'^([0-9]+)/as_html/$', self.as_html),
-# ] + super().get_urls()
-
-# return url_patterns
-
-# def as_pdf(self, request, object_id):
-# bill = self.get_object(request, object_id=object_id)
-# print(bill)
-
-# if bill is None:
-# raise self._get_404_exception(object_id)
-
-# output_file = NamedTemporaryFile()
-# bill_html = render_to_string(
-# "uncloud_pay/bill.html.j2",
-# {
-# 'bill': bill,
-# 'bill_records': bill.bill_records.all()
-# }
-# )
-
-# bytestring_to_pdf(bill_html.encode('utf-8'), output_file)
-# response = FileResponse(output_file, content_type="application/pdf")
-# response['Content-Disposition'] = f'filename="bill_{bill}.pdf"'
-
-# return response
-
-# def as_html(self, request, object_id):
-# bill = self.get_object(request, object_id=object_id)
-
-# if bill is None:
-# raise self._get_404_exception(object_id)
-
-# return render(request, 'uncloud_pay/bill.html.j2',
-# {'bill': bill,
-# 'bill_records': bill.bill_records.all()
-# })
-
-
-# bill_html = render_to_string("bill.html.j2", {'bill': bill,
-# 'bill_records': bill.bill_records.all()
-# })
-
-# bytestring_to_pdf(bill_html.encode('utf-8'), output_file)
-# response = FileResponse(output_file, content_type="application/pdf")
-
-# response['Content-Disposition'] = f'filename="bill_{bill}.pdf"'
-
-# return HttpResponse(template.render(context, request))
-# return response
-
-
-#admin.site.register(Bill, BillAdmin)
-admin.site.register(Product, ProductAdmin)
-
-for m in [
- BillingAddress,
- Order,
- BillRecord,
- Payment,
- ProductToRecurringPeriod,
- RecurringPeriod,
- StripeCreditCard,
- StripeCustomer,
- PricingPlan,
- VATRate
-]:
- admin.site.register(m)
diff --git a/uncloud_pay/apps.py b/uncloud_pay/apps.py
deleted file mode 100644
index 051ffb4..0000000
--- a/uncloud_pay/apps.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class UncloudPayConfig(AppConfig):
- name = 'uncloud_pay'
diff --git a/uncloud_pay/forms.py b/uncloud_pay/forms.py
deleted file mode 100644
index a98c00f..0000000
--- a/uncloud_pay/forms.py
+++ /dev/null
@@ -1,8 +0,0 @@
-from uncloud.forms import MainModelForm
-from .models import BillingAddress
-
-class BillingAddressForm(MainModelForm):
- class Meta:
- model = BillingAddress
- fields = ['full_name', 'street',
- 'city', 'postal_code', 'country', 'vat_number', 'active', 'owner']
\ No newline at end of file
diff --git a/uncloud_pay/helpers.py b/uncloud_pay/helpers.py
deleted file mode 100644
index f791564..0000000
--- a/uncloud_pay/helpers.py
+++ /dev/null
@@ -1,26 +0,0 @@
-from functools import reduce
-from datetime import datetime
-from rest_framework import mixins
-from rest_framework.viewsets import GenericViewSet
-from django.utils import timezone
-from calendar import monthrange
-
-def beginning_of_month(year, month):
- tz = timezone.get_current_timezone()
- return datetime(year=year, month=month, day=1, tzinfo=tz)
-
-def end_of_month(year, month):
- (_, days) = monthrange(year, month)
- tz = timezone.get_current_timezone()
- return datetime(year=year, month=month, day=days,
- hour=23, minute=59, second=59, tzinfo=tz)
-
-class ProductViewSet(mixins.CreateModelMixin,
- mixins.RetrieveModelMixin,
- mixins.ListModelMixin,
- GenericViewSet):
- """
- A customer-facing viewset that provides default `create()`, `retrieve()`
- and `list()`.
- """
- pass
diff --git a/uncloud_pay/management/commands/.gitignore b/uncloud_pay/management/commands/.gitignore
deleted file mode 100644
index cf5c7fa..0000000
--- a/uncloud_pay/management/commands/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-# Customer tests
-customer-*.py
diff --git a/uncloud_pay/management/commands/add-opennebula-vm-orders.py b/uncloud_pay/management/commands/add-opennebula-vm-orders.py
deleted file mode 100644
index 1d66790..0000000
--- a/uncloud_pay/management/commands/add-opennebula-vm-orders.py
+++ /dev/null
@@ -1,140 +0,0 @@
-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 *
-from uncloud_vm.models import *
-
-import sys
-
-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
- }
- )
-
- # 25206 + SSD
- vm25206 = VMProduct.objects.create(name="one-25206", cores=1, ram_in_gb=4, owner=user)
- vm25206.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
-
- # vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206, owner=user, size_in_gb=30)
- # vm25206_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
-
- # change 1
- vm25206.cores = 2
- vm25206.ram_in_gb = 8
- vm25206.save()
- vm25206.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))
-
- sys.exit(0)
-
- # change 2
- # vm25206_ssd.size_in_gb = 50
- # vm25206_ssd.save()
- # vm25206_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
-
- # 25206 done.
-
- # 25615
- vm25615 = VMProduct.objects.create(name="one-25615", cores=1, ram_in_gb=4, owner=user)
- vm25615.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
-
- # Change 2020-04-17
- vm25615.cores = 2
- vm25615.ram_in_gb = 8
- vm25615.save()
- vm25615.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))
-
- # vm25615_ssd = VMDiskProduct(vm=vm25615, owner=user, size_in_gb=30)
- # vm25615_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
- # vm25615_ssd.save()
-
- vm25208 = VMProduct.objects.create(name="one-25208", cores=1, ram_in_gb=4, owner=user)
- vm25208.create_order_at(timezone.make_aware(datetime.datetime(2020,3,5)))
-
- vm25208.cores = 2
- vm25208.ram_in_gb = 8
- vm25208.save()
- vm25208.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))
-
- Bill.create_next_bills_for_user(user, ending_date=end_of_month(timezone.make_aware(datetime.datetime(2020,7,31))))
-
- sys.exit(0)
-
-
- vm25615_ssd.size_in_gb = 50
- vm25615_ssd.save()
- vm25615_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
-
-
-
- vm25208_ssd = VMDiskProduct.objects.create(vm=vm25208,
- owner=user,
- size_in_gb=30)
-
-
-
- vm25208_ssd.size_in_gb = 50
- vm25208_ssd.save()
- vm25208_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
-
-
- # 25207
- vm25207 = VMProduct.objects.create(name="OpenNebula 25207",
- cores=1,
- ram_in_gb=4,
- owner=user)
-
- vm25207_ssd = VMDiskProduct.objects.create(vm=vm25207,
- owner=user,
- size_in_gb=30)
-
- vm25207_ssd.size_in_gb = 50
- vm25207_ssd.save()
- vm25207_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
-
-
- vm25207.cores = 2
- vm25207.ram_in_gb = 8
- vm25207.save()
- vm25207.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,6,19)))
-
-
- # FIXES: check starting times (they are slightly different)
- # add vm 25236
diff --git a/uncloud_pay/management/commands/bootstrap-user.py b/uncloud_pay/management/commands/bootstrap-user.py
deleted file mode 100644
index b78e80c..0000000
--- a/uncloud_pay/management/commands/bootstrap-user.py
+++ /dev/null
@@ -1,40 +0,0 @@
-from django.core.management.base import BaseCommand
-from django.contrib.auth import get_user_model
-import datetime
-
-from uncloud_pay.models import *
-
-class Command(BaseCommand):
- help = 'Bootstrap user (for testing)'
-
- 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 = BillingAddress.objects.get_or_create(
- owner=user,
- active=True,
- defaults={'organization': 'ungleich',
- 'name': 'Nico Schottelius',
- 'street': 'Hauptstrasse 14',
- 'city': 'Luchsingen',
- 'postal_code': '8775',
- 'country': 'CH' }
- )
-
-
- bills = Bill.objects.filter(owner=user)
-
- # not even one bill? create!
- if bills:
- bill = bills[0]
- else:
- bill = Bill.objects.create(owner=user)
-
- # find any order that is associated to this bill
- orders = Order.objects.filter(owner=user)
-
- print(f"Addr: {addr}")
- print(f"Bill: {bill}")
diff --git a/uncloud_pay/management/commands/charge-negative-balance.py b/uncloud_pay/management/commands/charge-negative-balance.py
deleted file mode 100644
index 8405bd3..0000000
--- a/uncloud_pay/management/commands/charge-negative-balance.py
+++ /dev/null
@@ -1,28 +0,0 @@
-from django.core.management.base import BaseCommand
-from uncloud_auth.models import User
-from uncloud_pay.models import Order, Bill, get_balance_for_user
-import uncloud_pay.stripe as uncloud_stripe
-
-from datetime import timedelta
-from django.utils import timezone
-
-class Command(BaseCommand):
- help = 'Generate bills and charge customers if necessary.'
-
- def add_arguments(self, parser):
- pass
-
- def handle(self, *args, **options):
- users = User.objects.all()
- print("Processing {} users.".format(users.count()))
- for user in users:
- balance = get_balance_for_user(user)
- if balance < 0:
- print("User {} has negative balance ({}), charging.".format(user.username, balance))
- amount_to_be_charged = abs(balance)
- result = uncloud_stripe.charge_customer(user, amount_to_be_charged)
- if result.status != 'succeeded':
- print("ERR: charging {} with method {} failed"
- .format(user.username, result)
- )
- print("=> Done.")
diff --git a/uncloud_pay/management/commands/generate-bills.py b/uncloud_pay/management/commands/generate-bills.py
deleted file mode 100644
index 5bd4519..0000000
--- a/uncloud_pay/management/commands/generate-bills.py
+++ /dev/null
@@ -1,35 +0,0 @@
-import logging
-
-from django.core.management.base import BaseCommand
-from uncloud_auth.models import User
-from uncloud_pay.models import Order, Bill
-from django.core.exceptions import ObjectDoesNotExist
-
-from datetime import timedelta, date
-from django.utils import timezone
-from uncloud_pay.models import Bill
-
-logger = logging.getLogger(__name__)
-
-class Command(BaseCommand):
- help = 'Generate bills and charge customers if necessary.'
-
- def add_arguments(self, parser):
- pass
-
- # TODO: use logger.*
- def handle(self, *args, **options):
- # Iterate over all 'active' users.
- # TODO: filter out inactive users.
- users = User.objects.all()
- print("Processing {} users.".format(users.count()))
-
- for user in users:
- now = timezone.now()
- Bill.generate_for(
- year=now.year,
- month=now.month,
- user=user)
-
- # We're done for this round :-)
- print("=> Done.")
diff --git a/uncloud_pay/management/commands/handle-overdue-bills.py b/uncloud_pay/management/commands/handle-overdue-bills.py
deleted file mode 100644
index 595fbc2..0000000
--- a/uncloud_pay/management/commands/handle-overdue-bills.py
+++ /dev/null
@@ -1,23 +0,0 @@
-from django.core.management.base import BaseCommand
-from uncloud_auth.models import User
-from uncloud_pay.models import Bill
-
-from datetime import timedelta
-from django.utils import timezone
-
-class Command(BaseCommand):
- help = 'Take action on overdue bills.'
-
- def add_arguments(self, parser):
- pass
-
- def handle(self, *args, **options):
- users = User.objects.all()
- print("Processing {} users.".format(users.count()))
- for user in users:
- for bill in Bill.get_overdue_for(user):
- print("/!\ Overdue bill for {}, {} with amount {}"
- .format(user.username, bill.uuid, bill.amount))
- # TODO: take action?
-
- print("=> Done.")
diff --git a/uncloud_pay/management/commands/import-vat-rates.py b/uncloud_pay/management/commands/import-vat-rates.py
deleted file mode 100644
index a741740..0000000
--- a/uncloud_pay/management/commands/import-vat-rates.py
+++ /dev/null
@@ -1,50 +0,0 @@
-from django.core.management.base import BaseCommand
-from uncloud_pay.models import VATRate
-
-import logging
-import urllib
-import csv
-import sys
-import io
-
-logger = logging.getLogger(__name__)
-
-class Command(BaseCommand):
- help = '''Imports VAT Rates. Assume vat rates of format https://github.com/kdeldycke/vat-rates/blob/master/vat_rates.csv'''
- vat_url = "https://raw.githubusercontent.com/ungleich/vat-rates/main/vat_rates.csv"
-
-
- def add_arguments(self, parser):
- parser.add_argument('--vat-url', default=self.vat_url)
-
- def handle(self, *args, **options):
- vat_url = options['vat_url']
- url_open = urllib.request.urlopen(vat_url)
-
- # map to fileio using stringIO
- csv_file = io.StringIO(url_open.read().decode('utf-8'))
- reader = csv.DictReader(csv_file)
-
- for row in reader:
- if row["territory_codes"] and len(row["territory_codes"].splitlines()) > 1:
- for code in row["territory_codes"].splitlines():
- VATRate.objects.get_or_create(
- starting_date=row["start_date"],
- ending_date=row["stop_date"] if row["stop_date"] != "" else None,
- territory_codes=code,
- currency_code=row["currency_code"],
- rate=row["rate"],
- rate_type=row["rate_type"],
- description=row["description"]
- )
- else:
- VATRate.objects.get_or_create(
- starting_date=row["start_date"],
- ending_date=row["stop_date"] if row["stop_date"] != "" else None,
- territory_codes=row["territory_codes"],
- currency_code=row["currency_code"],
- rate=row["rate"],
- rate_type=row["rate_type"],
- description=row["description"]
- )
- logger.info('All VAT Rates have been added!')
diff --git a/uncloud_pay/migrations/0001_initial.py b/uncloud_pay/migrations/0001_initial.py
deleted file mode 100644
index e65f3dd..0000000
--- a/uncloud_pay/migrations/0001_initial.py
+++ /dev/null
@@ -1,202 +0,0 @@
-# Generated by Django 3.1 on 2020-12-28 22:19
-
-from django.conf import settings
-import django.core.validators
-from django.db import migrations, models
-import django.db.models.deletion
-import django.utils.timezone
-import uncloud.models
-import uncloud_pay.models
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- migrations.swappable_dependency(settings.AUTH_USER_MODEL),
- ('uncloud_auth', '0001_initial'),
- ]
-
- operations = [
- migrations.CreateModel(
- name='Bill',
- 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(default=uncloud_pay.models.start_of_this_month)),
- ('ending_date', models.DateTimeField()),
- ('due_date', models.DateField(default=uncloud_pay.models.default_payment_delay)),
- ('is_final', models.BooleanField(default=False)),
- ],
- ),
- migrations.CreateModel(
- name='BillingAddress',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('full_name', models.CharField(max_length=256)),
- ('organization', models.CharField(blank=True, max_length=256, null=True)),
- ('street', models.CharField(max_length=256)),
- ('city', models.CharField(max_length=256)),
- ('postal_code', models.CharField(max_length=64)),
- ('country', uncloud.models.CountryField(blank=True, choices=[('AD', 'Andorra'), ('AE', 'United Arab Emirates'), ('AF', 'Afghanistan'), ('AG', 'Antigua & Barbuda'), ('AI', 'Anguilla'), ('AL', 'Albania'), ('AM', 'Armenia'), ('AN', 'Netherlands Antilles'), ('AO', 'Angola'), ('AQ', 'Antarctica'), ('AR', 'Argentina'), ('AS', 'American Samoa'), ('AT', 'Austria'), ('AU', 'Australia'), ('AW', 'Aruba'), ('AZ', 'Azerbaijan'), ('BA', 'Bosnia and Herzegovina'), ('BB', 'Barbados'), ('BD', 'Bangladesh'), ('BE', 'Belgium'), ('BF', 'Burkina Faso'), ('BG', 'Bulgaria'), ('BH', 'Bahrain'), ('BI', 'Burundi'), ('BJ', 'Benin'), ('BM', 'Bermuda'), ('BN', 'Brunei Darussalam'), ('BO', 'Bolivia'), ('BR', 'Brazil'), ('BS', 'Bahama'), ('BT', 'Bhutan'), ('BV', 'Bouvet Island'), ('BW', 'Botswana'), ('BY', 'Belarus'), ('BZ', 'Belize'), ('CA', 'Canada'), ('CC', 'Cocos (Keeling) Islands'), ('CF', 'Central African Republic'), ('CG', 'Congo'), ('CH', 'Switzerland'), ('CI', 'Ivory Coast'), ('CK', 'Cook Iislands'), ('CL', 'Chile'), ('CM', 'Cameroon'), ('CN', 'China'), ('CO', 'Colombia'), ('CR', 'Costa Rica'), ('CU', 'Cuba'), ('CV', 'Cape Verde'), ('CX', 'Christmas Island'), ('CY', 'Cyprus'), ('CZ', 'Czech Republic'), ('DE', 'Germany'), ('DJ', 'Djibouti'), ('DK', 'Denmark'), ('DM', 'Dominica'), ('DO', 'Dominican Republic'), ('DZ', 'Algeria'), ('EC', 'Ecuador'), ('EE', 'Estonia'), ('EG', 'Egypt'), ('EH', 'Western Sahara'), ('ER', 'Eritrea'), ('ES', 'Spain'), ('ET', 'Ethiopia'), ('FI', 'Finland'), ('FJ', 'Fiji'), ('FK', 'Falkland Islands (Malvinas)'), ('FM', 'Micronesia'), ('FO', 'Faroe Islands'), ('FR', 'France'), ('FX', 'France, Metropolitan'), ('GA', 'Gabon'), ('GB', 'United Kingdom (Great Britain)'), ('GD', 'Grenada'), ('GE', 'Georgia'), ('GF', 'French Guiana'), ('GH', 'Ghana'), ('GI', 'Gibraltar'), ('GL', 'Greenland'), ('GM', 'Gambia'), ('GN', 'Guinea'), ('GP', 'Guadeloupe'), ('GQ', 'Equatorial Guinea'), ('GR', 'Greece'), ('GS', 'South Georgia and the South Sandwich Islands'), ('GT', 'Guatemala'), ('GU', 'Guam'), ('GW', 'Guinea-Bissau'), ('GY', 'Guyana'), ('HK', 'Hong Kong'), ('HM', 'Heard & McDonald Islands'), ('HN', 'Honduras'), ('HR', 'Croatia'), ('HT', 'Haiti'), ('HU', 'Hungary'), ('ID', 'Indonesia'), ('IE', 'Ireland'), ('IL', 'Israel'), ('IN', 'India'), ('IO', 'British Indian Ocean Territory'), ('IQ', 'Iraq'), ('IR', 'Islamic Republic of Iran'), ('IS', 'Iceland'), ('IT', 'Italy'), ('JM', 'Jamaica'), ('JO', 'Jordan'), ('JP', 'Japan'), ('KE', 'Kenya'), ('KG', 'Kyrgyzstan'), ('KH', 'Cambodia'), ('KI', 'Kiribati'), ('KM', 'Comoros'), ('KN', 'St. Kitts and Nevis'), ('KP', "Korea, Democratic People's Republic of"), ('KR', 'Korea, Republic of'), ('KW', 'Kuwait'), ('KY', 'Cayman Islands'), ('KZ', 'Kazakhstan'), ('LA', "Lao People's Democratic Republic"), ('LB', 'Lebanon'), ('LC', 'Saint Lucia'), ('LI', 'Liechtenstein'), ('LK', 'Sri Lanka'), ('LR', 'Liberia'), ('LS', 'Lesotho'), ('LT', 'Lithuania'), ('LU', 'Luxembourg'), ('LV', 'Latvia'), ('LY', 'Libyan Arab Jamahiriya'), ('MA', 'Morocco'), ('MC', 'Monaco'), ('MD', 'Moldova, Republic of'), ('MG', 'Madagascar'), ('MH', 'Marshall Islands'), ('ML', 'Mali'), ('MN', 'Mongolia'), ('MM', 'Myanmar'), ('MO', 'Macau'), ('MP', 'Northern Mariana Islands'), ('MQ', 'Martinique'), ('MR', 'Mauritania'), ('MS', 'Monserrat'), ('MT', 'Malta'), ('MU', 'Mauritius'), ('MV', 'Maldives'), ('MW', 'Malawi'), ('MX', 'Mexico'), ('MY', 'Malaysia'), ('MZ', 'Mozambique'), ('NA', 'Namibia'), ('NC', 'New Caledonia'), ('NE', 'Niger'), ('NF', 'Norfolk Island'), ('NG', 'Nigeria'), ('NI', 'Nicaragua'), ('NL', 'Netherlands'), ('NO', 'Norway'), ('NP', 'Nepal'), ('NR', 'Nauru'), ('NU', 'Niue'), ('NZ', 'New Zealand'), ('OM', 'Oman'), ('PA', 'Panama'), ('PE', 'Peru'), ('PF', 'French Polynesia'), ('PG', 'Papua New Guinea'), ('PH', 'Philippines'), ('PK', 'Pakistan'), ('PL', 'Poland'), ('PM', 'St. Pierre & Miquelon'), ('PN', 'Pitcairn'), ('PR', 'Puerto Rico'), ('PT', 'Portugal'), ('PW', 'Palau'), ('PY', 'Paraguay'), ('QA', 'Qatar'), ('RE', 'Reunion'), ('RO', 'Romania'), ('RU', 'Russian Federation'), ('RW', 'Rwanda'), ('SA', 'Saudi Arabia'), ('SB', 'Solomon Islands'), ('SC', 'Seychelles'), ('SD', 'Sudan'), ('SE', 'Sweden'), ('SG', 'Singapore'), ('SH', 'St. Helena'), ('SI', 'Slovenia'), ('SJ', 'Svalbard & Jan Mayen Islands'), ('SK', 'Slovakia'), ('SL', 'Sierra Leone'), ('SM', 'San Marino'), ('SN', 'Senegal'), ('SO', 'Somalia'), ('SR', 'Suriname'), ('ST', 'Sao Tome & Principe'), ('SV', 'El Salvador'), ('SY', 'Syrian Arab Republic'), ('SZ', 'Swaziland'), ('TC', 'Turks & Caicos Islands'), ('TD', 'Chad'), ('TF', 'French Southern Territories'), ('TG', 'Togo'), ('TH', 'Thailand'), ('TJ', 'Tajikistan'), ('TK', 'Tokelau'), ('TM', 'Turkmenistan'), ('TN', 'Tunisia'), ('TO', 'Tonga'), ('TP', 'East Timor'), ('TR', 'Turkey'), ('TT', 'Trinidad & Tobago'), ('TV', 'Tuvalu'), ('TW', 'Taiwan, Province of China'), ('TZ', 'Tanzania, United Republic of'), ('UA', 'Ukraine'), ('UG', 'Uganda'), ('UM', 'United States Minor Outlying Islands'), ('US', 'United States of America'), ('UY', 'Uruguay'), ('UZ', 'Uzbekistan'), ('VA', 'Vatican City State (Holy See)'), ('VC', 'St. Vincent & the Grenadines'), ('VE', 'Venezuela'), ('VG', 'British Virgin Islands'), ('VI', 'United States Virgin Islands'), ('VN', 'Viet Nam'), ('VU', 'Vanuatu'), ('WF', 'Wallis & Futuna Islands'), ('WS', 'Samoa'), ('YE', 'Yemen'), ('YT', 'Mayotte'), ('YU', 'Yugoslavia'), ('ZA', 'South Africa'), ('ZM', 'Zambia'), ('ZR', 'Zaire'), ('ZW', 'Zimbabwe')], default='CH', max_length=2)),
- ('vat_number', models.CharField(blank=True, default='', max_length=100)),
- ('active', models.BooleanField(default=False)),
- ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- ),
- migrations.CreateModel(
- name='Product',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('name', models.CharField(max_length=256, unique=True)),
- ('description', models.CharField(max_length=1024)),
- ('config', models.JSONField()),
- ('currency', models.CharField(choices=[('CHF', 'Swiss Franc'), ('EUR', 'Euro'), ('USD', 'US Dollar')], default='CHF', max_length=32)),
- ],
- ),
- migrations.CreateModel(
- name='RecurringPeriod',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('name', models.CharField(max_length=100, unique=True)),
- ('duration_seconds', models.IntegerField(unique=True)),
- ],
- ),
- migrations.CreateModel(
- name='StripeCustomer',
- fields=[
- ('owner', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='uncloud_auth.user')),
- ('stripe_id', models.CharField(max_length=32)),
- ],
- ),
- migrations.CreateModel(
- name='VATRate',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('starting_date', models.DateField(blank=True, null=True)),
- ('ending_date', models.DateField(blank=True, null=True)),
- ('territory_codes', models.TextField(blank=True, default='')),
- ('currency_code', models.CharField(max_length=10)),
- ('rate', models.FloatField()),
- ('rate_type', models.TextField(blank=True, default='')),
- ('description', models.TextField(blank=True, default='')),
- ],
- ),
- migrations.CreateModel(
- name='StripeCreditCard',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('card_name', models.CharField(default='My credit card', max_length=128)),
- ('card_id', models.CharField(max_length=32)),
- ('last4', models.CharField(max_length=4)),
- ('brand', models.CharField(max_length=64)),
- ('expiry_date', models.DateField()),
- ('owner', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- ),
- migrations.CreateModel(
- name='ProductToRecurringPeriod',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('is_default', models.BooleanField(default=False)),
- ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.product')),
- ('recurring_period', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.recurringperiod')),
- ],
- ),
- migrations.AddField(
- model_name='product',
- name='recurring_periods',
- field=models.ManyToManyField(through='uncloud_pay.ProductToRecurringPeriod', to='uncloud_pay.RecurringPeriod'),
- ),
- migrations.CreateModel(
- name='PaymentMethod',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('source', models.CharField(choices=[('stripe', 'Stripe'), ('unknown', 'Unknown')], default='stripe', max_length=256)),
- ('description', models.TextField()),
- ('primary', models.BooleanField(default=False, editable=False)),
- ('stripe_payment_method_id', models.CharField(blank=True, max_length=32, null=True)),
- ('stripe_setup_intent_id', models.CharField(blank=True, max_length=32, null=True)),
- ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- ),
- migrations.CreateModel(
- name='Payment',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('amount', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
- ('source', models.CharField(choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral'), ('unknown', 'Unknown')], default='unknown', max_length=256)),
- ('timestamp', models.DateTimeField(auto_now_add=True)),
- ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- ),
- migrations.CreateModel(
- name='Order',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('description', models.TextField()),
- ('config', models.JSONField()),
- ('creation_date', models.DateTimeField(auto_now_add=True)),
- ('starting_date', models.DateTimeField(default=django.utils.timezone.now)),
- ('ending_date', models.DateTimeField(blank=True, null=True)),
- ('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)])),
- ('currency', models.CharField(choices=[('CHF', 'Swiss Franc'), ('EUR', 'Euro'), ('USD', 'US Dollar')], default='CHF', max_length=32)),
- ('should_be_billed', models.BooleanField(default=True)),
- ('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.CASCADE, related_name='parent_of', to='uncloud_pay.order')),
- ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.product')),
- ('recurring_period', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.recurringperiod')),
- ('replaces', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='replaced_by', to='uncloud_pay.order')),
- ],
- ),
- migrations.CreateModel(
- name='Membership',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('starting_date', models.DateField(blank=True, null=True)),
- ('ending_date', models.DateField(blank=True, null=True)),
- ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- ),
- migrations.CreateModel(
- name='BillRecord',
- 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()),
- ('is_recurring_record', models.BooleanField()),
- ('bill', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.bill')),
- ('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.order')),
- ],
- ),
- migrations.AddField(
- model_name='bill',
- name='billing_address',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.billingaddress'),
- ),
- migrations.AddField(
- model_name='bill',
- name='owner',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
- ),
- migrations.AddConstraint(
- model_name='producttorecurringperiod',
- constraint=models.UniqueConstraint(condition=models.Q(is_default=True), fields=('product',), name='one_default_recurring_period_per_product'),
- ),
- migrations.AddConstraint(
- model_name='producttorecurringperiod',
- constraint=models.UniqueConstraint(fields=('product', 'recurring_period'), name='recurring_period_once_per_product'),
- ),
- migrations.AddConstraint(
- 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'),
- ),
- ]
diff --git a/uncloud_pay/migrations/0002_auto_20201228_2244.py b/uncloud_pay/migrations/0002_auto_20201228_2244.py
deleted file mode 100644
index 4665553..0000000
--- a/uncloud_pay/migrations/0002_auto_20201228_2244.py
+++ /dev/null
@@ -1,24 +0,0 @@
-# Generated by Django 3.1 on 2020-12-28 22:44
-
-import django.core.validators
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0001_initial'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='payment',
- name='currency',
- field=models.CharField(choices=[('CHF', 'Swiss Franc'), ('EUR', 'Euro'), ('USD', 'US Dollar')], default='CHF', max_length=32),
- ),
- migrations.AlterField(
- model_name='payment',
- name='amount',
- field=models.DecimalField(decimal_places=2, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
- ),
- ]
diff --git a/uncloud_pay/migrations/0003_auto_20201228_2256.py b/uncloud_pay/migrations/0003_auto_20201228_2256.py
deleted file mode 100644
index b516bd5..0000000
--- a/uncloud_pay/migrations/0003_auto_20201228_2256.py
+++ /dev/null
@@ -1,28 +0,0 @@
-# Generated by Django 3.1 on 2020-12-28 22:56
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0002_auto_20201228_2244'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='order',
- name='currency',
- field=models.CharField(choices=[('CHF', 'Swiss Franc')], default='CHF', max_length=32),
- ),
- migrations.AlterField(
- model_name='payment',
- name='currency',
- field=models.CharField(choices=[('CHF', 'Swiss Franc')], default='CHF', max_length=32),
- ),
- migrations.AlterField(
- model_name='product',
- name='currency',
- field=models.CharField(choices=[('CHF', 'Swiss Franc')], default='CHF', max_length=32),
- ),
- ]
diff --git a/uncloud_pay/migrations/0004_stripecreditcard_active.py b/uncloud_pay/migrations/0004_stripecreditcard_active.py
deleted file mode 100644
index 3fb8015..0000000
--- a/uncloud_pay/migrations/0004_stripecreditcard_active.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.1 on 2020-12-28 23:34
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0003_auto_20201228_2256'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='stripecreditcard',
- name='active',
- field=models.BooleanField(default=True),
- ),
- ]
diff --git a/uncloud_pay/migrations/0005_auto_20201228_2335.py b/uncloud_pay/migrations/0005_auto_20201228_2335.py
deleted file mode 100644
index 814752e..0000000
--- a/uncloud_pay/migrations/0005_auto_20201228_2335.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.1 on 2020-12-28 23:35
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0004_stripecreditcard_active'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='stripecreditcard',
- name='active',
- field=models.BooleanField(default=False),
- ),
- ]
diff --git a/uncloud_pay/migrations/0006_auto_20201228_2337.py b/uncloud_pay/migrations/0006_auto_20201228_2337.py
deleted file mode 100644
index a164767..0000000
--- a/uncloud_pay/migrations/0006_auto_20201228_2337.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# Generated by Django 3.1 on 2020-12-28 23:37
-
-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),
- ('uncloud_pay', '0005_auto_20201228_2335'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='stripecreditcard',
- name='owner',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
- ),
- ]
diff --git a/uncloud_pay/migrations/0007_auto_20201228_2338.py b/uncloud_pay/migrations/0007_auto_20201228_2338.py
deleted file mode 100644
index 315a74b..0000000
--- a/uncloud_pay/migrations/0007_auto_20201228_2338.py
+++ /dev/null
@@ -1,17 +0,0 @@
-# Generated by Django 3.1 on 2020-12-28 23:38
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0006_auto_20201228_2337'),
- ]
-
- operations = [
- migrations.AddConstraint(
- model_name='stripecreditcard',
- constraint=models.UniqueConstraint(condition=models.Q(active=True), fields=('owner',), name='one_active_card_per_user'),
- ),
- ]
diff --git a/uncloud_pay/migrations/0008_payment_external_reference.py b/uncloud_pay/migrations/0008_payment_external_reference.py
deleted file mode 100644
index 0de20b6..0000000
--- a/uncloud_pay/migrations/0008_payment_external_reference.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.1 on 2020-12-29 00:04
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0007_auto_20201228_2338'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='payment',
- name='external_reference',
- field=models.CharField(default='', max_length=256),
- ),
- ]
diff --git a/uncloud_pay/migrations/0009_auto_20201229_0037.py b/uncloud_pay/migrations/0009_auto_20201229_0037.py
deleted file mode 100644
index fc195e4..0000000
--- a/uncloud_pay/migrations/0009_auto_20201229_0037.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.1 on 2020-12-29 00:37
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0008_payment_external_reference'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='payment',
- name='external_reference',
- field=models.CharField(blank=True, default='', max_length=256, null=True),
- ),
- ]
diff --git a/uncloud_pay/migrations/0010_auto_20201229_0042.py b/uncloud_pay/migrations/0010_auto_20201229_0042.py
deleted file mode 100644
index 6dd6a60..0000000
--- a/uncloud_pay/migrations/0010_auto_20201229_0042.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Generated by Django 3.1 on 2020-12-29 00:42
-
-from django.db import migrations, models
-import django.utils.timezone
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0009_auto_20201229_0037'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='payment',
- name='timestamp',
- field=models.DateTimeField(default=django.utils.timezone.now),
- ),
- ]
diff --git a/uncloud_pay/migrations/0011_auto_20210101_1308.py b/uncloud_pay/migrations/0011_auto_20210101_1308.py
deleted file mode 100644
index 942f430..0000000
--- a/uncloud_pay/migrations/0011_auto_20210101_1308.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Generated by Django 3.1 on 2021-01-01 13:08
-
-from django.db import migrations
-import uncloud.models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0010_auto_20201229_0042'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='billingaddress',
- name='country',
- field=uncloud.models.CountryField(choices=[('AD', 'Andorra'), ('AE', 'United Arab Emirates'), ('AF', 'Afghanistan'), ('AG', 'Antigua & Barbuda'), ('AI', 'Anguilla'), ('AL', 'Albania'), ('AM', 'Armenia'), ('AN', 'Netherlands Antilles'), ('AO', 'Angola'), ('AQ', 'Antarctica'), ('AR', 'Argentina'), ('AS', 'American Samoa'), ('AT', 'Austria'), ('AU', 'Australia'), ('AW', 'Aruba'), ('AZ', 'Azerbaijan'), ('BA', 'Bosnia and Herzegovina'), ('BB', 'Barbados'), ('BD', 'Bangladesh'), ('BE', 'Belgium'), ('BF', 'Burkina Faso'), ('BG', 'Bulgaria'), ('BH', 'Bahrain'), ('BI', 'Burundi'), ('BJ', 'Benin'), ('BM', 'Bermuda'), ('BN', 'Brunei Darussalam'), ('BO', 'Bolivia'), ('BR', 'Brazil'), ('BS', 'Bahama'), ('BT', 'Bhutan'), ('BV', 'Bouvet Island'), ('BW', 'Botswana'), ('BY', 'Belarus'), ('BZ', 'Belize'), ('CA', 'Canada'), ('CC', 'Cocos (Keeling) Islands'), ('CF', 'Central African Republic'), ('CG', 'Congo'), ('CH', 'Switzerland'), ('CI', 'Ivory Coast'), ('CK', 'Cook Iislands'), ('CL', 'Chile'), ('CM', 'Cameroon'), ('CN', 'China'), ('CO', 'Colombia'), ('CR', 'Costa Rica'), ('CU', 'Cuba'), ('CV', 'Cape Verde'), ('CX', 'Christmas Island'), ('CY', 'Cyprus'), ('CZ', 'Czech Republic'), ('DE', 'Germany'), ('DJ', 'Djibouti'), ('DK', 'Denmark'), ('DM', 'Dominica'), ('DO', 'Dominican Republic'), ('DZ', 'Algeria'), ('EC', 'Ecuador'), ('EE', 'Estonia'), ('EG', 'Egypt'), ('EH', 'Western Sahara'), ('ER', 'Eritrea'), ('ES', 'Spain'), ('ET', 'Ethiopia'), ('FI', 'Finland'), ('FJ', 'Fiji'), ('FK', 'Falkland Islands (Malvinas)'), ('FM', 'Micronesia'), ('FO', 'Faroe Islands'), ('FR', 'France'), ('FX', 'France, Metropolitan'), ('GA', 'Gabon'), ('GB', 'United Kingdom (Great Britain)'), ('GD', 'Grenada'), ('GE', 'Georgia'), ('GF', 'French Guiana'), ('GH', 'Ghana'), ('GI', 'Gibraltar'), ('GL', 'Greenland'), ('GM', 'Gambia'), ('GN', 'Guinea'), ('GP', 'Guadeloupe'), ('GQ', 'Equatorial Guinea'), ('GR', 'Greece'), ('GS', 'South Georgia and the South Sandwich Islands'), ('GT', 'Guatemala'), ('GU', 'Guam'), ('GW', 'Guinea-Bissau'), ('GY', 'Guyana'), ('HK', 'Hong Kong'), ('HM', 'Heard & McDonald Islands'), ('HN', 'Honduras'), ('HR', 'Croatia'), ('HT', 'Haiti'), ('HU', 'Hungary'), ('ID', 'Indonesia'), ('IE', 'Ireland'), ('IL', 'Israel'), ('IN', 'India'), ('IO', 'British Indian Ocean Territory'), ('IQ', 'Iraq'), ('IR', 'Islamic Republic of Iran'), ('IS', 'Iceland'), ('IT', 'Italy'), ('JM', 'Jamaica'), ('JO', 'Jordan'), ('JP', 'Japan'), ('KE', 'Kenya'), ('KG', 'Kyrgyzstan'), ('KH', 'Cambodia'), ('KI', 'Kiribati'), ('KM', 'Comoros'), ('KN', 'St. Kitts and Nevis'), ('KP', "Korea, Democratic People's Republic of"), ('KR', 'Korea, Republic of'), ('KW', 'Kuwait'), ('KY', 'Cayman Islands'), ('KZ', 'Kazakhstan'), ('LA', "Lao People's Democratic Republic"), ('LB', 'Lebanon'), ('LC', 'Saint Lucia'), ('LI', 'Liechtenstein'), ('LK', 'Sri Lanka'), ('LR', 'Liberia'), ('LS', 'Lesotho'), ('LT', 'Lithuania'), ('LU', 'Luxembourg'), ('LV', 'Latvia'), ('LY', 'Libyan Arab Jamahiriya'), ('MA', 'Morocco'), ('MC', 'Monaco'), ('MD', 'Moldova, Republic of'), ('MG', 'Madagascar'), ('MH', 'Marshall Islands'), ('ML', 'Mali'), ('MN', 'Mongolia'), ('MM', 'Myanmar'), ('MO', 'Macau'), ('MP', 'Northern Mariana Islands'), ('MQ', 'Martinique'), ('MR', 'Mauritania'), ('MS', 'Monserrat'), ('MT', 'Malta'), ('MU', 'Mauritius'), ('MV', 'Maldives'), ('MW', 'Malawi'), ('MX', 'Mexico'), ('MY', 'Malaysia'), ('MZ', 'Mozambique'), ('NA', 'Namibia'), ('NC', 'New Caledonia'), ('NE', 'Niger'), ('NF', 'Norfolk Island'), ('NG', 'Nigeria'), ('NI', 'Nicaragua'), ('NL', 'Netherlands'), ('NO', 'Norway'), ('NP', 'Nepal'), ('NR', 'Nauru'), ('NU', 'Niue'), ('NZ', 'New Zealand'), ('OM', 'Oman'), ('PA', 'Panama'), ('PE', 'Peru'), ('PF', 'French Polynesia'), ('PG', 'Papua New Guinea'), ('PH', 'Philippines'), ('PK', 'Pakistan'), ('PL', 'Poland'), ('PM', 'St. Pierre & Miquelon'), ('PN', 'Pitcairn'), ('PR', 'Puerto Rico'), ('PT', 'Portugal'), ('PW', 'Palau'), ('PY', 'Paraguay'), ('QA', 'Qatar'), ('RE', 'Reunion'), ('RO', 'Romania'), ('RU', 'Russian Federation'), ('RW', 'Rwanda'), ('SA', 'Saudi Arabia'), ('SB', 'Solomon Islands'), ('SC', 'Seychelles'), ('SD', 'Sudan'), ('SE', 'Sweden'), ('SG', 'Singapore'), ('SH', 'St. Helena'), ('SI', 'Slovenia'), ('SJ', 'Svalbard & Jan Mayen Islands'), ('SK', 'Slovakia'), ('SL', 'Sierra Leone'), ('SM', 'San Marino'), ('SN', 'Senegal'), ('SO', 'Somalia'), ('SR', 'Suriname'), ('ST', 'Sao Tome & Principe'), ('SV', 'El Salvador'), ('SY', 'Syrian Arab Republic'), ('SZ', 'Swaziland'), ('TC', 'Turks & Caicos Islands'), ('TD', 'Chad'), ('TF', 'French Southern Territories'), ('TG', 'Togo'), ('TH', 'Thailand'), ('TJ', 'Tajikistan'), ('TK', 'Tokelau'), ('TM', 'Turkmenistan'), ('TN', 'Tunisia'), ('TO', 'Tonga'), ('TP', 'East Timor'), ('TR', 'Turkey'), ('TT', 'Trinidad & Tobago'), ('TV', 'Tuvalu'), ('TW', 'Taiwan, Province of China'), ('TZ', 'Tanzania, United Republic of'), ('UA', 'Ukraine'), ('UG', 'Uganda'), ('UM', 'United States Minor Outlying Islands'), ('US', 'United States of America'), ('UY', 'Uruguay'), ('UZ', 'Uzbekistan'), ('VA', 'Vatican City State (Holy See)'), ('VC', 'St. Vincent & the Grenadines'), ('VE', 'Venezuela'), ('VG', 'British Virgin Islands'), ('VI', 'United States Virgin Islands'), ('VN', 'Viet Nam'), ('VU', 'Vanuatu'), ('WF', 'Wallis & Futuna Islands'), ('WS', 'Samoa'), ('YE', 'Yemen'), ('YT', 'Mayotte'), ('YU', 'Yugoslavia'), ('ZA', 'South Africa'), ('ZM', 'Zambia'), ('ZR', 'Zaire'), ('ZW', 'Zimbabwe')], default='CH', max_length=2),
- ),
- ]
diff --git a/uncloud_pay/migrations/0012_auto_20210630_0742.py b/uncloud_pay/migrations/0012_auto_20210630_0742.py
deleted file mode 100644
index 45e3dfe..0000000
--- a/uncloud_pay/migrations/0012_auto_20210630_0742.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 3.2.4 on 2021-06-30 07:42
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0011_auto_20210101_1308'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='billingaddress',
- name='vat_number_verified',
- field=models.BooleanField(default=False),
- ),
- migrations.AlterField(
- model_name='payment',
- name='source',
- field=models.CharField(choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral')], max_length=256),
- ),
- ]
diff --git a/uncloud_pay/migrations/0013_alter_billingaddress_owner.py b/uncloud_pay/migrations/0013_alter_billingaddress_owner.py
deleted file mode 100644
index 7597129..0000000
--- a/uncloud_pay/migrations/0013_alter_billingaddress_owner.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-03 15:23
-
-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),
- ('uncloud_pay', '0012_auto_20210630_0742'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='billingaddress',
- name='owner',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='billing_addresses', to=settings.AUTH_USER_MODEL),
- ),
- ]
diff --git a/uncloud_pay/migrations/0014_auto_20210703_1747.py b/uncloud_pay/migrations/0014_auto_20210703_1747.py
deleted file mode 100644
index 1c004d0..0000000
--- a/uncloud_pay/migrations/0014_auto_20210703_1747.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-03 17:47
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0013_alter_billingaddress_owner'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='billingaddress',
- name='stripe_tax_id',
- field=models.CharField(blank=True, default='', max_length=100),
- ),
- migrations.AddField(
- model_name='billingaddress',
- name='vat_number_validated_on',
- field=models.DateTimeField(blank=True, null=True),
- ),
- ]
diff --git a/uncloud_pay/migrations/0015_auto_20210705_0849.py b/uncloud_pay/migrations/0015_auto_20210705_0849.py
deleted file mode 100644
index dfb6d80..0000000
--- a/uncloud_pay/migrations/0015_auto_20210705_0849.py
+++ /dev/null
@@ -1,34 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-05 08:49
-
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0014_auto_20210703_1747'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='order',
- name='customer',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.stripecustomer'),
- ),
- migrations.AddField(
- model_name='order',
- name='status',
- field=models.CharField(choices=[('draft', 'Draft'), ('declined', 'Declined'), ('approved', 'Approved')], default='draft', max_length=100),
- ),
- migrations.AddField(
- model_name='order',
- name='stripe_charge_id',
- field=models.CharField(max_length=100, null=True),
- ),
- migrations.AddField(
- model_name='order',
- name='vm_id',
- field=models.IntegerField(default=0),
- ),
- ]
diff --git a/uncloud_pay/migrations/0016_pricingplan.py b/uncloud_pay/migrations/0016_pricingplan.py
deleted file mode 100644
index 505c141..0000000
--- a/uncloud_pay/migrations/0016_pricingplan.py
+++ /dev/null
@@ -1,29 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-06 13:21
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0015_auto_20210705_0849'),
- ]
-
- operations = [
- migrations.CreateModel(
- name='PricingPlan',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('name', models.CharField(max_length=255, unique=True)),
- ('vat_inclusive', models.BooleanField(default=True)),
- ('vat_percentage', models.DecimalField(blank=True, decimal_places=5, default=0, max_digits=7)),
- ('set_up_fees', models.DecimalField(decimal_places=2, default=0, max_digits=7)),
- ('cores_unit_price', models.DecimalField(decimal_places=2, default=0, max_digits=7)),
- ('ram_unit_price', models.DecimalField(decimal_places=2, default=0, max_digits=7)),
- ('storage_unit_price', models.DecimalField(decimal_places=2, default=0, max_digits=7)),
- ('discount_name', models.CharField(blank=True, max_length=255, null=True)),
- ('discount_amount', models.DecimalField(decimal_places=2, default=0, max_digits=6)),
- ('stripe_coupon_id', models.CharField(blank=True, max_length=255, null=True)),
- ],
- ),
- ]
diff --git a/uncloud_pay/migrations/0017_auto_20210706_1728.py b/uncloud_pay/migrations/0017_auto_20210706_1728.py
deleted file mode 100644
index 1571b10..0000000
--- a/uncloud_pay/migrations/0017_auto_20210706_1728.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-06 17:28
-
-from django.db import migrations
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0016_pricingplan'),
- ]
-
- operations = [
- migrations.RemoveField(
- model_name='paymentmethod',
- name='owner',
- ),
- migrations.DeleteModel(
- name='Payment',
- ),
- migrations.DeleteModel(
- name='PaymentMethod',
- ),
- ]
diff --git a/uncloud_pay/migrations/0018_payment.py b/uncloud_pay/migrations/0018_payment.py
deleted file mode 100644
index 47d6e3a..0000000
--- a/uncloud_pay/migrations/0018_payment.py
+++ /dev/null
@@ -1,30 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-06 17:47
-
-from django.conf import settings
-import django.core.validators
-from django.db import migrations, models
-import django.db.models.deletion
-import django.utils.timezone
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- migrations.swappable_dependency(settings.AUTH_USER_MODEL),
- ('uncloud_pay', '0017_auto_20210706_1728'),
- ]
-
- operations = [
- migrations.CreateModel(
- name='Payment',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('amount', models.DecimalField(decimal_places=2, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
- ('source', models.CharField(choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral')], max_length=256)),
- ('timestamp', models.DateTimeField(default=django.utils.timezone.now)),
- ('currency', models.CharField(choices=[('CHF', 'Swiss Franc')], default='CHF', max_length=32)),
- ('external_reference', models.CharField(blank=True, default='', max_length=256, null=True)),
- ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- ),
- ]
diff --git a/uncloud_pay/migrations/0019_order_pricing_plan.py b/uncloud_pay/migrations/0019_order_pricing_plan.py
deleted file mode 100644
index 5392ce6..0000000
--- a/uncloud_pay/migrations/0019_order_pricing_plan.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-06 19:18
-
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0018_payment'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='order',
- name='pricing_plan',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.pricingplan'),
- ),
- ]
diff --git a/uncloud_pay/migrations/0020_rename_is_final_bill_is_closed.py b/uncloud_pay/migrations/0020_rename_is_final_bill_is_closed.py
deleted file mode 100644
index f3419eb..0000000
--- a/uncloud_pay/migrations/0020_rename_is_final_bill_is_closed.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-07 20:18
-
-from django.db import migrations
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0019_order_pricing_plan'),
- ]
-
- operations = [
- migrations.RenameField(
- model_name='bill',
- old_name='is_final',
- new_name='is_closed',
- ),
- ]
diff --git a/uncloud_pay/migrations/0021_auto_20210709_0914.py b/uncloud_pay/migrations/0021_auto_20210709_0914.py
deleted file mode 100644
index 66e3dcb..0000000
--- a/uncloud_pay/migrations/0021_auto_20210709_0914.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-09 09:14
-
-from django.db import migrations
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0020_rename_is_final_bill_is_closed'),
- ]
-
- operations = [
- migrations.RemoveField(
- model_name='order',
- name='stripe_charge_id',
- ),
- migrations.RemoveField(
- model_name='order',
- name='vm_id',
- ),
- ]
diff --git a/uncloud_pay/migrations/0022_remove_order_status.py b/uncloud_pay/migrations/0022_remove_order_status.py
deleted file mode 100644
index 2b51be8..0000000
--- a/uncloud_pay/migrations/0022_remove_order_status.py
+++ /dev/null
@@ -1,17 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-11 08:32
-
-from django.db import migrations
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0021_auto_20210709_0914'),
- ]
-
- operations = [
- migrations.RemoveField(
- model_name='order',
- name='status',
- ),
- ]
diff --git a/uncloud_pay/migrations/0023_auto_20210730_1342.py b/uncloud_pay/migrations/0023_auto_20210730_1342.py
deleted file mode 100644
index e2e4c6f..0000000
--- a/uncloud_pay/migrations/0023_auto_20210730_1342.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-30 13:42
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0022_remove_order_status'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='payment',
- name='notes',
- field=models.TextField(blank=True, default='', null=True),
- ),
- migrations.AddField(
- model_name='payment',
- name='type',
- field=models.CharField(choices=[('send', 'Send Money'), ('receive', 'Receive Money')], default='send', max_length=256),
- ),
- ]
diff --git a/uncloud_pay/migrations/0024_auto_20210730_1441.py b/uncloud_pay/migrations/0024_auto_20210730_1441.py
deleted file mode 100644
index 97baebc..0000000
--- a/uncloud_pay/migrations/0024_auto_20210730_1441.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 3.2.4 on 2021-07-30 14:41
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0023_auto_20210730_1342'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='payment',
- name='source',
- field=models.CharField(blank=True, choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral')], max_length=256, null=True),
- ),
- migrations.AlterField(
- model_name='payment',
- name='type',
- field=models.CharField(choices=[('withdraw', 'Withdraw Money'), ('deposit', 'Deposit Money')], default='send', max_length=256),
- ),
- ]
diff --git a/uncloud_pay/migrations/0025_auto_20210803_2118.py b/uncloud_pay/migrations/0025_auto_20210803_2118.py
deleted file mode 100644
index 5be92a5..0000000
--- a/uncloud_pay/migrations/0025_auto_20210803_2118.py
+++ /dev/null
@@ -1,24 +0,0 @@
-# Generated by Django 3.2.4 on 2021-08-03 21:18
-
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0024_auto_20210730_1441'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='billrecord',
- name='bill',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bill_records', to='uncloud_pay.bill'),
- ),
- migrations.AlterField(
- model_name='payment',
- name='type',
- field=models.CharField(choices=[('withdraw', 'Withdraw Money'), ('deposit', 'Deposit Money')], default='deposit', max_length=256),
- ),
- ]
diff --git a/uncloud_pay/migrations/0026_remove_order_description.py b/uncloud_pay/migrations/0026_remove_order_description.py
deleted file mode 100644
index c81cdfe..0000000
--- a/uncloud_pay/migrations/0026_remove_order_description.py
+++ /dev/null
@@ -1,17 +0,0 @@
-# Generated by Django 3.2.4 on 2021-08-03 21:40
-
-from django.db import migrations
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0025_auto_20210803_2118'),
- ]
-
- operations = [
- migrations.RemoveField(
- model_name='order',
- name='description',
- ),
- ]
diff --git a/uncloud_pay/migrations/0027_alter_stripecreditcard_card_name.py b/uncloud_pay/migrations/0027_alter_stripecreditcard_card_name.py
deleted file mode 100644
index b468ed7..0000000
--- a/uncloud_pay/migrations/0027_alter_stripecreditcard_card_name.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.2.4 on 2021-08-12 16:00
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0026_remove_order_description'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='stripecreditcard',
- name='card_name',
- field=models.CharField(default='', max_length=128),
- ),
- ]
diff --git a/uncloud_pay/migrations/0028_bill_currency.py b/uncloud_pay/migrations/0028_bill_currency.py
deleted file mode 100644
index 27e3dc7..0000000
--- a/uncloud_pay/migrations/0028_bill_currency.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.2.4 on 2021-08-12 16:06
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0027_alter_stripecreditcard_card_name'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='bill',
- name='currency',
- field=models.CharField(choices=[('CHF', 'Swiss Franc')], default='CHF', max_length=32),
- ),
- ]
diff --git a/uncloud_pay/migrations/0029_auto_20210817_1129.py b/uncloud_pay/migrations/0029_auto_20210817_1129.py
deleted file mode 100644
index 6d8bc9d..0000000
--- a/uncloud_pay/migrations/0029_auto_20210817_1129.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 3.2.4 on 2021-08-17 11:29
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0028_bill_currency'),
- ]
-
- operations = [
- migrations.RenameField(
- model_name='pricingplan',
- old_name='storage_unit_price',
- new_name='storage_hd_unit_price',
- ),
- migrations.AddField(
- model_name='pricingplan',
- name='storage_ssd_unit_price',
- field=models.DecimalField(decimal_places=2, default=0, max_digits=7),
- ),
- ]
diff --git a/uncloud_pay/migrations/0030_pricingplan_monthly_maintenance_fees.py b/uncloud_pay/migrations/0030_pricingplan_monthly_maintenance_fees.py
deleted file mode 100644
index 2638b67..0000000
--- a/uncloud_pay/migrations/0030_pricingplan_monthly_maintenance_fees.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 3.2.4 on 2021-08-17 16:18
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0029_auto_20210817_1129'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='pricingplan',
- name='monthly_maintenance_fees',
- field=models.DecimalField(decimal_places=2, default=0, max_digits=7),
- ),
- ]
diff --git a/uncloud_pay/migrations/0031_auto_20210819_1304.py b/uncloud_pay/migrations/0031_auto_20210819_1304.py
deleted file mode 100644
index a3d75db..0000000
--- a/uncloud_pay/migrations/0031_auto_20210819_1304.py
+++ /dev/null
@@ -1,22 +0,0 @@
-# Generated by Django 3.2.4 on 2021-08-19 13:04
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('uncloud_pay', '0030_pricingplan_monthly_maintenance_fees'),
- ]
-
- operations = [
- migrations.RemoveField(
- model_name='bill',
- name='is_closed',
- ),
- migrations.AddField(
- model_name='bill',
- name='status',
- field=models.CharField(choices=[('new', 'New'), ('cancelled', 'Cancelled'), ('paid', 'Paid')], default='new', max_length=32),
- ),
- ]
diff --git a/uncloud_pay/migrations/__init__.py b/uncloud_pay/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py
deleted file mode 100644
index 51de3ad..0000000
--- a/uncloud_pay/models.py
+++ /dev/null
@@ -1,1256 +0,0 @@
-import logging
-import datetime
-import json
-
-from math import ceil
-from calendar import monthrange
-from decimal import Decimal
-
-from django.conf import settings
-from django.contrib.auth import get_user_model
-from django.core.validators import MinValueValidator
-from django.db import models
-from django.db.models import Q
-from django.utils.translation import gettext_lazy as _
-from django.utils import timezone
-# Verify whether or not to use them here
-from django.core.exceptions import ObjectDoesNotExist, ValidationError
-
-import uncloud_pay
-from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
-from uncloud.models import UncloudAddress, UncloudProvider
-from uncloud.selectors import filter_for_when
-from .services import *
-
-# Used to generate bill due dates.
-BILL_PAYMENT_DELAY=datetime.timedelta(days=settings.BILL_PAYMENT_DELAY)
-
-EU_COUNTRIES = ['at', 'be', 'bg', 'ch', 'cy', 'cz', 'hr', 'dk',
- 'ee', 'fi', 'fr', 'mc', 'de', 'gr', 'hu', 'ie', 'it',
- 'lv', 'lu', 'mt', 'nl', 'po', 'pt', 'ro','sk', 'si', 'es',
- 'se', 'gb']
-
-
-# Initialize logger.
-logger = logging.getLogger(__name__)
-
-def default_payment_delay():
- return timezone.now() + BILL_PAYMENT_DELAY
-
-class Currency(models.TextChoices):
- """
- Possible currencies to be billed
- """
- CHF = 'CHF', _('Swiss Franc')
-# EUR = 'EUR', _('Euro')
-# USD = 'USD', _('US Dollar')
-
-
-###
-# Stripe
-
-class StripeCustomer(models.Model):
- owner = models.OneToOneField( get_user_model(),
- primary_key=True,
- on_delete=models.CASCADE)
- stripe_id = models.CharField(max_length=32)
-
- def __str__(self):
- return self.owner.username
-
-
-class StripeCreditCard(models.Model):
- owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
-
- card_name = models.CharField(null=False, max_length=128, default="")
- card_id = models.CharField(null=False, max_length=32)
- last4 = models.CharField(null=False, max_length=4)
- brand = models.CharField(null=False, max_length=64)
- expiry_date = models.DateField(null=False)
- active = models.BooleanField(default=False)
-
- class Meta:
- constraints = [
- models.UniqueConstraint(fields=['owner'],
- condition=models.Q(active=True),
- name='one_active_card_per_user')
- ]
-
-
- def __str__(self):
- return f"{self.card_name}: {self.brand} {self.last4} ({self.expiry_date})"
-
- def delete(self, **kwargs):
- uncloud_pay.stripe.delete_card(self.card_id)
- super().delete(**kwargs)
-
-
- def activate(self):
- StripeCreditCard.objects.filter(owner=self.owner, active=True).update(active=False)
- self.active = True
- self.save()
-
-class Payment(models.Model):
- owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
- type = models.CharField(max_length=256,
- choices = (
- ('withdraw', 'Withdraw Money'),
- ('deposit', 'Deposit Money')
- ), null=False, blank=False, default="deposit")
-
- notes = models.TextField(default="", null=True, blank=True)
-
- amount = models.DecimalField(
- max_digits=AMOUNT_MAX_DIGITS,
- decimal_places=AMOUNT_DECIMALS,
- validators=[MinValueValidator(0)])
-
- source = models.CharField(max_length=256,
- choices = (
- ('wire', 'Wire Transfer'),
- ('stripe', 'Stripe'),
- ('voucher', 'Voucher'),
- ('referral', 'Referral'),
- ), null=True, blank=True,)
-
- timestamp = models.DateTimeField(default=timezone.now)
-
- currency = models.CharField(max_length=32, choices=Currency.choices, default=Currency.CHF)
-
- external_reference = models.CharField(max_length=256, default="", null=True, blank=True)
-
- def __str__(self):
- return f"{self.amount}{self.currency} from {self.owner} via {self.source} on {self.timestamp}"
-
- @classmethod
- def deposit(cls, owner, amount, source, currency='CHF', notes=''):
- if source == 'stripe':
- try:
- payment_intent = uncloud_pay.stripe.charge_customer(owner, amount, currency)
- if not payment_intent.status or payment_intent.status != 'succeeded':
- raise Exception("The payment has been failed, please try to activate another card")
- return cls.objects.create(owner=owner, type="deposit", amount=amount, external_reference=payment_intent["id"],
- currency=currency, source=source, notes=notes)
- except Exception as e:
- raise e
-
- @classmethod
- def withdraw(cls, owner, amount, currency='CHF', notes=''):
- return cls.objects.create(owner=owner, type="withdraw", amount=amount,
- currency=currency, notes=notes)
-
-
-
-# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
-class RecurringPeriodDefaultChoices(models.IntegerChoices):
- """
- This is an old class and being superseeded by the database model below
- """
- PER_365D = 365*24*3600, _('Per 365 days')
- PER_30D = 30*24*3600, _('Per 30 days')
- PER_WEEK = 7*24*3600, _('Per Week')
- PER_DAY = 24*3600, _('Per Day')
- PER_HOUR = 3600, _('Per Hour')
- PER_MINUTE = 60, _('Per Minute')
- PER_SECOND = 1, _('Per Second')
- ONE_TIME = 0, _('Onetime')
-
-# RecurringPeriods
-class RecurringPeriod(models.Model):
- """
- Available recurring periods.
- By default seeded from RecurringPeriodChoices
- """
-
- name = models.CharField(max_length=100, unique=True)
- duration_seconds = models.IntegerField(unique=True)
-
- @classmethod
- def populate_db_defaults(cls):
- for (seconds, name) in RecurringPeriodDefaultChoices.choices:
- obj, created = cls.objects.get_or_create(name=name,
- defaults={ 'duration_seconds': seconds })
-
- @staticmethod
- def secs_to_name(secs):
- name = ""
- days = 0
- hours = 0
-
- if secs > 24*3600:
- days = secs // (24*3600)
- secs -= (days*24*3600)
-
- if secs > 3600:
- hours = secs // 3600
- secs -= hours*3600
-
- return f"{days} days {hours} hours {secs} seconds"
-
- def __str__(self):
- duration = self.secs_to_name(self.duration_seconds)
-
- return f"{self.name} ({duration})"
-
-
-###
-# Bills.
-
-class BillingAddress(UncloudAddress):
- owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='billing_addresses')
- vat_number = models.CharField(max_length=100, default="", blank=True)
- vat_number_verified = models.BooleanField(default=False)
- vat_number_validated_on = models.DateTimeField(blank=True, null=True)
- stripe_tax_id = models.CharField(max_length=100, default="", blank=True)
- active = models.BooleanField(default=False)
-
- class Meta:
- constraints = [
- models.UniqueConstraint(fields=['owner'],
- condition=models.Q(active=True),
- name='one_active_billing_address_per_user')
- ]
-
- @classmethod
- def populate_db_defaults(cls):
- """
- Ensure we have at least one billing address that is associated with the uncloud-admin.
-
- This way we are sure that an UncloudProvider can be created.
-
- Cannot use get_or_create as that looks for exactly one.
-
- """
-
- owner = get_user_model().objects.get(username=settings.UNCLOUD_ADMIN_NAME)
- billing_address = cls.objects.filter(owner=owner).first()
-
- if not billing_address:
- billing_address = cls.objects.create(owner=owner,
- organization="uncloud admins",
- full_name="Uncloud Admin",
- street="Uncloudstreet. 42",
- city="Luchsingen",
- postal_code="8775",
- country="CH",
- active=True)
-
- def __str__(self):
- return "{} - {}, {}, {} {}, {}".format(
- self.owner,
- self.full_name, self.street, self.postal_code, self.city,
- self.country)
-
- @staticmethod
- def get_address_for(user):
- return BillingAddress.objects.get(owner=user)
-
-###
-# VAT
-
-class VATRate(models.Model):
- starting_date = models.DateField(blank=True, null=True)
- ending_date = models.DateField(blank=True, null=True)
- territory_codes = models.TextField(blank=True, default='')
- currency_code = models.CharField(max_length=10)
- rate = models.FloatField()
- rate_type = models.TextField(blank=True, default='')
- description = models.TextField(blank=True, default='')
-
- @staticmethod
- def get_vat_rate_for_country(country, when=None):
- """
- Returns the VAT rate for business to customer.
-
- B2B is always 0% with the exception of trading within the own country
- """
- vatrate = filter_for_when(VATRate.objects.filter(territory_codes=country), when).first()
-
- # By default we charge VAT. This affects:
- # - Same country sales (VAT applied)
- # - B2C to EU (VAT applied)
- rate = vatrate.rate if vatrate else 0
- if not country.lower().strip() in EU_COUNTRIES:
- rate = 0
-
- return rate
-
- @staticmethod
- def get_vat_rate(billing_address, when=None):
- rate = VATRate.get_vat_rate_for_country(billing_address.country, when)
-
- # Exception: if...
- # - the billing_address is in EU,
- # - the vat_number has been set
- # - the vat_number has been verified
- # Then we do not charge VAT
-
- if rate != 0 and billing_address.vat_number and billing_address.vat_number_verified:
- rate = 0
- return rate
-
-
- def __str__(self):
- return f"{self.territory_codes}: {self.starting_date} - {self.ending_date or ''}: {self.rate_type}"
-
-###
-# Products
-
-class Product(models.Model):
- """
- A product is something a user can order. To record the pricing, we
- create order that define a state in time.
-
- A product can have *one* one_time_order and/or *one*
- recurring_order.
-
- If either of them needs to be updated, a new order of the same
- type will be created and links to the previous order.
-
- """
-
- name = models.CharField(max_length=256, unique=True)
- description = models.CharField(max_length=1024)
- config = models.JSONField()
- recurring_periods = models.ManyToManyField(RecurringPeriod, through='ProductToRecurringPeriod')
- currency = models.CharField(max_length=32, choices=Currency.choices, default=Currency.CHF)
-
- @property
- def default_recurring_period(self):
- """
- Return the default recurring Period
- """
- return self.recurring_periods.get(producttorecurringperiod__is_default=True)
-
- @classmethod
- def populate_db_defaults(cls):
- recurring_period = RecurringPeriod.objects.get(name="Per 30 days")
-
- obj, created = cls.objects.get_or_create(name="Dual Stack Virtual Machine v1",
- description="A standard virtual machine",
- currency=Currency.CHF,
- config={
- 'features': {
- 'cores':
- { 'min': 1,
- 'max': 48
- },
- 'ram_gb':
- { 'min': 1,
- 'max': 256
- },
- 'ssd_gb':
- { 'min': 10
- },
- 'hdd_gb':
- { 'min': 0,
- },
- 'additional_ipv4_address':
- { 'min': 0,
- },
- }
- }
- )
-
- obj.recurring_periods.add(recurring_period, through_defaults= { 'is_default': True })
-
- obj, created = cls.objects.get_or_create(name="Dual Stack Virtual Machine v2",
- description="A standard virtual machine",
- currency=Currency.CHF,
- config={
- 'features': {
- 'base':
- { 'min': 1,
- 'max': 1,
- },
- 'cores':
- { 'min': 1,
- 'max': 48,
- },
- 'ram_gb':
- { 'min': 1,
- 'max': 256,
- },
- 'ssd_gb':
- { 'min': 10
- },
- 'hdd_gb':
- { 'min': 0
- },
- 'additional_ipv4_address':
- { 'min': 0,},
- }
- }
- )
-
- obj.recurring_periods.add(recurring_period, through_defaults= { 'is_default': True })
-
- obj, created = cls.objects.get_or_create(name="reverse DNS",
- description="Reverse DNS network",
- currency=Currency.CHF,
- config={
- 'parameters': [
- 'network'
- ]
- })
- obj.recurring_periods.add(recurring_period, through_defaults= { 'is_default': True })
-
-
- def __str__(self):
- return f"{self.name}"
-
- @property
- def recurring_orders(self):
- return self.orders.order_by('id').exclude(recurring_price=0)
-
- @property
- def last_recurring_order(self):
- return self.recurring_orders.last()
-
- @property
- def one_time_orders(self):
- return self.orders.order_by('id').filter(recurring_price=0)
-
- @property
- def last_one_time_order(self):
- return self.one_time_orders.last()
-
- # FIXME: this could/should be part of Order (?)
- def create_or_update_recurring_order(self, when_to_start=None, recurring_period=None):
- if not self.recurring_price:
- return
-
- if not recurring_period:
- recurring_period = self.default_recurring_period
-
- if not when_to_start:
- when_to_start = timezone.now()
-
- if self.last_recurring_order:
- if self.recurring_price < self.last_recurring_order.price:
-
- if when_to_start < self.last_recurring_order.next_cancel_or_downgrade_date:
- when_to_start = start_after(self.last_recurring_order.next_cancel_or_downgrade_date)
-
- when_to_end = end_before(when_to_start)
-
- new_order = Order.objects.create(owner=self.owner,
- billing_address=self.last_recurring_order.billing_address,
- starting_date=when_to_start,
- price=self.recurring_price,
- recurring_period=recurring_period,
- description=str(self),
- replaces=self.last_recurring_order)
-
- self.last_recurring_order.replace_with(new_order)
- self.orders.add(new_order)
- else:
- self.create_order(when_to_start, recurring_period)
-
- @property
- def is_recurring(self):
- return self.recurring_price > 0
-
- @property
- def billing_address(self):
- return self.order.billing_address
-
- def discounted_price_by_period(self, requested_period):
- """
- Each product has a standard recurring period for which
- we define a pricing. I.e. VPN is usually year, VM is usually monthly.
-
- The user can opt-in to use a different period, which influences the price:
- The longer a user commits, the higher the discount.
-
- Products can also be limited in the available periods. For instance
- a VPN only makes sense to be bought for at least one day.
-
- Rules are as follows:
-
- given a standard recurring period of ..., changing to ... modifies price ...
-
-
- # One month for free if buying / year, compared to a month: about 8.33% discount
- per_year -> per_month -> /11
- per_month -> per_year -> *11
-
- # Month has 30.42 days on average. About 7.9% discount to go monthly
- per_month -> per_day -> /28
- per_day -> per_month -> *28
-
- # Day has 24h, give one for free
- per_day -> per_hour -> /23
- per_hour -> per_day -> /23
-
-
- Examples
-
- VPN @ 120CHF/y becomes
- - 10.91 CHF/month (130.91 CHF/year)
- - 0.39 CHF/day (142.21 CHF/year)
-
- VM @ 15 CHF/month becomes
- - 165 CHF/month (13.75 CHF/month)
- - 0.54 CHF/day (16.30 CHF/month)
-
- """
-
- # FIXME: This logic needs to be phased out / replaced by product specific (?)
- # proportions. Maybe using the RecurringPeriod table to link the possible discounts/add ups
-
- if self.default_recurring_period == RecurringPeriod.PER_365D:
- if requested_period == RecurringPeriod.PER_365D:
- return self.recurring_price
- if requested_period == RecurringPeriod.PER_30D:
- return self.recurring_price/11.
- if requested_period == RecurringPeriod.PER_DAY:
- return self.recurring_price/11./28.
-
- elif self.default_recurring_period == RecurringPeriod.PER_30D:
- if requested_period == RecurringPeriod.PER_365D:
- return self.recurring_price*11
- if requested_period == RecurringPeriod.PER_30D:
- return self.recurring_price
- if requested_period == RecurringPeriod.PER_DAY:
- return self.recurring_price/28.
-
- elif self.default_recurring_period == RecurringPeriod.PER_DAY:
- if requested_period == RecurringPeriod.PER_365D:
- return self.recurring_price*11*28
- if requested_period == RecurringPeriod.PER_30D:
- return self.recurring_price*28
- if requested_period == RecurringPeriod.PER_DAY:
- return self.recurring_price
- else:
- # FIXME: use the right type of exception here!
- raise Exception("Did not implement the discounter for this case")
-
-
- def save(self, *args, **kwargs):
- # try:
- # ba = BillingAddress.get_address_for(self.owner)
- # except BillingAddress.DoesNotExist:
- # raise ValidationError("User does not have a billing address")
-
- # if not ba.active:
- # raise ValidationError("User does not have an active billing address")
-
-
- # Verify the required JSON fields
-
- super().save(*args, **kwargs)
-
-
-###
-# Pricing
-######
-import logging
-
-from django.db import models
-
-logger = logging.getLogger(__name__)
-
-class PricingPlan(models.Model):
- name = models.CharField(max_length=255, unique=True)
- vat_inclusive = models.BooleanField(default=True)
- vat_percentage = models.DecimalField(
- max_digits=7, decimal_places=5, blank=True, default=0
- )
- set_up_fees = models.DecimalField(
- max_digits=7, decimal_places=2, default=0
- )
- monthly_maintenance_fees = models.DecimalField(
- max_digits=7, decimal_places=2, default=0
- )
- cores_unit_price = models.DecimalField(
- max_digits=7, decimal_places=2, default=0
- )
- ram_unit_price = models.DecimalField(
- max_digits=7, decimal_places=2, default=0
- )
- storage_hd_unit_price = models.DecimalField(
- max_digits=7, decimal_places=2, default=0
- )
- storage_ssd_unit_price = models.DecimalField(
- max_digits=7, decimal_places=2, default=0
- )
- discount_name = models.CharField(max_length=255, null=True, blank=True)
- discount_amount = models.DecimalField(
- max_digits=6, decimal_places=2, default=0
- )
- stripe_coupon_id = models.CharField(max_length=255, null=True, blank=True)
-
- def __str__(self):
- display_str = self.name + ' => ' + ' - '.join([
- '{} Setup'.format(self.set_up_fees.normalize()),
- '{}/Core'.format(self.cores_unit_price.normalize()),
- '{}/GB RAM'.format(self.ram_unit_price.normalize()),
- '{}/GB SSD'.format(self.storage_ssd_unit_price.normalize()),
- '{}/GB HD'.format(self.storage_hd_unit_price.normalize()),
- '{}% VAT'.format(self.vat_percentage.normalize())
- if not self.vat_inclusive else 'VAT-Incl',
- ])
- if self.discount_amount:
- display_str = ' - '.join([
- display_str,
- '{} {}'.format(
- self.discount_amount,
- self.discount_name if self.discount_name else 'Discount'
- )
- ])
- return display_str
-
- @classmethod
- def get_by_name(cls, name):
- try:
- pricing = PricingPlan.objects.get(name=name)
- except Exception as e:
- logger.error(
- "Error getting VMPricing with name {name}. "
- "Details: {details}. Attempting to return default"
- "pricing.".format(name=name, details=str(e))
- )
- pricing = PricingPlan.get_default_pricing()
- return pricing
-
- @classmethod
- def get_default_pricing(cls):
- """ Returns the default pricing or None """
- try:
- default_pricing = PricingPlan.objects.get(name='default')
- except Exception as e:
- logger.error(str(e))
- default_pricing = None
- return default_pricing
-
-###
-# Orders.
-class Order(models.Model):
- """
- Order are assumed IMMUTABLE and used as SOURCE OF TRUST for generating
- bills. Do **NOT** mutate then!
-
- An one time order is "closed" (does not need to be billed anymore)
- if it has one bill record. Having more than one is a programming
- error.
-
- A recurring order is closed if it has been replaced
- (replaces__isnull=False) AND the ending_date is set AND it was
- billed the last time it needed to be billed (how to check the last
- item?)
-
- BOTH are closed, if they are ended/closed AND have been fully
- charged.
-
- Fully charged == fully billed: sum_of_order_usage == sum_of_bill_records
-
- """
-
- owner = models.ForeignKey(get_user_model(),
- on_delete=models.CASCADE,
- editable=True)
-
- billing_address = models.ForeignKey(BillingAddress,
- on_delete=models.CASCADE)
-
- # Let's forget about this one
- customer = models.ForeignKey(StripeCustomer, on_delete=models.CASCADE, null=True)
-
- product = models.ForeignKey(Product, blank=False, null=False, on_delete=models.CASCADE)
- config = models.JSONField()
-
- creation_date = models.DateTimeField(auto_now_add=True)
- starting_date = models.DateTimeField(default=timezone.now)
- ending_date = models.DateTimeField(blank=True, null=True)
-
- recurring_period = models.ForeignKey(RecurringPeriod,
- on_delete=models.CASCADE,
- editable=True)
-
- one_time_price = models.DecimalField(default=0.0,
- max_digits=AMOUNT_MAX_DIGITS,
- decimal_places=AMOUNT_DECIMALS,
- validators=[MinValueValidator(0)])
-
- recurring_price = models.DecimalField(default=0.0,
- max_digits=AMOUNT_MAX_DIGITS,
- decimal_places=AMOUNT_DECIMALS,
- validators=[MinValueValidator(0)])
-
- currency = models.CharField(max_length=32, choices=Currency.choices, default=Currency.CHF)
-
- replaces = models.ForeignKey('self',
- related_name='replaced_by',
- on_delete=models.CASCADE,
- blank=True,
- null=True)
-
- depends_on = models.ForeignKey('self',
- related_name='parent_of',
- on_delete=models.CASCADE,
- blank=True,
- null=True)
- pricing_plan = models.ForeignKey(PricingPlan, blank=False, null=True, on_delete=models.CASCADE)
-
- should_be_billed = models.BooleanField(default=True)
-
- @property
- def earliest_ending_date(self):
- """
- Recurring orders cannot end before finishing at least one recurring period.
-
- One time orders have a recurring period of 0, so this work universally
- """
-
- return self.starting_date + datetime.timedelta(seconds=self.recurring_period.duration_seconds)
-
-
- def next_cancel_or_downgrade_date(self, until_when=None):
- """
- Return the next proper ending date after n times the
- recurring_period, where n is an integer that applies for downgrading
- or cancelling.
- """
-
- if not until_when:
- until_when = timezone.now()
-
- if until_when < self.starting_date:
- raise ValidationError("Cannot end before start of start of order")
-
- if self.recurring_period.duration_seconds > 0:
- delta = until_when - self.starting_date
-
- num_times = ceil(delta.total_seconds() / self.recurring_period.duration_seconds)
-
- next_date = self.starting_date + datetime.timedelta(seconds=num_times * self.recurring_period.duration_seconds)
- else:
- next_date = self.starting_date
-
- return next_date
-
- def get_ending_date_for_bill(self, bill):
- """
- Determine the ending date given a specific bill
- """
-
- # If the order is quit, charge the final amount / finish (????)
- # Probably not a good idea -- FIXME :continue until usual
- if self.ending_date:
- this_ending_date = self.ending_date
- else:
- if self.next_cancel_or_downgrade_date(bill.ending_date) > bill.ending_date:
- this_ending_date = self.next_cancel_or_downgrade_date(bill.ending_date)
- else:
- this_ending_date = bill.ending_date
-
- return this_ending_date
-
-
- @property
- def count_billed(self):
- """
- How many times this order was billed so far.
- This logic is mainly thought to be for recurring bills, but also works for one time bills
- """
-
- return sum([ br.quantity for br in self.bill_records.all() ])
-
- def cancel(self):
- self.ending_date = timezone.now()
- self.should_be_billed = False
- self.save()
-
- def count_used(self, when=None):
- """
- How many times this order was billed so far.
- This logic is mainly thought to be for recurring bills, but also works for one time bills
- """
-
- if self.is_one_time:
- return 1
-
- if not when:
- when = timezone.now()
-
- # Cannot be used after it ended
- if self.ending_date and when > self.ending_date:
- when = self.ending_date
-
- return (when - self.starting_date) / self.default_recurring_period
-
- @property
- def all_usage_billed(self, when=None):
- """
- Returns true if this order does not need any further billing
- ever. In other words: is this order "closed"?
- """
-
- if self.count_billed == self.count_used(when):
- return True
- else:
- return False
-
- @property
- def is_recurring(self):
- return self.recurring_price > 0
-
- @property
- def is_one_time(self):
- return not self.is_recurring
-
- @property
- def description(self):
- desc = self.product.description + "( "
- config = json.loads(self.config)
- if config and config["cores"]:
- desc = f"{desc} {config['cores']} Cores,"
- if config and config["memory"]:
- desc = f"{desc} {config['memory']} RAM,"
- if config and config["storage"]:
- desc = f"{desc} {config['storage']} GB SSD,"
- desc += " )"
- return desc
-
- def replace_with(self, new_order):
- new_order.replaces = self
- self.ending_date = end_before(new_order.starting_date)
- self.save()
-
- def update_order(self, config, starting_date=None):
- """
- Updating an order means creating a new order and reference the previous order
- """
-
- if not starting_date:
- starting_date = timezone.now()
-
- new_order = self.__class__(owner=self.owner,
- billing_address=self.billing_address,
- product=self.product,
- config=config,
- pricing_plan=self.pricing_plan,
- starting_date=starting_date,
- currency=self.currency
- )
-
- new_order.recurring_price = new_order.calculate_recurring_price()
- new_order.replaces = self
- new_order.save()
-
- self.ending_date = end_before(new_order.starting_date)
- self.save()
-
- return new_order
-
-
- def create_bill_record(self, bill):
- br = None
- if self.recurring_price != 0:
- records = BillRecord.objects.filter(order=self).all()
- if not records:
- if self.one_time_price:
- br = BillRecord.objects.create(bill=bill,
- order=self,
- starting_date=self.starting_date,
- ending_date=bill.ending_date,
- is_recurring_record=False)
- else:
- br = self.create_new_bill_record_for_recurring_order(bill)
- else:
- opened_recurring_record = BillRecord.objects.filter(bill=bill, order=self, is_recurring_record=True).first()
- if opened_recurring_record:
- br = opened_recurring_record
- self.update_bill_record_for_recurring_order(br, bill)
- else:
- br = self.create_new_bill_record_for_recurring_order(bill)
- return br
-
- def update_bill_record_for_recurring_order(self,
- bill_record,
- bill):
- """
- Possibly update a bill record according to the information in the bill
- """
-
- # If the order has an ending date set, we might need to adjust the bill_record
- if self.ending_date:
- if bill_record.ending_date != self.ending_date:
- bill_record.ending_date = self.ending_date
-
- else:
- # recurring, not terminated, should go until at least end of bill
- if bill_record.ending_date < bill.ending_date:
- bill_record.ending_date = bill.ending_date
-
- bill_record.save()
-
- def create_new_bill_record_for_recurring_order(self, bill):
- """
- Create a new bill record
- """
- last_bill_record = BillRecord.objects.filter(order=self).order_by('id').last()
-
- starting_date=self.starting_date
-
- if last_bill_record:
- # We already charged beyond the end of this bill's period
- if last_bill_record.ending_date >= bill.ending_date:
- return
-
- # This order is terminated or replaced
- if self.ending_date:
- # And the last bill record already covered us -> nothing to be done anymore
- if last_bill_record.ending_date == self.ending_date:
- return
-
- starting_date = start_after(last_bill_record.ending_date)
- ending_date = self.get_ending_date_for_bill(bill)
-
- return BillRecord.objects.create(bill=bill,
- order=self,
- starting_date=starting_date,
- ending_date=ending_date,
- is_recurring_record=True)
-
- def calculate_recurring_price(self):
- try:
- config = json.loads(self.config)
- recurring_price = self.pricing_plan.monthly_maintenance_fees
- if 'cores' in config:
- recurring_price += self.pricing_plan.cores_unit_price * int(config['cores'])
- if 'memory' in config:
- recurring_price += self.pricing_plan.ram_unit_price * int(config['memory'])
- if 'storage' in config:
- #TODO Fix the ssd static value
- recurring_price += (10 * self.pricing_plan.storage_ssd_unit_price) + (self.pricing_plan.storage_hd_unit_price * int(config['storage']))
-
- vat_rate = VATRate.get_vat_rate(self.billing_address)
- vat_validation_status = "verified" if self.billing_address.vat_number_validated_on and self.billing_address.vat_number_verified else False
- subtotal, subtotal_after_discount, price_after_discount_with_vat, vat, vat_percent, vat_amount, discount = uncloud_pay.utils.apply_vat_discount(
- recurring_price, self.pricing_plan,
- vat_rate=vat_rate * 100, vat_validation_status = vat_validation_status
- )
- return price_after_discount_with_vat
- except Exception as e:
- logger.error("An error occurred while parsing the config obj", e)
- return 0
-
- def check_parameters(self):
- if 'parameters' in self.product.config:
- for parameter in self.product.config['parameters']:
- if not parameter in self.config['parameters']:
- raise ValidationError(f"Required parameter '{parameter}' is missing.")
-
-
- def save(self, *args, **kwargs):
- # Calculate the price of the order when we create it
- # IMMUTABLE fields -- need to create new order to modify them
- # However this is not enforced here...
- if self._state.adding:
- self.recurring_price = self.calculate_recurring_price()
-
- if self.recurring_period_id is None:
- self.recurring_period = self.product.default_recurring_period
-
- try:
- prod_period = self.product.recurring_periods.get(producttorecurringperiod__recurring_period=self.recurring_period)
- except ObjectDoesNotExist:
- raise ValidationError(f"Recurring Period {self.recurring_period} not allowed for product {self.product}")
-
- self.check_parameters()
-
- if self.ending_date and self.ending_date < self.starting_date:
- raise ValidationError("End date cannot be before starting date")
-
-
- super().save(*args, **kwargs)
-
-
- def __str__(self):
- return f"Order {self.id}: {self.description}"
-
-class Bill(models.Model):
- """
- A bill is a representation of usage at a specific time
- """
- owner = models.ForeignKey(get_user_model(),
- on_delete=models.CASCADE)
-
- creation_date = models.DateTimeField(auto_now_add=True)
- starting_date = models.DateTimeField(default=start_of_this_month)
- ending_date = models.DateTimeField()
- due_date = models.DateField(default=default_payment_delay)
-
-
- billing_address = models.ForeignKey(BillingAddress,
- on_delete=models.CASCADE,
- editable=True,
- null=False)
-
- currency = models.CharField(max_length=32, choices=Currency.choices, default=Currency.CHF)
-
- # FIXME: editable=True -> is in the admin, but also editable in DRF
- # Maybe filter fields in the serializer?
-
- status = models.CharField(max_length=32, choices= (
- ('new', 'New'),
- ('cancelled', 'Cancelled'),
- ('paid', 'Paid')
- ), null=False, blank=False, default="new")
-
- class Meta:
- constraints = [
- models.UniqueConstraint(fields=['owner',
- 'starting_date',
- 'ending_date' ],
- name='one_bill_per_month_per_user')
- ]
-
- def close(self, status):
- """
- Close/finish a bill
- """
- self.status = status
- if not self.ending_date:
- self.ending_date = timezone.now()
- self.save()
-
- @property
- def sum(self):
- bill_records = BillRecord.objects.filter(bill=self)
- return sum([ br.sum for br in bill_records ])
-
- @property
- def subtotal(self):
- bill_records = BillRecord.objects.filter(bill=self)
- return sum([ br.subtotal for br in bill_records ])
-
- @property
- def vat_amount(self):
- return round(self.vat_rate * self.subtotal, 2)
-
- @property
- def vat_rate(self):
- return VATRate.get_vat_rate(self.billing_address, when=self.ending_date)
-
-
- @classmethod
- def create_bills_for_all_users(cls):
- """
- Create next bill for each user
- """
-
- for owner in get_user_model().objects.all():
- cls.create_next_bills_for_user(owner)
-
- @classmethod
- def create_next_bills_for_user(cls, owner, ending_date=None):
- """
- Create one bill per billing address, as the VAT rates might be different
- for each address
- """
-
- bills = []
- for billing_address in BillingAddress.objects.filter(owner=owner):
- bill = cls.create_next_bill_for_user_address(billing_address, ending_date)
- if bill:
- bills.append(bill)
-
- return bills
-
- @classmethod
- def create_next_bill_for_user_address(cls, billing_address, ending_date=None):
- """
- Create the next bill for a specific billing address of a user
- """
-
- owner = billing_address.owner
-
- all_orders = Order.objects.filter(Q(owner__id=owner.id), Q(should_be_billed=True),
- Q(billing_address__id=billing_address.id)
- ).order_by('id')
-
- if len(all_orders) > 0:
- bill = cls.get_or_create_bill(billing_address, ending_date=ending_date)
- for order in all_orders:
- order.create_bill_record(bill)
- return bill
- else:
- # This Customer Hasn't any active orders
- return False
-
- @classmethod
- def create_next_bill_for_order(cls, order, ending_date=None):
- """
- Create the next bill for a specific order of a user
- """
- bill = cls.get_or_create_bill(order.billing_address, ending_date=ending_date)
- order.create_bill_record(bill)
- return bill
-
-
- @classmethod
- def get_or_create_bill(cls, billing_address, ending_date=None):
- """
- Get / reuse last bill if it is not yet closed
-
- Create bill, if there is no bill or if bill is closed.
- """
-
- last_bill = cls.objects.filter(billing_address=billing_address).order_by('id').last()
-
- all_orders = Order.objects.filter(billing_address=billing_address).order_by('id')
- first_order = all_orders.first()
-
- bill = None
-
- # Get date & bill from previous bill, if it exists
- if last_bill:
- if last_bill.status == 'new':
- bill = last_bill
- starting_date = last_bill.starting_date
- ending_date = bill.ending_date
- else:
- starting_date = last_bill.ending_date + datetime.timedelta(seconds=1)
- else:
- # Might be an idea to make this the start of the month, too
- if first_order:
- starting_date = first_order.starting_date
- else:
- starting_date = timezone.now()
-
- if not ending_date:
- ending_date = end_of_month(starting_date)
-
- if not bill:
- bill = cls.objects.create(
- owner=billing_address.owner,
- starting_date=starting_date,
- ending_date=ending_date,
- billing_address=billing_address)
-
-
- return bill
-
- def __str__(self):
- return f"{self.owner}-{self.id}"
-
-
-class BillRecord(models.Model):
- """
- Entry of a bill, dynamically generated from an order.
- """
-
- bill = models.ForeignKey(Bill, on_delete=models.CASCADE, related_name='bill_records')
- order = models.ForeignKey(Order, on_delete=models.CASCADE)
-
- creation_date = models.DateTimeField(auto_now_add=True)
- starting_date = models.DateTimeField()
- ending_date = models.DateTimeField()
-
- is_recurring_record = models.BooleanField(blank=False, null=False)
-
- @property
- def quantity(self):
- """ Determine the quantity by the duration"""
- if not self.is_recurring_record:
- return 1
-
- record_delta = self.ending_date.date() - self.starting_date.date()
- if self.order.recurring_period and self.order.recurring_period.duration_seconds > 0:
- return int(record_delta.total_seconds() / self.order.recurring_period.duration_seconds)
- else:
- return 1
-
- @property
- def sum(self):
- if self.is_recurring_record:
- return round(float(self.order.recurring_price) * self.quantity, 2)
- else:
- return self.order.one_time_price
-
- @property
- def description(self):
- if self.order:
- return self.order.description
- return ''
-
- @property
- def price(self):
- if self.is_recurring_record:
- return self.order.recurring_price
- else:
- return self.order.one_time_price
-
- @property
- def subtotal(self):
- billing_address_ins = self.order.billing_address
- vat_rate = VATRate.get_vat_rate(billing_address_ins)
- vat_validation_status = "verified" if billing_address_ins.vat_number_validated_on and billing_address_ins.vat_number_verified else False
- config = json.loads(self.order.config)
- pricing = uncloud_pay.utils.get_order_total_with_vat(
- config["cores"], config["memory"], config["storage"], self.order.pricing_plan.name,
- vat_rate=vat_rate * 100, vat_validation_status = vat_validation_status
- )
- return pricing['subtotal_after_discount']
-
- def __str__(self):
- if self.is_recurring_record:
- bill_line = f"{self.starting_date} - {self.ending_date}: {self.quantity} x {self.order}"
- else:
- bill_line = f"{self.starting_date}: {self.order}"
-
- return bill_line
-
- def save(self, *args, **kwargs):
- if self.ending_date < self.starting_date:
- raise ValidationError("End date cannot be before starting date")
-
- super().save(*args, **kwargs)
-
-
-class ProductToRecurringPeriod(models.Model):
- """
- Intermediate manytomany mapping class that allows storing the default recurring period
- for a product
- """
-
- recurring_period = models.ForeignKey(RecurringPeriod, on_delete=models.CASCADE)
- product = models.ForeignKey(Product, on_delete=models.CASCADE)
-
- is_default = models.BooleanField(default=False)
-
- class Meta:
- constraints = [
- models.UniqueConstraint(fields=['product'],
- condition=models.Q(is_default=True),
- name='one_default_recurring_period_per_product'),
- models.UniqueConstraint(fields=['product', 'recurring_period'],
- name='recurring_period_once_per_product')
- ]
-
- def __str__(self):
- return f"{self.product} - {self.recurring_period} (default: {self.is_default})"
-
-
-class Membership(models.Model):
- owner = models.ForeignKey(get_user_model(),
- on_delete=models.CASCADE)
-
- starting_date = models.DateField(blank=True, null=True)
- ending_date = models.DateField(blank=True, null=True)
-
-
- @classmethod
- def user_has_membership(user, when):
- """
- Return true if user has membership at a point of time,
- return false if that is not the case
- """
-
- pass
-
- # cls.objects.filter(owner=user,
- # starting_date)
diff --git a/uncloud_pay/selectors.py b/uncloud_pay/selectors.py
deleted file mode 100644
index 098e90b..0000000
--- a/uncloud_pay/selectors.py
+++ /dev/null
@@ -1,25 +0,0 @@
-from django.utils import timezone
-from django.db import transaction
-from .models import *
-
-def get_deposit_payments_for_user(user):
- payments = [ payment.amount for payment in Payment.objects.filter(owner=user, type='deposit')]
- return sum(payments)
-
-def get_spendings_for_user(user):
- spendings = [payment.amount for payment in Payment.objects.filter(owner=user, type='withdraw')]
- return sum(spendings)
-
-@transaction.atomic
-def get_balance_for_user(user):
- return float(get_deposit_payments_for_user(user) - get_spendings_for_user(user))
-
-@transaction.atomic
-def has_enough_balance(user, due_amount):
- balance = get_balance_for_user(user)
- if balance >= due_amount:
- return True
- return False
-
-def get_billing_address_for_user(user):
- return BillingAddress.objects.filter(owner=user, active=True).first()
diff --git a/uncloud_pay/serializers.py b/uncloud_pay/serializers.py
deleted file mode 100644
index 9f52574..0000000
--- a/uncloud_pay/serializers.py
+++ /dev/null
@@ -1,132 +0,0 @@
-from django.contrib.auth import get_user_model
-from rest_framework import serializers
-from uncloud_auth.serializers import UserSerializer
-from django.utils.translation import gettext_lazy as _
-from stripe.error import CardError
-
-from .models import *
-import uncloud_pay.stripe as uncloud_stripe
-from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
-
-###
-# 2020-12 Checked code
-
-class StripeCreditCardSerializer(serializers.HyperlinkedModelSerializer):
- class Meta:
- model = StripeCreditCard
- exclude = [ "card_id", "owner" ]
- read_only_fields = [ "last4", "brand", "expiry_date" ]
-
-class PaymentSerializer(serializers.ModelSerializer):
- owner = serializers.HiddenField(default=serializers.CurrentUserDefault())
-
- class Meta:
- model = Payment
- fields = '__all__'
- read_only_fields = [ "external_reference", "source", "timestamp" ]
-
- def validate(self, data):
- data["source"] = "stripe"
- return data
-
- def create(self, validated_data):
- try:
- if validated_data['type'] == 'deposit':
- return Payment.deposit(validated_data['owner'], validated_data['amount'], validated_data['source'], currency=validated_data['currency'], notes=validated_data['notes'])
- else:
- return Payment.objects.create(**validated_data)
- except CardError as err:
- raise serializers.ValidationError(err.user_message)
-
-class BalanceSerializer(serializers.Serializer):
- balance = serializers.DecimalField(max_digits=AMOUNT_MAX_DIGITS, decimal_places=AMOUNT_DECIMALS)
-
-class BillingAddressSerializer(serializers.ModelSerializer):
- class Meta:
- model = BillingAddress
- exclude = [ "owner" ]
-
-
-class VATRateSerializer(serializers.ModelSerializer):
-
- class Meta:
- model = VATRate
- fields = '__all__'
-
-
-################################################################################
-# Unchecked code
-
-
-###
-# Orders & Products.
-
-class OrderSerializer(serializers.ModelSerializer):
- owner = serializers.PrimaryKeyRelatedField(queryset=get_user_model().objects.all())
-
- def __init__(self, *args, **kwargs):
- # Don't pass the 'fields' arg up to the superclass
- admin = kwargs.pop('admin', None)
-
- # Instantiate the superclass normally
- super(OrderSerializer, self).__init__(*args, **kwargs)
-
- # Only allows owner in admin mode.
- if not admin:
- self.fields.pop('owner')
-
- def create(self, validated_data):
- billing_address = BillingAddress.get_preferred_address_for(validated_data["owner"])
- instance = Order(billing_address=billing_address, **validated_data)
- instance.save()
-
- return instance
-
- def validate_owner(self, value):
- if BillingAddress.get_preferred_address_for(value) == None:
- raise serializers.ValidationError("Owner does not have a valid billing address.")
-
- return value
-
- class Meta:
- model = Order
- read_only_fields = ['replaced_by', 'depends_on']
- fields = ['owner', 'description', 'creation_date', 'starting_date', 'ending_date',
- 'recurring_period', 'recurring_price', 'one_time_price',
- 'config', 'pricing_plan', 'should_be_billed'] + read_only_fields
-
-
-###
-# Bills
-
-# TODO: remove magic numbers for decimal fields
-class BillRecordSerializer(serializers.Serializer):
- order = serializers.HyperlinkedRelatedField(
- view_name='order-detail',
- read_only=True)
- description = serializers.CharField()
- one_time_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
- recurring_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
-# recurring_period = serializers.ChoiceField()
- recurring_count = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
- vat_rate = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
- vat_amount = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
- amount = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
- total = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
-
-class BillSerializer(serializers.ModelSerializer):
- billing_address = BillingAddressSerializer(read_only=True)
- records = BillRecordSerializer(many=True, read_only=True)
-
- class Meta:
- model = Bill
- fields = ['owner', 'sum', 'vat_rate',
- 'due_date', 'creation_date', 'starting_date', 'ending_date',
- 'records', 'status', 'billing_address']
-
-# We do not want users to mutate the country / VAT number of an address, as it
-# will change VAT on existing bills.
-class UpdateBillingAddressSerializer(serializers.ModelSerializer):
- class Meta:
- model = BillingAddress
- fields = ['street', 'city', 'postal_code']
diff --git a/uncloud_pay/services.py b/uncloud_pay/services.py
deleted file mode 100644
index 84a7c8d..0000000
--- a/uncloud_pay/services.py
+++ /dev/null
@@ -1,34 +0,0 @@
-import datetime
-from calendar import monthrange
-from django.utils import timezone
-
-
-def start_of_month(a_day):
- """ Returns first of the month of a given datetime object"""
- return a_day.replace(day=1,hour=0,minute=0,second=0, microsecond=0)
-
-def end_of_month(a_day):
- """ Returns first of the month of a given datetime object"""
-
- _, last_day = monthrange(a_day.year, a_day.month)
- return a_day.replace(day=last_day,hour=23,minute=59,second=59, microsecond=0)
-
-def start_of_this_month():
- """ Returns first of this month"""
- a_day = timezone.now()
- return a_day.replace(day=1,hour=0,minute=0,second=0, microsecond=0)
-
-def end_of_this_month():
- """ Returns first of this month"""
- a_day = timezone.now()
-
- _, last_day = monthrange(a_day.year, a_day.month)
- return a_day.replace(day=last_day,hour=23,minute=59,second=59, microsecond=0)
-
-def end_before(a_date):
- """ Return suitable datetimefield for ending just before a_date """
- return a_date - datetime.timedelta(seconds=1)
-
-def start_after(a_date):
- """ Return suitable datetimefield for starting just after a_date """
- return a_date + datetime.timedelta(seconds=1)
diff --git a/uncloud_pay/static/uncloud_pay/css/invoice.css b/uncloud_pay/static/uncloud_pay/css/invoice.css
deleted file mode 100644
index 3d8d04f..0000000
--- a/uncloud_pay/static/uncloud_pay/css/invoice.css
+++ /dev/null
@@ -1,114 +0,0 @@
-body {
- font-family: Avenir;
- background: white;
- font-weight: 500;
- line-height: 1.1em;
- font-size: 16px;
- margin: auto;
-}
-p {
- display: block;
- -webkit-margin-before: 14px;
- -webkit-margin-after: 14px;
- -webkit-margin-start: 0px;
- -webkit-margin-end: 0px;
-}
-.bold {
- font-weight: bold;
-}
-.d1 {
- line-height:1.1em;
- width: 60%;
- float: left;
-}
-.d2 {
- line-height:1.5em;
- padding-top: 15px;
- font-style: normal;
- width: 40%;
- float: left;
-}
-.d4 {
- line-height:1.5em;
- width:40%;
- float: left;
-}
-.b1 {
- width: 45%;
- float: left;
-}
-.b2 {
- width: 55%;
- float: left;
- text-align: right;
- left: 0;
-}
-.d5 {
- width: 100%;
-}
-.d6 {
- width: 68%;
- float: left;
- font-size: 13px;
-}
-.d7 {
- width: 32%;
- float: left;
-}
-.wf {
- width: 100%;
-}
-hr {
- border: 0;
- clear:both;
- display: inline-block;
- width: 100%;
- background-color:gray;
- height: 1px;
- }
- .tl {
- text-align: left;
- margin-left: 5px;
- }
-
- .tr {
- text-align: right;
- margin-right: 5px;
- float: right;
- }
- .tc {
- text-align: center;
- }
- .pc p {
- display: block;
- -webkit-margin-before: 3px;
- -webkit-margin-after: 5px;
- -webkit-margin-start: 0px;
- -webkit-margin-end: 0px;
-}
- .th {
- border-top: 1px solid gray;
- border-bottom: 1px solid gray;
-
- }
- .ts {
- font-size: 14px;
- }
- .icon {
- width: 16px;
- height: 14px;
- vertical-align: middle;
- margin-right: 2px;
- }
- .footer {
- margin-top: 70px;
- font-size: 14px;
- }
-
- .footer p {
- display: block;
- -webkit-margin-before: 5px;
- -webkit-margin-after: 5px;
- -webkit-margin-start: 0px;
- -webkit-margin-end: 0px;
-}
\ No newline at end of file
diff --git a/uncloud_pay/static/uncloud_pay/images/call.png b/uncloud_pay/static/uncloud_pay/images/call.png
deleted file mode 100644
index e774362..0000000
Binary files a/uncloud_pay/static/uncloud_pay/images/call.png and /dev/null differ
diff --git a/uncloud_pay/static/uncloud_pay/images/company-small.jpg b/uncloud_pay/static/uncloud_pay/images/company-small.jpg
deleted file mode 100644
index a7b575f..0000000
Binary files a/uncloud_pay/static/uncloud_pay/images/company-small.jpg and /dev/null differ
diff --git a/uncloud_pay/static/uncloud_pay/images/home.png b/uncloud_pay/static/uncloud_pay/images/home.png
deleted file mode 100644
index 24428e7..0000000
Binary files a/uncloud_pay/static/uncloud_pay/images/home.png and /dev/null differ
diff --git a/uncloud_pay/static/uncloud_pay/images/msg.png b/uncloud_pay/static/uncloud_pay/images/msg.png
deleted file mode 100644
index 3b7b0c7..0000000
Binary files a/uncloud_pay/static/uncloud_pay/images/msg.png and /dev/null differ
diff --git a/uncloud_pay/static/uncloud_pay/images/twitter.png b/uncloud_pay/static/uncloud_pay/images/twitter.png
deleted file mode 100644
index 4db6da0..0000000
Binary files a/uncloud_pay/static/uncloud_pay/images/twitter.png and /dev/null differ
diff --git a/uncloud_pay/static/uncloud_pay/webfonts/Avenir-Book.ttf b/uncloud_pay/static/uncloud_pay/webfonts/Avenir-Book.ttf
deleted file mode 100644
index 84ae914..0000000
Binary files a/uncloud_pay/static/uncloud_pay/webfonts/Avenir-Book.ttf and /dev/null differ
diff --git a/uncloud_pay/stripe.py b/uncloud_pay/stripe.py
deleted file mode 100644
index 7d8d757..0000000
--- a/uncloud_pay/stripe.py
+++ /dev/null
@@ -1,255 +0,0 @@
-import stripe
-import stripe.error
-import logging
-import datetime
-
-from django.core.exceptions import ObjectDoesNotExist, ValidationError
-from django.conf import settings
-from django.contrib.auth import get_user_model
-
-from .models import StripeCustomer, StripeCreditCard
-
-logger = logging.getLogger(__name__)
-
-CURRENCY = 'chf'
-
-stripe.api_key = settings.STRIPE_KEY
-
-def handle_stripe_error(f):
- def handle_problems(*args, **kwargs):
- response = {
- 'paid': False,
- 'response_object': None,
- 'error': None
- }
-
- common_message = "Currently it is not possible to make payments. Please try agin later."
- try:
- response_object = f(*args, **kwargs)
- return response_object
- except stripe.error.CardError as e:
- # Since it's a decline, stripe.error.CardError will be caught
- body = e.json_body
- logging.error(str(e))
-
- raise e # For error handling.
- except stripe.error.RateLimitError:
- logging.error("Too many requests made to the API too quickly.")
- raise Exception(common_message)
- except stripe.error.InvalidRequestError as e:
- logging.error(str(e))
- raise Exception('Invalid parameters.')
- except stripe.error.AuthenticationError as e:
- # Authentication with Stripe's API failed
- # (maybe you changed API keys recently)
- logging.error(str(e))
- raise Exception(common_message)
- except stripe.error.APIConnectionError as e:
- logging.error(str(e))
- raise Exception(common_message)
- except stripe.error.StripeError as e:
- # XXX: maybe send email
- logging.error(str(e))
- raise Exception(common_message)
-
- return handle_problems
-
-def public_api_key():
- return settings.STRIPE_PUBLIC_KEY
-
-def get_customer_id_for(user):
- try:
- # .get() raise if there is no matching entry.
- return StripeCustomer.objects.get(owner=user).stripe_id
- except ObjectDoesNotExist:
- # No entry yet - making a new one.
- try:
- customer = create_customer(user.username, user.email)
- uncloud_stripe_mapping = StripeCustomer.objects.create(
- owner=user, stripe_id=customer.id)
- return uncloud_stripe_mapping.stripe_id
- except Exception as e:
- return None
-
-@handle_stripe_error
-def create_setup_intent(customer_id):
- return stripe.SetupIntent.create(customer=customer_id)
-
-@handle_stripe_error
-def get_setup_intent(setup_intent_id):
- return stripe.SetupIntent.retrieve(setup_intent_id)
-
-@handle_stripe_error
-def get_payment_method(payment_method_id):
- return stripe.PaymentMethod.retrieve(payment_method_id)
-
-@handle_stripe_error
-def get_card_from_payment(user, payment_method_id):
- payment_method = stripe.PaymentMethod.retrieve(payment_method_id)
- if payment_method:
- if 'card' in payment_method:
- sync_cards_for_user(user)
- return payment_method['card']
- return False
-
-
-@handle_stripe_error
-def attach_payment_method(payment_method_id, user):
- customer_id = get_customer_id_for(user)
- ret = stripe.PaymentMethod.attach(payment_method_id, customer=customer_id)
- sync_cards_for_user(user)
- return ret
-
-@handle_stripe_error
-def create_customer(name, email):
- return stripe.Customer.create(name=name, email=email)
-
-@handle_stripe_error
-def get_customer(customer_id):
- return stripe.Customer.retrieve(customer_id)
-
-@handle_stripe_error
-def get_customer_cards(customer_id):
-
- cards = []
- stripe_cards = stripe.PaymentMethod.list(
- customer=customer_id,
- type="card",
- )
-
- for stripe_card in stripe_cards["data"]:
- card = {}
- card['brand'] = stripe_card["card"]["brand"]
- card['last4'] = stripe_card["card"]["last4"]
- card['month'] = stripe_card["card"]["exp_month"]
- card['year'] = stripe_card["card"]["exp_year"]
- card['id'] = stripe_card["id"]
-
- cards.append(card)
-
- return cards
-
-@handle_stripe_error
-def delete_card(card_id):
- return stripe.PaymentMethod.detach(card_id)
-
-def sync_cards_for_user(user):
- customer_id = get_customer_id_for(user)
- cards = get_customer_cards(customer_id)
-
- active_cards = StripeCreditCard.objects.filter(owner=user,
- active=True)
-
- if len(active_cards) > 0:
- has_active_card = True
- else:
- has_active_card = False
-
- for card in cards:
- active = False
-
- if not has_active_card:
- active = True
- has_active_card = True
-
- StripeCreditCard.objects.get_or_create(card_id=card['id'],
- owner = user,
- defaults = {
- 'last4': card['last4'],
- 'brand': card['brand'],
- 'expiry_date': datetime.date(card['year'],
- card['month'],
- 1),
- 'active': active
- }
- )
-
-@handle_stripe_error
-def charge_customer(user, amount, currency='CHF', card=False):
- # Amount is in CHF but stripes requires smallest possible unit.
- # https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount
- # FIXME: might need to be adjusted for other currencies
-
- if currency == 'CHF':
- adjusted_amount = int(amount * 100)
- else:
- return Exception("Programming error: unsupported currency")
-
- try:
- card = card or StripeCreditCard.objects.get(owner=user,
- active=True)
-
- except StripeCreditCard.DoesNotExist:
- raise ValidationError("No active credit card - cannot create payment")
-
- customer_id = get_customer_id_for(user)
-
- return stripe.PaymentIntent.create(
- amount=adjusted_amount,
- currency=currency,
- customer=customer_id,
- payment_method=card.card_id,
- off_session=True,
- confirm=True,
- )
-
-@handle_stripe_error
-def get_payment_intent(user, amount, currency='CHF', card=False):
- # Amount is in CHF but stripes requires smallest possible unit.
- # https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount
- # FIXME: might need to be adjusted for other currencies
-
- if currency == 'CHF':
- adjusted_amount = int(amount * 100)
- else:
- return Exception("Programming error: unsupported currency")
-
- try:
- card = card or StripeCreditCard.objects.get(owner=user,
- active=True)
-
- except StripeCreditCard.DoesNotExist:
- raise ValidationError("No active credit card - cannot create payment")
-
- customer_id = get_customer_id_for(user)
-
- return stripe.PaymentIntent.create(
- amount=adjusted_amount,
- currency=currency,
- customer=customer_id,
- payment_method=card.card_id,
- setup_future_usage='off_session',
- confirm=False,
- )
-
-@handle_stripe_error
-def get_or_create_tax_id_for_user(stripe_customer_id, vat_number,
- type="eu_vat", country=""):
- def compare_vat_numbers(vat1, vat2):
- _vat1 = vat1.replace(" ", "").replace(".", "").replace("-","")
- _vat2 = vat2.replace(" ", "").replace(".", "").replace("-","")
- return True if _vat1 == _vat2 else False
-
- tax_ids_list = stripe.Customer.list_tax_ids(
- stripe_customer_id,
- limit=100,
- )
- for tax_id_obj in tax_ids_list.data:
- if compare_vat_numbers(tax_id_obj.value, vat_number):
- return tax_id_obj
- else:
- logger.debug(
- "{val1} is not equal to {val2} or {con1} not same as "
- "{con2}".format(val1=tax_id_obj.value, val2=vat_number,
- con1=tax_id_obj.country.lower(),
- con2=country.lower().strip()))
- logger.debug(
- "tax id obj does not exist for {val}. Creating a new one".format(
- val=vat_number
- ))
- tax_id_obj = stripe.Customer.create_tax_id(
- stripe_customer_id,
- type=type,
- value=vat_number,
- )
- return tax_id_obj
diff --git a/uncloud_pay/templates/uncloud_pay/bill.html.j2 b/uncloud_pay/templates/uncloud_pay/bill.html.j2
deleted file mode 100644
index 7cf10f8..0000000
--- a/uncloud_pay/templates/uncloud_pay/bill.html.j2
+++ /dev/null
@@ -1,1059 +0,0 @@
-{% load static %}
-
-
-
-
-
-
-
-
- {{ bill }}
-
-
-
-
-
-
-
-
-

-
-
-
-
- ungleich glarus ag
-
Bahnhofstrasse 1
-
8783 Linthal
-
Switzerland
-
-
-
- {{ bill.billing_address.organization }}
- {{ bill.billing_address.name }}
- {{ bill.owner.email }}
- {{ bill.billing_address.street }}
- {{ bill.billing_address.country }} {{ bill.billing_address.postal_code }} {{ bill.billing_address.city }}
-
-
-
-
- Bill id: {{ bill }}
-
{{ bill.starting_date|date:"Ymd" }} -
- {{ bill.ending_date|date:"Ymd" }}
-
-
-
-
-
Invoice
-
-
-
-
- | Detail |
- Price/Unit |
- Units |
- Total price |
-
-
-
- {% for record in bill_records %}
-
- | {{ record.starting_date|date:"Ymd-H:i:s" }}
- - {{ record.ending_date|date:"Ymd-H:i:s" }}
- {{ record.order }}
- |
- {{ record.price|floatformat:2 }} |
- {{ record.quantity|floatformat:2 }} |
- {{ record.sum|floatformat:2 }} |
-
- {% endfor %}
-
-
-
-
- Total (excl. VAT)
- {{ bill.amount }}
-
-
- VAT 7.7%
- {{ bill.vat_amount|floatformat:2 }}
-
-
-
-
- Total amount to be paid
- {{ bill.sum|floatformat:2 }}
-
-
-
-
-
-
diff --git a/uncloud_pay/templates/uncloud_pay/includes/_card.html b/uncloud_pay/templates/uncloud_pay/includes/_card.html
deleted file mode 100644
index 9cb9462..0000000
--- a/uncloud_pay/templates/uncloud_pay/includes/_card.html
+++ /dev/null
@@ -1,33 +0,0 @@
-{% load i18n %}
-
diff --git a/uncloud_pay/templates/uncloud_pay/includes/invoice_footer.html b/uncloud_pay/templates/uncloud_pay/includes/invoice_footer.html
deleted file mode 100644
index aebdab5..0000000
--- a/uncloud_pay/templates/uncloud_pay/includes/invoice_footer.html
+++ /dev/null
@@ -1,78 +0,0 @@
-{% load static i18n %}
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/uncloud_pay/templates/uncloud_pay/invoice.html b/uncloud_pay/templates/uncloud_pay/invoice.html
deleted file mode 100644
index e8a3cfa..0000000
--- a/uncloud_pay/templates/uncloud_pay/invoice.html
+++ /dev/null
@@ -1,129 +0,0 @@
-{% load static i18n %}
-
-
-
-
-
-
-
- {%trans "Invoice: " %} {{bill.id}}
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/uncloud_pay/templates/uncloud_pay/register_stripe.html b/uncloud_pay/templates/uncloud_pay/register_stripe.html
deleted file mode 100644
index 6d192a4..0000000
--- a/uncloud_pay/templates/uncloud_pay/register_stripe.html
+++ /dev/null
@@ -1,80 +0,0 @@
-{% extends 'uncloud/base.html' %}
-
-{% block head_extra %}
-
-
-{% endblock %}
-
-{% block content %}
-
- {% csrf_token %}
-
-
Register Credit Card with Stripe
-
- By submitting I authorise to send instructions to
- the financial institution that issued my card to take
- payments from my card account in accordance with the
- terms of my agreement with you.
-
-
-
-
-
-
-
-
The card will be registered with stripe.
-
-
-
-
-
-
-
-{% endblock %}
diff --git a/uncloud_pay/tests.py b/uncloud_pay/tests.py
deleted file mode 100644
index 4f2aded..0000000
--- a/uncloud_pay/tests.py
+++ /dev/null
@@ -1,546 +0,0 @@
-from django.test import TestCase
-from django.contrib.auth import get_user_model
-from datetime import datetime, date, timedelta
-from django.utils import timezone
-
-from .models import *
-from uncloud_service.models import GenericServiceProduct
-from uncloud.models import UncloudProvider, UncloudNetwork
-
-import json
-
-vm_product_config = {
- 'features': {
- 'cores':
- { 'min': 1,
- 'max': 48
- },
- 'ram_gb':
- { 'min': 1,
- 'max': 256
- },
- },
-}
-
-vm_order_config = json.dumps({
- 'cores': 1,
- 'memory': 2,
- 'storage': 100
-})
-
-vm_order_downgrade_config = {
- 'features': {
- 'cores': 1,
- 'ram_gb': 1
- }
-}
-
-vm_order_upgrade_config = {
- 'features': {
- 'cores': 4,
- 'ram_gb': 4
- }
-}
-
-
-class ProductTestCase(TestCase):
- """
- Test products and products <-> order interaction
- """
-
- def setUp(self):
- self.user = get_user_model().objects.create(
- username='random_user',
- email='jane.random@domain.tld')
-
- self.ba = BillingAddress.objects.create(
- owner=self.user,
- organization = 'Test org',
- street="unknown",
- city="unknown",
- postal_code="somewhere else",
- active=True)
-
- RecurringPeriod.populate_db_defaults()
- self.default_recurring_period = RecurringPeriod.objects.get(name="Per 30 days")
-
- def test_create_product(self):
- """
- Create a sample product
- """
- p = Product.objects.create(name="Testproduct",
- description="Only for testing",
- config=vm_product_config)
-
- p.recurring_periods.add(self.default_recurring_period,
- through_defaults= { 'is_default': True })
-
-
-class OrderTestCase(TestCase):
- """
- The heart of ordering products
- """
-
- def setUp(self):
- self.pricing_plan = PricingPlan.objects.create(name="PricingSample", set_up_fees=35, cores_unit_price=3,
- ram_unit_price=4, storage_ssd_unit_price=0.35, storage_hd_unit_price=0.02)
- self.user = get_user_model().objects.create(
- username='random_user',
- email='jane.random@domain.tld')
-
- self.ba = BillingAddress.objects.create(
- owner=self.user,
- organization = 'Test org',
- street="unknown",
- city="unknown",
- postal_code="somewhere else",
- active=True)
-
- self.product = Product.objects.create(name="Testproduct",
- description="Only for testing",
- config=vm_product_config)
-
- RecurringPeriod.populate_db_defaults()
- self.default_recurring_period = RecurringPeriod.objects.get(name="Per 30 days")
-
- self.product.recurring_periods.add(self.default_recurring_period,
- through_defaults= { 'is_default': True })
-
-
- def test_order_invalid_recurring_period(self):
- """
- Order a products with a recurringperiod that is not added to the product
- """
-
- order_config = json.dumps({
- 'cores': 1,
- 'memory':2,
- 'storage': 100
- })
- o = Order.objects.create(owner=self.user,
- billing_address=self.ba,
- pricing_plan = self.pricing_plan,
- product=self.product,
- config=order_config)
-
-
- def test_order_product(self):
- """
- Order a product, ensure the order has correct price setup
- """
- order_config = json.dumps({
- 'cores': 1,
- 'memory':2,
- 'storage': 100
- })
- o = Order.objects.create(owner=self.user,
- billing_address=self.ba,
- pricing_plan = self.pricing_plan,
- product=self.product,
- config=order_config)
-
- self.assertEqual(o.one_time_price, 0)
- self.assertEqual(o.recurring_price, 16.5)
-
- def test_change_order(self):
- """
- Change an order and ensure that
- - a new order is created
- - the price is correct in the new order
- """
- order_config = json.dumps({
- 'cores': 2,
- 'memory':4,
- 'storage': 200,
- })
- order1 = Order.objects.create(owner=self.user,
- billing_address=self.ba,
- pricing_plan = self.pricing_plan,
- product=self.product,
- config=order_config)
-
- self.assertEqual(order1.one_time_price, 0)
- self.assertEqual(order1.recurring_price, 29.5)
-
-
-class ModifyOrderTestCase(TestCase):
- """
- Test typical order flows like
- - cancelling
- - downgrading
- - upgrading
- """
-
- def setUp(self):
- self.user = get_user_model().objects.create(
- username='random_user',
- email='jane.random@domain.tld')
- self.pricing_plan = PricingPlan.objects.create(name="PricingSample", set_up_fees=35, cores_unit_price=3,
- ram_unit_price=4, storage_ssd_unit_price=0.35, storage_hd_unit_price=0.02)
- self.order1_config = json.dumps({
- 'cores': 2,
- 'memory':4,
- 'storage': 200
- })
- self.order2_config = json.dumps({
- 'cores': 1,
- 'memory':2,
- 'storage': 100
- })
- self.ba = BillingAddress.objects.create(
- owner=self.user,
- organization = 'Test org',
- street="unknown",
- city="unknown",
- postal_code="somewhere else",
- active=True)
-
- self.product = Product.objects.create(name="Testproduct",
- description="Only for testing",
- config=vm_product_config)
-
- RecurringPeriod.populate_db_defaults()
- self.default_recurring_period = RecurringPeriod.objects.get(name="Per 30 days")
-
- self.product.recurring_periods.add(self.default_recurring_period,
- through_defaults= { 'is_default': True })
-
-
- def test_change_order(self):
- """
- Test changing an order
-
- Expected result:
-
- - Old order should be closed before new order starts
- - New order should start at starting data
- """
-
- user = self.user
-
- starting_price = 16
- downgrade_price = 8
-
- starting_date = timezone.make_aware(datetime.datetime(2019,3,3))
- ending1_date = starting_date + datetime.timedelta(days=15)
- change1_date = start_after(ending1_date)
-
- bill_ending_date = change1_date + datetime.timedelta(days=1)
-
-
- order1 = Order.objects.create(owner=self.user,
- billing_address=BillingAddress.get_address_for(self.user),
- product=self.product,
- config=self.order1_config,
- pricing_plan=self.pricing_plan,
- starting_date=starting_date)
-
- order1.update_order(self.order2_config, starting_date=change1_date)
-
- bills = Bill.create_next_bills_for_user(user, ending_date=bill_ending_date)
-
- bill = bills[0]
- bill_records = BillRecord.objects.filter(bill=bill)
-
- self.assertEqual(len(bill_records), 2)
-
- self.assertEqual(bill_records[0].starting_date, starting_date)
- self.assertEqual(bill_records[0].ending_date, ending1_date)
-
- self.assertEqual(bill_records[1].starting_date, change1_date)
-
-
-
- def test_downgrade_product(self):
- """
- Test downgrading behaviour:
-
- We create a recurring product (recurring time: 30 days) and downgrade after 15 days.
-
- We create the bill right AFTER the end of the first order.
-
- Expected result:
-
- - First bill record for 30 days
- - Second bill record starting after 30 days
- - Bill contains two bill records
-
- """
-
- user = self.user
-
- starting_price = 16
- downgrade_price = 8
-
- starting_date = timezone.make_aware(datetime.datetime(2019,3,3))
- first_order_should_end_at = starting_date + datetime.timedelta(days=30)
- change1_date = start_after(starting_date + datetime.timedelta(days=15))
- bill_ending_date = change1_date + datetime.timedelta(days=1)
- order1 = Order.objects.create(owner=self.user,
- billing_address=BillingAddress.get_address_for(self.user),
- product=self.product,
- pricing_plan=self.pricing_plan,
- config=self.order1_config,
- starting_date=starting_date)
-
- bills = Bill.create_next_bills_for_user(user, ending_date=bill_ending_date)
-
- bill = bills[0]
- bill_records = BillRecord.objects.filter(bill=bill)
-
- self.assertEqual(len(bill_records), 1)
- self.assertEqual(bill_records[0].starting_date, starting_date)
-
- order1.update_order(self.order2_config, starting_date=change1_date)
- bills = Bill.create_next_bills_for_user(user, ending_date=bill_ending_date)
- bill_records = BillRecord.objects.filter(bill=bill)
- self.assertEqual(len(bill_records), 2)
- self.assertEqual(bill_records[0].order.ending_date.date(), change1_date.date())
-
-
-class BillTestCase(TestCase):
- """
- Test aspects of billing / creating a bill
- """
-
- def setUp(self):
- RecurringPeriod.populate_db_defaults()
-
- self.pricing_plan = PricingPlan.objects.create(name="PricingSample", set_up_fees=35, cores_unit_price=3,
- ram_unit_price=4, storage_ssd_unit_price=0.35, storage_hd_unit_price=0.02)
-
- self.user_without_address = get_user_model().objects.create(
- username='no_home_person',
- email='far.away@domain.tld')
-
- self.user = get_user_model().objects.create(
- username='jdoe',
- email='john.doe@domain.tld')
-
- self.recurring_user = get_user_model().objects.create(
- username='recurrent_product_user',
- email='jane.doe@domain.tld')
-
- self.user_addr = BillingAddress.objects.create(
- owner=self.user,
- organization = 'Test org',
- street="unknown",
- city="unknown",
- postal_code="unknown",
- active=True)
-
- self.recurring_user_addr = BillingAddress.objects.create(
- owner=self.recurring_user,
- organization = 'Test org',
- street="Somewhere",
- city="Else",
- postal_code="unknown",
- active=True)
-
- self.order_meta = {}
- self.order_meta[1] = {
- 'starting_date': timezone.make_aware(datetime.datetime(2020,3,3)),
- 'ending_date': timezone.make_aware(datetime.datetime(2020,4,17)),
- 'price': 15,
- 'description': ''
- }
-
- self.product = Product.objects.create(name="Product Sample",
- description="Not only for testing, but for joy",
- config=vm_product_config)
-
-
- self.vm = Product.objects.create(name="Super Fast VM",
- description="Zooooom",
- config=vm_product_config)
-
-
- RecurringPeriod.populate_db_defaults()
- self.default_recurring_period = RecurringPeriod.objects.get(name="Per 30 days")
-
- self.onetime_recurring_period = RecurringPeriod.objects.get(name="Onetime")
-
- self.product.recurring_periods.add(self.onetime_recurring_period,
- through_defaults= { 'is_default': True })
-
- self.vm.recurring_periods.add(self.default_recurring_period,
- through_defaults= { 'is_default': True })
-
-
- # used for generating multiple bills
- self.bill_dates = [
- timezone.make_aware(datetime.datetime(2020,3,31)),
- timezone.make_aware(datetime.datetime(2020,4,30)),
- timezone.make_aware(datetime.datetime(2020,5,31)),
- ]
-
-
- def order_product(self):
- return Order.objects.create(
- owner=self.user,
- recurring_period=RecurringPeriod.objects.get(name="Onetime"),
- product=self.product,
- billing_address=BillingAddress.get_address_for(self.user),
- starting_date=self.order_meta[1]['starting_date'],
- ending_date=self.order_meta[1]['ending_date'],
- pricing_plan=self.pricing_plan,
- config=vm_order_config)
-
- def order_vm(self, owner=None):
-
- if not owner:
- owner = self.recurring_user
-
- return Order.objects.create(
- owner=owner,
- product=self.vm,
- config=vm_order_config,
- pricing_plan=self.pricing_plan,
- billing_address=BillingAddress.get_address_for(self.recurring_user),
- starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
- )
-
- def test_bill_one_time_with_recurring(self):
- """
- Validate that if the order contains one_time_price and recurring_pricing
- One Bill records should be created
- """
-
- order = Order.objects.create(
- owner=self.user,
- product=self.vm,
- config=vm_order_config,
- pricing_plan=self.pricing_plan,
- one_time_price = 35,
- billing_address=BillingAddress.get_address_for(self.user),
- starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
- )
-
- bill = Bill.create_next_bill_for_user_address(self.user_addr)
-
- self.assertEqual(order.billrecord_set.count(), 1)
- record = order.billrecord_set.first()
- self.assertEqual(record.is_recurring_record, False)
- self.assertEqual(record.price, 35)
- self.assertEqual(record.quantity, 1)
- self.assertEqual(record.sum, 35)
- #close the bill as it has been paid
- bill.close(status="paid")
- bill2 = Bill.create_next_bill_for_user_address(self.user_addr)
- self.assertNotEqual(bill.id, bill2.id)
- self.assertEqual(order.billrecord_set.count(), 2)
- record = BillRecord.objects.filter(bill=bill2, order=order).first()
- self.assertEqual(record.is_recurring_record, True)
- self.assertEqual(record.price, 16.5)
- self.assertEqual(record.quantity, 1)
- self.assertEqual(record.sum, 16.5)
-
- def test_bill_one_time_one_bill_record(self):
- """
- Ensure there is only 1 bill record per order
- """
-
- order = self.order_product()
-
- bill = Bill.create_next_bill_for_user_address(self.user_addr)
-
- self.assertEqual(order.billrecord_set.count(), 1)
-
- def test_bill_sum_onetime(self):
- """
- Check the bill sum for a single one time order
- """
-
- order = self.order_product()
- self.assertEqual(order.recurring_price, 16.5)
- bill = Bill.create_next_bill_for_user_address(self.user_addr)
- self.assertEqual(order.billrecord_set.count(), 1)
- record = order.billrecord_set.first()
- self.assertEqual(record.price, 16.5)
- self.assertEqual(record.quantity, 1)
- self.assertEqual(bill.sum, 16.5)
-
-
- def test_bill_creates_record_for_recurring_order(self):
- """
- Ensure there is only 1 bill record per order
- """
-
- order = self.order_vm()
- bill = Bill.create_next_bill_for_user_address(self.recurring_user_addr)
-
- self.assertEqual(order.billrecord_set.count(), 1)
- self.assertEqual(bill.bill_records.count(), 1)
-
-
- def test_new_bill_after_closing(self):
- """
- After closing a bill and the user has a recurring product,
- the next bill run should create e new bill
- """
-
- order = self.order_vm()
-
- for ending_date in self.bill_dates:
- b = Bill.create_next_bill_for_user_address(self.recurring_user_addr, ending_date)
- b.close(status="paid")
-
- bill_count = Bill.objects.filter(owner=self.recurring_user).count()
-
- self.assertEqual(len(self.bill_dates), bill_count)
-
-
-
-class BillingAddressTestCase(TestCase):
- def setUp(self):
- self.user = get_user_model().objects.create(
- username='random_user',
- email='jane.random@domain.tld')
-
-
- def test_user_no_address(self):
- """
- Raise an error, when there is no address
- """
-
- self.assertRaises(BillingAddress.DoesNotExist,
- BillingAddress.get_address_for,
- self.user)
-
-class VATRatesTestCase(TestCase):
- def setUp(self):
- self.user = get_user_model().objects.create(
- username='random_user',
- email='jane.random@domain.tld')
-
- self.user_addr = BillingAddress.objects.create(
- owner=self.user,
- organization = 'Test org',
- street="unknown",
- city="unknown",
- postal_code="unknown",
- country="CH",
- active=True)
-
- UncloudNetwork.populate_db_defaults()
- UncloudProvider.populate_db_defaults()
-
- VATRate.objects.create(territory_codes="CH", currency_code="CHF", rate=7.7,
- starting_date=timezone.make_aware(datetime.datetime(2000,1,1)))
-
-
-
- def test_get_rate_for_user(self):
- """
- Raise an error, when there is no address
- """
- rate = VATRate.get_vat_rate(self.user_addr)
- self.assertEqual(rate, 7.7)
- self.user_addr.vat_number_verified = True
- self.user_addr.vat_number = "11111"
- rate1 = VATRate.get_vat_rate(self.user_addr)
- self.assertEqual(rate1, 0)
- rate2 = VATRate.get_vat_rate_for_country('CH')
- self.assertEqual(rate, 7.7)
- rate2 = VATRate.get_vat_rate_for_country('EG')
- self.assertEqual(rate2, 0)
diff --git a/uncloud_pay/urls.py b/uncloud_pay/urls.py
deleted file mode 100644
index f30becb..0000000
--- a/uncloud_pay/urls.py
+++ /dev/null
@@ -1,14 +0,0 @@
-from django.urls import path, include
-from django.conf import settings
-from .views import *
-
-app_name = 'uncloud_pay'
-
-urlpatterns = [
- path('cards/activate', CardActivateView.as_view(), name='card_activate'),
- path('billing/', PaymentsView.as_view(), name='billing'),
- path('billing/cards', CardsView.as_view(), name='cards'),
- path('billing/bills', BillsView.as_view(), name='bills'),
- path('order/success/', OrderSuccessView.as_view(), name='order_success'),
- path('order/invoice/
/download/', InvoiceDownloadView.as_view(), name='invoice_download'),
-]
diff --git a/uncloud_pay/utils.py b/uncloud_pay/utils.py
deleted file mode 100644
index 08db9d0..0000000
--- a/uncloud_pay/utils.py
+++ /dev/null
@@ -1,166 +0,0 @@
-import logging
-import decimal
-import datetime
-
-from . import stripe as uncloud_stripe
-import stripe
-from .models import PricingPlan, BillingAddress
-
-logger = logging.getLogger(__name__)
-
-eu_countries = ['at', 'be', 'bg', 'ch', 'cy', 'cz', 'hr', 'dk',
- 'ee', 'fi', 'fr', 'mc', 'de', 'gr', 'hu', 'ie', 'it',
- 'lv', 'lu', 'mt', 'nl', 'po', 'pt', 'ro','sk', 'si', 'es',
- 'se', 'gb']
-
-def validate_vat_number(stripe_customer_id, billing_address_id):
- try:
- billing_address = BillingAddress.objects.get(id=billing_address_id)
- except BillingAddress.DoesNotExist as dne:
- billing_address = None
- except BillingAddress.MultipleObjectsReturned as mor:
- billing_address = BillingAddress.objects.filter(id=billing_address_id).order_by('-id').first()
- if billing_address is not None:
- logger.debug("BillingAddress found: %s %s" % (
- billing_address_id, str(billing_address)))
- if billing_address.country.lower().strip() not in eu_countries:
- return {
- "validated_on": "",
- "status": "not_needed"
- }
- if billing_address.vat_number_validated_on and billing_address.vat_number_verified:
- return {
- "validated_on": billing_address.vat_number_validated_on,
- "status": "verified"
- }
- else:
- if billing_address.stripe_tax_id:
- logger.debug("We have a tax id %s" % billing_address.stripe_tax_id)
- tax_id_obj = stripe.Customer.retrieve_tax_id(
- stripe_customer_id,
- billing_address.stripe_tax_id,
- )
- if tax_id_obj.verification.status == "verified":
- logger.debug("Latest status on Stripe=%s. Updating" %
- tax_id_obj.verification.status)
- # update billing address
- billing_address.vat_number_validated_on = datetime.datetime.now()
- billing_address.vat_number_verified = True
- billing_address.save()
- return {
- "status": "verified",
- "validated_on": billing_address.vat_number_validated_on
- }
- else:
- billing_address.vat_number_validated_on = datetime.datetime.now()
- billing_address.vat_number_verified = False
- billing_address.save()
- else:
- logger.debug("Creating a tax id")
- tax_id_obj = create_tax_id(
- stripe_customer_id, billing_address_id,
- "ch_vat" if billing_address.country.lower() == "ch" else "eu_vat",
- )
- else:
- logger.debug("invalid billing address")
- return {
- "status": "invalid billing address",
- "validated_on": ""
- }
- return {
- "status": tax_id_obj.verification.status if 'verification' in tax_id_obj else "unknown",
- "validated_on": datetime.datetime.now() if tax_id_obj.verification.status == "verified" else ""
- }
-
-def create_tax_id(stripe_customer_id, billing_address_id, type):
- try:
- billing_address = BillingAddress.objects.get(id=billing_address_id)
- except BillingAddress.DoesNotExist as dne:
- billing_address = None
- logger.debug("BillingAddress does not exist for %s" % billing_address_id)
- except BillingAddress.MultipleObjectsReturned as mor:
- logger.debug("Multiple BillingAddress exist for %s" % billing_address_id)
- billing_address = BillingAddress.objects.filter(billing_address_id).order_by('-id').first()
-
- tax_id_obj = None
- if billing_address:
- try:
- tax_id_obj = uncloud_stripe.get_or_create_tax_id_for_user(
- stripe_customer_id,
- vat_number=billing_address.vat_number,
- type=type,
- country=billing_address.country
- )
- billing_address.stripe_tax_id = tax_id_obj.id
- billing_address.vat_number_verified = True if tax_id_obj.verification.status == "verified" else False
- billing_address.save()
- return tax_id_obj
- except Exception as e:
- logger.debug("Received none in tax_id_obj")
- return {
- 'verification': None,
- 'error': str(e)
- }
-
-def apply_vat_discount(subtotal, pricing_plan, vat_rate=False, vat_validation_status=False):
- vat_percent = vat_rate
- if pricing_plan.vat_inclusive or (vat_validation_status and vat_validation_status in ["verified", "not_needed"]):
- vat_percent = decimal.Decimal(0)
- vat = decimal.Decimal(0)
- else:
- vat = subtotal * decimal.Decimal(vat_rate) * decimal.Decimal(0.01)
- discount_amount = 0
- discount_amount_with_vat = 0
- if pricing_plan.discount_amount:
- discount_amount = round(float(pricing_plan.discount_amount), 2)
- discount_amount_with_vat = decimal.Decimal(discount_amount) * (1 + decimal.Decimal(vat_rate) * decimal.Decimal(0.01))
- discount_amount_with_vat = discount_amount_with_vat
-
- subtotal = round(float(subtotal), 2)
- vat_percent = round(float(vat_percent), 2)
- discount = {
- 'name': pricing_plan.discount_name or 'Discount',
- 'amount': discount_amount,
- 'amount_with_vat': round(float(discount_amount_with_vat), 2)
- }
- subtotal_after_discount = subtotal - discount["amount"]
- vat_amount = round(vat_percent * 0.01 * subtotal_after_discount, 2)
- price_after_discount_with_vat = round((subtotal - discount['amount']) * (1 + vat_percent * 0.01), 2)
-
- return (subtotal, round(float(subtotal_after_discount), 2), price_after_discount_with_vat,
- round(float(vat), 2), vat_percent, vat_amount, discount)
-
-
-def get_order_total_with_vat(cores, memory, storage,
- pricing_name='default', vat_rate=False, vat_validation_status=False):
- try:
- pricing = PricingPlan.objects.get(name=pricing_name)
- except Exception as ex:
- logger.error(
- "Error getting PricingPlan object for {pricing_name}."
- "Details: {details}".format(
- pricing_name=pricing_name, details=str(ex)
- )
- )
- return None
- recurring_price = pricing.monthly_maintenance_fees + (decimal.Decimal(cores) * pricing.cores_unit_price) + \
- (decimal.Decimal(memory) * pricing.ram_unit_price) + \
- (decimal.Decimal(10) * (pricing.storage_ssd_unit_price)) + \
- (decimal.Decimal(storage) * (pricing.storage_hd_unit_price))
- subtotal = pricing.set_up_fees + recurring_price
- subtotal, subtotal_after_discount, price_after_discount_with_vat, vat, vat_percent, vat_amount, discount = \
- apply_vat_discount(subtotal, pricing, vat_rate, vat_validation_status)
- return {
- "name": pricing.name,
- 'recurring_price': round(float(recurring_price), 2),
- "subtotal": subtotal,
- "discount": discount,
- "vat": vat, "vat_percent": vat_percent,
- 'vat_amount': vat_amount,
- "vat_validation_status": vat_validation_status,
- "subtotal_after_discount": subtotal_after_discount,
- "total": price_after_discount_with_vat
- }
-
-
-
diff --git a/uncloud_pay/views.py b/uncloud_pay/views.py
deleted file mode 100644
index d880857..0000000
--- a/uncloud_pay/views.py
+++ /dev/null
@@ -1,460 +0,0 @@
-from django.contrib.auth.mixins import LoginRequiredMixin
-from django.views.generic.base import TemplateView, View
-from django.views.generic import DetailView
-from django.shortcuts import render
-from django.views.decorators.cache import cache_control
-from django.db import transaction
-from django.contrib.auth import get_user_model
-from rest_framework import viewsets, mixins, permissions, status, views
-from rest_framework.renderers import TemplateHTMLRenderer
-from rest_framework.response import Response
-from rest_framework.decorators import action
-from rest_framework.reverse import reverse
-from rest_framework.decorators import renderer_classes
-from vat_validator import validate_vat, vies
-from vat_validator.countries import EU_COUNTRY_CODES
-from hardcopy import bytestring_to_pdf
-from django.core.files.temp import NamedTemporaryFile
-from django.views.generic.list import ListView
-from django.http import FileResponse, HttpResponseRedirect
-from django.template.loader import render_to_string
-#from wkhtmltopdf.views import PDFTemplateResponse
-from copy import deepcopy
-
-import json
-import logging
-
-from .models import *
-from .serializers import *
-from .selectors import *
-from .utils import get_order_total_with_vat
-
-from datetime import datetime
-from vat_validator import sanitize_vat
-import uncloud_pay.stripe as uncloud_stripe
-from django.contrib.auth.decorators import login_required
-from django.utils.decorators import method_decorator
-from django.http import JsonResponse
-import stripe
-
-logger = logging.getLogger(__name__)
-
-
-class PricingView(View):
- def get(self, request, **args):
- vat_rate = False
- vat_validation_status = False
- address = False
- selected_country = request.GET.get('country', False)
- if self.request.user and self.request.user.is_authenticated:
- address = get_billing_address_for_user(self.request.user)
- if address and (address.country == selected_country or not selected_country):
- vat_rate = VATRate.get_vat_rate(address)
- vat_validation_status = "verified" if address.vat_number_validated_on and address.vat_number_verified else False
- elif selected_country:
- vat_rate = VATRate.get_vat_rate_for_country(selected_country)
- vat_validation_status = False
-
- pricing = get_order_total_with_vat(
- request.GET.get('cores'),
- request.GET.get('memory'),
- request.GET.get('storage'),
- pricing_name = args['name'],
- vat_rate = vat_rate * 100,
- vat_validation_status = vat_validation_status
- )
- return JsonResponse(pricing)
-
-class CardActivateView(View):
- def post(self, request, **args):
- card_id = request.POST.get('card_id')
- if card_id:
- matched_card = StripeCreditCard.objects.filter(owner=self.request.user, id=card_id).first()
- matched_card.activate()
- return JsonResponse({'success': 1})
- else:
- return JsonResponse({'error': "Please select a card"})
-
-class RegisterCard(TemplateView):
- template_name = "uncloud_pay/register_stripe.html"
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
-
- def get_context_data(self, **kwargs):
- customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
- setup_intent = uncloud_stripe.create_setup_intent(customer_id)
- context = super().get_context_data(**kwargs)
- context['client_secret'] = setup_intent.client_secret
- context['username'] = self.request.user.username
- context['stripe_pk'] = uncloud_stripe.public_api_key
- return context
-
-class OrderSuccessView(DetailView):
- template_name = "uncloud_pay/order_success.html"
- context_object_name = "order"
- model = Order
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- @cache_control(no_cache=True, must_revalidate=True, no_store=True)
- def get(self, request, *args, **kwargs):
- if ('order' not in request.session or ('bill_id' not in request.session)):
- return HttpResponseRedirect(reverse('nextcloud:index'))
- context = {
- 'order': self.request.session.get('order'),
- 'bill_id': self.request.session['bill_id'],
- 'balance': get_balance_for_user(self.request.user)
- }
- return render(request, self.template_name, context)
-
-class InvoiceDownloadView(View):
- template = 'uncloud_pay/invoice.html'
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_context_data(self, **kwargs):
- context = {'base_url': f'{self.request.scheme}://{self.request.get_host()}'}
- return context
-
- def get(self, request, bill_id):
- cmd_options = settings.REPORT_FORMAT
- context = self.get_context_data()
- bill = Bill.objects.get(owner=self.request.user, id=bill_id)
- if bill:
- context['bill'] = bill
- context['vat_rate'] = str(round(bill.vat_rate * 100, 2))
- context['tax_amount'] = round(bill.vat_rate * bill.subtotal, 2)
- return PDFTemplateResponse(request=request,
- template=self.template,
- filename = f"bill-{bill_id}.pdf",
- cmd_options= cmd_options,
- footer_template= 'uncloud_pay/includes/invoice_footer.html',
- context= context)
-
-class PaymentsView(ListView):
- template_name = "uncloud_pay/payments.html"
- model = Payment
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_context_data(self, **kwargs):
- context = super(PaymentsView, self).get_context_data(**kwargs)
- context.update({
- 'balance': get_balance_for_user(self.request.user),
- 'type': self.request.GET.get('type')
- })
- return context
-
- def get_queryset(self):
- if self.request.GET.get('type'):
- return Payment.objects.filter(owner=self.request.user, type=self.request.GET.get('type')).order_by('-timestamp')
- return Payment.objects.filter(owner=self.request.user).order_by('-timestamp')
-
-class CardsView(ListView):
- template_name = "uncloud_pay/cards.html"
- model = StripeCreditCard
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_context_data(self, **kwargs):
- context = super(CardsView, self).get_context_data(**kwargs)
- customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
- setup_intent = uncloud_stripe.create_setup_intent(customer_id)
-
- context = super().get_context_data(**kwargs)
- context.update({
- 'balance': get_balance_for_user(self.request.user),
- 'client_secret': setup_intent.client_secret,
- 'username': self.request.user.username,
- 'stripe_pk':uncloud_stripe.public_api_key,
- 'min_amount': settings.MIN_PER_TRANSACTION
- })
- return context
-
- def get_queryset(self):
- uncloud_stripe.sync_cards_for_user(self.request.user)
- return StripeCreditCard.objects.filter(owner=self.request.user).order_by('-active')
-
-class BillsView(ListView):
- template_name = "uncloud_pay/bills.html"
- model = Bill
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_context_data(self, **kwargs):
- context = super(BillsView, self).get_context_data(**kwargs)
-
- context = super().get_context_data(**kwargs)
- context.update({
- 'balance': get_balance_for_user(self.request.user),
- })
- return context
-
- def get_queryset(self):
- return Bill.objects.filter(owner=self.request.user).order_by('-creation_date')
-
-class CreditCardViewSet(mixins.RetrieveModelMixin,
- mixins.UpdateModelMixin,
- mixins.DestroyModelMixin,
- mixins.ListModelMixin,
- viewsets.GenericViewSet):
-
- serializer_class = StripeCreditCardSerializer
- permission_classes = [permissions.IsAuthenticated]
-
- def list(self, request):
- uncloud_stripe.sync_cards_for_user(self.request.user)
- return super().list(request)
-
- def get_queryset(self):
- return StripeCreditCard.objects.filter(owner=self.request.user)
-
-class PaymentViewSet(viewsets.ModelViewSet):
- serializer_class = PaymentSerializer
- permission_classes = [permissions.IsAuthenticated]
-
- def get_queryset(self):
- return Payment.objects.filter(owner=self.request.user)
-
-class BalanceViewSet(viewsets.ViewSet):
- permission_classes = [permissions.IsAuthenticated]
-
- def list(self, request):
- serializer = BalanceSerializer(data={
- 'balance': get_balance_for_user(self.request.user)
- })
- serializer.is_valid()
- return Response(serializer.data)
-
-
-class ListCards(TemplateView):
- template_name = "uncloud_pay/list_stripe.html"
-
- @method_decorator(login_required)
- def dispatch(self, *args, **kwargs):
- return super().dispatch(*args, **kwargs)
-
- def get_context_data(self, **kwargs):
- customer_id = uncloud_stripe.get_customer_id_for(self.request.user)
- cards = uncloud_stripe.get_customer_cards(customer_id)
-
- context = super().get_context_data(**kwargs)
- context['cards'] = cards
- context['username'] = self.request.user
-
- return context
-
-###
-# Bills and Orders.
-
-class BillViewSet(viewsets.ReadOnlyModelViewSet):
- serializer_class = BillSerializer
- permission_classes = [permissions.IsAuthenticated]
-
- def get_queryset(self):
- return Bill.objects.filter(owner=self.request.user)
-
-
- @action(detail=False, methods=['get'])
- def unpaid(self, request):
- serializer = self.get_serializer(
- Bill.get_unpaid_for(self.request.user),
- many=True)
- return Response(serializer.data)
-
- @action(detail=True, methods=['get'])
- def download(self, *args, **kwargs):
- """
- Allow to download
- """
- bill = self.get_object()
- provider = UncloudProvider.get_provider()
- output_file = NamedTemporaryFile()
- bill_html = render_to_string("bill.html.j2", {'bill': bill})
-
- bytestring_to_pdf(bill_html.encode('utf-8'), output_file)
- response = FileResponse(output_file, content_type="application/pdf")
- response['Content-Disposition'] = 'filename="{}_{}.pdf"'.format(
- bill.reference, bill.uuid
- )
-
- return response
-
-
-class OrderViewSet(viewsets.ReadOnlyModelViewSet):
- serializer_class = OrderSerializer
- permission_classes = [permissions.IsAuthenticated]
-
- def get_queryset(self):
- return Order.objects.filter(owner=self.request.user)
-
-class VATRateViewSet(viewsets.ReadOnlyModelViewSet):
- serializer_class = VATRateSerializer
- permission_classes = [permissions.IsAuthenticated]
- queryset = VATRate.objects.all()
-
-class BillingAddressViewSet(mixins.CreateModelMixin,
- mixins.RetrieveModelMixin,
- mixins.UpdateModelMixin,
- mixins.ListModelMixin,
- viewsets.GenericViewSet):
- permission_classes = [permissions.IsAuthenticated]
-
- def get_serializer_class(self):
- if self.action == 'update':
- return UpdateBillingAddressSerializer
- else:
- return BillingAddressSerializer
-
- def get_queryset(self):
- return self.request.user.billing_addresses.all()
-
- def create(self, request):
- serializer = self.get_serializer(data=request.data)
- serializer.is_valid(raise_exception=True)
-
- # Validate VAT numbers.
- country = serializer.validated_data["country"]
-
- # We ignore empty VAT numbers.
- if 'vat_number' in serializer.validated_data and serializer.validated_data["vat_number"] != "":
- vat_number = serializer.validated_data["vat_number"]
-
- if not validate_vat(country, vat_number):
- return Response(
- {'error': 'Malformed VAT number.'},
- status=status.HTTP_400_BAD_REQUEST)
- elif country in EU_COUNTRY_CODES:
- # XXX: make a synchroneous call to a third patry API here might not be a good idea..
- try:
- vies_state = vies.check_vat(country, vat_number)
- if not vies_state.valid:
- return Response(
- {'error': 'European VAT number does not exist in VIES.'},
- status=status.HTTP_400_BAD_REQUEST)
- except Exception as e:
- logger.warning(e)
- return Response(
- {'error': 'Could not validate EU VAT number against VIES. Try again later..'},
- status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-
-
- serializer.save(owner=request.user)
- return Response(serializer.data)
-
-###
-# Admin stuff.
-
-class AdminPaymentViewSet(viewsets.ModelViewSet):
- serializer_class = PaymentSerializer
- permission_classes = [permissions.IsAdminUser]
-
- def get_queryset(self):
- return Payment.objects.all()
-
- def create(self, request):
- serializer = self.get_serializer(data=request.data)
- serializer.is_valid(raise_exception=True)
- serializer.save(timestamp=datetime.now())
-
- headers = self.get_success_headers(serializer.data)
- return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
-
-# Bills are generated from orders and should not be created or updated by hand.
-class AdminBillViewSet(BillViewSet):
- serializer_class = BillSerializer
- permission_classes = [permissions.IsAdminUser]
-
- def get_queryset(self):
- return Bill.objects.all()
-
- @action(detail=False, methods=['get'])
- def unpaid(self, request):
- unpaid_bills = []
- # XXX: works but we can do better than number of users + 1 SQL requests...
- for user in get_user_model().objects.all():
- unpaid_bills = unpaid_bills + Bill.get_unpaid_for(self.request.user)
-
- serializer = self.get_serializer(unpaid_bills, many=True)
- return Response(serializer.data)
-
- @action(detail=False, methods=['post'])
- def generate(self, request):
- users = get_user_model().objects.all()
-
- generated_bills = []
- for user in users:
- now = timezone.now()
- generated_bills = generated_bills + Bill.generate_for(
- year=now.year,
- month=now.month,
- user=user)
-
- return Response(
- map(lambda b: b.reference, generated_bills),
- status=status.HTTP_200_OK)
-
-class AdminOrderViewSet(mixins.ListModelMixin,
- mixins.RetrieveModelMixin,
- mixins.CreateModelMixin,
- mixins.UpdateModelMixin,
- viewsets.GenericViewSet):
- serializer_class = OrderSerializer
- permission_classes = [permissions.IsAdminUser]
-
- def get_serializer(self, *args, **kwargs):
- return self.serializer_class(*args, **kwargs, admin=True)
-
- def get_queryset(self):
- return Order.objects.all()
-
- # Updates create a new order and terminate the 'old' one.
- @transaction.atomic
- def update(self, request, *args, **kwargs):
- order = self.get_object()
- partial = kwargs.pop('partial', False)
- serializer = self.get_serializer(order, data=request.data, partial=partial)
- serializer.is_valid(raise_exception=True)
-
- # Clone existing order for replacement.
- replacing_order = deepcopy(order)
-
- # Yes, that's how you make a new entry in DB:
- # https://docs.djangoproject.com/en/3.0/topics/db/queries/#copying-model-instances
- replacing_order.pk = None
-
- for attr, value in serializer.validated_data.items():
- setattr(replacing_order, attr, value)
-
- # Save replacing order and terminate 'previous' one.
- replacing_order.save()
- order.replaced_by = replacing_order
- order.save()
- order.terminate()
-
- return Response(replacing_order)
-
- @action(detail=True, methods=['post'])
- def terminate(self, request, pk):
- order = self.get_object()
- if order.is_terminated:
- return Response(
- {'error': 'Order is already terminated.'},
- status=status.HTTP_500_INTERNAL_SERVER_ERROR)
-
- else:
- order.terminate()
- return Response({}, status=status.HTTP_200_OK)
diff --git a/uncloud_service/__init__.py b/uncloud_service/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_service/admin.py b/uncloud_service/admin.py
deleted file mode 100644
index 8c38f3f..0000000
--- a/uncloud_service/admin.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.contrib import admin
-
-# Register your models here.
diff --git a/uncloud_service/apps.py b/uncloud_service/apps.py
deleted file mode 100644
index 190bd35..0000000
--- a/uncloud_service/apps.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class UngleichServiceConfig(AppConfig):
- name = 'uncloud_service'
diff --git a/uncloud_service/models.py b/uncloud_service/models.py
deleted file mode 100644
index a37e42b..0000000
--- a/uncloud_service/models.py
+++ /dev/null
@@ -1,63 +0,0 @@
-from django.db import models
-from uncloud_pay.models import Product, RecurringPeriod, AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS
-from uncloud_vm.models import VMProduct, VMDiskImageProduct
-from django.core.validators import MinValueValidator
-
-class MatrixServiceProduct(models.Model):
- monthly_managment_fee = 20
-
- description = "Managed Matrix HomeServer"
-
- # Specific to Matrix-as-a-Service
- vm = models.ForeignKey(
- VMProduct, on_delete=models.CASCADE
- )
- domain = models.CharField(max_length=255, default='domain.tld')
-
- # Default recurring price is PER_MONT, see Product class.
- # def recurring_price(self, recurring_period=RecurringPeriod.PER_30D):
- # return self.monthly_managment_fee
-
- @staticmethod
- def base_image():
- # TODO: find a way to safely reference debian 10 image.
-#e return VMDiskImageProduct.objects.get(uuid="93e564c5-adb3-4741-941f-718f76075f02")
- return False
-
- # @staticmethod
- # def allowed_recurring_periods():
- # return list(filter(
- # lambda pair: pair[0] in [RecurringPeriod.PER_30D],
- # RecurringPeriod.choices))
-
- @property
- def one_time_price(self):
- return 30
-
-class GenericServiceProduct(models.Model):
- custom_description = models.TextField()
- custom_recurring_price = models.DecimalField(default=0.0,
- max_digits=AMOUNT_MAX_DIGITS,
- decimal_places=AMOUNT_DECIMALS,
- validators=[MinValueValidator(0)])
- custom_one_time_price = models.DecimalField(default=0.0,
- max_digits=AMOUNT_MAX_DIGITS,
- decimal_places=AMOUNT_DECIMALS,
- validators=[MinValueValidator(0)])
-
- @property
- def recurring_price(self):
- # FIXME: handle recurring_period somehow.
- return self.custom_recurring_price
-
- @property
- def description(self):
- return self.custom_description
-
- @property
- def one_time_price(self):
- return self.custom_one_time_price
-
- @staticmethod
- def allowed_recurring_periods():
- return RecurringPeriod.choices
diff --git a/uncloud_service/serializers.py b/uncloud_service/serializers.py
deleted file mode 100644
index bc6d753..0000000
--- a/uncloud_service/serializers.py
+++ /dev/null
@@ -1,60 +0,0 @@
-from rest_framework import serializers
-from .models import *
-from uncloud_vm.serializers import ManagedVMProductSerializer
-from uncloud_vm.models import VMProduct
-from uncloud_pay.models import RecurringPeriod, BillingAddress
-
-# XXX: the OrderSomethingSomthingProductSerializer classes add a lot of
-# boilerplate: can we reduce it somehow?
-
-class MatrixServiceProductSerializer(serializers.ModelSerializer):
- vm = ManagedVMProductSerializer()
-
- class Meta:
- model = MatrixServiceProduct
- fields = ['order', 'owner', 'status', 'vm', 'domain',
- 'recurring_period']
- read_only_fields = ['order', 'owner', 'status']
-
-class OrderMatrixServiceProductSerializer(MatrixServiceProductSerializer):
- # recurring_period = serializers.ChoiceField(
- # choices=MatrixServiceProduct.allowed_recurring_periods())
-
- def __init__(self, *args, **kwargs):
- super(OrderMatrixServiceProductSerializer, self).__init__(*args, **kwargs)
- self.fields['billing_address'] = serializers.ChoiceField(
- choices=BillingAddress.get_addresses_for(
- self.context['request'].user)
- )
-
- class Meta:
- model = MatrixServiceProductSerializer.Meta.model
- fields = MatrixServiceProductSerializer.Meta.fields + [
- 'recurring_period', 'billing_address'
- ]
- read_only_fields = MatrixServiceProductSerializer.Meta.read_only_fields
-
-class GenericServiceProductSerializer(serializers.ModelSerializer):
- class Meta:
- model = GenericServiceProduct
- fields = ['order', 'owner', 'status', 'custom_recurring_price',
- 'custom_description', 'custom_one_time_price']
- read_only_fields = [ 'owner', 'status']
-
-class OrderGenericServiceProductSerializer(GenericServiceProductSerializer):
- # recurring_period = serializers.ChoiceField(
- # choices=GenericServiceProduct.allowed_recurring_periods())
-
- def __init__(self, *args, **kwargs):
- super(OrderGenericServiceProductSerializer, self).__init__(*args, **kwargs)
- self.fields['billing_address'] = serializers.ChoiceField(
- choices=BillingAddress.get_addresses_for(
- self.context['request'].user)
- )
-
- class Meta:
- model = GenericServiceProductSerializer.Meta.model
- fields = GenericServiceProductSerializer.Meta.fields + [
- 'recurring_period', 'billing_address'
- ]
- read_only_fields = GenericServiceProductSerializer.Meta.read_only_fields
diff --git a/uncloud_service/tests.py b/uncloud_service/tests.py
deleted file mode 100644
index 7ce503c..0000000
--- a/uncloud_service/tests.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.test import TestCase
-
-# Create your tests here.
diff --git a/uncloud_service/views.py b/uncloud_service/views.py
deleted file mode 100644
index abd4a05..0000000
--- a/uncloud_service/views.py
+++ /dev/null
@@ -1,128 +0,0 @@
-from rest_framework import viewsets, permissions
-from rest_framework.response import Response
-from django.db import transaction
-from django.utils import timezone
-
-from .models import *
-from .serializers import *
-
-from uncloud_pay.helpers import ProductViewSet
-from uncloud_pay.models import Order
-from uncloud_vm.models import VMProduct, VMDiskProduct
-
-def create_managed_vm(cores, ram, disk_size, image, order):
- # Create VM
- disk = VMDiskProduct(
- owner=order.owner,
- order=order,
- size_in_gb=disk_size,
- image=image)
- vm = VMProduct(
- name="Managed Service Host",
- owner=order.owner,
- cores=cores,
- ram_in_gb=ram,
- primary_disk=disk)
- disk.vm = vm
-
- vm.save()
- disk.save()
-
- return vm
-
-
-class MatrixServiceProductViewSet(ProductViewSet):
- permission_classes = [permissions.IsAuthenticated]
- serializer_class = MatrixServiceProductSerializer
-
- def get_queryset(self):
- return MatrixServiceProduct.objects.filter(owner=self.request.user)
-
- def get_serializer_class(self):
- if self.action == 'create':
- return OrderMatrixServiceProductSerializer
- else:
- return MatrixServiceProductSerializer
-
- @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.objects.create(
- recurring_period=order_recurring_period,
- owner=request.user,
- billing_address=order_billing_address,
- starting_date=timezone.now()
- )
- order.save()
-
- # Create unerderlying VM.
- data = serializer.validated_data.pop('vm')
- vm = create_managed_vm(
- order=order,
- cores=data['cores'],
- ram=data['ram_in_gb'],
- disk_size=data['primary_disk']['size_in_gb'],
- image=MatrixServiceProduct.base_image())
-
- # Create service.
- service = serializer.save(
- order=order,
- owner=request.user,
- vm=vm)
-
- return Response(serializer.data)
-
-class GenericServiceProductViewSet(ProductViewSet):
- permission_classes = [permissions.IsAuthenticated]
-
- def get_queryset(self):
- return GenericServiceProduct.objects.filter(owner=self.request.user)
-
- def get_serializer_class(self):
- if self.action == 'create':
- return OrderGenericServiceProductSerializer
- else:
- return GenericServiceProductSerializer
-
- @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.objects.create(
- recurring_period=order_recurring_period,
- owner=request.user,
- billing_address=order_billing_address,
- starting_date=timezone.now()
- )
- order.save()
-
- # Create service.
- print(serializer.validated_data)
- service = serializer.save(order=order, owner=request.user)
-
- # XXX: Move this to some kind of on_create hook in parent
- # Product class?
- order.add_record(
- service.one_time_price,
- service.recurring_price,
- service.description)
-
- # XXX: Move this to some kind of on_create hook in parent
- # Product class?
- order.add_record(
- service.one_time_price,
- service.recurring_price,
- service.description)
-
- return Response(serializer.data)
diff --git a/uncloud_storage/__init__.py b/uncloud_storage/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_storage/admin.py b/uncloud_storage/admin.py
deleted file mode 100644
index 8c38f3f..0000000
--- a/uncloud_storage/admin.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.contrib import admin
-
-# Register your models here.
diff --git a/uncloud_storage/apps.py b/uncloud_storage/apps.py
deleted file mode 100644
index 38b2301..0000000
--- a/uncloud_storage/apps.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class UncloudStorageConfig(AppConfig):
- name = 'uncloud_storage'
diff --git a/uncloud_storage/models.py b/uncloud_storage/models.py
deleted file mode 100644
index 0dac5c2..0000000
--- a/uncloud_storage/models.py
+++ /dev/null
@@ -1,7 +0,0 @@
-from django.db import models
-from django.utils.translation import gettext_lazy as _
-
-
-class StorageClass(models.TextChoices):
- HDD = 'HDD', _('HDD')
- SSD = 'SSD', _('SSD')
diff --git a/uncloud_storage/tests.py b/uncloud_storage/tests.py
deleted file mode 100644
index 7ce503c..0000000
--- a/uncloud_storage/tests.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.test import TestCase
-
-# Create your tests here.
diff --git a/uncloud_storage/views.py b/uncloud_storage/views.py
deleted file mode 100644
index 91ea44a..0000000
--- a/uncloud_storage/views.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.shortcuts import render
-
-# Create your views here.
diff --git a/uncloud_v3/.gitignore b/uncloud_v3/.gitignore
deleted file mode 100644
index 0330071..0000000
--- a/uncloud_v3/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-venv
-.env
diff --git a/uncloud_v3/Dockerfile b/uncloud_v3/Dockerfile
deleted file mode 100644
index c953d76..0000000
--- a/uncloud_v3/Dockerfile
+++ /dev/null
@@ -1,30 +0,0 @@
-# Currently not working due do ldap:
-#
-# /usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lldap_r
-#
-# While trying to install python-ldap
-
-FROM python:3.10.0-alpine3.15
-
-WORKDIR /usr/src/app
-
-# FIX https://github.com/python-ldap/python-ldap/issues/432
-RUN echo 'INPUT ( libldap.so )' > /usr/lib/libldap_r.so
-
-COPY requirements.txt ./
-# OS requirements for building wheel and for operating uncloud
-RUN apk update && apk add \
- gcc \
- git \
- libxml2-dev \
- libxslt-dev \
- libffi-dev \
- openldap-dev \
- postgresql-dev \
- python3-dev \
- musl-dev \
- nginx \
- wireguard-tools-wg
-RUN pip install --no-cache-dir -r requirements.txt
-
-COPY . .
diff --git a/uncloud_v3/Makefile b/uncloud_v3/Makefile
deleted file mode 100644
index cadb844..0000000
--- a/uncloud_v3/Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
-all: requirements
-
-run: requirements
- . ./env && python manage.py runserver
-
-requirements: venv
- . ./venv/bin/activate && pip install -r requirements.txt
-
-venv:
- python3 -m venv venv
diff --git a/uncloud_v3/README.md b/uncloud_v3/README.md
deleted file mode 100644
index 1d53093..0000000
--- a/uncloud_v3/README.md
+++ /dev/null
@@ -1,71 +0,0 @@
-## Kubernetes integration
-
-### Development / Minikube
-
-To setup a development environment, start minikube on your local
-machine. Use `kubectl get nodes` to verify minikube is up and running.
-
-## Settings
-
-### Environment variables / Settings / Environment
-
-* `SECRET_KEY`
-* `DEBUG`
-* `DATABASE`
-
-## Versions
-
-#### Future (unplanned)
-
-* When/where to add timeframe constraints
-* Timeframe slug-or-id
- * Maybe slug and backlink to avail products
-* Timeframe in product
- * Should a product define list of time frames AND resources?
- * Then can do autoselect on and only show
- complete ones
- * resources are also timeframe bound
- * name != unique (?)
- * how do we link?
- * stays towards resource
- * we only show resources which have price_per_time with one of
- our timeframes
- * And we filter out timeframes that don't have all resources
- * Can we filter drop down in admin?
- * yes: ModelAdmin.formfield_for_manytomany(db_field, request, **kwargs)¶
-* resources should have a slug
- * can be used as an identifier and non unique names
-
-#### 3.1 (validation release, planned)
-
-* Ensure that one resource cannot have multiple price_per_timeframe of
- the same timeframe
-
-
-#### 3.0.2 (planned)
-
-* Add basic validation to ordering
-
-#### 3.0.1 (planned)
-
-* Show products [done]
-* Link to ProductOrderForm [done]
-* Find suitable timeframes for a product [done]
-* Continue to resources / add resources
- * Need to list resources [done]
- * Need to create manytomany relations for each resource resoluting
- in ResourceOrders1
- * Need to pass in the price for the selected timeframe [done]
- * On submit
- * Create ProductOrder
- * Create ResourceOrder(s)
-
-#### 3.0.0 (2022-01-14)
-
-* Introduce ProductOrderView
-
-
-## Pre-Production requirements
-
-* Products need to ensure *all* resources are consistent for different timeframes
-* Products cannot have same resource linked twice in same timeframe
diff --git a/uncloud_v3/app/__init__.py b/uncloud_v3/app/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_v3/app/admin.py b/uncloud_v3/app/admin.py
deleted file mode 100644
index 27ac5b2..0000000
--- a/uncloud_v3/app/admin.py
+++ /dev/null
@@ -1,17 +0,0 @@
-from django.contrib import admin
-
-from .models import *
-
-
-for m in [
- Currency,
- Order,
- OneTimePrice,
- PricePerTime,
- Product,
- ProductOrder,
- Resource,
- ResourceOrder,
- TimeFrame,
-]:
- admin.site.register(m)
diff --git a/uncloud_v3/app/apps.py b/uncloud_v3/app/apps.py
deleted file mode 100644
index ed327d2..0000000
--- a/uncloud_v3/app/apps.py
+++ /dev/null
@@ -1,6 +0,0 @@
-from django.apps import AppConfig
-
-
-class AppConfig(AppConfig):
- default_auto_field = 'django.db.models.BigAutoField'
- name = 'app'
diff --git a/uncloud_v3/app/forms.py b/uncloud_v3/app/forms.py
deleted file mode 100644
index af42715..0000000
--- a/uncloud_v3/app/forms.py
+++ /dev/null
@@ -1,27 +0,0 @@
-from django import forms
-
-class ProductOneTimeOrderForm(forms.Form):
- """
- For products that only contain onetimeresoures
- """
-
- product = forms.SlugField(required=True, disabled=True)
-
- def __init__(self, resources, *args, **kwargs):
- super().__init__(*args, **kwargs)
- for res in resources:
- print(res)
- field_name = f"{res.slug}"
- self.fields[field_name] = forms.FloatField(required=True, label=res.name)
-
- def clean(self):
- cleaned_data = super().clean()
- print("Cleaning form myself ...")
-
-
-class ProductOrderForm(ProductOneTimeOrderForm):
- """
- For recurring products (might also have OneTime items
- """
-
- timeframe = forms.SlugField(required=False, disabled=True)
diff --git a/uncloud_v3/app/management/commands/test-data.py b/uncloud_v3/app/management/commands/test-data.py
deleted file mode 100644
index 7e01b28..0000000
--- a/uncloud_v3/app/management/commands/test-data.py
+++ /dev/null
@@ -1,75 +0,0 @@
-from django.core.management.base import BaseCommand
-from app.models import *
-
-
-class Command(BaseCommand):
- help = 'Add test data'
-
-# def add_arguments(self, parser):
- #parser.add_argument('--username', type=str, required=True)
-
- def handle(self, *args, **options):
- currency, created = Currency.objects.get_or_create(defaults=
- {
- "slug": "CHF",
- "name": "Swiss Franc",
- "short_name": "CHF"
- })
- for timeframe in [ (3600, "1 hour", "1-hour"),
- (86400, "1 day", "1-day"),
- (7*86400, "7 days", "7-days"),
- (30*86400, "30 days", "30-days"),
- (365*86400, "365 days", "365 days") ]:
- TimeFrame.objects.get_or_create(slug=timeframe[2],
- defaults=
- {
- "name": timeframe[1],
- "seconds": timeframe[0]
- })
-
- for ppt in [
- ("1-day", 1, currency),
- ("1-day", 2, currency),
- ("30-days", 10, currency), # Nextcloud
- ("30-days", 15, currency), # Gitea
- ("30-days", 35, currency), # Matrix
- ("30-days", 29, currency),
- ("30-days", 55, currency) ]:
- tf = TimeFrame.objects.get(slug=ppt[0])
-
- PricePerTime.objects.get_or_create(timeframe=tf,
- value=ppt[1],
- defaults=
- {
- "currency": currency
- })
-
- for res in [
- ("cpu-1", "CPU", "Core(s)", None, None),
- ("cpu-min-max", "CPU", "Core(s)", 1, 20),
- ("ram-1", "RAM", "GB", None, None),
- ("ram-min-max", "RAM", "GB", 1, 200),
- ("matrix-maintenance", "Matrix Maintenance Fee", "", 1, 1),
- ("nextcloud-maintenance", "Nextcloud Maintenance Fee", "", 1, 1),
- ("gitea-maintenance", "Gitea Maintenance Fee", "", 1, 1),
-
- ]:
- Resource.objects.get_or_create(slug=res[0],
- defaults=
- {
- "name": res[1],
- "unit": res[2],
- "minimum_units": res[3],
- "maximum_units": res[4]
- })
- # Link to PPT -- later
- # for ppt_res in res[5]:
- # ppt = PricePerTime.objects.get(
-
-
- for product in [
- ("matrix", "Matrix"),
- ("nextcloud", "Nextcloud"),
- ("gitea", "Gitea") ]:
- Product.objects.get_or_create(slug=product[0],
- defaults = { "name": product[1] })
diff --git a/uncloud_v3/app/migrations/0001_initial.py b/uncloud_v3/app/migrations/0001_initial.py
deleted file mode 100644
index 3302d90..0000000
--- a/uncloud_v3/app/migrations/0001_initial.py
+++ /dev/null
@@ -1,115 +0,0 @@
-# Generated by Django 4.0 on 2022-01-16 16:44
-
-from django.db import migrations, models
-import django.db.models.deletion
-import django.utils.timezone
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- ('uauth', '0001_initial'),
- ]
-
- operations = [
- migrations.CreateModel(
- name='Currency',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('slug', models.SlugField(null=True, unique=True)),
- ('name', models.CharField(max_length=128, unique=True)),
- ('short_name', models.CharField(max_length=3, unique=True)),
- ],
- ),
- migrations.CreateModel(
- name='PricePerTime',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('value', models.FloatField()),
- ('currency', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.currency')),
- ],
- ),
- migrations.CreateModel(
- name='Product',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('slug', models.SlugField(null=True, unique=True)),
- ('name', models.CharField(max_length=128, unique=True)),
- ],
- ),
- migrations.CreateModel(
- name='Resource',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('slug', models.SlugField(null=True, unique=True)),
- ('name', models.CharField(max_length=128)),
- ('unit', models.CharField(max_length=128)),
- ('minimum_units', models.FloatField(blank=True, null=True)),
- ('maximum_units', models.FloatField(blank=True, null=True)),
- ('step_size', models.FloatField(default=1)),
- ('price_per_time', models.ManyToManyField(blank=True, to='app.PricePerTime')),
- ],
- ),
- migrations.CreateModel(
- name='TimeFrame',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('slug', models.SlugField(null=True, unique=True)),
- ('name', models.CharField(max_length=128, unique=True)),
- ('seconds', models.IntegerField(blank=True, null=True)),
- ],
- ),
- migrations.CreateModel(
- name='ResourceOrder',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('value', models.FloatField()),
- ('resource', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.resource')),
- ],
- ),
- migrations.CreateModel(
- name='ProductOrder',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.product')),
- ('resources', models.ManyToManyField(to='app.ResourceOrder')),
- ('timeframe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.timeframe')),
- ],
- ),
- migrations.AddField(
- model_name='product',
- name='resources',
- field=models.ManyToManyField(blank=True, to='app.Resource'),
- ),
- migrations.AddField(
- model_name='product',
- name='timeframes',
- field=models.ManyToManyField(blank=True, to='app.TimeFrame'),
- ),
- migrations.AddField(
- model_name='pricepertime',
- name='timeframe',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.timeframe'),
- ),
- migrations.CreateModel(
- name='Order',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('creation_date', models.DateTimeField(auto_now_add=True)),
- ('starting_date', models.DateTimeField(default=django.utils.timezone.now)),
- ('ending_date', models.DateTimeField(blank=True, null=True)),
- ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='uauth.user')),
- ('product', models.ManyToManyField(blank=True, to='app.ProductOrder')),
- ],
- ),
- migrations.CreateModel(
- name='OneTimePrice',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('value', models.FloatField()),
- ('currency', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.currency')),
- ],
- ),
- ]
diff --git a/uncloud_v3/app/migrations/0002_resource_onetime_price.py b/uncloud_v3/app/migrations/0002_resource_onetime_price.py
deleted file mode 100644
index 5350a89..0000000
--- a/uncloud_v3/app/migrations/0002_resource_onetime_price.py
+++ /dev/null
@@ -1,18 +0,0 @@
-# Generated by Django 4.0 on 2022-01-30 09:17
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('app', '0001_initial'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='resource',
- name='onetime_price',
- field=models.ManyToManyField(blank=True, to='app.OneTimePrice'),
- ),
- ]
diff --git a/uncloud_v3/app/migrations/0003_remove_resource_onetime_price_resource_onetime_price.py b/uncloud_v3/app/migrations/0003_remove_resource_onetime_price_resource_onetime_price.py
deleted file mode 100644
index ccdca59..0000000
--- a/uncloud_v3/app/migrations/0003_remove_resource_onetime_price_resource_onetime_price.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 4.0 on 2022-01-30 10:06
-
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('app', '0002_resource_onetime_price'),
- ]
-
- operations = [
- migrations.RemoveField(
- model_name='resource',
- name='onetime_price',
- ),
- migrations.AddField(
- model_name='resource',
- name='onetime_price',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='app.onetimeprice'),
- ),
- ]
diff --git a/uncloud_v3/app/migrations/0004_alter_onetimeprice_options_and_more.py b/uncloud_v3/app/migrations/0004_alter_onetimeprice_options_and_more.py
deleted file mode 100644
index 7efe960..0000000
--- a/uncloud_v3/app/migrations/0004_alter_onetimeprice_options_and_more.py
+++ /dev/null
@@ -1,23 +0,0 @@
-# Generated by Django 4.0 on 2022-01-30 10:39
-
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('app', '0003_remove_resource_onetime_price_resource_onetime_price'),
- ]
-
- operations = [
- migrations.AlterModelOptions(
- name='onetimeprice',
- options={'ordering': ('value',)},
- ),
- migrations.AlterField(
- model_name='productorder',
- name='timeframe',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='app.timeframe'),
- ),
- ]
diff --git a/uncloud_v3/app/migrations/__init__.py b/uncloud_v3/app/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_v3/app/models.py b/uncloud_v3/app/models.py
deleted file mode 100644
index 3ae8a56..0000000
--- a/uncloud_v3/app/models.py
+++ /dev/null
@@ -1,173 +0,0 @@
-from django.db import models
-from django.contrib.auth import get_user_model
-from django.utils import timezone
-from django.urls import reverse
-from django.db.models import Q
-
-class Currency(models.Model):
- slug = models.SlugField(null=True, unique=True)
- name = models.CharField(max_length=128, unique=True)
- short_name = models.CharField(max_length=3, unique=True)
-
- def __str__(self):
- return f"{self.name} ({self.short_name})"
-
-class TimeFrame(models.Model):
- slug = models.SlugField(null=True, unique=True)
- name = models.CharField(max_length=128, unique=True)
- seconds = models.IntegerField(null=True, blank=True)
-
- @staticmethod
- def secs_to_name(secs):
- name = ""
- days = 0
- hours = 0
-
- if secs >= 24*3600:
- days = secs // (24*3600)
- secs -= (days*24*3600)
-
- if secs >= 3600:
- hours = secs // 3600
- secs -= hours*3600
-
- return f"{days} days {hours} hours {secs} seconds"
-
- def __str__(self):
- #return "{} ({})".format(self.name, self.secs_to_name(self.seconds))
- return f"{self.name}"
-
-class OneTimePrice(models.Model):
- value = models.FloatField()
- currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
-
- class Meta:
- ordering = ('value',)
-
- def __str__(self):
- return f"{self.value} {self.currency.short_name}"
-
-class PricePerTime(models.Model):
- timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
- value = models.FloatField()
- currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
-
- def __str__(self):
- return f"{self.value}{self.currency.short_name}/{self.timeframe}"
-
-class Resource(models.Model):
- slug = models.SlugField(null=True, unique=True) # primary identifier
- name = models.CharField(max_length=128, unique=False) # CPU, RAM
- unit = models.CharField(max_length=128) # Count, GB
- minimum_units = models.FloatField(null=True, blank=True) # might have min
- maximum_units = models.FloatField(null=True, blank=True) # might have max
- step_size = models.FloatField(default=1) # step size
-
- price_per_time = models.ManyToManyField(PricePerTime, blank=True)
- #onetime_price = models.ManyToManyField(OneTimePrice, blank=True)
- onetime_price = models.ForeignKey(OneTimePrice, null=True, on_delete=models.CASCADE)
-
- def __str__(self):
- if self.minimum_units:
- minimum = self.minimum_units
- else:
- minimum = "No minimum"
- if self.maximum_units:
- maximum = self.maximum_units
- else:
- maximum = "No maximum"
-
- pricing = ", ".join([str(x) for x in self.price_per_time.all()])
-
- #return f"{self.slug}: {minimum}-{maximum} (+/-){self.step_size} {self.unit} ({pricing})"
- return f"{self.name} ({self.slug})"
-
-
-class Product(models.Model):
- """
- Describes a product a user can buy
- """
-
- slug = models.SlugField(null=True, unique=True)
- name = models.CharField(max_length=128, unique=True)
-
- resources = models.ManyToManyField(Resource, blank=True) # List of REQUIRED resources
- timeframes = models.ManyToManyField(TimeFrame, blank=True) # List of POSSIBLE timeframes
-
- def has_one_time_price(self):
- has_otp = False
-
- for res in self.resources.all():
- if res.onetime_price:
- has_otp = True
- break
-
- return has_otp
-
- def valid_timeframes(self):
- """
- Return all timeframes that have all resources configured
- """
-
- valid_tf = []
-
- num_res = self.resources.all().count()
-
- for tf in self.timeframes.all():
- # Get all distinct source for this timeframe
- res = self.resources.filter(price_per_time__timeframe=tf).distinct().count()
-
- if res == num_res:
- valid_tf.append(tf)
-
- return valid_tf
-
- def get_absolute_url(self):
- return reverse('product-detail', kwargs={'slug' : self.slug})
-
- def __str__(self):
- return self.name
-
-class ResourceOrder(models.Model):
- """
- Resources that have been ordered
-
- We need to record the selected value *and* potentially the
- calculated price
-
- """
- value = models.FloatField()
- resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
-
- def __str__(self):
- return f"{self.value} x {self.resource}"
-
-
-class ProductOrder(models.Model):
- """
- Describes a product a user bought
- """
- product = models.ForeignKey(Product, on_delete=models.CASCADE)
- timeframe = models.ForeignKey(TimeFrame, null=True, blank=True, on_delete=models.CASCADE)
- resources = models.ManyToManyField(ResourceOrder)
-
- def __str__(self):
- if self.timeframe:
- txt = f"Order {self.id}: {self.product} for {self.timeframe}"
- else:
- txt = f"Order {self.id}: {self.product}"
-
- return txt
-
-
-
-class Order(models.Model):
- owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, editable=False)
-
- creation_date = models.DateTimeField(auto_now_add=True)
- starting_date = models.DateTimeField(default=timezone.now)
- ending_date = models.DateTimeField(blank=True, null=True)
-
- product = models.ManyToManyField(ProductOrder, blank=True)
-
- #textconfigs = models.ManyToManyField(ResourceConfig)
diff --git a/uncloud_v3/app/services.py b/uncloud_v3/app/services.py
deleted file mode 100644
index ce98c66..0000000
--- a/uncloud_v3/app/services.py
+++ /dev/null
@@ -1,29 +0,0 @@
-from django.shortcuts import get_object_or_404
-
-from .models import *
-
-def order_product(product, timeframe, formdata):
- """
- Order a product with given parameters
- """
-
- print(formdata)
- po = ProductOrder(product=product, timeframe=timeframe)
- po.save()
-
- for res, value in formdata.items():
- print(f"{res}={value}")
-
- # skip fixed fields
- if res == 'product' or res == 'timeframe':
- continue
-
- resource = get_object_or_404(Resource, slug=res)
- ro = ResourceOrder.objects.create(value=value, resource=resource)
- po.resources.add(ro)
-
- # Ordering without a timeframe
- # if not timeframe:
- # product = models.ForeignKey(Product, on_delete=models.CASCADE)
- # timeframe = models.ForeignKey(TimeFrame, null=True, on_delete=models.CASCADE)
- # resources = models.ManyToManyField(ResourceOrder)
diff --git a/uncloud_v3/app/templates/app/index.html b/uncloud_v3/app/templates/app/index.html
deleted file mode 100644
index 2ada352..0000000
--- a/uncloud_v3/app/templates/app/index.html
+++ /dev/null
@@ -1,12 +0,0 @@
-Welcome to uncloud
-
-What is uncloud?
-
-At ungleich we have developed uncloud as an order system that allows
-you to manage all your resources.
-
-What can I do with uncloud?
-
-
diff --git a/uncloud_v3/app/templates/app/product_detail.html b/uncloud_v3/app/templates/app/product_detail.html
deleted file mode 100644
index 5fd672b..0000000
--- a/uncloud_v3/app/templates/app/product_detail.html
+++ /dev/null
@@ -1,20 +0,0 @@
-{{ object.name }}
-
-(description to be added here)
-
-
diff --git a/uncloud_v3/app/templates/app/product_list.html b/uncloud_v3/app/templates/app/product_list.html
deleted file mode 100644
index b622c7f..0000000
--- a/uncloud_v3/app/templates/app/product_list.html
+++ /dev/null
@@ -1,13 +0,0 @@
-Select Product
-
-Product List
-
- {% for product in object_list %}
- {% if product.slug %}
- -
- {{ product.name }}
-
- {% endif %}
-
- {% endfor %}
-
diff --git a/uncloud_v3/app/templates/app/productorder_form.html b/uncloud_v3/app/templates/app/productorder_form.html
deleted file mode 100644
index 0235c78..0000000
--- a/uncloud_v3/app/templates/app/productorder_form.html
+++ /dev/null
@@ -1,13 +0,0 @@
-Order {{ product }}
-
-{% if timeframe %}
-Timeframe: {{ timeframe }}
-{% endif %}
-
-
diff --git a/uncloud_v3/app/tests.py b/uncloud_v3/app/tests.py
deleted file mode 100644
index 7ce503c..0000000
--- a/uncloud_v3/app/tests.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.test import TestCase
-
-# Create your tests here.
diff --git a/uncloud_v3/app/views.py b/uncloud_v3/app/views.py
deleted file mode 100644
index 0478578..0000000
--- a/uncloud_v3/app/views.py
+++ /dev/null
@@ -1,98 +0,0 @@
-from django.shortcuts import render, get_object_or_404
-from django.urls import reverse
-
-from django.views.generic.edit import CreateView, FormView
-from django.views.generic.base import TemplateView
-from django.views.generic.list import ListView
-from django.views.generic.detail import DetailView
-
-from .models import *
-from .forms import *
-from .services import *
-
-class ProductOneTimeOrderView(FormView):
- form_class = ProductOneTimeOrderForm
- template_name = 'app/productorder_form.html'
-
- def get_success_url(self):
- return "/"
-
- def get_form_kwargs(self):
- kwargs = super().get_form_kwargs()
-
- # Set the product so the form can retrieve the resources
- product = get_object_or_404(Product, slug=self.kwargs['product'])
- kwargs['resources'] = product.resources.all()
- return kwargs
-
- def get_initial(self):
- """
- Initial values for the form
- """
-
- initial = super().get_initial()
-
- initial['product'] = self.kwargs['product']
-
- if 'timeframe' in self.kwargs:
- initial['timeframe'] = self.kwargs['timeframe']
- return initial
-
- def get_context_data(self, **kwargs):
- context = super().get_context_data(**kwargs)
- context['product'] = get_object_or_404(Product, slug=self.kwargs['product'])
- if 'timeframe' in context:
- context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe'])
- return context
-
- def form_valid(self, form):
- product = get_object_or_404(Product, slug=form.cleaned_data['product'])
-
- print("We got a valid form, let's create the order, listing fields:\n------")
- for f in form.fields:
- print(f)
- print(form.cleaned_data)
-
- if 'timeframe' in form.cleaned_data:
- timeframe = get_object_or_404(TimeFrame, slug=form.cleaned_data['timeframe'])
- else:
- timeframe = None
-
- order_product(product, timeframe, form.cleaned_data)
-
- return super().form_valid(form)
-
-class ProductOrderView(ProductOneTimeOrderView):
- form_class = ProductOrderForm
-
-class ProductDetailView(DetailView):
- model = Product
-
- def get_context_data(self, **kwargs):
- context = super().get_context_data(**kwargs)
- context['productorder'] = reverse('product-order', kwargs={'product': self.object.slug })
- context['timeframes'] = context['product'].valid_timeframes()
- context['has_one_time_price'] = context['product'].has_one_time_price()
- print(context)
- return context
-
-
-class ProductListView(ListView):
- model = Product
-
-
-class ProductSelectView(CreateView):
- model = ProductOrder
- fields = ['product' ]
-
-class IndexView(TemplateView):
- template_name = "app/index.html"
-
-class Yearly(TemplateView):
- template_name = "app/config_product.html"
-
- def get_context_data(self, **kwargs):
- context = super(Yearly, self).get_context_data(**kwargs)
- context['current_year'] = self.current_year
- context['current_month'] = self.current_month
- return context
diff --git a/uncloud_v3/build.sh b/uncloud_v3/build.sh
deleted file mode 100755
index 1e7f492..0000000
--- a/uncloud_v3/build.sh
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/bin/sh
-
-set -x
-
-name=uncloud:$(git describe)
-docker build -t ${name} .
-
-# check for args
-if [ $# -ge 1 ]; then
- target=$1; shift
-else
- target=""
-fi
-
-case "$target" in
- dev)
- url=harbor.default.svc.c2.k8s.ooo/ungleich-public
- docker tag $name ${url}/${name}
- docker push ${url}/${name}
- ;;
- prod)
- url=harbor.ungleich.svc.p10.k8s.ooo/ungleich-public
- docker tag $name ${url}/${name}
- docker push ${url}/${name}
- ;;
- "")
- echo "Not pushing anywhere"
- ;;
- *)
- echo "Unknown argument $target, ignoring"
- exit 1
- ;;
-esac
diff --git a/uncloud_v3/helm/.helmignore b/uncloud_v3/helm/.helmignore
deleted file mode 100644
index 0e8a0eb..0000000
--- a/uncloud_v3/helm/.helmignore
+++ /dev/null
@@ -1,23 +0,0 @@
-# Patterns to ignore when building packages.
-# This supports shell glob matching, relative path matching, and
-# negation (prefixed with !). Only one pattern per line.
-.DS_Store
-# Common VCS dirs
-.git/
-.gitignore
-.bzr/
-.bzrignore
-.hg/
-.hgignore
-.svn/
-# Common backup files
-*.swp
-*.bak
-*.tmp
-*.orig
-*~
-# Various IDEs
-.project
-.idea/
-*.tmproj
-.vscode/
diff --git a/uncloud_v3/helm/Chart.yaml b/uncloud_v3/helm/Chart.yaml
deleted file mode 100644
index a17805d..0000000
--- a/uncloud_v3/helm/Chart.yaml
+++ /dev/null
@@ -1,24 +0,0 @@
-apiVersion: v2
-name: django
-description: A Helm chart for Kubernetes
-
-# A chart can be either an 'application' or a 'library' chart.
-#
-# Application charts are a collection of templates that can be packaged into versioned archives
-# to be deployed.
-#
-# Library charts provide useful utilities or functions for the chart developer. They're included as
-# a dependency of application charts to inject those utilities and functions into the rendering
-# pipeline. Library charts do not define any templates and therefore cannot be deployed.
-type: application
-
-# This is the chart version. This version number should be incremented each time you make changes
-# to the chart and its templates, including the app version.
-# Versions are expected to follow Semantic Versioning (https://semver.org/)
-version: 0.1.0
-
-# This is the version number of the application being deployed. This version number should be
-# incremented each time you make changes to the application. Versions are not expected to
-# follow Semantic Versioning. They should reflect the version the application is using.
-# It is recommended to use it with quotes.
-appVersion: "1.16.0"
diff --git a/uncloud_v3/helm/templates/NOTES.txt b/uncloud_v3/helm/templates/NOTES.txt
deleted file mode 100644
index 140bcc6..0000000
--- a/uncloud_v3/helm/templates/NOTES.txt
+++ /dev/null
@@ -1,22 +0,0 @@
-1. Get the application URL by running these commands:
-{{- if .Values.ingress.enabled }}
-{{- range $host := .Values.ingress.hosts }}
- {{- range .paths }}
- http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
- {{- end }}
-{{- end }}
-{{- else if contains "NodePort" .Values.service.type }}
- export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "django.fullname" . }})
- export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
- echo http://$NODE_IP:$NODE_PORT
-{{- else if contains "LoadBalancer" .Values.service.type }}
- NOTE: It may take a few minutes for the LoadBalancer IP to be available.
- You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "django.fullname" . }}'
- export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "django.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
- echo http://$SERVICE_IP:{{ .Values.service.port }}
-{{- else if contains "ClusterIP" .Values.service.type }}
- export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "django.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
- export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
- echo "Visit http://127.0.0.1:8080 to use your application"
- kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
-{{- end }}
diff --git a/uncloud_v3/helm/templates/_helpers.tpl b/uncloud_v3/helm/templates/_helpers.tpl
deleted file mode 100644
index bbc3dda..0000000
--- a/uncloud_v3/helm/templates/_helpers.tpl
+++ /dev/null
@@ -1,62 +0,0 @@
-{{/*
-Expand the name of the chart.
-*/}}
-{{- define "django.name" -}}
-{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
-{{- end }}
-
-{{/*
-Create a default fully qualified app name.
-We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
-If release name contains chart name it will be used as a full name.
-*/}}
-{{- define "django.fullname" -}}
-{{- if .Values.fullnameOverride }}
-{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
-{{- else }}
-{{- $name := default .Chart.Name .Values.nameOverride }}
-{{- if contains $name .Release.Name }}
-{{- .Release.Name | trunc 63 | trimSuffix "-" }}
-{{- else }}
-{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
-{{- end }}
-{{- end }}
-{{- end }}
-
-{{/*
-Create chart name and version as used by the chart label.
-*/}}
-{{- define "django.chart" -}}
-{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
-{{- end }}
-
-{{/*
-Common labels
-*/}}
-{{- define "django.labels" -}}
-helm.sh/chart: {{ include "django.chart" . }}
-{{ include "django.selectorLabels" . }}
-{{- if .Chart.AppVersion }}
-app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
-{{- end }}
-app.kubernetes.io/managed-by: {{ .Release.Service }}
-{{- end }}
-
-{{/*
-Selector labels
-*/}}
-{{- define "django.selectorLabels" -}}
-app.kubernetes.io/name: {{ include "django.name" . }}
-app.kubernetes.io/instance: {{ .Release.Name }}
-{{- end }}
-
-{{/*
-Create the name of the service account to use
-*/}}
-{{- define "django.serviceAccountName" -}}
-{{- if .Values.serviceAccount.create }}
-{{- default (include "django.fullname" .) .Values.serviceAccount.name }}
-{{- else }}
-{{- default "default" .Values.serviceAccount.name }}
-{{- end }}
-{{- end }}
diff --git a/uncloud_v3/helm/templates/deployment.yaml b/uncloud_v3/helm/templates/deployment.yaml
deleted file mode 100644
index 951bc0d..0000000
--- a/uncloud_v3/helm/templates/deployment.yaml
+++ /dev/null
@@ -1,61 +0,0 @@
-apiVersion: apps/v1
-kind: Deployment
-metadata:
- name: {{ include "django.fullname" . }}
- labels:
- {{- include "django.labels" . | nindent 4 }}
-spec:
- {{- if not .Values.autoscaling.enabled }}
- replicas: {{ .Values.replicaCount }}
- {{- end }}
- selector:
- matchLabels:
- {{- include "django.selectorLabels" . | nindent 6 }}
- template:
- metadata:
- {{- with .Values.podAnnotations }}
- annotations:
- {{- toYaml . | nindent 8 }}
- {{- end }}
- labels:
- {{- include "django.selectorLabels" . | nindent 8 }}
- spec:
- {{- with .Values.imagePullSecrets }}
- imagePullSecrets:
- {{- toYaml . | nindent 8 }}
- {{- end }}
- serviceAccountName: {{ include "django.serviceAccountName" . }}
- securityContext:
- {{- toYaml .Values.podSecurityContext | nindent 8 }}
- containers:
- - name: {{ .Chart.Name }}
- securityContext:
- {{- toYaml .Values.securityContext | nindent 12 }}
- image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
- imagePullPolicy: {{ .Values.image.pullPolicy }}
- ports:
- - name: http
- containerPort: 80
- protocol: TCP
- livenessProbe:
- httpGet:
- path: /
- port: http
- readinessProbe:
- httpGet:
- path: /
- port: http
- resources:
- {{- toYaml .Values.resources | nindent 12 }}
- {{- with .Values.nodeSelector }}
- nodeSelector:
- {{- toYaml . | nindent 8 }}
- {{- end }}
- {{- with .Values.affinity }}
- affinity:
- {{- toYaml . | nindent 8 }}
- {{- end }}
- {{- with .Values.tolerations }}
- tolerations:
- {{- toYaml . | nindent 8 }}
- {{- end }}
diff --git a/uncloud_v3/helm/templates/hpa.yaml b/uncloud_v3/helm/templates/hpa.yaml
deleted file mode 100644
index 7be290c..0000000
--- a/uncloud_v3/helm/templates/hpa.yaml
+++ /dev/null
@@ -1,28 +0,0 @@
-{{- if .Values.autoscaling.enabled }}
-apiVersion: autoscaling/v2beta1
-kind: HorizontalPodAutoscaler
-metadata:
- name: {{ include "django.fullname" . }}
- labels:
- {{- include "django.labels" . | nindent 4 }}
-spec:
- scaleTargetRef:
- apiVersion: apps/v1
- kind: Deployment
- name: {{ include "django.fullname" . }}
- minReplicas: {{ .Values.autoscaling.minReplicas }}
- maxReplicas: {{ .Values.autoscaling.maxReplicas }}
- metrics:
- {{- if .Values.autoscaling.targetCPUUtilizationPercentage }}
- - type: Resource
- resource:
- name: cpu
- targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }}
- {{- end }}
- {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }}
- - type: Resource
- resource:
- name: memory
- targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }}
- {{- end }}
-{{- end }}
diff --git a/uncloud_v3/helm/templates/ingress.yaml b/uncloud_v3/helm/templates/ingress.yaml
deleted file mode 100644
index 5115351..0000000
--- a/uncloud_v3/helm/templates/ingress.yaml
+++ /dev/null
@@ -1,61 +0,0 @@
-{{- if .Values.ingress.enabled -}}
-{{- $fullName := include "django.fullname" . -}}
-{{- $svcPort := .Values.service.port -}}
-{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
- {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
- {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}}
- {{- end }}
-{{- end }}
-{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
-apiVersion: networking.k8s.io/v1
-{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
-apiVersion: networking.k8s.io/v1beta1
-{{- else -}}
-apiVersion: extensions/v1beta1
-{{- end }}
-kind: Ingress
-metadata:
- name: {{ $fullName }}
- labels:
- {{- include "django.labels" . | nindent 4 }}
- {{- with .Values.ingress.annotations }}
- annotations:
- {{- toYaml . | nindent 4 }}
- {{- end }}
-spec:
- {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
- ingressClassName: {{ .Values.ingress.className }}
- {{- end }}
- {{- if .Values.ingress.tls }}
- tls:
- {{- range .Values.ingress.tls }}
- - hosts:
- {{- range .hosts }}
- - {{ . | quote }}
- {{- end }}
- secretName: {{ .secretName }}
- {{- end }}
- {{- end }}
- rules:
- {{- range .Values.ingress.hosts }}
- - host: {{ .host | quote }}
- http:
- paths:
- {{- range .paths }}
- - path: {{ .path }}
- {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
- pathType: {{ .pathType }}
- {{- end }}
- backend:
- {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
- service:
- name: {{ $fullName }}
- port:
- number: {{ $svcPort }}
- {{- else }}
- serviceName: {{ $fullName }}
- servicePort: {{ $svcPort }}
- {{- end }}
- {{- end }}
- {{- end }}
-{{- end }}
diff --git a/uncloud_v3/helm/templates/service.yaml b/uncloud_v3/helm/templates/service.yaml
deleted file mode 100644
index 4d94ef7..0000000
--- a/uncloud_v3/helm/templates/service.yaml
+++ /dev/null
@@ -1,15 +0,0 @@
-apiVersion: v1
-kind: Service
-metadata:
- name: {{ include "django.fullname" . }}
- labels:
- {{- include "django.labels" . | nindent 4 }}
-spec:
- type: {{ .Values.service.type }}
- ports:
- - port: {{ .Values.service.port }}
- targetPort: http
- protocol: TCP
- name: http
- selector:
- {{- include "django.selectorLabels" . | nindent 4 }}
diff --git a/uncloud_v3/helm/templates/serviceaccount.yaml b/uncloud_v3/helm/templates/serviceaccount.yaml
deleted file mode 100644
index c686c91..0000000
--- a/uncloud_v3/helm/templates/serviceaccount.yaml
+++ /dev/null
@@ -1,12 +0,0 @@
-{{- if .Values.serviceAccount.create -}}
-apiVersion: v1
-kind: ServiceAccount
-metadata:
- name: {{ include "django.serviceAccountName" . }}
- labels:
- {{- include "django.labels" . | nindent 4 }}
- {{- with .Values.serviceAccount.annotations }}
- annotations:
- {{- toYaml . | nindent 4 }}
- {{- end }}
-{{- end }}
diff --git a/uncloud_v3/helm/templates/tests/test-connection.yaml b/uncloud_v3/helm/templates/tests/test-connection.yaml
deleted file mode 100644
index d3e8da4..0000000
--- a/uncloud_v3/helm/templates/tests/test-connection.yaml
+++ /dev/null
@@ -1,15 +0,0 @@
-apiVersion: v1
-kind: Pod
-metadata:
- name: "{{ include "django.fullname" . }}-test-connection"
- labels:
- {{- include "django.labels" . | nindent 4 }}
- annotations:
- "helm.sh/hook": test
-spec:
- containers:
- - name: wget
- image: busybox
- command: ['wget']
- args: ['{{ include "django.fullname" . }}:{{ .Values.service.port }}']
- restartPolicy: Never
diff --git a/uncloud_v3/helm/values.yaml b/uncloud_v3/helm/values.yaml
deleted file mode 100644
index 52f442d..0000000
--- a/uncloud_v3/helm/values.yaml
+++ /dev/null
@@ -1,82 +0,0 @@
-# Default values for django.
-# This is a YAML-formatted file.
-# Declare variables to be passed into your templates.
-
-replicaCount: 1
-
-image:
- repository: nginx
- pullPolicy: IfNotPresent
- # Overrides the image tag whose default is the chart appVersion.
- tag: ""
-
-imagePullSecrets: []
-nameOverride: ""
-fullnameOverride: ""
-
-serviceAccount:
- # Specifies whether a service account should be created
- create: true
- # Annotations to add to the service account
- annotations: {}
- # The name of the service account to use.
- # If not set and create is true, a name is generated using the fullname template
- name: ""
-
-podAnnotations: {}
-
-podSecurityContext: {}
- # fsGroup: 2000
-
-securityContext: {}
- # capabilities:
- # drop:
- # - ALL
- # readOnlyRootFilesystem: true
- # runAsNonRoot: true
- # runAsUser: 1000
-
-service:
- type: ClusterIP
- port: 80
-
-ingress:
- enabled: false
- className: ""
- annotations: {}
- # kubernetes.io/ingress.class: nginx
- # kubernetes.io/tls-acme: "true"
- hosts:
- - host: chart-example.local
- paths:
- - path: /
- pathType: ImplementationSpecific
- tls: []
- # - secretName: chart-example-tls
- # hosts:
- # - chart-example.local
-
-resources: {}
- # We usually recommend not to specify default resources and to leave this as a conscious
- # choice for the user. This also increases chances charts run on environments with little
- # resources, such as Minikube. If you do want to specify resources, uncomment the following
- # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
- # limits:
- # cpu: 100m
- # memory: 128Mi
- # requests:
- # cpu: 100m
- # memory: 128Mi
-
-autoscaling:
- enabled: false
- minReplicas: 1
- maxReplicas: 100
- targetCPUUtilizationPercentage: 80
- # targetMemoryUtilizationPercentage: 80
-
-nodeSelector: {}
-
-tolerations: []
-
-affinity: {}
diff --git a/uncloud_v3/manage.py b/uncloud_v3/manage.py
deleted file mode 100755
index 8d5f066..0000000
--- a/uncloud_v3/manage.py
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/usr/bin/env python
-"""Django's command-line utility for administrative tasks."""
-import os
-import sys
-
-
-def main():
- """Run administrative tasks."""
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'uncloud.settings')
- try:
- from django.core.management import execute_from_command_line
- except ImportError as exc:
- raise ImportError(
- "Couldn't import Django. Are you sure it's installed and "
- "available on your PYTHONPATH environment variable? Did you "
- "forget to activate a virtual environment?"
- ) from exc
- execute_from_command_line(sys.argv)
-
-
-if __name__ == '__main__':
- main()
diff --git a/uncloud_v3/requirements.txt b/uncloud_v3/requirements.txt
deleted file mode 100644
index 673b4a9..0000000
--- a/uncloud_v3/requirements.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-# Django basics
-Django==4.0
-djangorestframework
-django-auth-ldap
diff --git a/uncloud_v3/uauth/__init__.py b/uncloud_v3/uauth/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_v3/uauth/admin.py b/uncloud_v3/uauth/admin.py
deleted file mode 100644
index 222a7a2..0000000
--- a/uncloud_v3/uauth/admin.py
+++ /dev/null
@@ -1,6 +0,0 @@
-from django.contrib import admin
-
-from django.contrib.auth.admin import UserAdmin
-from .models import User
-
-admin.site.register(User, UserAdmin)
diff --git a/uncloud_v3/uauth/apps.py b/uncloud_v3/uauth/apps.py
deleted file mode 100644
index ff1cccd..0000000
--- a/uncloud_v3/uauth/apps.py
+++ /dev/null
@@ -1,6 +0,0 @@
-from django.apps import AppConfig
-
-
-class UauthConfig(AppConfig):
- default_auto_field = 'django.db.models.BigAutoField'
- name = 'uauth'
diff --git a/uncloud_v3/uauth/migrations/0001_initial.py b/uncloud_v3/uauth/migrations/0001_initial.py
deleted file mode 100644
index 8b25595..0000000
--- a/uncloud_v3/uauth/migrations/0001_initial.py
+++ /dev/null
@@ -1,44 +0,0 @@
-# Generated by Django 4.0 on 2022-01-02 18:03
-
-import django.contrib.auth.models
-import django.contrib.auth.validators
-from django.db import migrations, models
-import django.utils.timezone
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- ('auth', '0012_alter_user_first_name_max_length'),
- ]
-
- operations = [
- migrations.CreateModel(
- name='User',
- fields=[
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('password', models.CharField(max_length=128, verbose_name='password')),
- ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
- ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
- ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
- ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
- ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
- ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
- ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
- ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
- ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
- ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
- ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
- ],
- options={
- 'verbose_name': 'user',
- 'verbose_name_plural': 'users',
- 'abstract': False,
- },
- managers=[
- ('objects', django.contrib.auth.models.UserManager()),
- ],
- ),
- ]
diff --git a/uncloud_v3/uauth/migrations/__init__.py b/uncloud_v3/uauth/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_v3/uauth/models.py b/uncloud_v3/uauth/models.py
deleted file mode 100644
index 10ade16..0000000
--- a/uncloud_v3/uauth/models.py
+++ /dev/null
@@ -1,7 +0,0 @@
-from django.db import models
-from django.contrib.auth.models import AbstractUser
-
-class User(AbstractUser):
- """
- Custom class base
- """
diff --git a/uncloud_v3/uauth/tests.py b/uncloud_v3/uauth/tests.py
deleted file mode 100644
index 7ce503c..0000000
--- a/uncloud_v3/uauth/tests.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.test import TestCase
-
-# Create your tests here.
diff --git a/uncloud_v3/uauth/views.py b/uncloud_v3/uauth/views.py
deleted file mode 100644
index 91ea44a..0000000
--- a/uncloud_v3/uauth/views.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.shortcuts import render
-
-# Create your views here.
diff --git a/uncloud_v3/uncloud/__init__.py b/uncloud_v3/uncloud/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_v3/uncloud/asgi.py b/uncloud_v3/uncloud/asgi.py
deleted file mode 100644
index ced64a0..0000000
--- a/uncloud_v3/uncloud/asgi.py
+++ /dev/null
@@ -1,16 +0,0 @@
-"""
-ASGI config for uncloud project.
-
-It exposes the ASGI callable as a module-level variable named ``application``.
-
-For more information on this file, see
-https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
-"""
-
-import os
-
-from django.core.asgi import get_asgi_application
-
-os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'uncloud.settings')
-
-application = get_asgi_application()
diff --git a/uncloud_v3/uncloud/settings.py b/uncloud_v3/uncloud/settings.py
deleted file mode 100644
index bc77e62..0000000
--- a/uncloud_v3/uncloud/settings.py
+++ /dev/null
@@ -1,127 +0,0 @@
-"""
-Django settings for uncloud project.
-
-Generated by 'django-admin startproject' using Django 4.0.
-
-For more information on this file, see
-https://docs.djangoproject.com/en/4.0/topics/settings/
-
-For the full list of settings and their values, see
-https://docs.djangoproject.com/en/4.0/ref/settings/
-"""
-
-from pathlib import Path
-import os
-
-# Build paths inside the project like this: BASE_DIR / 'subdir'.
-BASE_DIR = Path(__file__).resolve().parent.parent
-
-
-# Quick-start development settings - unsuitable for production
-# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
-
-# SECURITY WARNING: keep the secret key used in production secret!
-SECRET_KEY = os.environ['SECRET_KEY'] if 'SECRET_KEY' in os.environ else 'a bad secret key'
-
-# SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = True
-
-ALLOWED_HOSTS = ['*']
-
-
-# Application definition
-
-INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- 'app',
- 'uauth'
-]
-
-MIDDLEWARE = [
- 'django.middleware.security.SecurityMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'django.middleware.common.CommonMiddleware',
- 'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
- 'django.contrib.messages.middleware.MessageMiddleware',
- 'django.middleware.clickjacking.XFrameOptionsMiddleware',
-]
-
-ROOT_URLCONF = 'uncloud.urls'
-
-TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [],
- 'APP_DIRS': True,
- 'OPTIONS': {
- 'context_processors': [
- 'django.template.context_processors.debug',
- 'django.template.context_processors.request',
- 'django.contrib.auth.context_processors.auth',
- 'django.contrib.messages.context_processors.messages',
- ],
- },
- },
-]
-
-WSGI_APPLICATION = 'uncloud.wsgi.application'
-
-
-# Database
-# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
-
-DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': BASE_DIR / 'db.sqlite3',
- }
-}
-
-
-# Password validation
-# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
-
-AUTH_PASSWORD_VALIDATORS = [
- {
- 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
- },
-]
-
-AUTH_USER_MODEL = 'uauth.User'
-
-# Internationalization
-# https://docs.djangoproject.com/en/4.0/topics/i18n/
-
-LANGUAGE_CODE = 'en-us'
-
-TIME_ZONE = 'UTC'
-
-USE_I18N = True
-
-USE_TZ = True
-
-
-# Static files (CSS, JavaScript, Images)
-# https://docs.djangoproject.com/en/4.0/howto/static-files/
-
-STATIC_URL = 'static/'
-
-# Default primary key field type
-# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
-
-DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
diff --git a/uncloud_v3/uncloud/urls.py b/uncloud_v3/uncloud/urls.py
deleted file mode 100644
index 33b46b8..0000000
--- a/uncloud_v3/uncloud/urls.py
+++ /dev/null
@@ -1,30 +0,0 @@
-"""uncloud URL Configuration
-
-The `urlpatterns` list routes URLs to views. For more information please see:
- https://docs.djangoproject.com/en/4.0/topics/http/urls/
-Examples:
-Function views
- 1. Add an import: from my_app import views
- 2. Add a URL to urlpatterns: path('', views.home, name='home')
-Class-based views
- 1. Add an import: from other_app.views import Home
- 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
-Including another URLconf
- 1. Import the include() function: from django.urls import include, path
- 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
-"""
-from django.contrib import admin
-from django.urls import path
-from app import views as appviews
-
-urlpatterns = [
- path('admin/', admin.site.urls),
- path('order', appviews.ProductSelectView.as_view()),
- path('order//', appviews.ProductOrderView.as_view(), name='product-order'),
- path('order/recurring///', appviews.ProductOrderView.as_view(), name='product-order-tf'),
- path('order/onetime//', appviews.ProductOneTimeOrderView.as_view(), name='product-order-onetime'),
- path('product/', appviews.ProductListView.as_view(), name='products'),
- path('product//', appviews.ProductDetailView.as_view(), name='product-detail'),
- path('', appviews.IndexView.as_view(), name='index'),
-
-]
diff --git a/uncloud_v3/uncloud/wsgi.py b/uncloud_v3/uncloud/wsgi.py
deleted file mode 100644
index c443b14..0000000
--- a/uncloud_v3/uncloud/wsgi.py
+++ /dev/null
@@ -1,16 +0,0 @@
-"""
-WSGI config for uncloud project.
-
-It exposes the WSGI callable as a module-level variable named ``application``.
-
-For more information on this file, see
-https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
-"""
-
-import os
-
-from django.core.wsgi import get_wsgi_application
-
-os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'uncloud.settings')
-
-application = get_wsgi_application()
diff --git a/uncloud_v4/__init__.py b/uncloud_v4/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_v4/admin.py b/uncloud_v4/admin.py
deleted file mode 100644
index 6f3bc50..0000000
--- a/uncloud_v4/admin.py
+++ /dev/null
@@ -1,19 +0,0 @@
-from django.contrib import admin
-
-# Register your models here.
-from uncloud_vm.models import *
-from uncloud_pay.models import Order
-
-class VMDiskInline(admin.TabularInline):
- model = VMDiskProduct
-
-class OrderInline(admin.TabularInline):
- model = Order
-
-class VMProductAdmin(admin.ModelAdmin):
- inlines = [
- VMDiskInline
- ]
-
-admin.site.register(VMProduct, VMProductAdmin)
-admin.site.register(VMDiskProduct)
diff --git a/uncloud_v4/apps.py b/uncloud_v4/apps.py
deleted file mode 100644
index c5e94a5..0000000
--- a/uncloud_v4/apps.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class UncloudVmConfig(AppConfig):
- name = 'uncloud_vm'
diff --git a/uncloud_v4/management/commands/vm.py b/uncloud_v4/management/commands/vm.py
deleted file mode 100644
index 667c5ad..0000000
--- a/uncloud_v4/management/commands/vm.py
+++ /dev/null
@@ -1,119 +0,0 @@
-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")
diff --git a/uncloud_v4/migrations/0001_initial.py b/uncloud_v4/migrations/0001_initial.py
deleted file mode 100644
index 4ec089a..0000000
--- a/uncloud_v4/migrations/0001_initial.py
+++ /dev/null
@@ -1,111 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 10:38
-
-from django.conf import settings
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- migrations.swappable_dependency(settings.AUTH_USER_MODEL),
- ]
-
- operations = [
- migrations.CreateModel(
- name='VMCluster',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('extra_data', models.JSONField(blank=True, editable=False, null=True)),
- ('name', models.CharField(max_length=128, unique=True)),
- ],
- options={
- 'abstract': False,
- },
- ),
- migrations.CreateModel(
- name='VMDiskImageProduct',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('extra_data', models.JSONField(blank=True, editable=False, null=True)),
- ('name', models.CharField(max_length=256)),
- ('is_os_image', models.BooleanField(default=False)),
- ('is_public', models.BooleanField(default=False, editable=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'), ('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)),
- ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- options={
- 'abstract': False,
- },
- ),
- migrations.CreateModel(
- name='VMHost',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('extra_data', models.JSONField(blank=True, editable=False, null=True)),
- ('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'), ('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)),
- ('vmcluster', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmcluster')),
- ],
- options={
- 'abstract': False,
- },
- ),
- migrations.CreateModel(
- name='VMProduct',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('name', models.CharField(blank=True, max_length=32, null=True)),
- ('cores', models.IntegerField()),
- ('ram_in_gb', models.FloatField()),
- ('vmcluster', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmcluster')),
- ('vmhost', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmhost')),
- ],
- ),
- migrations.CreateModel(
- name='VMSnapshotProduct',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('gb_ssd', models.FloatField(editable=False)),
- ('gb_hdd', models.FloatField(editable=False)),
- ('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='snapshots', to='uncloud_vm.vmproduct')),
- ],
- ),
- 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=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('size_in_gb', models.FloatField(blank=True)),
- ('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')),
- ('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmproduct')),
- ],
- ),
- 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')),
- ('primary_disk', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmdiskproduct')),
- ],
- bases=('uncloud_vm.vmproduct',),
- ),
- ]
diff --git a/uncloud_v4/migrations/__init__.py b/uncloud_v4/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_v4/models.py b/uncloud_v4/models.py
deleted file mode 100644
index b23e276..0000000
--- a/uncloud_v4/models.py
+++ /dev/null
@@ -1,72 +0,0 @@
-from django.db import models
-from django.contrib.auth import get_user_model
-
-# 1st mixin: owner alike?
-# 2nd: time frames
-
-
-class Nextcloudv1(models.Model):
- name = models.CharField(max_length=128, unique=True)
-
- owner = models.ForeignKey(
- get_user_model(), on_delete=models.CASCADE, editable=False
- )
-
- domain = models.CharField(max_length=253)
-
- cpu_cores = models.FloatField(null=True, blank=True)
- ram_in_gb = models.FloatField(null=True, blank=True)
- storage_in_gb = models.FloatField(null=True, blank=True)
-
-
- def __str__(self):
- return f"Nextcloud {domain}: {cores}C {ram_in_gb}GB RAM {storage} GB storage from {owner}"
-
-
-class ResourceConfig(models.Model):
- """
- Describes an actual setting of a resource
- """
- value = models.FloatField()
- resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
-
-
-class ProductTextConfiguration(models.Model):
- name = models.CharField(max_length=128, unique=True) # Domain, etc.
-
-class ProductTextFieldConfiguration(models.Model):
- name = models.CharField(max_length=128, unique=True) # homeserver.Yaml, etc.
-
-class ProductTextConfig(models.Model):
- """
- Describes an actual setting of a resource
- """
- value = models.CharField(max_length=256)
- config = models.ForeignKey(ProductTextConfiguration, on_delete=models.CASCADE)
-
-class ProductTextFieldConfig(models.Model):
- """
- Describes an actual setting of a resource
- """
- value = models.TextField()
- config = models.ForeignKey(ProductTextFieldConfiguration, on_delete=models.CASCADE)
-
-
- # 3600
- # 86400
- # 30*86400
-
-# class Nextcloudv2(Product):
-# domain = models.CharField(max_length=253)
-
-# def __str__(self):
-# return f"Nextcloud {domain} {owner}"
-
-# class ProductPricing(models.Model):
-# """
-# Defines the allowed time frames and the available resources
-# """
-
-# name = models.CharField(max_length=128, unique=True) # cpu 2022, ipv4 2022
-# timeframes = models.ManyToManyField(TimeFrame)
-# resources = models.ManyToManyField(Resource)
diff --git a/uncloud_v4/serializers.py b/uncloud_v4/serializers.py
deleted file mode 100644
index a60d10b..0000000
--- a/uncloud_v4/serializers.py
+++ /dev/null
@@ -1,143 +0,0 @@
-from django.contrib.auth import get_user_model
-
-from rest_framework import serializers
-
-from .models import *
-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.id))
-
- 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 = VMWithOSProduct
- fields = [ 'cores', 'ram_in_gb', 'primary_disk']
-
-class VMProductSerializer(serializers.ModelSerializer):
- primary_disk = CreateVMDiskProductSerializer()
- snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
- disks = VMDiskProductSerializer(many=True, read_only=True)
-
- class Meta:
- model = VMWithOSProduct
- fields = ['order', 'owner', 'status', 'name', 'cores',
- 'ram_in_gb', 'primary_disk', 'snapshots', 'disks', 'extra_data']
- read_only_fields = ['order', 'owner', 'status']
-
-class OrderVMProductSerializer(VMProductSerializer):
- # recurring_period = serializers.ChoiceField(
- # choices=VMWithOSProduct.allowed_recurring_periods())
-
- def __init__(self, *args, **kwargs):
- super(VMProductSerializer, self).__init__(*args, **kwargs)
-
- class Meta:
- model = VMProductSerializer.Meta.model
- fields = VMProductSerializer.Meta.fields + [ 'recurring_period' ]
- 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 = ['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
diff --git a/uncloud_v4/tests.py b/uncloud_v4/tests.py
deleted file mode 100644
index e5d403f..0000000
--- a/uncloud_v4/tests.py
+++ /dev/null
@@ -1,98 +0,0 @@
-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.'
-# )
-
-# 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'
-# )
-# )
diff --git a/uncloud_v4/views.py b/uncloud_v4/views.py
deleted file mode 100644
index 67f8656..0000000
--- a/uncloud_v4/views.py
+++ /dev/null
@@ -1,261 +0,0 @@
-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, BillingAddress
-
-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 = VMWithOSProduct.objects.all()
- else:
- obj = VMWithOSProduct.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")
-
- # Create disk image.
- disk = VMDiskProduct(owner=request.user,
- **serializer.validated_data.pop("primary_disk"))
- vm = VMWithOSProduct(owner=request.user, primary_disk=disk,
- **serializer.validated_data)
- disk.vm = vm # XXX: Is this really needed?
-
- # Create VM and Disk orders.
- vm_order = Order.from_product(
- vm,
- recurring_period=order_recurring_period,
- starting_date=timezone.now()
- )
-
- disk_order = Order.from_product(
- disk,
- recurring_period=order_recurring_period,
- starting_date=timezone.now(),
- depends_on=vm_order
- )
-
-
- # Commit to DB.
- vm.order = vm_order
- vm.save()
- vm_order.save()
-
- disk.order = disk_order
- disk_order.save()
- 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)
diff --git a/uncloud_vm/__init__.py b/uncloud_vm/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_vm/admin.py b/uncloud_vm/admin.py
deleted file mode 100644
index 6f3bc50..0000000
--- a/uncloud_vm/admin.py
+++ /dev/null
@@ -1,19 +0,0 @@
-from django.contrib import admin
-
-# Register your models here.
-from uncloud_vm.models import *
-from uncloud_pay.models import Order
-
-class VMDiskInline(admin.TabularInline):
- model = VMDiskProduct
-
-class OrderInline(admin.TabularInline):
- model = Order
-
-class VMProductAdmin(admin.ModelAdmin):
- inlines = [
- VMDiskInline
- ]
-
-admin.site.register(VMProduct, VMProductAdmin)
-admin.site.register(VMDiskProduct)
diff --git a/uncloud_vm/apps.py b/uncloud_vm/apps.py
deleted file mode 100644
index c5e94a5..0000000
--- a/uncloud_vm/apps.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class UncloudVmConfig(AppConfig):
- name = 'uncloud_vm'
diff --git a/uncloud_vm/management/commands/vm.py b/uncloud_vm/management/commands/vm.py
deleted file mode 100644
index 667c5ad..0000000
--- a/uncloud_vm/management/commands/vm.py
+++ /dev/null
@@ -1,119 +0,0 @@
-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")
diff --git a/uncloud_vm/migrations/0001_initial.py b/uncloud_vm/migrations/0001_initial.py
deleted file mode 100644
index 4ec089a..0000000
--- a/uncloud_vm/migrations/0001_initial.py
+++ /dev/null
@@ -1,111 +0,0 @@
-# Generated by Django 3.1 on 2020-12-13 10:38
-
-from django.conf import settings
-from django.db import migrations, models
-import django.db.models.deletion
-
-
-class Migration(migrations.Migration):
-
- initial = True
-
- dependencies = [
- migrations.swappable_dependency(settings.AUTH_USER_MODEL),
- ]
-
- operations = [
- migrations.CreateModel(
- name='VMCluster',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('extra_data', models.JSONField(blank=True, editable=False, null=True)),
- ('name', models.CharField(max_length=128, unique=True)),
- ],
- options={
- 'abstract': False,
- },
- ),
- migrations.CreateModel(
- name='VMDiskImageProduct',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('extra_data', models.JSONField(blank=True, editable=False, null=True)),
- ('name', models.CharField(max_length=256)),
- ('is_os_image', models.BooleanField(default=False)),
- ('is_public', models.BooleanField(default=False, editable=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'), ('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)),
- ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
- ],
- options={
- 'abstract': False,
- },
- ),
- migrations.CreateModel(
- name='VMHost',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('extra_data', models.JSONField(blank=True, editable=False, null=True)),
- ('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'), ('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)),
- ('vmcluster', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmcluster')),
- ],
- options={
- 'abstract': False,
- },
- ),
- migrations.CreateModel(
- name='VMProduct',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('name', models.CharField(blank=True, max_length=32, null=True)),
- ('cores', models.IntegerField()),
- ('ram_in_gb', models.FloatField()),
- ('vmcluster', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmcluster')),
- ('vmhost', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmhost')),
- ],
- ),
- migrations.CreateModel(
- name='VMSnapshotProduct',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('gb_ssd', models.FloatField(editable=False)),
- ('gb_hdd', models.FloatField(editable=False)),
- ('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='snapshots', to='uncloud_vm.vmproduct')),
- ],
- ),
- 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=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('size_in_gb', models.FloatField(blank=True)),
- ('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')),
- ('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmproduct')),
- ],
- ),
- 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')),
- ('primary_disk', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmdiskproduct')),
- ],
- bases=('uncloud_vm.vmproduct',),
- ),
- ]
diff --git a/uncloud_vm/migrations/__init__.py b/uncloud_vm/migrations/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/uncloud_vm/models.py b/uncloud_vm/models.py
deleted file mode 100644
index c605779..0000000
--- a/uncloud_vm/models.py
+++ /dev/null
@@ -1,189 +0,0 @@
-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):
- name = models.CharField(max_length=128, unique=True)
-
-class VMHost(UncloudModel):
- # 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(models.Model):
- 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
- )
-
- 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
-
- @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_365D,
- # RecurringPeriod.PER_30D, RecurringPeriod.PER_HOUR],
- # RecurringPeriod.choices))
-
-
- def __str__(self):
- return f"VM id={self.id},name={self.name},cores={self.cores},ram_in_gb={self.ram_in_gb}"
-
-
-class VMWithOSProduct(VMProduct):
- primary_disk = models.ForeignKey('VMDiskProduct', on_delete=models.CASCADE, null=True)
-
-
-class VMDiskImageProduct(UncloudModel):
- """
- Images are used for cloning/linking.
-
- They are the base for images.
-
- """
-
- 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.id,
- self.name,
- self.size_in_gb)
-
-
-# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
-class VMDiskType(models.TextChoices):
- """
- Types of disks that can be attached to VMs
- """
- CEPH_SSD = 'ceph/ssd'
- CEPH_HDD = 'ceph/hdd'
- LOCAL_SSD = 'local/ssd'
- LOCAL_HDD = 'local/hdd'
-
-
-class VMDiskProduct(models.Model):
- """
- 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,
- blank=True, null=True)
-
- size_in_gb = models.FloatField(blank=True)
-
- disk_type = models.CharField(
- max_length=20,
- choices=VMDiskType.choices,
- default=VMDiskType.CEPH_SSD)
-
- def __str__(self):
- return f"Disk {self.size_in_gb}GB ({self.disk_type}) for {self.vm}"
-
- @property
- def recurring_price(self):
- if self.disk_type == VMDiskType.CEPH_SSD:
- price_per_gb = 3.5/10
- elif self.disk_type == VMDiskType.CEPH_HDD:
- price_per_gb = 1.5/100
- elif self.disk_type == VMDiskType.LOCAL_SSD:
- price_per_gb = 3.5/10
- elif self.disk_type == VMDiskType.CEPH_HDD:
- price_per_gb = 1.5/100
-
- return self.size_in_gb * price_per_gb
-
-
-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(models.Model):
- gb_ssd = models.FloatField(editable=False)
- gb_hdd = models.FloatField(editable=False)
-
- vm = models.ForeignKey(VMProduct,
- related_name='snapshots',
- on_delete=models.CASCADE)
diff --git a/uncloud_vm/serializers.py b/uncloud_vm/serializers.py
deleted file mode 100644
index a60d10b..0000000
--- a/uncloud_vm/serializers.py
+++ /dev/null
@@ -1,143 +0,0 @@
-from django.contrib.auth import get_user_model
-
-from rest_framework import serializers
-
-from .models import *
-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.id))
-
- 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 = VMWithOSProduct
- fields = [ 'cores', 'ram_in_gb', 'primary_disk']
-
-class VMProductSerializer(serializers.ModelSerializer):
- primary_disk = CreateVMDiskProductSerializer()
- snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
- disks = VMDiskProductSerializer(many=True, read_only=True)
-
- class Meta:
- model = VMWithOSProduct
- fields = ['order', 'owner', 'status', 'name', 'cores',
- 'ram_in_gb', 'primary_disk', 'snapshots', 'disks', 'extra_data']
- read_only_fields = ['order', 'owner', 'status']
-
-class OrderVMProductSerializer(VMProductSerializer):
- # recurring_period = serializers.ChoiceField(
- # choices=VMWithOSProduct.allowed_recurring_periods())
-
- def __init__(self, *args, **kwargs):
- super(VMProductSerializer, self).__init__(*args, **kwargs)
-
- class Meta:
- model = VMProductSerializer.Meta.model
- fields = VMProductSerializer.Meta.fields + [ 'recurring_period' ]
- 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 = ['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
diff --git a/uncloud_vm/tests.py b/uncloud_vm/tests.py
deleted file mode 100644
index e5d403f..0000000
--- a/uncloud_vm/tests.py
+++ /dev/null
@@ -1,98 +0,0 @@
-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.'
-# )
-
-# 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'
-# )
-# )
diff --git a/uncloud_vm/views.py b/uncloud_vm/views.py
deleted file mode 100644
index 67f8656..0000000
--- a/uncloud_vm/views.py
+++ /dev/null
@@ -1,261 +0,0 @@
-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, BillingAddress
-
-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 = VMWithOSProduct.objects.all()
- else:
- obj = VMWithOSProduct.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")
-
- # Create disk image.
- disk = VMDiskProduct(owner=request.user,
- **serializer.validated_data.pop("primary_disk"))
- vm = VMWithOSProduct(owner=request.user, primary_disk=disk,
- **serializer.validated_data)
- disk.vm = vm # XXX: Is this really needed?
-
- # Create VM and Disk orders.
- vm_order = Order.from_product(
- vm,
- recurring_period=order_recurring_period,
- starting_date=timezone.now()
- )
-
- disk_order = Order.from_product(
- disk,
- recurring_period=order_recurring_period,
- starting_date=timezone.now(),
- depends_on=vm_order
- )
-
-
- # Commit to DB.
- vm.order = vm_order
- vm.save()
- vm_order.save()
-
- disk.order = disk_order
- disk_order.save()
- 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)