From 89f5015f119d9db4cf4312b28e1cfe469f2f8538 Mon Sep 17 00:00:00 2001 From: jyejare Date: Fri, 25 Oct 2024 14:55:13 +0530 Subject: [PATCH] VMWare cleanup alignment with Develop branch --- cloudwash/cli.py | 10 ++++++++- cloudwash/client.py | 6 +++++ cloudwash/constants.py | 1 + cloudwash/entities/providers.py | 9 ++++++++ cloudwash/entities/resources/vms.py | 35 ++++++++++++++++++++++------- cloudwash/providers/vmware.py | 25 +++++++++++++++++++++ conf/vmware.yaml.template | 17 ++++++++++++++ settings.yaml.template | 17 ++++++++++++++ 8 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 cloudwash/providers/vmware.py create mode 100644 conf/vmware.yaml.template diff --git a/cloudwash/cli.py b/cloudwash/cli.py index ec96a2a32..7272843a7 100644 --- a/cloudwash/cli.py +++ b/cloudwash/cli.py @@ -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))) @@ -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") diff --git a/cloudwash/client.py b/cloudwash/client.py index b155dec50..6bc5c0bb0 100644 --- a/cloudwash/client.py +++ b/cloudwash/client.py @@ -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" diff --git a/cloudwash/constants.py b/cloudwash/constants.py index 750d16139..ad6a1822f 100644 --- a/cloudwash/constants.py +++ b/cloudwash/constants.py @@ -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'] diff --git a/cloudwash/entities/providers.py b/cloudwash/entities/providers.py index 33dcd7cb8..9fbe347e2 100644 --- a/cloudwash/entities/providers.py +++ b/cloudwash/entities/providers.py @@ -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: @@ -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): @@ -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 diff --git a/cloudwash/entities/resources/vms.py b/cloudwash/entities/resources/vms.py index b919b5126..3c54bdd55 100644 --- a/cloudwash/entities/resources/vms.py +++ b/cloudwash/entities/resources/vms.py @@ -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}") @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/cloudwash/providers/vmware.py b/cloudwash/providers/vmware.py new file mode 100644 index 000000000..c5c5f99b6 --- /dev/null +++ b/cloudwash/providers/vmware.py @@ -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) diff --git a/conf/vmware.yaml.template b/conf/vmware.yaml.template new file mode 100644 index 000000000..949e8f5f8 --- /dev/null +++ b/conf/vmware.yaml.template @@ -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: [] diff --git a/settings.yaml.template b/settings.yaml.template index af7c65a7b..3a4a75b39 100644 --- a/settings.yaml.template +++ b/settings.yaml.template @@ -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: []