Skip to content

Commit

Permalink
IPI disk removal
Browse files Browse the repository at this point in the history
Signed-off-by: Shay Rozen <[email protected]>
  • Loading branch information
shyRozen committed Nov 24, 2024
1 parent ccd8359 commit 3bb1e26
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ocs_ci/deployment/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,14 @@ 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"])
for vm in all_vms:
vsphere.remove_disks_with_main_disk(vm)

try:
run_cmd(
Expand Down
43 changes: 43 additions & 0 deletions ocs_ci/utility/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1742,3 +1772,16 @@ 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):
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

0 comments on commit 3bb1e26

Please sign in to comment.