feat(docker): add async port change and docker compose reload for services
- Introduce async `change_port` to update service port and reload docker compose - Add `run_docker_command` for running docker compose commands asynchronously - Implement `_get_random_port` utility for selecting available ports - Refactor service discovery and port change logic to use new async methods - Update proxy and manager logic to support new port change workflow
This commit is contained in:
@@ -2,7 +2,7 @@ from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from integrations.docker.compose import find_docker_compose
|
||||
from integrations.docker.compose import find_docker_compose, run_docker_command
|
||||
from integrations.docker.ports import _get_port_index, _replace_port_in_mapping
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ def _backup_path_for(compose_path: Path) -> Path:
|
||||
return compose_path.with_name(f"{compose_path.name}.bak")
|
||||
|
||||
|
||||
def change_port(
|
||||
folder: Path,
|
||||
async def change_port(
|
||||
service_name: str,
|
||||
folder: Path,
|
||||
port_from: int,
|
||||
port_to: int,
|
||||
) -> bool:
|
||||
@@ -22,7 +22,7 @@ def change_port(
|
||||
if compose_data is None:
|
||||
return False
|
||||
|
||||
docker_compose, filename = compose_data
|
||||
docker_compose, compose_path = compose_data
|
||||
compose_services = docker_compose.get("services") or {}
|
||||
if not compose_services:
|
||||
return False
|
||||
@@ -41,10 +41,19 @@ def change_port(
|
||||
|
||||
mappings[idx] = _replace_port_in_mapping(mappings[idx], port_from, port_to)
|
||||
|
||||
compose_path = folder / filename
|
||||
backup_path = _backup_path_for(compose_path)
|
||||
original_text = compose_path.read_text()
|
||||
|
||||
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
|
||||
result = await run_docker_command(
|
||||
compose_path,
|
||||
["up", "-d", service_name],
|
||||
)
|
||||
|
||||
if result is None or result.get("status") != "success":
|
||||
compose_path.write_text(original_text)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user