Skip to content

Commit

Permalink
Merge pull request #146 from jyejare/vmware_alignment
Browse files Browse the repository at this point in the history
[Develop] VMWare cleanup alignment with Develop branch
  • Loading branch information
jyejare authored Oct 26, 2024
2 parents 6d1c1bb + 89f5015 commit bf9e8c4
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 9 deletions.
10 changes: 9 additions & 1 deletion cloudwash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cloudwash.providers.azure import cleanup as azureCleanup
from cloudwash.providers.gce import cleanup as gceCleanup
from cloudwash.providers.podman import cleanup as podmanCleanup
from cloudwash.providers.vmware import cleanup as vmwareCleanup

# Adding the pythonpath for importing modules from cloudwash packages
# sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
Expand Down Expand Up @@ -134,7 +135,14 @@ def podman(ctx, containers):
@click.pass_context
def vmware(ctx, vms, discs, nics, _all):
validate_provider(ctx.command.name)
# TODO: Further TO_BE_IMPLEMENTED
is_dry_run = ctx.parant.params['dry']
vmwareCleanup(
vms=vms,
discs=discs,
nics=nics,
_all=_all,
dry_run=is_dry_run,
)


@cleanup_providers.command(help="Cleanup RHEV provider")
Expand Down
6 changes: 6 additions & 0 deletions cloudwash/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def compute_client(compute_resource, **kwargs):
username=settings.podman.auth.username,
port=settings.podman.auth.ssh_port,
)
elif compute_resource == "vmware":
client = wrapanapi.VMWareSystem(
hostname=settings.vmware.auth.vcenter,
username=settings.vmware.auth.username,
password=settings.vmware.auth.password,
)
else:
raise ValueError(
f"{compute_resource} is an incorrect value. It should be one of azure or gce or ec2"
Expand Down
1 change: 1 addition & 0 deletions cloudwash/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
aws_data = ['VMS', 'NICS', 'DISCS', 'PIPS', 'RESOURCES', 'STACKS']
azure_data = ['VMS', 'NICS', 'DISCS', 'IMAGES', 'PIPS', 'RESOURCES']
gce_data = ['VMS', 'NICS', 'DISCS']
vmware_data = ['VMS', 'NICS', 'DISCS']
container_data = ['CONTAINERS']
9 changes: 9 additions & 0 deletions cloudwash/entities/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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.vms import CleanVMWareVMs


class providerCleanup:
Expand All @@ -26,6 +27,8 @@ def vms(self):
return CleanAWSVms(client=self.client)
elif 'GCE' in providerclass:
return CleanGCEVMs(client=self.client)
elif 'VMWare' in providerclass:
return CleanVMWareVMs(client=self.client)

@property
def discs(self):
Expand Down Expand Up @@ -84,6 +87,12 @@ def __init__(self, client):
super().__init__(client)


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


class PodmanCleanup(providerCleanup):
def __init__(self, client):
self.client = client
Expand Down
35 changes: 27 additions & 8 deletions cloudwash/entities/resources/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ def list(self):

def remove(self, **kwargs):
for vm_name in self._delete:
self.client.get_vm(vm_name, **kwargs).delete()
logger.info(f"Removed VMs: \n{self._delete}")
try:
self.client.get_vm(vm_name, **kwargs).delete()
logger.info(f"Removed VMs: \n{self._delete}")
except Exception:
logger.exception(f"Error removing VM {vm_name}")

def stop(self, **kwargs):
for vm_name in self._stop:
self.client.get_vm(vm_name, **kwargs).stop()
logger.info(f"Stopped VMs: \n{self._stop}")
try:
self.client.get_vm(vm_name, **kwargs).stop()
logger.info(f"Stopped VMs: \n{self._stop}")
except Exception:
logger.exception(f"Error stopping VM {vm_name}")

def skip(self):
logger.info(f"Skipped VMs: \n{self._skip}")
Expand All @@ -55,8 +61,6 @@ def list(self):
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()
Expand Down Expand Up @@ -86,7 +90,6 @@ def list(self):
):
if vm.name in settings.azure.exceptions.vm.stop_list:
self._stop.append(vm.name)
continue
elif vm.name.startswith(settings.azure.criteria.vm.delete_vm):
self._delete.append(vm.name)
self._set_dry()
Expand All @@ -104,7 +107,6 @@ def list(self):
elif total_running_time(vm).minutes >= settings.gce.criteria.vm.sla_minutes:
if vm.name in settings.gce.exceptions.vm.stop_list:
self._stop.append(vm.name)
continue
elif vm.name.startswith(settings.gce.criteria.vm.delete_vm):
self._delete.append(vm.name)
self._set_dry()
Expand All @@ -114,3 +116,20 @@ def remove(self):

def stop(self):
super().stop(zone=self.client.cleaning_zone)


class CleanVMWareVMs(CleanVMs):
def list(self):

allvms = self.client.list_vms()

for vm in allvms:
if vm.name in settings.vmware.exceptions.vm.vm_list:
self._skip.append(vm.name)
continue
elif total_running_time(vm).minutes >= settings.vmware.criteria.vm.sla_minutes:
if vm.name in settings.vmware.exceptions.vm.stop_list:
self._stop.append(vm.name)
elif vm.name.startswith(settings.vmware.criteria.vm.delete_vm):
self._delete.append(vm.name)
self._set_dry()
25 changes: 25 additions & 0 deletions cloudwash/providers/vmware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""VMWare CR Cleanup Utilities"""
from cloudwash.client import compute_client
from cloudwash.constants import vmware_data as data
from cloudwash.entities.providers import VMWareCleanup
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)
dry_data['PROVIDER'] = "VMWARE"
if kwargs["nics"] or kwargs["_all"]:
logger.warning("Cloudwash does not supports NICs operation for VMWare yet!")
if kwargs["discs"] or kwargs["_all"]:
logger.warning("Cloudwash does not supports DISCs operation for VMWare yet!")

with compute_client("vmware") as vmware_client:
for items in data:
dry_data[items]['delete'] = []
vmware_cleanup = VMWareCleanup(client=vmware_client)
if kwargs["vms"] or kwargs["_all"]:
vmware_cleanup.vms.cleanup()
if is_dry_run:
echo_dry(dry_data)
17 changes: 17 additions & 0 deletions conf/vmware.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
VMWARE:
AUTH:
VCENTER:
USERNAME:
PASSWORD:
CRITERIA:
VM:
# The VM to be deleted with prepend string, e.g VM name that starts with 'test'
DELETE_VM: 'test'
# Number of minutes the deletable VM should be allowed to live, e.g 120 minutes = 2 Hours
SLA_MINUTES: 120
EXCEPTIONS:
VM:
# VM names that would be skipped from cleanup
VM_LIST: []
# VMs that would be stopped from current running state
STOP_LIST: []
17 changes: 17 additions & 0 deletions settings.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,20 @@ PODMAN:
SKIP_LIST: []
# Containers to stop if running but not delete
STOP_LIST: []
VMWARE:
AUTH:
VCENTER:
USERNAME:
PASSWORD:
CRITERIA:
VM:
# The VM to be deleted with prepend string, e.g VM name that starts with 'test'
DELETE_VM: 'test'
# Number of minutes the deletable VM should be allowed to live, e.g 120 minutes = 2 Hours
SLA_MINUTES: 120
EXCEPTIONS:
VM:
# VM names that would be skipped from cleanup
VM_LIST: []
# VMs that would be stopped from current running state
STOP_LIST: []

0 comments on commit bf9e8c4

Please sign in to comment.