feat: payment check before creation and save gitea passwd in DB
ci/woodpecker/push/woodpecker Pipeline failed Details
ci/woodpecker/pr/woodpecker Pipeline failed Details

pull/1/head
Aravinth Manivannan 2022-06-25 18:03:04 +05:30
parent beb4b29c49
commit 871a05ddd3
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
5 changed files with 122 additions and 6 deletions

View File

@ -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"
),
),
],
),
]

View File

@ -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'),
),
]

View File

@ -1,3 +1,27 @@
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# 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 <http://www.gnu.org/licenses/>.
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)

View File

@ -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)

View File

@ -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()