Optimized code

This commit is contained in:
Kevin Veen-Birkenbach 2024-01-09 18:44:52 +01:00
parent a3e94497a9
commit 135838173c

View File

@ -20,7 +20,8 @@ def start_containers(containers):
run_command(f"docker start {container_list}") run_command(f"docker start {container_list}")
def is_database(image): def is_database(image):
return "postgres" in image or "mariadb" in image databases = {"postgres", "mariadb", "redis", "memcached"}
return any(database in image for database in databases)
def is_symbolic_link(file_path): def is_symbolic_link(file_path):
return os.path.islink(file_path) return os.path.islink(file_path)
@ -46,6 +47,15 @@ def pause_and_move(storage_path, volume, volume_path, containers):
start_containers(containers) start_containers(containers)
def has_container_with_database(containers):
for container in containers:
# Get the image of the container
image = get_image(container)
if is_database(image):
return True
return False
if __name__ == "__main__": if __name__ == "__main__":
# Argument parser setup # Argument parser setup
parser = argparse.ArgumentParser(description='Migrate Docker volumes to SSD or HDD based on container image.') parser = argparse.ArgumentParser(description='Migrate Docker volumes to SSD or HDD based on container image.')
@ -68,18 +78,12 @@ if __name__ == "__main__":
volume_path = get_volume_path(volume) volume_path = get_volume_path(volume)
if is_symbolic_link(volume_path): if is_symbolic_link(volume_path):
print(f"Skipped Volume {volume}. The storage path {volume_path} is a symbolic link.") print(f"Skipped Volume {volume}. The storage path {volume_path} is a symbolic link.")
elif has_container_with_database(containers):
for container in containers: print(f"Container {container} with database image found. Safing volume {volume} on SSD.")
# Get the image of the container pause_and_move(ssd_path,volume,containers)
image = get_image(container) else:
print(f"Container {container} with file image found. Safing volume {volume} on HDD.")
# Check if the image contains "postgres" or "mariadb" pause_and_move(hdd_path,volume,containers)
if is_database(image):
print(f"Container {container} with database image {image} found, using volume {volume}.")
pause_and_move(ssd_path,volume,containers)
else:
print(f"Container {container} with file image {image} found, using volume {volume}.")
pause_and_move(hdd_path,volume,containers)
print("Operation completed.") print("Operation completed.")