docker-installationen/scripts/docker_compose_creator/volume_utils.py

65 lines
2.5 KiB
Python

import os
from permission_utils import set_permissions, check_permissions, set_permissions_recursive
def setup_volume_paths(service_name, volumes):
created_paths = []
for volume in volumes:
if ':' in volume:
host_path, _ = volume.split(':', 1)
if host_path.startswith('../containerdaten'):
_, path = host_path.split('../containerdaten/', 1)
full_path = os.path.expanduser(f"~/docker/containerdaten/{path}")
created_paths.extend(setup_volume_path(service_name, full_path))
# Setze Berechtigungen für alle erstellten Pfade am Ende
for path in created_paths:
set_permissions_recursive(path)
def setup_volume_path(service_name, full_path):
created_paths = []
if not os.path.exists(full_path):
os.makedirs(full_path, exist_ok=True)
print(f"Created directory: {full_path}")
else:
print(f"Directory already exists: {full_path}")
created_paths.append(full_path)
while True:
create_file_choice = input(f"Do you want to create a file in {full_path}? (y/n): ").lower()
if create_file_choice != 'y':
break
file_name = input("Enter the file name: ")
file_path = create_file(full_path, file_name)
if file_path:
created_paths.append(file_path)
return created_paths
def create_file(path, filename):
full_path = os.path.join(path, filename)
try:
with open(full_path, 'w') as f:
pass # Create an empty file
print(f"Created file: {full_path}")
return full_path
except IOError as e:
print(f"Error creating file: {e}")
return None
def check_existing_volumes(compose_data):
if 'services' in compose_data:
for service in compose_data['services'].values():
if 'volumes' in service:
for volume in service['volumes']:
if ':' in volume:
host_path, _ = volume.split(':', 1)
if host_path.startswith('../containerdaten'):
_, path = host_path.split('../containerdaten/', 1)
full_path = os.path.expanduser(f"~/docker/containerdaten/{path}")
if os.path.exists(full_path):
print(f"Directory exists: {full_path}")
check_permissions(full_path)
else:
print(f"Directory does not exist: {full_path}")