From f51555573bbbe64c6a5bdab189cec31570e0630d Mon Sep 17 00:00:00 2001 From: Jesper Utoft Date: Sat, 19 Aug 2023 14:42:10 +0200 Subject: [PATCH] Use python base image & add more input args to allow changing mount directory Fixes #7563, #7560 --- Dockerfile | 36 +++++++++---------- docker-compose.yml | 11 ++++-- src/run_tribler_headless.py | 30 +++++++++++++--- .../components/restapi/rest/rest_manager.py | 8 ++--- .../core/components/restapi/rest/settings.py | 2 ++ 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/Dockerfile b/Dockerfile index bc4441291d3..1677aab84d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,29 @@ -# The base image already builds the libtorrent dependency so only Python pip packages -# are necessary to be installed to run Tribler core process. -FROM triblercore/libtorrent:1.2.10-x +# libtorrent-1.2.9 does not support python 3.11 yet +FROM python:3.10-slim -# Update the base system and install required libsodium and Python pip -RUN apt update && apt upgrade -y -RUN apt install -y libsodium23 python3-pip git - -RUN useradd -ms /bin/bash user -USER user -WORKDIR /home/user +RUN apt-get update \ + && apt-get install -y libsodium23 \ + && rm -rf /var/lib/apt/lists/* # Then, install pip dependencies so that it can be cached and does not # need to be built every time the source code changes. # This reduces the docker build time. -RUN mkdir requirements -COPY ./requirements-core.txt requirements/core-requirements.txt -RUN pip3 install -r requirements/core-requirements.txt +COPY ./requirements-core.txt /app/tribler/core-requirements.txt +RUN pip3 install -r /app/tribler/core-requirements.txt # Copy the source code and set the working directory -COPY ./ tribler -WORKDIR /home/user/tribler +COPY ./src /app/tribler/src/ +WORKDIR /app/tribler/ -# Set the REST API port and expose it +# Set to -1 to use the default ENV CORE_API_PORT=20100 -EXPOSE 20100 +ENV IPV8_PORT=7759 +ENV TORRENT_PORT=-1 +ENV DOWNLOAD_DIR=/downloads +ENV TSTATEDIR=/state + +VOLUME /state +VOLUME /downloads # Only run the core process with --core switch -CMD ["./src/tribler.sh", "--core"] +CMD exec python3 /app/tribler/src/run_tribler_headless.py --restapi_http_port=${CORE_API_PORT} --ipv8=${IPV8_PORT} --libtorrent=${TORRENT_PORT} --restapi_http_host=0.0.0.0 --restapi_https_host=0.0.0.0 "--statedir=${TSTATEDIR}" "--downloaddir=${DOWNLOAD_DIR}" diff --git a/docker-compose.yml b/docker-compose.yml index 5c2a3c59504..b3ed345c5cc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,14 @@ services: network_mode: "host" build: . volumes: - - ~/.Tribler:/home/user/.Tribler + - "~/.Tribler:/state" + - "~/TriblerDownloads:/downloads" environment: CORE_API_PORT: 20100 - + CORE_API_KEY: TEST + TORRENT_PORT: 7000 + TSTATEDIR: "/state" + ports: + - "20100:20100" # core api port + - "7000:7000" # torrent listen port + - "7759:7759" # ipv8 listen port diff --git a/src/run_tribler_headless.py b/src/run_tribler_headless.py index 3380936c559..235a612d8ac 100644 --- a/src/run_tribler_headless.py +++ b/src/run_tribler_headless.py @@ -89,9 +89,24 @@ async def signal_handler(sig): print("Starting Tribler") - if options.restapi > 0: + if options.restapi_http_port > 0 or options.restapi_https_port > 0: config.api.http_enabled = True - config.api.http_port = options.restapi + + if options.restapi_http_port > 0: + config.api.http_port = options.restapi_http_port + + if options.restapi_http_host: + config.api.http_host = options.restapi_http_host + + if options.restapi_https_port > 0: + config.api.https_port = options.restapi_https_port + + if options.restapi_https_host: + config.api.https_host = options.restapi_https_host + + api_key = os.environ.get('CORE_API_KEY') + if api_key and (config.api.key != api_key): + config.api.key = api_key if options.ipv8 > 0: config.ipv8.port = options.ipv8 @@ -101,6 +116,9 @@ async def signal_handler(sig): if options.libtorrent != -1 and options.libtorrent > 0: config.libtorrent.port = options.libtorrent + if options.downloaddir: + config.download_defaults.saveas = options.downloaddir + if options.ipv8_bootstrap_override is not None: config.ipv8.bootstrap_override = options.ipv8_bootstrap_override @@ -129,7 +147,11 @@ def main(argv): parser.add_argument('--help', '-h', action='help', default=argparse.SUPPRESS, help='Show this help message and exit') parser.add_argument('--statedir', '-s', default=None, help='Use an alternate statedir') - parser.add_argument('--restapi', '-p', default=-1, type=int, help='Use an alternate port for REST API') + parser.add_argument('--downloaddir', default=None, help='Use an alternative download directory') + parser.add_argument('--restapi_http_port', '--restapi', '-p', default=-1, type=int, help='Use an alternate port for http REST API') + parser.add_argument('--restapi_http_host', default=None, type=str, help='Use an alternate listen address for http REST API') + parser.add_argument('--restapi_https_port', default=-1, type=int, help='Use an alternate port for https REST API') + parser.add_argument('--restapi_https_host', default=None, type=str, help='Use an alternate listen address for https REST API') parser.add_argument('--ipv8', '-i', default=-1, type=int, help='Use an alternate port for the IPv8') parser.add_argument('--libtorrent', '-l', default=-1, type=int, help='Use an alternate port for libtorrent') parser.add_argument('--ipv8_bootstrap_override', '-b', default=None, type=str, @@ -146,7 +168,7 @@ def main(argv): coro = service.start_tribler(args) ensure_future(coro) - if sys.platform == 'win32': + if sys.platform == 'win32' and sys.version < 3.8: # Unfortunately, this is needed on Windows for Ctrl+C to work consistently. # Should no longer be needed in Python 3.8. async def wakeup(): diff --git a/src/tribler/core/components/restapi/rest/rest_manager.py b/src/tribler/core/components/restapi/rest/rest_manager.py index 460b75aeee2..6a231c578d8 100644 --- a/src/tribler/core/components/restapi/rest/rest_manager.py +++ b/src/tribler/core/components/restapi/rest/rest_manager.py @@ -99,8 +99,6 @@ def __init__(self, config: APISettings, root_endpoint: RootEndpoint, state_dir=N self.config = config self.state_dir = state_dir - self.http_host = '127.0.0.1' - self.https_host = '0.0.0.0' self.shutdown_timeout = shutdown_timeout def get_endpoint(self, name): @@ -152,8 +150,8 @@ async def start(self): self._logger.info('Https enabled') await self.start_https_site() - self._logger.info(f'Swagger docs: http://{self.http_host}:{self.config.http_port}/docs') - self._logger.info(f'Swagger JSON: http://{self.http_host}:{self.config.http_port}/docs/swagger.json') + self._logger.info(f'Swagger docs: http://{self.config.http_host}:{self.config.http_port}/docs') + self._logger.info(f'Swagger JSON: http://{self.config.http_host}:{self.config.http_port}/docs/swagger.json') async def start_http_site(self): api_port = max(self.config.http_port, 0) # if the value in config is -1 we convert it to 0 @@ -183,7 +181,7 @@ async def start_https_site(self): ssl_context.load_cert_chain(cert) port = self.config.https_port - self.site_https = web.TCPSite(self.runner, self.https_host, port, ssl_context=ssl_context) + self.site_https = web.TCPSite(self.runner, self.config.https_host, port, ssl_context=ssl_context) await self.site_https.start() self._logger.info("Started HTTPS REST API: %s", self.site_https.name) diff --git a/src/tribler/core/components/restapi/rest/settings.py b/src/tribler/core/components/restapi/rest/settings.py index 399b9c18385..ee2c7ee77c7 100644 --- a/src/tribler/core/components/restapi/rest/settings.py +++ b/src/tribler/core/components/restapi/rest/settings.py @@ -9,7 +9,9 @@ class APISettings(TriblerConfigSection): http_enabled: bool = False http_port: int = -1 + http_host: str = "127.0.0.1" https_enabled: bool = False + https_host: str = "0.0.0.0" https_port: int = -1 https_certfile: str = '' key: Optional[str] = None