|
| 1 | +import logging |
| 2 | +import pathlib |
| 3 | +import socket |
| 4 | +import tempfile |
| 5 | +import time |
| 6 | +from typing import AsyncIterator, Callable |
| 7 | + |
| 8 | +import trio |
| 9 | + |
| 10 | +try: |
| 11 | + from contextlib import asynccontextmanager |
| 12 | +except ImportError: |
| 13 | + # We use this as a fallback to support Python 3.6 |
| 14 | + from async_generator import asynccontextmanager # type: ignore |
| 15 | + |
| 16 | + |
| 17 | +DOPPLE_FILE = pathlib.Path(__file__).parent.parent / "dopple.py" |
| 18 | + |
| 19 | + |
| 20 | +@asynccontextmanager |
| 21 | +async def run_eth_client_in_docker( |
| 22 | + generate_cmd_fn: Callable[[pathlib.Path], str], |
| 23 | + generate_ipc_path_fn: Callable[[pathlib.Path], pathlib.Path], |
| 24 | +) -> AsyncIterator[pathlib.Path]: |
| 25 | + """ |
| 26 | + Create an ``asynccontextmanager`` that runs an Ethereum client within docker |
| 27 | + under the user account of the host user. A temporary directory is created on |
| 28 | + the host system and should be mapped into the docker container to allow the |
| 29 | + client to share IPC files with the host system. Yield the IPC path of the client. |
| 30 | +
|
| 31 | + :param generate_cmd_fn: A function taking the ``Path`` of the temporary directory |
| 32 | + and generating the relevant part of the docker command to launch the client with |
| 33 | + the mapped directory. |
| 34 | +
|
| 35 | + :param generate_ipc_path_fn: A function taking the ``Path`` of the temprorary directory |
| 36 | + and generating the ``Path`` of the IPC file that dopple can connect to. |
| 37 | + """ |
| 38 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 39 | + temp_dir = pathlib.Path(tmpdir) |
| 40 | + |
| 41 | + start_cmd = ( |
| 42 | + f"docker run --rm --name {temp_dir.name} " |
| 43 | + f"--user $(id -u):$(id -g) {generate_cmd_fn(temp_dir)}" |
| 44 | + ) |
| 45 | + stop_cmd = f"docker stop {temp_dir.name}" |
| 46 | + |
| 47 | + ipc_path = generate_ipc_path_fn(temp_dir) |
| 48 | + logging.debug(f"Starting client, ipc path: {ipc_path}") |
| 49 | + async with await trio.open_process(start_cmd, shell=True): |
| 50 | + logging.debug("Started client, waiting for IPC socket to be ready") |
| 51 | + wait_for_socket(ipc_path) |
| 52 | + logging.debug("IPC ready") |
| 53 | + yield ipc_path |
| 54 | + logging.debug("Killing client") |
| 55 | + await trio.run_process(stop_cmd, shell=True) |
| 56 | + logging.debug("Killed client") |
| 57 | + |
| 58 | + |
| 59 | +@asynccontextmanager |
| 60 | +async def run_dopple_as_script(ipc_path: pathlib.Path) -> AsyncIterator[None]: |
| 61 | + """ |
| 62 | + Run geth, then run dopple as a script file to connect to it. |
| 63 | + """ |
| 64 | + logging.debug("Starting dopple") |
| 65 | + async with await trio.open_process([DOPPLE_FILE, ipc_path]) as proc: |
| 66 | + logging.debug("Started dopple") |
| 67 | + await trio.sleep(0.5) |
| 68 | + yield |
| 69 | + logging.debug("Terminating dopple") |
| 70 | + proc.terminate() |
| 71 | + await proc.wait() |
| 72 | + logging.debug("Terminated dopple") |
| 73 | + |
| 74 | + |
| 75 | +def wait_for_socket(ipc_path: pathlib.Path, timeout: int = 10) -> None: |
| 76 | + """ |
| 77 | + Wait for the ``ipc_path`` to be ready. |
| 78 | + """ |
| 79 | + start = time.monotonic() |
| 80 | + while time.monotonic() < start + timeout: |
| 81 | + try: |
| 82 | + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 83 | + sock.connect(str(ipc_path)) |
| 84 | + sock.settimeout(timeout) |
| 85 | + except (FileNotFoundError, socket.error): |
| 86 | + time.sleep(0.01) |
| 87 | + else: |
| 88 | + break |
0 commit comments