Skip to content

Commit

Permalink
Cloudwash supporting Containers cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jyejare committed Sep 16, 2024
1 parent 97ee8a4 commit 7b91c68
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions cloudwash/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aws_data = ['VMS', 'NICS', 'DISCS', 'PIPS', 'RESOURCES', 'STACKS']
azure_data = ['VMS', 'NICS', 'DISCS', 'IMAGES', 'PIPS', 'RESOURCES']
gce_data = ['VMS', 'NICS', 'DISCS']
container_data = ['CONTAINERS']
11 changes: 11 additions & 0 deletions cloudwash/entities/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cloudwash.entities.resources.vms import CleanAWSVms
from cloudwash.entities.resources.vms import CleanAzureVMs
from cloudwash.entities.resources.vms import CleanGCEVMs
from cloudwash.entities.resources.containers import CleanPodmanContainers


class providerCleanup:
Expand Down Expand Up @@ -81,3 +82,13 @@ class GCECleanup(providerCleanup):
def __init__(self, client):
self.client = client
super().__init__(client)


class PodmanCleanup(providerCleanup):
def __init__(self, client):
self.client = client
super().__init__(client)

@property
def containers(self):
return CleanPodmanContainers(client=self.client)
26 changes: 26 additions & 0 deletions cloudwash/entities/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,32 @@ def _set_dry(self):
pass


class ContainerCleanup(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 = []
Expand Down
46 changes: 46 additions & 0 deletions cloudwash/entities/resources/containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from cloudwash.config import settings
from cloudwash.entities.resources.base import ContainerCleanup
from cloudwash.logger import logger
from cloudwash.utils import dry_data


class CleanContainers(ContainerCleanup):
def __init__(self, client):
self.client = client
self._delete = []
self._stop = []
self.list()

def _set_dry(self):
dry_data['CONTAINERS']['delete'] = self._delete
dry_data['CONTAINERS']['delete'] = self._stop

def list(self):
pass

def stop(self):
pass

def remove(self):
pass

def cleanup(self):
if not settings.dry_run:
self.remove()


class CleanPodmanContainers(CleanContainers):
def list(self):
if settings.podman.criteria.containers.something:
pass
# rdiscs = self.client.get_all_unattached_volumes()
# rdiscs = [rdisc["VolumeId"] for rdisc in rdiscs]
# self._delete.extend(rdiscs)
self._set_dry()

def stop(self):
pass

def remove(self):
# self.client.remove_all_unused_volumes()
logger.info(f"Removed Podman Containers: \n{self._delete}")
25 changes: 25 additions & 0 deletions cloudwash/entities/resources/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,28 @@ def list(self):
]
self._delete.extend(remove_images)
self._set_dry()


class CleanPodmanImages(CleanImages):
def remove(self):
pass
# self.client.delete_compute_image_by_resource_group(image_list=self._delete)
# logger.info(f"Removed Images: \n{self._delete}")

def list(self):
pass
# if settings.azure.criteria.image.unassigned:
# images_list = self.client.list_compute_images_by_resource_group(free_images=True)
# image_names = [image.name for image in images_list]
# # Filter out the images not to be removed.
# remove_images = [
# image for image in image_names if image not in settings.azure.exceptions.images
# ]
# if settings.azure.criteria.image.delete_image:
# remove_images = [
# image
# for image in remove_images
# if image.startswith(settings.azure.criteria.image.delete_image)
# ]
# self._delete.extend(remove_images)
self._set_dry()
23 changes: 23 additions & 0 deletions cloudwash/providers/podman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Azure CR Cleanup Utilities"""
from cloudwash.client import compute_client
from cloudwash.config import settings
from cloudwash.constants import container_data as data
from cloudwash.entities.providers import PodmanCleanup
from cloudwash.logger import logger
from cloudwash.utils import dry_data
from cloudwash.utils import echo_dry


def cleanup(**kwargs):
is_dry_run = kwargs.get("dry_run", False)
for items in data:
dry_data[items]['delete'] = []
with compute_client("podman") as podman_client:
podmancleanup = PodmanCleanup(client=podman_client)
# Actual Cleaning and dry execution
if kwargs["containers"] or kwargs["_all"]:
podmancleanup.containers.cleanup()
if kwargs["images"] or kwargs["_all"]:
podmancleanup.images.cleanup()
if is_dry_run:
echo_dry(dry_data)

0 comments on commit 7b91c68

Please sign in to comment.