feat: delete_vm takes only one parameter

wip-site
Aravinth Manivannan 2022-07-01 19:54:20 +05:30
parent 8fc20d16be
commit f86dd2ff37
Signed by untrusted user: realaravinth
GPG Key ID: AD9F0F08E855ED88
3 changed files with 9 additions and 13 deletions

View File

@ -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]:

View File

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

View File

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