From 927c2a7703706fd55d47584683079d4ebd62a6aa Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 28 Jun 2022 01:24:43 +0530 Subject: [PATCH] feat: vm delete management command --- dash/utils.py | 3 ++ infrastructure/management/commands/vm.py | 36 ++++++++++++++++++------ infrastructure/tests.py | 7 +++++ infrastructure/utils.py | 8 ++++++ 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/dash/utils.py b/dash/utils.py index a01c6cf..2f3e3d6 100644 --- a/dash/utils.py +++ b/dash/utils.py @@ -36,6 +36,9 @@ class VmException(Exception): self.error = str(code) self.code = code + def __str__(self): + return self.error + def create_instance(vm_name: str, configuration_name: str, user: User) -> Instance: """ diff --git a/infrastructure/management/commands/vm.py b/infrastructure/management/commands/vm.py index 34cf258..540e53d 100644 --- a/infrastructure/management/commands/vm.py +++ b/infrastructure/management/commands/vm.py @@ -12,6 +12,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from enum import Enum, unique from django.core.management.base import BaseCommand from django.core.exceptions import ValidationError @@ -26,6 +27,15 @@ from dash.utils import create_instance from infrastructure.utils import create_vm_if_not_exists +@unique +class Actions(Enum): + CREATE = "create" + DELETE = "delete" + + def __str__(self) -> str: + return self.name + + class Command(BaseCommand): help = "Get user ID from username" action_key = "action" @@ -36,7 +46,7 @@ class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument( self.action_key, - type=str, + type=Actions, help="VM action: create/delete", ) @@ -73,31 +83,39 @@ class Command(BaseCommand): self.stdout.write(self.style.WARN("flavour no match")) size = flavor user = get_user_model().objects.get(username=owner) - instance = create_instance(vm_name=vm_name, configuration_name=size, user=user) - create_instance - create_vm_if_not_exists(instance) - print("Instance created") + try: + instance = create_instance( + vm_name=vm_name, configuration_name=size, user=user + ) + create_vm_if_not_exists(instance) + print("Instance created") + except Exception as e: + self.stderr.write(self.style.ERROR(f"error: {str(e)}")) def delete_vm(self, *args, **options): - create_vm + from infrastructure.utils import delete_vm + + vm_name = options[self.vm_name_key] + instance = Instance.objects.get(name=vm_name) + delete_vm(instance=instance, owner=instance.owned_by.username) def handle(self, *args, **options): for i in [self.action_key, self.vm_name_key]: if i not in options: self.stdout.write(self.style.ERROR(f"Please provide {i}")) return - if options[self.action_key] == "create": + if options[self.action_key] == Actions.CREATE: for i in [self.flavor_key, self.owner_key]: if i not in options: self.stdout.write(self.style.ERROR(f"Please provide {i}")) return self.create_vm(*args, **options) - elif options[self.action_key] == "delete": + elif options[self.action_key] == Actions.DELETE: self.delete_vm(*args, **options) else: self.stdout.write( - self.style.ERROR("Unknown action: {options[self.action_key]}") + self.style.ERROR(f"Unknown action: {options[self.action_key]}") ) return diff --git a/infrastructure/tests.py b/infrastructure/tests.py index 3105d1b..d71ea48 100644 --- a/infrastructure/tests.py +++ b/infrastructure/tests.py @@ -140,3 +140,10 @@ class InfraUtilTest(TestCase): self.assertEqual(instance_created.instance, instance) self.assertEqual(instance_created.created, True) + + call_command("vm", "delete", vm_name) + out = stdout.getvalue() + + self.assertEqual(Instance.objects.filter(name=vm_name).exists(), False) + host_vars_dir = infra._host_vars_dir(subdomain) + self.assertEqual(host_vars_dir.exists(), False) diff --git a/infrastructure/utils.py b/infrastructure/utils.py index 601ad1f..efd0f61 100644 --- a/infrastructure/utils.py +++ b/infrastructure/utils.py @@ -37,6 +37,14 @@ def create_vm_if_not_exists(instance: Instance): instance.save() +def delete_vm(instance: Instance, owner: str): + infra = Infra() + infra.remove_vm(instance=instance) + if InstanceCreated.objects.filter(instance=instance).exists(): + InstanceCreated.objects.get(instance=instance).delete() + instance.delete() + + class Infra: """ Utility function to manage infrastructure repository