From f86dd2ff3713136871834aaec1e66d9a063f6aca Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 1 Jul 2022 19:54:20 +0530 Subject: [PATCH] feat: delete_vm takes only one parameter --- infrastructure/management/commands/vm.py | 2 +- infrastructure/utils.py | 12 +++++------- infrastructure/views.py | 8 +++----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/infrastructure/management/commands/vm.py b/infrastructure/management/commands/vm.py index ed44ab2..2495a20 100644 --- a/infrastructure/management/commands/vm.py +++ b/infrastructure/management/commands/vm.py @@ -119,7 +119,7 @@ class Command(BaseCommand): vm_name = options[self.vm_name_key] if Instance.objects.filter(name=vm_name).exists(): instance = Instance.objects.get(name=vm_name) - delete_vm(instance=instance, owner=instance.owned_by.username) + delete_vm(instance=instance) def handle(self, *args, **options): for i in [self.action_key, self.vm_name_key]: diff --git a/infrastructure/utils.py b/infrastructure/utils.py index 9261b0b..ce6251e 100644 --- a/infrastructure/utils.py +++ b/infrastructure/utils.py @@ -20,6 +20,7 @@ from pathlib import Path from threading import Thread, Event from time import sleep +import sh from django.utils.crypto import get_random_string from django.template.loader import render_to_string from django.core.mail import send_mail @@ -37,8 +38,6 @@ class Worker(Thread): self.job = job super().__init__() - ######### self.daemon = True - def run(self): gitea_uri = Infra.get_gitea_uri(instance=self.job.instance) woodpecker = Infra.get_woodpecker_uri(instance=self.job.instance) @@ -46,14 +45,12 @@ class Worker(Thread): try: print(f"[ping] Trying to reach {gitea_uri}") resp = requests.get(gitea_uri) - print(resp.status_code) if resp.status_code == 200: break except Exception: return False sleep(10) - print("sending email") job = self.job self.job = None email = job.instance.owned_by.email @@ -73,7 +70,6 @@ Woodpecker CI: {woodpecker} recipient_list=[email], ) job.delete() - print("job deleted") def create_vm_if_not_exists(instance: Instance) -> (str, Commit): @@ -96,7 +92,7 @@ def create_vm_if_not_exists(instance: Instance) -> (str, Commit): return None -def delete_vm(instance: Instance, owner: str): +def delete_vm(instance: Instance): infra = Infra() infra.remove_vm(instance=instance) if InstanceCreated.objects.filter(instance=instance).exists(): @@ -229,7 +225,9 @@ class Infra: ) def _pull(self): - self.repo.git.pull(env=self.env, rebase="true") + self.repo.git.fetch(env=self.env) + + # TODO: switch to using Git cmd @staticmethod def translate_size(instance: Instance) -> str: diff --git a/infrastructure/views.py b/infrastructure/views.py index 37a3292..7607c6c 100644 --- a/infrastructure/views.py +++ b/infrastructure/views.py @@ -26,7 +26,7 @@ from accounts.decorators import confirm_access from dash.models import Instance from billing.utils import payment_fullfilled -from .utils import create_vm_if_not_exists, Infra +from .utils import create_vm_if_not_exists, Infra, delete_vm def default_ctx(title: str, username: str): @@ -84,7 +84,5 @@ def delete_instance(request, instance_name: str): Dashboard homepage view """ instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user) - infra = Infra() - infra.remove_vm(instance=instance) - # TODO: push isn't implemented yet - return HttpResponse() + delete_vm(instance=instance) + return redirect(reverse("dash.instances.list"))