feat: enhance service discovery and proxy management
This commit is contained in:
@@ -3,7 +3,7 @@ from pathlib import Path
|
||||
|
||||
import yaml
|
||||
from integrations.docker.models import DockerContainer
|
||||
from integrations.docker.ports import _get_service_port
|
||||
from integrations.docker.ports import _get_service_binding
|
||||
|
||||
COMPOSE_NAMES = (
|
||||
"compose.yml",
|
||||
@@ -50,20 +50,31 @@ def get_services(folder: Path) -> list[DockerContainer] | None:
|
||||
if not isinstance(mappings, list) or not mappings:
|
||||
continue
|
||||
|
||||
ports = [
|
||||
p
|
||||
for p in (_get_service_port(mapping) for mapping in mappings)
|
||||
if p is not None
|
||||
bindings = [
|
||||
{"published": published, "host": host}
|
||||
for published, host in (
|
||||
_get_service_binding(mapping) for mapping in mappings
|
||||
)
|
||||
if published is not None
|
||||
]
|
||||
if not ports:
|
||||
if not bindings:
|
||||
continue
|
||||
|
||||
services.append({"name": service_name, "ports": ports, "folder": folder})
|
||||
services.append(
|
||||
{
|
||||
"name": service_name,
|
||||
"ports": [binding["published"] for binding in bindings],
|
||||
"bindings": bindings,
|
||||
"folder": folder,
|
||||
}
|
||||
)
|
||||
|
||||
return services
|
||||
|
||||
|
||||
def get_all_services_in_dir(directory: str | Path) -> list[DockerContainer]:
|
||||
def get_all_services_in_dir(
|
||||
directory: str | Path,
|
||||
) -> list[DockerContainer]:
|
||||
path = Path(directory)
|
||||
if not path.is_dir():
|
||||
return []
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
from pathlib import Path
|
||||
|
||||
from core.config import settings
|
||||
|
||||
|
||||
def service_scan_root() -> Path:
|
||||
configured = settings.SERVICE_SCAN_ROOT
|
||||
if configured:
|
||||
return Path(configured).expanduser()
|
||||
return Path.home()
|
||||
@@ -16,6 +16,8 @@ async def change_port(
|
||||
folder: Path,
|
||||
port_from: int,
|
||||
port_to: int,
|
||||
host_to: str | None = None,
|
||||
rewrite_host: bool = False,
|
||||
) -> bool:
|
||||
compose_data = find_docker_compose(folder)
|
||||
if compose_data is None:
|
||||
@@ -38,7 +40,13 @@ async def change_port(
|
||||
if idx is None:
|
||||
return False
|
||||
|
||||
mappings[idx] = _replace_port_in_mapping(mappings[idx], port_from, port_to)
|
||||
mappings[idx] = _replace_port_in_mapping(
|
||||
mappings[idx],
|
||||
port_from,
|
||||
port_to,
|
||||
host_to=host_to,
|
||||
rewrite_host=rewrite_host,
|
||||
)
|
||||
|
||||
backup_path = _backup_path_for(compose_path)
|
||||
original_text = compose_path.read_text()
|
||||
|
||||
@@ -2,7 +2,13 @@ from pathlib import Path
|
||||
from typing import TypedDict
|
||||
|
||||
|
||||
class DockerPortBinding(TypedDict):
|
||||
published: int
|
||||
host: str | None
|
||||
|
||||
|
||||
class DockerContainer(TypedDict):
|
||||
name: str
|
||||
ports: list[int]
|
||||
bindings: list[DockerPortBinding]
|
||||
folder: Path
|
||||
|
||||
@@ -14,48 +14,64 @@ def get_random_port() -> int:
|
||||
raise RuntimeError(f"Could not find an available port: {e}") from e
|
||||
|
||||
|
||||
def _get_service_port(mapping: str | int | Mapping[str, object]) -> int | None:
|
||||
def _get_service_binding(
|
||||
mapping: str | int | Mapping[str, object],
|
||||
) -> tuple[int | None, str | None]:
|
||||
if isinstance(mapping, int):
|
||||
return mapping
|
||||
return mapping, None
|
||||
|
||||
if isinstance(mapping, Mapping):
|
||||
published = mapping.get("published")
|
||||
if published is None:
|
||||
return None
|
||||
return None, None
|
||||
try:
|
||||
return int(published)
|
||||
except TypeError, ValueError:
|
||||
return None
|
||||
port = int(published)
|
||||
except TypeError:
|
||||
return None, None
|
||||
except ValueError:
|
||||
return None, None
|
||||
host = mapping.get("host_ip")
|
||||
return port, str(host) if host not in {None, ""} else None
|
||||
|
||||
parts = str(mapping).split(":")
|
||||
match parts:
|
||||
case [""]:
|
||||
return None
|
||||
return None, None
|
||||
case [port] | [port, _]:
|
||||
try:
|
||||
return int(port)
|
||||
return int(port), None
|
||||
except ValueError:
|
||||
return None
|
||||
case ["localhost" | "127.0.0.1", _, _]:
|
||||
return None
|
||||
case [_, port, _]:
|
||||
return None, None
|
||||
case [host, port, _]:
|
||||
try:
|
||||
return int(port)
|
||||
return int(port), host or None
|
||||
except ValueError:
|
||||
return None
|
||||
return None, None
|
||||
case _:
|
||||
return None
|
||||
return None, None
|
||||
|
||||
|
||||
def _get_service_port(mapping: str | int | Mapping[str, object]) -> int | None:
|
||||
port, _host = _get_service_binding(mapping)
|
||||
return port
|
||||
|
||||
|
||||
def _replace_port_in_mapping(
|
||||
mapping: str | int | Mapping[str, object],
|
||||
port_from: int,
|
||||
port_to: int,
|
||||
host_to: str | None = None,
|
||||
rewrite_host: bool = False,
|
||||
) -> str | dict[str, object]:
|
||||
if isinstance(mapping, Mapping):
|
||||
updated = dict(mapping)
|
||||
if _get_service_port(updated) == port_from:
|
||||
updated["published"] = str(port_to)
|
||||
if rewrite_host:
|
||||
if host_to is None:
|
||||
updated.pop("host_ip", None)
|
||||
else:
|
||||
updated["host_ip"] = host_to
|
||||
return updated
|
||||
|
||||
parts = str(mapping).split(":")
|
||||
@@ -63,8 +79,14 @@ def _replace_port_in_mapping(
|
||||
case [_]:
|
||||
return str(port_to)
|
||||
case [_, container_port]:
|
||||
if rewrite_host and host_to:
|
||||
return f"{host_to}:{port_to}:{container_port}"
|
||||
return f"{port_to}:{container_port}"
|
||||
case [ip, _, container_port]:
|
||||
if rewrite_host:
|
||||
if host_to:
|
||||
return f"{host_to}:{port_to}:{container_port}"
|
||||
return f"{port_to}:{container_port}"
|
||||
return f"{ip}:{port_to}:{container_port}"
|
||||
case _:
|
||||
return str(port_to)
|
||||
|
||||
Reference in New Issue
Block a user