43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
from pathlib import Path
|
|
|
|
from integrations import docker
|
|
from integrations.docker import DockerContainer
|
|
from integrations.docker.ports import _get_random_port
|
|
|
|
|
|
async def create_proxy(
|
|
port: int,
|
|
service: DockerContainer,
|
|
code: str,
|
|
) -> dict[str, object]:
|
|
"""
|
|
Utility function that will:
|
|
- run ``integrations/docker`` utilities to change the old port of original service to a random one
|
|
/- write ``code`` into ``/tmp/theplug/{random}.py``
|
|
- start a subprocess via asyncio
|
|
- return the proxy infos (pid, )
|
|
"""
|
|
path = Path(f"/tmp/theplug/{service['name']}.py")
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
path.write_text(code, encoding="utf-8")
|
|
|
|
# da qui bisogna:
|
|
# scegliere una porta random alla quale spostare il docker
|
|
# modificare il docker via integrations.docker.editor.change_port ( service.folder, service.name, port, random_nuova_port )
|
|
random_port = _get_random_port()
|
|
changed = await docker.change_port(
|
|
service["name"],
|
|
service["folder"],
|
|
port,
|
|
random_port,
|
|
)
|
|
|
|
return {
|
|
"service": service["name"],
|
|
"previous_port": port,
|
|
"upstream_port": random_port,
|
|
"script_path": str(path),
|
|
"port_changed": changed,
|
|
}
|