Skip to content

Commit

Permalink
Merge pull request #116 from jyejare/jyejare-abstract-refactor
Browse files Browse the repository at this point in the history
[Develop] Code Abstraction/Standardisation of Resource Types
  • Loading branch information
jyejare authored Nov 3, 2023
2 parents 86f5369 + c14f163 commit 5f91930
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 28 deletions.
Empty file added cloudwash/entities/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions cloudwash/entities/base.py
Original file line number Diff line number Diff line change
@@ -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
Empty file.
56 changes: 56 additions & 0 deletions cloudwash/entities/resources/aws.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 4 additions & 28 deletions cloudwash/providers/aws.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -97,26 +82,17 @@ 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]

# 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:
Expand Down

0 comments on commit 5f91930

Please sign in to comment.