From e889f7cf245d087c368fe88a7cf169ca545267e6 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 24 Dec 2023 16:06:44 +0100 Subject: [PATCH] Removed bug https://project.veen.world/projects/cymais/work_packages/90 https://chat.openai.com/share/77291a8a-47cc-4057-a59b-1f0298b23024 --- roles/heal-docker/files/heal-docker.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/roles/heal-docker/files/heal-docker.py b/roles/heal-docker/files/heal-docker.py index 85508817..e570a37f 100644 --- a/roles/heal-docker/files/heal-docker.py +++ b/roles/heal-docker/files/heal-docker.py @@ -4,6 +4,9 @@ # import subprocess import time +import os + +errors = 0 def bash(command): print(command) @@ -26,6 +29,13 @@ def print_bash(command): print(list_to_string(output)) return output +def find_docker_compose_file(directory): + for root, _, files in os.walk(directory): + for file in files: + if file == 'docker-compose.yml': + return os.path.join(root, file) + return None + waiting_time=600 blocker_running=True while blocker_running: @@ -48,8 +58,16 @@ for failed_container in failed_containers: unfiltered_failed_docker_compose_repositories.append(failed_container.split('-')[0]) filtered_failed_docker_compose_repositories=list(dict.fromkeys(unfiltered_failed_docker_compose_repositories)) -for filtered_failed_docker_compose_repository in filtered_failed_docker_compose_repositories: - print("restarting unhealthy container: " + filtered_failed_docker_compose_repository) - print_bash('cd /home/administrator/docker-compose/' + filtered_failed_docker_compose_repository + '/ && docker-compose restart') -print("finished restart procedure.") \ No newline at end of file +for filtered_failed_docker_compose_repository in filtered_failed_docker_compose_repositories: + compose_file_path = find_docker_compose_file('/home/administrator/docker-compose/' + filtered_failed_docker_compose_repository) + + if compose_file_path: + print("Restarting unhealthy container in:", compose_file_path) + print_bash(f'cd {os.path.dirname(compose_file_path)} && docker-compose restart') + else: + print("Error: Docker Compose file not found for:", filtered_failed_docker_compose_repository) + errors += 1 + +print("finished restart procedure.") +exit(errors) \ No newline at end of file