- Implement compose.py with functions to find compose files, list services, and aggregate service info - Add editor.py for changing service ports with backup support - Define DockerService model in models.py - Refactor ports.py to support new port mapping logic and helpers - Expose Docker Compose utilities in __init__.py
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
from utils.docker.compose import find_docker_compose
|
|
from utils.docker.ports import _get_port_index, _replace_port_in_mapping
|
|
|
|
|
|
def _backup_path_for(compose_path: Path) -> Path:
|
|
if compose_path.suffix in {".yml", ".yaml"}:
|
|
return compose_path.with_name(f"{compose_path.stem}.bak{compose_path.suffix}")
|
|
return compose_path.with_name(f"{compose_path.name}.bak")
|
|
|
|
|
|
def change_port(
|
|
folder: Path,
|
|
service_name: str,
|
|
port_from: int,
|
|
port_to: int,
|
|
) -> bool:
|
|
compose_data = find_docker_compose(folder)
|
|
if compose_data is None:
|
|
return False
|
|
|
|
docker_compose, filename = compose_data
|
|
compose_services = docker_compose.get("services") or {}
|
|
if not compose_services:
|
|
return False
|
|
|
|
service_config = compose_services.get(service_name)
|
|
if not isinstance(service_config, dict):
|
|
return False
|
|
|
|
mappings = service_config.get("ports")
|
|
if not isinstance(mappings, list):
|
|
return False
|
|
|
|
idx = _get_port_index(mappings, port_from)
|
|
if idx is None:
|
|
return False
|
|
|
|
mappings[idx] = _replace_port_in_mapping(mappings[idx], port_from, port_to)
|
|
|
|
compose_path = folder / filename
|
|
backup_path = _backup_path_for(compose_path)
|
|
backup_path.write_text(compose_path.read_text())
|
|
compose_path.write_text(yaml.dump(docker_compose, default_flow_style=False, sort_keys=False))
|
|
|
|
# TODO: run docker compose commands
|
|
return True
|