From c14f16331b35dfe85cc063a071691ab6859578e9 Mon Sep 17 00:00:00 2001 From: jyejare Date: Mon, 24 Jul 2023 00:30:16 +0530 Subject: [PATCH] Abstraction of Resources for cleanup More: AWS Cleanup for VMs changed with new structure Pre Drydata improvements before major refactor --- cloudwash/entities/__init__.py | 0 cloudwash/entities/base.py | 61 ++++++++++++++++++++++++ cloudwash/entities/resources/__init__.py | 0 cloudwash/entities/resources/aws.py | 56 ++++++++++++++++++++++ cloudwash/providers/aws.py | 32 ++----------- 5 files changed, 121 insertions(+), 28 deletions(-) create mode 100644 cloudwash/entities/__init__.py create mode 100644 cloudwash/entities/base.py create mode 100644 cloudwash/entities/resources/__init__.py create mode 100644 cloudwash/entities/resources/aws.py diff --git a/cloudwash/entities/__init__.py b/cloudwash/entities/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/cloudwash/entities/base.py b/cloudwash/entities/base.py new file mode 100644 index 000000000..403066801 --- /dev/null +++ b/cloudwash/entities/base.py @@ -0,0 +1,61 @@ +from abc import ABC +from abc import abstractmethod + + +class ResourceCleanup(ABC): + @abstractmethod + def cleanup(self): + pass + + @abstractmethod + def list(self): + pass + + @abstractmethod + def _set_dry(self): + pass + + +class VMsCleanup(ResourceCleanup): + @abstractmethod + def list(self): + pass + + @abstractmethod + def cleanup(self): + pass + + @abstractmethod + def stop(self): + pass + + @abstractmethod + def remove(self): + pass + + @abstractmethod + def skip(self): + pass + + @abstractmethod + def _set_dry(self): + pass + + +class ResourceCleanupManager: + def __init__(self): + self.resources = [] + + def add(self, resource): + self.resources.append(resource) + + def remove(self): + for resource in self.resources: + resource.remove() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.remove() + return False diff --git a/cloudwash/entities/resources/__init__.py b/cloudwash/entities/resources/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/cloudwash/entities/resources/aws.py b/cloudwash/entities/resources/aws.py new file mode 100644 index 000000000..0c86b5d89 --- /dev/null +++ b/cloudwash/entities/resources/aws.py @@ -0,0 +1,56 @@ +from cloudwash.config import settings +from cloudwash.entities.base import VMsCleanup +from cloudwash.logger import logger +from cloudwash.utils import dry_data +from cloudwash.utils import total_running_time + + +class CleanVMs(VMsCleanup): + def __init__(self, awsclient): + self.client = awsclient + self._delete = [] + self._stop = [] + self._skip = [] + self.list() + + def _set_dry(self): + # VMsContainer = namedtuple('VMsCotainer', ['delete', 'stop', 'skip']) + # return VMsContainer(self._delete, self._stop, self._skip) + dry_data['VMS']['delete'] = self._delete + dry_data['VMS']['stop'] = self._stop + dry_data['VMS']['skip'] = self._skip + + def list(self): + all_vms = self.client.list_vms() + + for vm in all_vms: + if vm.name in settings.aws.exceptions.vm.vm_list: + self._delete.append(vm.name) + continue + + elif total_running_time(vm).minutes >= settings.aws.criteria.vm.sla_minutes: + if vm.name in settings.aws.exceptions.vm.stop_list: + self._stop.append(vm.name) + continue + + elif vm.name.startswith(settings.aws.criteria.vm.delete_vm): + self._delete.append(vm.name) + self._set_dry() + + def remove(self): + for vm_name in self._delete: + self.client.get_vm(vm_name).delete() + logger.info(f"Removed VMs: \n{self._delete}") + + def stop(self): + for vm_name in self._stop: + self.client.get_vm(vm_name).stop() + logger.info(f"Stopped VMs: \n{self._stop}") + + def skip(self): + logger.info(f"Skipped VMs: \n{self._skip}") + + def cleanup(self): + self.remove() + self.stop() + self.skip() diff --git a/cloudwash/providers/aws.py b/cloudwash/providers/aws.py index 89579050a..3a5359a16 100644 --- a/cloudwash/providers/aws.py +++ b/cloudwash/providers/aws.py @@ -1,6 +1,7 @@ """ec2 CR Cleanup Utilities""" from cloudwash.client import compute_client from cloudwash.config import settings +from cloudwash.entities.resources.aws import CleanVMs from cloudwash.logger import logger from cloudwash.utils import dry_data from cloudwash.utils import echo_dry @@ -16,26 +17,10 @@ def cleanup(**kwargs): with compute_client("aws", aws_region="us-west-2") as client: regions = client.list_regions() for region in regions: - dry_data['VMS']['stop'] = [] - dry_data['VMS']['skip'] = [] for items in data: dry_data[items]['delete'] = [] with compute_client("aws", aws_region=region) as aws_client: # Dry Data Collection Defs - def dry_vms(): - all_vms = aws_client.list_vms() - for vm in all_vms: - if vm.name in settings.aws.exceptions.vm.vm_list: - dry_data["VMS"]["skip"].append(vm.name) - continue - elif total_running_time(vm).minutes >= settings.aws.criteria.vm.sla_minutes: - if vm.name in settings.aws.exceptions.vm.stop_list: - dry_data["VMS"]["stop"].append(vm.name) - continue - elif vm.name.startswith(settings.aws.criteria.vm.delete_vm): - dry_data["VMS"]["delete"].append(vm.name) - return dry_data["VMS"] - def dry_nics(): rnics = [] if settings.aws.criteria.nic.unassigned: @@ -97,13 +82,6 @@ def dry_stacks(): return rstacks - # Remove / Stop VMs - def remove_vms(avms): - # Remove VMs - [aws_client.get_vm(vm_name).delete() for vm_name in avms["delete"]] - # Stop VMs - [aws_client.get_vm(vm_name).stop() for vm_name in avms["stop"]] - # Delete CloudFormations def remove_stacks(stacks): [aws_client.get_stack(stack_name).delete() for stack_name in stacks] @@ -111,12 +89,10 @@ def remove_stacks(stacks): # Actual Cleaning and dry execution logger.info(f"\nResources from the region: {region}") if kwargs["vms"] or kwargs["_all"]: - avms = dry_vms() + vms_cleanup = CleanVMs(awsclient=aws_client) if not is_dry_run: - remove_vms(avms=avms) - logger.info(f"Stopped VMs: \n{avms['stop']}") - logger.info(f"Removed VMs: \n{avms['delete']}") - logger.info(f"Skipped VMs: \n{avms['skip']}") + vms_cleanup.cleanup() + if kwargs["nics"] or kwargs["_all"]: rnics = dry_nics() if not is_dry_run and rnics: