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:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
@@ -19,7 +20,7 @@ def _get_visible_folders(directory: Path) -> list[Path]:
|
||||
]
|
||||
|
||||
|
||||
def find_docker_compose(path: Path) -> tuple[dict, str] | None:
|
||||
def find_docker_compose(path: Path) -> tuple[dict, Path] | None:
|
||||
if not path or not path.is_dir():
|
||||
return None
|
||||
|
||||
@@ -27,7 +28,7 @@ def find_docker_compose(path: Path) -> tuple[dict, str] | None:
|
||||
file = path / name
|
||||
if file.is_file():
|
||||
content = yaml.safe_load(file.read_text()) or {}
|
||||
return content, file.name
|
||||
return content, file
|
||||
|
||||
return None
|
||||
|
||||
@@ -61,7 +62,7 @@ def get_services(folder: Path) -> list[DockerContainer] | None:
|
||||
return services
|
||||
|
||||
|
||||
def get_all_services(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 []
|
||||
@@ -73,3 +74,27 @@ def get_all_services(directory: str | Path) -> list[DockerContainer]:
|
||||
services.extend(sub_services)
|
||||
|
||||
return services
|
||||
|
||||
|
||||
async def run_docker_command(
|
||||
file: Path,
|
||||
command: list[str]
|
||||
) -> dict | None:
|
||||
if not command:
|
||||
return None
|
||||
|
||||
full_command = ["docker", "compose", "-f", f"{file}", *command]
|
||||
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
*full_command,
|
||||
cwd=str(file.parent),
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE
|
||||
)
|
||||
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
return {"status": "success", "output": stdout.decode()}
|
||||
else:
|
||||
return {"status": "error", "reason": stderr.decode()}
|
||||
|
||||
Reference in New Issue
Block a user