diff --git a/matrixhosting/forms.py b/matrixhosting/forms.py index 6f60f3c..691da27 100644 --- a/matrixhosting/forms.py +++ b/matrixhosting/forms.py @@ -5,6 +5,7 @@ from django.forms import ModelForm from django.utils.translation import get_language, ugettext_lazy as _ from django.core.exceptions import ValidationError from .validators import domain_name_validator +from .models import VMInstance from uncloud_pay.models import BillingAddress @@ -37,7 +38,24 @@ class InitialRequestForm(MainForm): storage = forms.IntegerField(label='Storage', min_value=100, max_value=10000, initial=100) pricing_name = forms.CharField(required=True) -class RequestHostedVMForm(InitialRequestForm): +class RequestDomainsNamesForm(MainForm): + homeserver_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Homeserver Name *'})) + webclient_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Webclient Name *'})) + is_open_registration = forms.BooleanField(required=False, initial=False) + + def clean_homeserver_name(self): + homeserver_name = self.cleaned_data['homeserver_name'] + if VMInstance.objects.filter(homeserver_domain=f"{homeserver_name}.matrix.ungleich.cloud").exists(): + raise ValidationError("homeserver name already exists") + return homeserver_name + + def clean_webclient_name(self): + webclient_name = self.cleaned_data['webclient_name'] + if VMInstance.objects.filter(webclient_domain=f"{webclient_name}.matrix.0co2.cloud").exists(): + raise ValidationError("webclient name already exists") + return webclient_name + +class RequestDomainsForm(MainForm): matrix_domain = DomainNameField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Matrix Domain *'})) homeserver_domain = DomainNameField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Homeserver Domain *'})) webclient_domain = DomainNameField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Webclient Domain *'})) diff --git a/matrixhosting/migrations/0010_auto_20210806_1511.py b/matrixhosting/migrations/0010_auto_20210806_1511.py new file mode 100644 index 0000000..6d8a257 --- /dev/null +++ b/matrixhosting/migrations/0010_auto_20210806_1511.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.4 on 2021-08-06 15:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('matrixhosting', '0009_vminstance_vm_id'), + ] + + operations = [ + migrations.RemoveField( + model_name='vminstance', + name='vm_id', + ), + migrations.AddField( + model_name='vminstance', + name='vm_name', + field=models.CharField(blank=True, max_length=256, null=True), + ), + ] diff --git a/matrixhosting/migrations/0011_alter_vminstance_vm_name.py b/matrixhosting/migrations/0011_alter_vminstance_vm_name.py new file mode 100644 index 0000000..09e864b --- /dev/null +++ b/matrixhosting/migrations/0011_alter_vminstance_vm_name.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.4 on 2021-08-06 15:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('matrixhosting', '0010_auto_20210806_1511'), + ] + + operations = [ + migrations.AlterField( + model_name='vminstance', + name='vm_name', + field=models.CharField(editable=False, max_length=253, unique=True), + ), + ] diff --git a/matrixhosting/migrations/0012_auto_20210808_1651.py b/matrixhosting/migrations/0012_auto_20210808_1651.py new file mode 100644 index 0000000..e5495e1 --- /dev/null +++ b/matrixhosting/migrations/0012_auto_20210808_1651.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.4 on 2021-08-08 16:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('matrixhosting', '0011_alter_vminstance_vm_name'), + ] + + operations = [ + migrations.AddField( + model_name='vminstance', + name='homeserver_domain', + field=models.CharField(blank=True, max_length=253, null=True, unique=True), + ), + migrations.AddField( + model_name='vminstance', + name='webclient_domain', + field=models.CharField(blank=True, max_length=253, null=True, unique=True), + ), + ] diff --git a/matrixhosting/migrations/0013_auto_20210808_1652.py b/matrixhosting/migrations/0013_auto_20210808_1652.py new file mode 100644 index 0000000..cde5ea9 --- /dev/null +++ b/matrixhosting/migrations/0013_auto_20210808_1652.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.4 on 2021-08-08 16:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('matrixhosting', '0012_auto_20210808_1651'), + ] + + operations = [ + migrations.AlterField( + model_name='vminstance', + name='homeserver_domain', + field=models.CharField(blank=True, max_length=253, unique=True), + ), + migrations.AlterField( + model_name='vminstance', + name='webclient_domain', + field=models.CharField(blank=True, max_length=253, unique=True), + ), + ] diff --git a/matrixhosting/models.py b/matrixhosting/models.py index 8945f69..a995d99 100644 --- a/matrixhosting/models.py +++ b/matrixhosting/models.py @@ -1,9 +1,10 @@ import logging import uuid import os +import json import sys import gitlab -from jinja2 import Environment, FileSystemLoader +import yaml from django.db import models from django.conf import settings @@ -21,7 +22,11 @@ class VMInstance(models.Model): on_delete=models.CASCADE, editable=True) - vm_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) + vm_name = models.CharField(max_length=253, editable=False, unique=True) + + homeserver_domain = models.CharField(max_length=253, unique=True, blank=True) + + webclient_domain = models.CharField(max_length=253, unique=True, blank=True) config = models.JSONField(null=False, blank=False) @@ -32,24 +37,18 @@ class VMInstance(models.Model): termination_date = models.DateTimeField(blank=True, null=True) def save(self, *args, **kwargs): - # Read the deployment yaml file and render the template - # Then save it as new yaml file and push it to github repo + # Save it as new yaml file and push it to github repo if 'test' in sys.argv: return super().save(*args, **kwargs) - template_dir = os.path.join(os.path.dirname(__file__), 'yaml') - env = Environment(loader = FileSystemLoader(template_dir),autoescape = True) - tmpl = env.get_template('deployment.yaml.tmpl') - result = tmpl.render( - name=self.vm_id - ) + result = yaml.dump(self.config) gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_OAUTH_TOKEN) project = gl.projects.get(settings.GITLAB_PROJECT_ID) - project.files.create({'file_path': settings.GITLAB_YAML_DIR + f'{self.vm_id}.yaml', + project.files.create({'file_path': settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml', 'branch': 'master', 'content': result, 'author_email': settings.GITLAB_AUTHOR_EMAIL, 'author_name': settings.GITLAB_AUTHOR_NAME, - 'commit_message': f'Add New Deployment for {self.vm_id}'}) + 'commit_message': f'Add New Deployment for matrix-{self.vm_name}'}) super().save(*args, **kwargs) def delete(self, *args, **kwargs): @@ -59,11 +58,11 @@ class VMInstance(models.Model): return super().delete(*args, **kwargs) gl = gitlab.Gitlab(settings.GITLAB_SERVER, oauth_token=settings.GITLAB_OAUTH_TOKEN) project = gl.projects.get(settings.GITLAB_PROJECT_ID) - f_path = settings.GITLAB_YAML_DIR + f'{self.vm_id}.yaml' + f_path = settings.GITLAB_YAML_DIR + f'matrix-{self.vm_name}.yaml' file = project.files.get(file_path=f_path, ref='master') if file: project.files.delete(file_path=f_path, - commit_message=f'Delete {self.vm_id}', branch='master', + commit_message=f'Delete matrix-{self.vm_name}', branch='master', author_email=settings.GITLAB_AUTHOR_EMAIL, author_name=settings.GITLAB_AUTHOR_NAME) diff --git a/matrixhosting/signals.py b/matrixhosting/signals.py index 494a1fc..bdee9c7 100644 --- a/matrixhosting/signals.py +++ b/matrixhosting/signals.py @@ -1,3 +1,4 @@ +import json from matrixhosting.models import VMInstance from uncloud_pay.models import Order from django.db.models.signals import post_save @@ -5,6 +6,14 @@ from django.dispatch import receiver @receiver(post_save, sender=Order) def create_instance(sender, instance, created, **kwargs): + if not created: + return machine = VMInstance.objects.filter(order=instance).first() if not machine: - VMInstance.objects.create(owner=instance.owner, order=instance, config=instance.config) \ No newline at end of file + order_config = json.loads(instance.config) + instance_config = {'cpuCores': order_config['cores'], 'ram': order_config['memory'], 'storage': order_config['storage'], + 'matrixDomain': order_config['matrix_domain'], 'homeserverDomain': order_config['homeserver_domain'], + 'webClientDomain': order_config['webclient_domain'], 'isOpenRegistration': order_config['is_open_registration']} + VMInstance.objects.create(owner=instance.owner, order=instance, vm_name=order_config['homeserver_domain'], + homeserver_domain=order_config['homeserver_domain'],webclient_domain=order_config['webclient_domain'], + config=instance_config) \ No newline at end of file diff --git a/matrixhosting/static/matrixhosting/css/invoice.css b/matrixhosting/static/matrixhosting/css/invoice.css index 60ee335..3d8d04f 100644 --- a/matrixhosting/static/matrixhosting/css/invoice.css +++ b/matrixhosting/static/matrixhosting/css/invoice.css @@ -24,6 +24,7 @@ p { .d2 { line-height:1.5em; padding-top: 15px; + font-style: normal; width: 40%; float: left; } diff --git a/matrixhosting/static/matrixhosting/js/order.js b/matrixhosting/static/matrixhosting/js/order.js index abf1973..bc5381e 100644 --- a/matrixhosting/static/matrixhosting/js/order.js +++ b/matrixhosting/static/matrixhosting/js/order.js @@ -24,12 +24,7 @@ $( document ).ready(function() { $('#createvm-modal-title').text("Error Occurred"); $('#createvm-modal-body').html(data.error.message); } else { - // The payment has succeeded - // Display a success message - window.location.href = '/order/success/'; - // modal_btn.attr('href', data.redirect).removeClass('sr-only sr-only-focusable'); - // $('#createvm-modal-title').text("Order Succeeded"); - // $('#createvm-modal-body').html("Order has been added and the instance will be ready soon"); + window.location.href = data.redirect; } } }); diff --git a/matrixhosting/static/matrixhosting/js/payment.js b/matrixhosting/static/matrixhosting/js/payment.js index 6fdf6fd..6ec03b6 100644 --- a/matrixhosting/static/matrixhosting/js/payment.js +++ b/matrixhosting/static/matrixhosting/js/payment.js @@ -20,7 +20,8 @@ function fetch_pricing() { dataType: 'json', success: function (data) { if (data && data['total']) { - $('#total').text(data['total']); + $('#total').text(data['total'] + " CHF"); + $('#recurring_price').text(data['recurring_price'] + " CHF"); } } }); diff --git a/matrixhosting/static/matrixhosting/js/table.js b/matrixhosting/static/matrixhosting/js/table.js new file mode 100644 index 0000000..58691b2 --- /dev/null +++ b/matrixhosting/static/matrixhosting/js/table.js @@ -0,0 +1,6 @@ +$(document).ready(function () { + $('input[name="filter"]').change(function(e) { + $(location).attr('href', $(e.target).data('url')); + }); + +}); diff --git a/matrixhosting/templates/matrixhosting/dashboard.html b/matrixhosting/templates/matrixhosting/dashboard.html deleted file mode 100644 index d183665..0000000 --- a/matrixhosting/templates/matrixhosting/dashboard.html +++ /dev/null @@ -1,127 +0,0 @@ -{% extends "matrixhosting/base.html" %} {% load static i18n %} -{% block content%} - -{% csrf_token %} -
-
-
-
- - - - - - - - - - - - - - - - {% for object in object_list %} - - - - - - - - - - {% if object.ending_date %} - - {% else %} - - {% endif %} - - {% endfor %} - -
#DescriptionStarting AtConfigPricing PlanOneTime PriceRecurring PriceEnding At
{{ object.id }}{{ object.description }}{{ object.starting_date }}{{ object.config }}{{ object.pricing_plan}}{{ object.one_time_price }}{{ object.recurring_price }}{{ object.ending_date }} - -
-
-
-
-
- - - - - -{% endblock %} - -{% block js_extra %} - -{% endblock %} diff --git a/matrixhosting/templates/matrixhosting/includes/_navbar.html b/matrixhosting/templates/matrixhosting/includes/_navbar.html index 4aa8c71..d6da1e9 100644 --- a/matrixhosting/templates/matrixhosting/includes/_navbar.html +++ b/matrixhosting/templates/matrixhosting/includes/_navbar.html @@ -17,17 +17,17 @@ {% url 'matrix:index' as index_url %} - {% url 'matrix:payments' as payments_url %} + {% url 'matrix:billing' as payments_url %}