def cleanup_env_build_docker_artifacts(filters): """Cleanup container(s) and images given filters. Args: filters: Returns: """ # Actually we are just looking for a single container, but the # syntax is the same as looking for N. containers_to_prune = docker_client.containers.list(filters=filters, all=True) tries = 0 while containers_to_prune: docker_client.containers.prune(filters=filters) containers_to_prune = docker_client.containers.list(filters=filters, all=True) # Be as responsive as possible, only sleep at the first # iteration if necessary. if containers_to_prune: tries += 1 if tries > 100: logging.error("Could not prune containers: %s" % [con.attrs for con in containers_to_prune]) break time.sleep(1) # Only get to this point once there are no depending containers> # We DO NOT use all=True as an argument here because it will also # return intermediate layers, which will not be pruneable if the # build is successful. Those layers will be automatically deleted # when the env image is deleted. What we are actually doing here # is getting the last image created by the build process by getting # all the images created by the build process. Removing n-1 of them # will result in a no op, but 1 of them will cause the "ancestor # images to be removed as well. images_to_prune = docker_images_list_safe(docker_client, filters=filters) tries = 0 while images_to_prune: docker_client.images.prune(filters=filters) images_to_prune = docker_images_list_safe(docker_client, filters=filters) # Be as responsive as possible, only sleep at the first # iteration if necessary. if images_to_prune: tries += 1 if tries > 100: logging.error("Could not prune images: %s" % [img.attrs for img in images_to_prune]) break time.sleep(1)
def delete_project_environment_images(project_uuid): """Delete environment images of a project. All environment images related to the project are removed from the environment, running environment builds are stopped and removed from the db. Dangling docker images are also removed. Args: project_uuid: """ # cleanup references to the builds and dangling images # of all environments of this project delete_project_builds(project_uuid) delete_project_dangling_images(project_uuid) filters = { "label": [ f"_orchest_env_build_is_intermediate=0", f"_orchest_project_uuid={project_uuid}", ] } images_to_remove = docker_images_list_safe(docker_client, filters=filters) image_remove_exceptions = [] # try with repeat because there might be a race condition # where the aborted runs are still using the image for img in images_to_remove: docker_images_rm_safe(docker_client, img.id)
def delete_project_environment_dangling_images(project_uuid, environment_uuid): """Removes dangling images related to an environment. Dangling images are images that have been left nameless and tag-less and which are not referenced by any run or experiment which are pending or running. Args: project_uuid: environment_uuid: """ # look only through runs belonging to the project # consider only docker ids related to the environment_uuid filters = { "label": [ f"_orchest_env_build_is_intermediate=0", f"_orchest_project_uuid={project_uuid}", f"_orchest_environment_uuid={environment_uuid}", ] } project_env_images = docker_images_list_safe(docker_client, filters=filters) for docker_img in project_env_images: remove_if_dangling(docker_img)
def _collateral(self, project_uuid: str): filters = { "label": [ "_orchest_env_build_is_intermediate=0", f"_orchest_project_uuid={project_uuid}", ] } images_to_remove = docker_images_list_safe(docker_client, filters=filters) # Try with repeat because there might be a race condition # where the aborted runs are still using the image. for img in images_to_remove: docker_images_rm_safe(docker_client, img.id)