From 1c31f82f7c5f6979da3981c8a331b6a92a72eeea Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 9 Jan 2024 12:47:00 +0100 Subject: [PATCH] Added draft for performance optimation --- roles/storage/files/optimize.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 roles/storage/files/optimize.py diff --git a/roles/storage/files/optimize.py b/roles/storage/files/optimize.py new file mode 100644 index 00000000..631f28e9 --- /dev/null +++ b/roles/storage/files/optimize.py @@ -0,0 +1,46 @@ +import subprocess +import os +import shutil + +def run_command(command): + """ Run a shell command and return its output """ + return subprocess.check_output(command, shell=True).decode('utf-8').strip() + +# Path on the SSD where data will be moved +ssd_path = "/path/to/ssd" + +# List all Docker volumes +volumes = run_command("docker volume ls -q").splitlines() + +for volume in volumes: + # List all containers using this volume + containers = run_command(f"docker ps -q --filter volume={volume}").splitlines() + + for container in containers: + # Get the image of the container + image = run_command(f"docker inspect --format='{{{{.Config.Image}}}}' {container}") + + # Check if the image contains "postgres" or "mariadb" + if "postgres" in image or "mariadb" in image: + print(f"Container {container} with database image {image} found, using volume {volume}.") + + # Stop the container + run_command(f"docker stop {container}") + + # Get the mount point of the volume + volume_path = run_command(f"docker volume inspect --format '{{{{ .Mountpoint }}}}' {volume}") + + # Create a new directory on the SSD + ssd_volume_path = os.path.join(ssd_path, volume) + os.makedirs(ssd_volume_path, exist_ok=True) + + # Move the data + for item in os.listdir(volume_path): + shutil.move(os.path.join(volume_path, item), ssd_volume_path) + + # Create a symbolic link + os.symlink(ssd_volume_path, volume_path) + else: + print(f"Container {container} with file image {image} found, using volume {volume}.") + +print("Operation completed.")