From 0dac51661664fe1a965e119aa48a5b52773a7fd4 Mon Sep 17 00:00:00 2001 From: Shay Rozen Date: Mon, 25 Nov 2024 00:10:53 +0200 Subject: [PATCH] IPI disk removal Signed-off-by: Shay Rozen --- ocs_ci/cleanup/vsphere/cleanup.py | 5 ++- ocs_ci/deployment/vmware.py | 9 ++++++ ocs_ci/utility/vsphere.py | 54 +++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/ocs_ci/cleanup/vsphere/cleanup.py b/ocs_ci/cleanup/vsphere/cleanup.py index e7dd5e75ca5..ffdfd7ec494 100644 --- a/ocs_ci/cleanup/vsphere/cleanup.py +++ b/ocs_ci/cleanup/vsphere/cleanup.py @@ -85,10 +85,13 @@ def delete_ipi_nodes(vsphere, cluster_name): vms_ipi = [] for vm in vms_dc: - if cluster_name in vm.name: + if cluster_name in vm.name and "generated-zone" not in vm.name: vms_ipi.append(vm) logger.info(vm.name) if vms_ipi: + for vm in vms_ipi: + logger.info(f"removing disk for vm {vm.name}") + vsphere.remove_disks_with_main_disk(vm) vsphere.destroy_vms(vms_ipi) diff --git a/ocs_ci/deployment/vmware.py b/ocs_ci/deployment/vmware.py index b2ca2800435..727e5bc62ff 100644 --- a/ocs_ci/deployment/vmware.py +++ b/ocs_ci/deployment/vmware.py @@ -1604,6 +1604,15 @@ def destroy_cluster(self, log_level="DEBUG"): template_folder = get_infra_id(self.cluster_path) else: logger.warning("metadata.json file doesn't exist.") + vsphere = VSPHERE( + config.ENV_DATA["vsphere_server"], + config.ENV_DATA["vsphere_user"], + config.ENV_DATA["vsphere_password"], + ) + all_vms = vsphere.get_vms_by_string(config.ENV_DATA["cluster_name"]) + vsphere.stop_vms(all_vms) + for vm in all_vms: + vsphere.remove_disks_with_main_disk(vm) try: run_cmd( diff --git a/ocs_ci/utility/vsphere.py b/ocs_ci/utility/vsphere.py index d2a3b2de3ef..a09139b3540 100644 --- a/ocs_ci/utility/vsphere.py +++ b/ocs_ci/utility/vsphere.py @@ -818,6 +818,36 @@ def get_used_unit_number(self, vm): if hasattr(device.backing, "fileName") and device.unitNumber != 0 ] + def remove_disks_with_main_disk(self, vm): + """ + Removes all disks for a VM + + Args: + vm (vim.VirtualMachine): VM instance + + """ + extra_disk_unit_numbers = self.get_used_unit_number_with_all_unit_number(vm) + if extra_disk_unit_numbers: + for each_disk_unit_number in extra_disk_unit_numbers: + self.remove_disk(vm=vm, identifier=each_disk_unit_number) + + def get_used_unit_number_with_all_unit_number(self, vm): + """ + Gets the used unit numbers including main disk for a VM + + Args: + vm (vim.VirtualMachine): VM instance + + Returns: + list: list of unit numbers + + """ + return [ + device.unitNumber + for device in vm.config.hardware.device + if hasattr(device.backing, "fileName") + ] + def check_folder_exists(self, name, cluster, dc): """ Checks whether folder exists in Templates @@ -1742,3 +1772,27 @@ def get_volume_path(self, volume_id, datastore_name, datacenter_name): volume_path = vstorage_object.config.backing.filePath logger.debug(f"File path for volume {volume_id} is `{volume_path}`") return volume_path + + def get_vms_by_string(self, str_to_match): + """ + Gets the VM's with search string + + Args: + str_to_match (str): String to match VM's + + Returns: + list: VM instance + + """ + + content = self.get_content + container = content.rootFolder + view_type = [vim.VirtualMachine] + recursive = True + + container_view = content.viewManager.CreateContainerView( + container, view_type, recursive + ) + vms = [vm for vm in container_view.view if str_to_match in vm.name] + container_view.Destroy() + return vms