From bc81481c4cf01c00ac79d750cc457417697f015e Mon Sep 17 00:00:00 2001 From: gmega Date: Fri, 25 Oct 2024 19:20:55 -0300 Subject: [PATCH] Dockerize deluge --- docker/bin/create_config.py | 89 +++++++++++++++++++++++++++++++++++++ docker/bin/start.sh | 13 ++++++ docker/deluge.Dockerfile | 36 +++++++++++++++ setup.py | 1 + 4 files changed, 139 insertions(+) create mode 100644 docker/bin/create_config.py create mode 100755 docker/bin/start.sh create mode 100644 docker/deluge.Dockerfile diff --git a/docker/bin/create_config.py b/docker/bin/create_config.py new file mode 100644 index 000000000..c1e46c55f --- /dev/null +++ b/docker/bin/create_config.py @@ -0,0 +1,89 @@ +# Simple wrapper on top of the Deluge config manager which allows us to generate +# an initial config file from env vars. +# +import copy +import os +import sys +from dataclasses import dataclass +from typing import Optional, Dict + +from deluge.configmanager import ConfigManager, set_config_dir +from deluge.core.preferencesmanager import DEFAULT_PREFS + + +@dataclass +class ConfigOption: + name: str + description: str + default: Optional[str] = None + + +OPTIONS = [ + ConfigOption( + name='DELUGE_CONFIG_DIR', + default='/var/lib/deluge', + description='Directory where config files should be stored', + ), + ConfigOption( + name='DELUGE_DOWNLOAD_DIR', + default='/var/lib/deluge/downloads', + description='Directory where download files should be stored after completion', + ), + ConfigOption( + 'DELUGE_TORRENTFILE_DIR', + default='/var/lib/deluge/torrentfiles', + description='Directory where known torrent files should be stored', + ), + ConfigOption( + name='DELUGE_RPC_PORT', + default='9889', + description='Port on which to listen for RPC requests', + ), + ConfigOption( + name='DELUGE_LISTEN_PORTS', + default='6881,6891', + description='Listen ports for DHT and transfer connections' + ), + ConfigOption( + name='DELUGE_EXTERNAL_IP', + description='IP on which to listen for connections', + ) +] + + +def read_options() -> Dict[str, str]: + options = {} + for opt in OPTIONS: + if opt.name not in os.environ: + if not opt.default: + print(f'Required option missing {opt.name}: {opt.description}') + sys.exit(1) + value = opt.default + else: + value = os.environ[opt.name] + + options[opt.name] = value + + return options + + +def main(): + options = read_options() + set_config_dir(options['DELUGE_CONFIG_DIR']) + + defaults = copy.deepcopy(DEFAULT_PREFS) + + defaults['listen_random_port'] = None + defaults['listen_interface'] = options['DELUGE_EXTERNAL_IP'] + defaults['outgoing_interface'] = options['DELUGE_EXTERNAL_IP'] + defaults['daemon_port'] = int(options['DELUGE_RPC_PORT']) + defaults['download_location'] = options['DELUGE_DOWNLOAD_DIR'] + defaults['torrentfiles_location'] = options['DELUGE_TORRENTFILE_DIR'] + defaults['listen_ports'] = [int(p) for p in options['DELUGE_LISTEN_PORTS'].split(',')] + + manager = ConfigManager('core.conf', defaults=defaults) + manager.save() + + +if __name__ == '__main__': + main() diff --git a/docker/bin/start.sh b/docker/bin/start.sh new file mode 100755 index 000000000..6ce7e767d --- /dev/null +++ b/docker/bin/start.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e + +if [ -z "${DELUGE_EXTERNAL_IP}" ]; then + echo "Warning: the listen IP address for the container has not been set." + DELUGE_EXTERNAL_IP=$(hostname -i) + echo "Using ${DELUGE_EXTERNAL_IP}" + export DELUGE_EXTERNAL_IP +fi + +cd "${DELUGE_APP}" +python ./docker/bin/create_config.py +python -m deluge.core.daemon_entry -c "${DELUGE_CONFIG_DIR}" -d -L debug \ No newline at end of file diff --git a/docker/deluge.Dockerfile b/docker/deluge.Dockerfile new file mode 100644 index 000000000..25f284f5d --- /dev/null +++ b/docker/deluge.Dockerfile @@ -0,0 +1,36 @@ +# It's easier to use miniconda or we'll have to build python-boost manually. +FROM continuumio/miniconda3:24.9.2-0 + +ENV DELUGE_APP=/opt/deluge +ENV DELUGE_CONFIG_DIR=/var/lib/deluge +ENV DELUGE_DOWNLOAD_DIR=/var/lib/deluge/download +ENV DELUGE_TORRENTFILE_DIR=/var/lib/deluge/torrents +ENV DELUGE_RPC_PORT=6890 +ENV DELUGE_LISTEN_PORTS=6891,6892 + +RUN mkdir -p ${DELUGE_APP} +WORKDIR ${DELUGE_APP} + +# SHELL modifies the shell form to a login shell so we +# can autoactivate the conda env at every RUN command. +SHELL ["/bin/bash", "--login", "-c"] + +RUN conda create -y -n 'deluge' python=3.8 + +# Populates the .bashrc so that conda is initialized +# and the proper env is activated before every call to RUN. +RUN conda init bash +RUN echo "conda activate deluge" > ~/.bashrc + +RUN conda install -y anaconda::py-boost\ + anaconda::gxx_linux-64\ + anaconda:openssl + +COPY . ./ +RUN git submodule update --init --recursive &&\ + cd vendor/libtorrent/bindings/python &&\ + python setup.py build_ext install + +RUN pip install . + +ENTRYPOINT ["bash", "--login", "-c", "${DELUGE_APP}/docker/bin/start.sh"] diff --git a/setup.py b/setup.py index 6161d60d0..fb36c7175 100755 --- a/setup.py +++ b/setup.py @@ -550,6 +550,7 @@ install_requires = [ "pywin32; sys_platform == 'win32'", "certifi; sys_platform == 'win32'", 'zope.interface', + 'prometheus-client>=0.21.0', ] extras_require = { 'all': [