|
23 | 23 | from otcextensions.sdk import sdk_proxy |
24 | 24 | from otcextensions.sdk.obs.v1 import container as _container |
25 | 25 | from otcextensions.sdk.obs.v1 import obj as _obj |
| 26 | +from openstack import utils as os_utils |
| 27 | + |
26 | 28 |
|
27 | 29 | DEFAULT_OBJECT_SEGMENT_SIZE = 1073741824 # 1GB |
28 | 30 | DEFAULT_MAX_FILE_SIZE = int((5 * 1024 * 1024 * 1024 + 2) / 2) |
@@ -772,3 +774,126 @@ def _abort_multipart_upload( |
772 | 774 | """ |
773 | 775 | url = f'{endpoint}?uploadId={upload_id}' |
774 | 776 | return self.delete(url, requests_auth=self._get_req_auth(endpoint)) |
| 777 | + |
| 778 | + def wait_for_delete_object(self, obj, container=None, |
| 779 | + interval=2, wait=300): |
| 780 | + """Waits for an object to be deleted. |
| 781 | +
|
| 782 | + :param obj: The value can be the name of an object or a |
| 783 | + :class:`~otcextensions.sdk.obs.v1.obj.Object` instance. |
| 784 | + :param container: The value can be the name of a container or a |
| 785 | + :class:`~otcextensions.sdk.obs.v1.container.Container`. |
| 786 | + :param int interval: The time in seconds to wait between checks. |
| 787 | + :param int wait: The maximum time in seconds to wait for the object |
| 788 | + to be deleted. |
| 789 | +
|
| 790 | + :returns: The last head response from the server. |
| 791 | + :raises: :class:`~openstack.exceptions.ResourceTimeout` if the |
| 792 | + object is not deleted in the specified time. |
| 793 | + """ |
| 794 | + obj = self._get_resource(_obj.Object, obj) |
| 795 | + container = self._get_container_name(obj, container) |
| 796 | + for _ in os_utils.iterate_timeout( |
| 797 | + timeout=wait, |
| 798 | + message=f"Timeout waiting for {obj.name} " |
| 799 | + f"in {container} to delete", |
| 800 | + wait=interval, |
| 801 | + ): |
| 802 | + try: |
| 803 | + obj.fetch(self, container=container, skip_cache=True) |
| 804 | + except exceptions.NotFoundException: |
| 805 | + return obj |
| 806 | + |
| 807 | + raise exceptions.ResourceTimeout( |
| 808 | + f"Object {obj.name} in {container} was not " |
| 809 | + f"deleted in {wait} seconds" |
| 810 | + ) |
| 811 | + |
| 812 | + def wait_for_delete_container(self, container, interval=2, wait=180): |
| 813 | + """Waits for a container to be deleted. |
| 814 | +
|
| 815 | + :param container: The value can be either the name of a container or a |
| 816 | + :class:`~otcextensions.sdk.obs.v1.container.Container` |
| 817 | + instance. |
| 818 | + :param int interval: The time in seconds to wait between checks. |
| 819 | + :param int wait: The maximum time in seconds to wait for the container |
| 820 | + to be deleted. |
| 821 | + :returns: The last head response from the server. |
| 822 | + :raises: :class:`~openstack.exceptions.ResourceTimeout` if the |
| 823 | + container is not deleted in the specified time. |
| 824 | + """ |
| 825 | + container = self._get_resource(_container.Container, container) |
| 826 | + |
| 827 | + for _ in os_utils.iterate_timeout( |
| 828 | + timeout=wait, |
| 829 | + message=f"Timeout waiting for container" |
| 830 | + f" {container.name} to delete", |
| 831 | + wait=interval, |
| 832 | + ): |
| 833 | + try: |
| 834 | + self.get_container(container.name) |
| 835 | + except exceptions.NotFoundException: |
| 836 | + return container |
| 837 | + raise exceptions.ResourceTimeout( |
| 838 | + f"Container {container.name} was not deleted in {wait} seconds" |
| 839 | + ) |
| 840 | + |
| 841 | + def _get_cleanup_dependencies(self): |
| 842 | + return { |
| 843 | + 'obs': { |
| 844 | + 'before': ['network'] |
| 845 | + } |
| 846 | + } |
| 847 | + |
| 848 | + def _service_cleanup( |
| 849 | + self, |
| 850 | + dry_run=True, |
| 851 | + client_status_queue=None, |
| 852 | + identified_resources=None, |
| 853 | + filters=None, |
| 854 | + resource_evaluation_fn=None, |
| 855 | + skip_resources=None, |
| 856 | + cont_name=None, |
| 857 | + ): |
| 858 | + pass |
| 859 | + containers = [] |
| 860 | + for container in self.containers(): |
| 861 | + objects = [] |
| 862 | + for obj in self.objects(container=container.name): |
| 863 | + need_delete = self._service_cleanup_del_res( |
| 864 | + lambda o: self.delete_object(o, container=container.name), |
| 865 | + obj, |
| 866 | + dry_run=dry_run, |
| 867 | + client_status_queue=client_status_queue, |
| 868 | + identified_resources=identified_resources, |
| 869 | + filters=filters, |
| 870 | + resource_evaluation_fn=resource_evaluation_fn |
| 871 | + ) |
| 872 | + if not dry_run and need_delete: |
| 873 | + objects.append(obj) |
| 874 | + for obj in objects: |
| 875 | + try: |
| 876 | + self.wait_for_delete_object(obj, container=container.name) |
| 877 | + except Exception as e: |
| 878 | + self.log.warning( |
| 879 | + f"Failed to delete object {obj.name} " |
| 880 | + f"from {container.name}: {e}" |
| 881 | + ) |
| 882 | + need_delete = self._service_cleanup_del_res( |
| 883 | + self.delete_container, |
| 884 | + container, |
| 885 | + dry_run=dry_run, |
| 886 | + client_status_queue=client_status_queue, |
| 887 | + identified_resources=identified_resources, |
| 888 | + filters=filters, |
| 889 | + resource_evaluation_fn=resource_evaluation_fn |
| 890 | + ) |
| 891 | + if not dry_run and need_delete: |
| 892 | + containers.append(container) |
| 893 | + for container in containers: |
| 894 | + try: |
| 895 | + self.wait_for_delete_container(container) |
| 896 | + except Exception as e: |
| 897 | + self.log.warning( |
| 898 | + f"Failed to delete container {container.name}: {e}" |
| 899 | + ) |
0 commit comments