mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Solved bugs and optimized storage-optimiter
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import subprocess
|
||||
import os
|
||||
import shutil
|
||||
import argparse
|
||||
|
||||
def run_command(command):
|
||||
""" Run a shell command and return its output """
|
||||
return subprocess.check_output(command, shell=True).decode('utf-8').strip()
|
||||
|
||||
def stop_containers(containers):
|
||||
"""Stop a list of containers."""
|
||||
container_list = ' '.join(containers)
|
||||
print(f"Stopping containers {container_list}...")
|
||||
run_command(f"docker stop {container_list}")
|
||||
|
||||
def start_containers(containers):
|
||||
"""Start a list of containers."""
|
||||
container_list = ' '.join(containers)
|
||||
print(f"Starting containers {container_list}...")
|
||||
run_command(f"docker start {container_list}")
|
||||
|
||||
def is_database(image):
|
||||
databases = {"postgres", "mariadb", "redis", "memcached"}
|
||||
return any(database in image for database in databases)
|
||||
|
||||
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}")
|
||||
|
||||
def get_image(container):
|
||||
return run_command(f"docker inspect --format='{{{{.Config.Image}}}}' {container}")
|
||||
|
||||
def pause_and_move(storage_path, volume, volume_path, containers):
|
||||
stop_containers(containers)
|
||||
# Create a new directory on the Storage
|
||||
storage_volume_path = os.path.join(storage_path, 'data', 'docker', 'volumes', 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)
|
||||
|
||||
# Ensure the volume_path is empty and remove it
|
||||
if not os.listdir(volume_path):
|
||||
os.rmdir(volume_path)
|
||||
|
||||
# Create a symbolic link
|
||||
os.symlink(storage_volume_path, volume_path)
|
||||
|
||||
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__":
|
||||
# Argument parser setup
|
||||
parser = argparse.ArgumentParser(description='Migrate Docker volumes to SSD or HDD based on container image.')
|
||||
parser.add_argument('--rapid-storage-path', type=str, required=True, help='Path to the SSD storage')
|
||||
parser.add_argument('--mass-storage-path', type=str, required=True, help='Path to the HDD storage')
|
||||
|
||||
# Parse arguments
|
||||
args = parser.parse_args()
|
||||
|
||||
# Set paths from arguments
|
||||
rapid_storage_path = args.rapid_storage_path
|
||||
mass_storage_path = args.mass_storage_path
|
||||
|
||||
# List all Docker volumes
|
||||
volumes = run_command("docker volume ls -q").splitlines()
|
||||
|
||||
for volume in volumes:
|
||||
volume_path = get_volume_path(volume)
|
||||
containers = run_command(f"docker ps -q --filter volume={volume}").splitlines()
|
||||
if not containers:
|
||||
print(f"Skipped Volume {volume}. It does not belong to a running container.")
|
||||
elif is_symbolic_link(volume_path):
|
||||
print(f"Skipped Volume {volume}. The storage path {volume_path} is a symbolic link.")
|
||||
elif has_container_with_database(containers):
|
||||
print(f"Safing volume {volume} on SSD.")
|
||||
pause_and_move(rapid_storage_path, volume, volume_path, containers)
|
||||
else:
|
||||
print(f"Safing volume {volume} on HDD.")
|
||||
pause_and_move(mass_storage_path, volume, volume_path, containers)
|
||||
|
||||
print("Operation completed.")
|
||||
|
21
roles/system-storage-optimizer/tasks/main.yml
Normal file
21
roles/system-storage-optimizer/tasks/main.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
- name: "create {{storage_optimizer_directory}}"
|
||||
file:
|
||||
path: "{{storage_optimizer_directory}}"
|
||||
state: directory
|
||||
mode: 0755
|
||||
|
||||
- name: create system-storage-optimizer.cymais.service
|
||||
template:
|
||||
src: system-storage-optimizer.service.j2
|
||||
dest: /etc/systemd/system/system-storage-optimizer.cymais.service
|
||||
|
||||
- name: create system-storage-optimizer.py
|
||||
copy:
|
||||
src: system-storage-optimizer.py
|
||||
dest: "{{storage_optimizer_script}}"
|
||||
mode: 0755
|
||||
|
||||
- name: "optimize storage performance"
|
||||
systemd:
|
||||
name: system-storage-optimizer.cymais.service
|
||||
state: started
|
@@ -0,0 +1,8 @@
|
||||
[Unit]
|
||||
Description=Optimize storage paths
|
||||
OnFailure=systemd-notifier.cymais@%n.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStartPre=/bin/sh -c '/usr/bin/python {{ path_system_lock_script }} {{ system_maintenance_services | join(' ') }} --ignore system-storage-optimizer'
|
||||
ExecStart=/bin/sh -c '/usr/bin/python {{storage_optimizer_script}} --rapid-storage-path {{path_rapid_storage}} --mass-storage-path {{path_mass_storage}}'
|
2
roles/system-storage-optimizer/vars/main.yml
Normal file
2
roles/system-storage-optimizer/vars/main.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
storage_optimizer_directory: "{{path_administrator_scripts}}system-storage-optimizer/"
|
||||
storage_optimizer_script: "{{storage_optimizer_directory}}system-storage-optimizer.py"
|
Reference in New Issue
Block a user