[Fix] Exceptions not captured

This commit is contained in:
Vinicius Silva 2023-05-16 14:53:41 -03:00
parent b49b83795f
commit b0ebb5d503
2 changed files with 32 additions and 25 deletions

View File

@ -4,7 +4,6 @@ from danixfs import Danix
from datetime import datetime from datetime import datetime
from settings import SNAPSHOT_LIMIT from settings import SNAPSHOT_LIMIT
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
class Environment(models.Model): class Environment(models.Model):
filesystem_name = models.UUIDField() filesystem_name = models.UUIDField()
status = models.BooleanField(default=True) status = models.BooleanField(default=True)
@ -39,7 +38,7 @@ class Environment(models.Model):
@staticmethod @staticmethod
def rm_environment(filesystem_name): def rm_environment(filesystem_name):
try:
environment = Environment.objects.filter(filesystem_name=filesystem_name) environment = Environment.objects.filter(filesystem_name=filesystem_name)
if environment.count() == 0: if environment.count() == 0:
@ -50,8 +49,10 @@ class Environment(models.Model):
Danix.rm(filesystem_name) Danix.rm(filesystem_name)
env = environment.first() env = environment.first()
env.delete() env.delete()
print("Environment removed successfully!") print(f"Environment removed successfully! - {filesystem_name}")
exit(0) except Exception:
print("Environment does not exist!")
exit(1)
@staticmethod @staticmethod
@ -138,8 +139,7 @@ class Snapshot(models.Model):
if resp == 0: if resp == 0:
Snapshot.objects.get(snapshot_name=snapshot_name).delete() Snapshot.objects.get(snapshot_name=snapshot_name).delete()
print("Snapshot removed successfully") print(f"Snapshot removed successfully - {snapshot_name}")
exit(0)
else: else:
print("Error: Snapshot can not remove") print("Error: Snapshot can not remove")
exit(1) exit(1)
@ -179,10 +179,11 @@ class Snapshot(models.Model):
def create(subsystem_name): def create(subsystem_name):
try: try:
environment = Environment.objects.get(filesystem_name=subsystem_name) environment = Environment.objects.get(filesystem_name=subsystem_name)
environment_id = environment.id environment_id = environment.id
snapshots = Snapshot.objects.filter(environment_id=environment_id) snapshots = Snapshot.objects.filter(environment_id=environment_id)
if snapshots.count() > int(SNAPSHOT_LIMIT): if snapshots.count() >= int(SNAPSHOT_LIMIT):
print("Snapshot limit exceeded! Please remove 1 snapshot to continue") print("Snapshot limit exceeded! Please remove 1 snapshot to continue")
exit(1) exit(1)
else: else:
@ -207,8 +208,7 @@ class Snapshot(models.Model):
print("Snapshot create error!") print("Snapshot create error!")
exit(1) exit(1)
except ValidationError: except Exception:
print("Snapshot create error: Environment does not exist!") print("Snapshot create error: Environment does not exist!")
exit(1) exit(1)

View File

@ -27,13 +27,6 @@ usages.add_argument("-sb", "--snapshotback", help="Back snapshot", requ
usages.add_argument("-o", "--option", choices=["clike", "java", "python", "ruby"], required=False) usages.add_argument("-o", "--option", choices=["clike", "java", "python", "ruby"], required=False)
#fullstack_package = parser.add_argument_group("WebStack", "")
#fullstack_package.add_argument("-d","--db", type=str, required=False)
#fullstack_package.add_argument("-w", "--webserver", type=str, required=False)
#fullstack_package.add_argument("-f", "--framework", type=str, required=False)
#fullstack_package.add_argument("-e", "--env_name", type=str, required=False)
args = parser.parse_args() args = parser.parse_args()
languanges_and_softwares = { languanges_and_softwares = {
@ -55,7 +48,15 @@ if args.start:
Environment.start_environment(args.start) Environment.start_environment(args.start)
if args.rm: if args.rm:
Environment.rm_environment(args.rm) user_confirm = input("Type 'y' to continue: ")
if user_confirm == 'y':
environments = str(args.rm).split(" ")
for environment in environments:
Environment.rm_environment(environment)
exit(0)
print("[Danix]: System abort!")
if args.stop: if args.stop:
Environment.stop_environment(args.stop) Environment.stop_environment(args.stop)
@ -84,5 +85,11 @@ if args.snapshotremove:
user_confirm = input("Type y to continue: ") user_confirm = input("Type y to continue: ")
if user_confirm == 'y': if user_confirm == 'y':
Snapshot.rm_snapshot(args.snapshotremove)
snapshots = str(args.snapshotremove).split(" ")
for snapshot in snapshots:
Snapshot.rm_snapshot(snapshot)
exit(0)
print("[Danix]: System abort!") print("[Danix]: System abort!")