2024-01-09 12:47:00 +01:00
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
import shutil
|
2024-01-09 18:31:44 +01:00
|
|
|
import argparse
|
2024-01-09 12:47:00 +01:00
|
|
|
|
|
|
|
def run_command(command):
|
|
|
|
""" Run a shell command and return its output """
|
|
|
|
return subprocess.check_output(command, shell=True).decode('utf-8').strip()
|
|
|
|
|
2024-01-09 14:20:30 +01:00
|
|
|
def stop_containers(containers):
|
|
|
|
"""Stop a list of containers."""
|
|
|
|
container_list = ' '.join(containers)
|
|
|
|
print(f"Stopping containers {container_list}...")
|
2024-01-09 18:31:44 +01:00
|
|
|
run_command(f"docker stop {container_list}")
|
|
|
|
|
2024-01-09 14:20:30 +01:00
|
|
|
def start_containers(containers):
|
|
|
|
"""Start a list of containers."""
|
|
|
|
container_list = ' '.join(containers)
|
2024-01-09 18:31:44 +01:00
|
|
|
print(f"Starting containers {container_list}...")
|
|
|
|
run_command(f"docker start {container_list}")
|
2024-01-09 14:20:30 +01:00
|
|
|
|
|
|
|
def is_database(image):
|
2024-01-09 18:44:52 +01:00
|
|
|
databases = {"postgres", "mariadb", "redis", "memcached"}
|
|
|
|
return any(database in image for database in databases)
|
2024-01-09 14:20:30 +01:00
|
|
|
|
|
|
|
def is_symbolic_link(file_path):
|
|
|
|
return os.path.islink(file_path)
|
|
|
|
|
|
|
|
def get_volume_path(volume):
|
|
|
|
return run_command(f"docker volume inspect --format '{{{{ .Mountpoint }}}}' {volume}")
|
|
|
|
|
2024-01-09 18:31:44 +01:00
|
|
|
def get_image(container):
|
2024-01-09 14:20:30 +01:00
|
|
|
return run_command(f"docker inspect --format='{{{{.Config.Image}}}}' {container}")
|
|
|
|
|
2024-01-09 18:31:44 +01:00
|
|
|
def pause_and_move(storage_path, volume, volume_path, containers):
|
2024-01-09 14:20:30 +01:00
|
|
|
stop_containers(containers)
|
|
|
|
# Create a new directory on the Storage
|
|
|
|
storage_volume_path = os.path.join(storage_path, volume)
|
|
|
|
os.makedirs(storage_volume_path, exist_ok=False)
|
|
|
|
|
|
|
|
# Move the data
|
|
|
|
for item in os.listdir(volume_path):
|
|
|
|
shutil.move(os.path.join(volume_path, item), storage_volume_path)
|
|
|
|
|
|
|
|
# Create a symbolic link
|
|
|
|
os.symlink(storage_volume_path, volume_path)
|
|
|
|
|
|
|
|
start_containers(containers)
|
|
|
|
|
2024-01-09 18:44:52 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-01-09 18:27:43 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# Argument parser setup
|
|
|
|
parser = argparse.ArgumentParser(description='Migrate Docker volumes to SSD or HDD based on container image.')
|
|
|
|
parser.add_argument('--ssd_path', type=str, required=True, help='Path to the SSD storage')
|
|
|
|
parser.add_argument('--hdd_path', type=str, required=True, help='Path to the HDD storage')
|
2024-01-09 14:20:30 +01:00
|
|
|
|
2024-01-09 18:27:43 +01:00
|
|
|
# Parse arguments
|
|
|
|
args = parser.parse_args()
|
2024-01-09 12:47:00 +01:00
|
|
|
|
2024-01-09 18:27:43 +01:00
|
|
|
# Set paths from arguments
|
|
|
|
ssd_path = args.ssd_path
|
|
|
|
hdd_path = args.hdd_path
|
2024-01-09 12:47:00 +01:00
|
|
|
|
2024-01-09 18:27:43 +01:00
|
|
|
# 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()
|
|
|
|
volume_path = get_volume_path(volume)
|
|
|
|
if is_symbolic_link(volume_path):
|
|
|
|
print(f"Skipped Volume {volume}. The storage path {volume_path} is a symbolic link.")
|
2024-01-09 18:44:52 +01:00
|
|
|
elif has_container_with_database(containers):
|
|
|
|
print(f"Container {container} with database image found. Safing volume {volume} on SSD.")
|
|
|
|
pause_and_move(ssd_path,volume,containers)
|
|
|
|
else:
|
|
|
|
print(f"Container {container} with file image found. Safing volume {volume} on HDD.")
|
|
|
|
pause_and_move(hdd_path,volume,containers)
|
2024-01-09 18:27:43 +01:00
|
|
|
|
|
|
|
print("Operation completed.")
|
|
|
|
|