From 871a05ddd35268ad5c99a43f20cbea6aea810c2d Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 25 Jun 2022 18:03:04 +0530 Subject: [PATCH] feat: payment check before creation and save gitea passwd in DB --- infrastructure/migrations/0001_initial.py | 37 +++++++++++++++++++ .../0002_instancecreated_gitea_password.py | 18 +++++++++ infrastructure/models.py | 26 ++++++++++++- infrastructure/tests.py | 34 +++++++++++++++-- infrastructure/views.py | 13 ++++++- 5 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 infrastructure/migrations/0001_initial.py create mode 100644 infrastructure/migrations/0002_instancecreated_gitea_password.py diff --git a/infrastructure/migrations/0001_initial.py b/infrastructure/migrations/0001_initial.py new file mode 100644 index 0000000..d7a6dcc --- /dev/null +++ b/infrastructure/migrations/0001_initial.py @@ -0,0 +1,37 @@ +# Generated by Django 4.0.3 on 2022-06-25 10:48 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ("dash", "0006_auto_20220619_0800"), + ] + + operations = [ + migrations.CreateModel( + name="InstanceCreated", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("creted", models.BooleanField(default=False)), + ( + "instance", + models.ForeignKey( + on_delete=django.db.models.deletion.PROTECT, to="dash.instance" + ), + ), + ], + ), + ] diff --git a/infrastructure/migrations/0002_instancecreated_gitea_password.py b/infrastructure/migrations/0002_instancecreated_gitea_password.py new file mode 100644 index 0000000..8a126d2 --- /dev/null +++ b/infrastructure/migrations/0002_instancecreated_gitea_password.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.3 on 2022-06-25 12:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('infrastructure', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='instancecreated', + name='gitea_password', + field=models.CharField(default=None, max_length=32, verbose_name='Name of this configuration'), + ), + ] diff --git a/infrastructure/models.py b/infrastructure/models.py index 71a8362..a74f4ab 100644 --- a/infrastructure/models.py +++ b/infrastructure/models.py @@ -1,3 +1,27 @@ +# Copyright © 2022 Aravinth Manivannan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . from django.db import models -# Create your models here. +from dash.models import Instance + + +class InstanceCreated(models.Model): + instance = models.ForeignKey(Instance, on_delete=models.PROTECT) + gitea_password = models.CharField( + "Name of this configuration", + default=None, + max_length=32, + ) + creted = models.BooleanField(default=False) diff --git a/infrastructure/tests.py b/infrastructure/tests.py index 71e3310..2ddaac4 100644 --- a/infrastructure/tests.py +++ b/infrastructure/tests.py @@ -39,9 +39,10 @@ class InfraUtilTest(TestCase): "INFRA": { "HOSTEA_REPO": { "PATH": "/tmp/hostea/dashboard/test_path_util/repo/", - "REMOTE": "git@gitea.hostea.org:Hostea/payments.git", - "SSH_KEY": "/src/atm/.ssh/id", - } + "REMOTE": "git@git.batsense.net:realaravinth/dummy-hostea-dash-test", + "SSH_KEY": "/src/atm/.ssh/aravinth", + }, + "HOSTEA_DOMAIN": "hostea.org", } } ) @@ -74,3 +75,30 @@ class InfraUtilTest(TestCase): base.joinpath(f"inventory/hosts-scripts/{subdomain}-host.sh"), infra._hostscript_path(subdomain=subdomain), ) + + @override_settings( + HOSTEA={ + "INFRA": { + "HOSTEA_REPO": { + "PATH": "/tmp/hostea/dashboard/test_add_vm/repo/", + "REMOTE": "git@git.batsense.net:realaravinth/dummy-hostea-dash-test", + "SSH_KEY": "/src/atm/.ssh/aravinth", + }, + "HOSTEA_DOMAIN": "hostea.org", + } + } + ) + def test_add_vm(self): + infra = Infra() + c = Client() + login_util(self, c, "accounts.home") + subdomain = "add_vm" + + base = infra.repo_path + + create_instance_util( + t=self, c=c, instance_name=subdomain, config=self.instance_config[0] + ) + + instance = Instance.objects.get(name=subdomain) + woodpecker_agent_secret = infra.add_vm(instance=instance) diff --git a/infrastructure/views.py b/infrastructure/views.py index d315f18..e634e11 100644 --- a/infrastructure/views.py +++ b/infrastructure/views.py @@ -23,8 +23,10 @@ from django.urls import reverse from accounts.decorators import confirm_access from dash.models import Instance +from billing.utils import payment_fullfilled from .utils import Infra +from .models import InstanceCreated def default_ctx(title: str, username: str): @@ -44,9 +46,16 @@ def create_instance(request, instance_name: str): Dashboard homepage view """ instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user) + if not payment_fullfilled(instance=instance): + return redirect(reverse("billing.invoice.generate", args=(instance_name,))) + infra = Infra() - infra.add_vm(instance=instance) - # TODO: push isn't implemented yet + if not InstanceCreated.objects.filter(instance=instance).exists(): + instance = InstanceCreated.objects.create(instance=instance, created=True) + instance.save() + gitea_password = infra.add_vm(instance=instance) + instance.gitea_password = gitea_password + instance.save() return HttpResponse()