forked from Hostea/dashboard
feat: delete_vm takes only one parameter
parent
8fc20d16be
commit
f86dd2ff37
|
@ -119,7 +119,7 @@ class Command(BaseCommand):
|
||||||
vm_name = options[self.vm_name_key]
|
vm_name = options[self.vm_name_key]
|
||||||
if Instance.objects.filter(name=vm_name).exists():
|
if Instance.objects.filter(name=vm_name).exists():
|
||||||
instance = Instance.objects.get(name=vm_name)
|
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):
|
def handle(self, *args, **options):
|
||||||
for i in [self.action_key, self.vm_name_key]:
|
for i in [self.action_key, self.vm_name_key]:
|
||||||
|
|
|
@ -20,6 +20,7 @@ from pathlib import Path
|
||||||
from threading import Thread, Event
|
from threading import Thread, Event
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
import sh
|
||||||
from django.utils.crypto import get_random_string
|
from django.utils.crypto import get_random_string
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail
|
||||||
|
@ -37,8 +38,6 @@ class Worker(Thread):
|
||||||
self.job = job
|
self.job = job
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
######### self.daemon = True
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
gitea_uri = Infra.get_gitea_uri(instance=self.job.instance)
|
gitea_uri = Infra.get_gitea_uri(instance=self.job.instance)
|
||||||
woodpecker = Infra.get_woodpecker_uri(instance=self.job.instance)
|
woodpecker = Infra.get_woodpecker_uri(instance=self.job.instance)
|
||||||
|
@ -46,14 +45,12 @@ class Worker(Thread):
|
||||||
try:
|
try:
|
||||||
print(f"[ping] Trying to reach {gitea_uri}")
|
print(f"[ping] Trying to reach {gitea_uri}")
|
||||||
resp = requests.get(gitea_uri)
|
resp = requests.get(gitea_uri)
|
||||||
print(resp.status_code)
|
|
||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
break
|
break
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
sleep(10)
|
sleep(10)
|
||||||
|
|
||||||
print("sending email")
|
|
||||||
job = self.job
|
job = self.job
|
||||||
self.job = None
|
self.job = None
|
||||||
email = job.instance.owned_by.email
|
email = job.instance.owned_by.email
|
||||||
|
@ -73,7 +70,6 @@ Woodpecker CI: {woodpecker}
|
||||||
recipient_list=[email],
|
recipient_list=[email],
|
||||||
)
|
)
|
||||||
job.delete()
|
job.delete()
|
||||||
print("job deleted")
|
|
||||||
|
|
||||||
|
|
||||||
def create_vm_if_not_exists(instance: Instance) -> (str, Commit):
|
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
|
return None
|
||||||
|
|
||||||
|
|
||||||
def delete_vm(instance: Instance, owner: str):
|
def delete_vm(instance: Instance):
|
||||||
infra = Infra()
|
infra = Infra()
|
||||||
infra.remove_vm(instance=instance)
|
infra.remove_vm(instance=instance)
|
||||||
if InstanceCreated.objects.filter(instance=instance).exists():
|
if InstanceCreated.objects.filter(instance=instance).exists():
|
||||||
|
@ -229,7 +225,9 @@ class Infra:
|
||||||
)
|
)
|
||||||
|
|
||||||
def _pull(self):
|
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
|
@staticmethod
|
||||||
def translate_size(instance: Instance) -> str:
|
def translate_size(instance: Instance) -> str:
|
||||||
|
|
|
@ -26,7 +26,7 @@ from accounts.decorators import confirm_access
|
||||||
from dash.models import Instance
|
from dash.models import Instance
|
||||||
from billing.utils import payment_fullfilled
|
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):
|
def default_ctx(title: str, username: str):
|
||||||
|
@ -84,7 +84,5 @@ def delete_instance(request, instance_name: str):
|
||||||
Dashboard homepage view
|
Dashboard homepage view
|
||||||
"""
|
"""
|
||||||
instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user)
|
instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user)
|
||||||
infra = Infra()
|
delete_vm(instance=instance)
|
||||||
infra.remove_vm(instance=instance)
|
return redirect(reverse("dash.instances.list"))
|
||||||
# TODO: push isn't implemented yet
|
|
||||||
return HttpResponse()
|
|
||||||
|
|
Loading…
Reference in New Issue