feat: vm delete management command
ci/woodpecker/push/woodpecker Pipeline was successful Details

pull/10/head
Aravinth Manivannan 2022-06-28 01:24:43 +05:30
parent 0606c4ade0
commit 927c2a7703
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 45 additions and 9 deletions

View File

@ -36,6 +36,9 @@ class VmException(Exception):
self.error = str(code) self.error = str(code)
self.code = code self.code = code
def __str__(self):
return self.error
def create_instance(vm_name: str, configuration_name: str, user: User) -> Instance: def create_instance(vm_name: str, configuration_name: str, user: User) -> Instance:
""" """

View File

@ -12,6 +12,7 @@
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from enum import Enum, unique
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.core.exceptions import ValidationError 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 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): class Command(BaseCommand):
help = "Get user ID from username" help = "Get user ID from username"
action_key = "action" action_key = "action"
@ -36,7 +46,7 @@ class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument( parser.add_argument(
self.action_key, self.action_key,
type=str, type=Actions,
help="VM action: create/delete", help="VM action: create/delete",
) )
@ -73,31 +83,39 @@ class Command(BaseCommand):
self.stdout.write(self.style.WARN("flavour no match")) self.stdout.write(self.style.WARN("flavour no match"))
size = flavor size = flavor
user = get_user_model().objects.get(username=owner) user = get_user_model().objects.get(username=owner)
instance = create_instance(vm_name=vm_name, configuration_name=size, user=user) try:
create_instance instance = create_instance(
create_vm_if_not_exists(instance) vm_name=vm_name, configuration_name=size, user=user
print("Instance created") )
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): 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): 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]:
if i not in options: if i not in options:
self.stdout.write(self.style.ERROR(f"Please provide {i}")) self.stdout.write(self.style.ERROR(f"Please provide {i}"))
return return
if options[self.action_key] == "create": if options[self.action_key] == Actions.CREATE:
for i in [self.flavor_key, self.owner_key]: for i in [self.flavor_key, self.owner_key]:
if i not in options: if i not in options:
self.stdout.write(self.style.ERROR(f"Please provide {i}")) self.stdout.write(self.style.ERROR(f"Please provide {i}"))
return return
self.create_vm(*args, **options) self.create_vm(*args, **options)
elif options[self.action_key] == "delete": elif options[self.action_key] == Actions.DELETE:
self.delete_vm(*args, **options) self.delete_vm(*args, **options)
else: else:
self.stdout.write( self.stdout.write(
self.style.ERROR("Unknown action: {options[self.action_key]}") self.style.ERROR(f"Unknown action: {options[self.action_key]}")
) )
return return

View File

@ -140,3 +140,10 @@ class InfraUtilTest(TestCase):
self.assertEqual(instance_created.instance, instance) self.assertEqual(instance_created.instance, instance)
self.assertEqual(instance_created.created, True) 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)

View File

@ -37,6 +37,14 @@ def create_vm_if_not_exists(instance: Instance):
instance.save() 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: class Infra:
""" """
Utility function to manage infrastructure repository Utility function to manage infrastructure repository