From 00ff6df82f24d7e87705072be3f98c87a836d311 Mon Sep 17 00:00:00 2001 From: Florian Kauer Date: Wed, 10 May 2023 05:03:21 +0000 Subject: [PATCH 1/6] Cleanup lock file when init fails Before, if the initialization of the service fails, the lock file was not removed and thus a second attempt to start detd fails. This is fixed by catching the exception and cleaning up the lock file. Signed-off-by: Florian Kauer --- detd/service.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/detd/service.py b/detd/service.py index 557b22b..98e0a24 100644 --- a/detd/service.py +++ b/detd/service.py @@ -72,16 +72,21 @@ def __init__(self, test_mode=False, log_filename=None): # We create the lock file even before calling parent's constructor self.setup_lock_file() - self.setup_unix_domain_socket() + try: + self.setup_unix_domain_socket() - super().__init__(_SERVICE_UNIX_DOMAIN_SOCKET, ServiceRequestHandler) + super().__init__(_SERVICE_UNIX_DOMAIN_SOCKET, ServiceRequestHandler) - self.test_mode = test_mode + self.test_mode = test_mode - self.manager = Manager() + self.manager = Manager() - signal.signal(signal.SIGINT, self.terminate) - signal.signal(signal.SIGTERM, self.terminate) + signal.signal(signal.SIGINT, self.terminate) + signal.signal(signal.SIGTERM, self.terminate) + except Exception as ex: + self.cleanup_lock_file() + logger.exception("Exception while initializing service") + raise def setup_lock_file(self): @@ -142,7 +147,9 @@ def cleanup(self): if os.path.exists(self.server_address): raise + self.cleanup_lock_file() + def cleanup_lock_file(self): # Clean-up lock file if not Check.is_valid_file(Service._SERVICE_LOCK_FILE): logger.error(f"{Service.SERVICE_LOCK_FILE} is not a valid file") From a2c04b6b756a5b9469c2bf1562be8dce867f1610 Mon Sep 17 00:00:00 2001 From: Florian Kauer Date: Wed, 10 May 2023 05:05:59 +0000 Subject: [PATCH 2/6] Create directory for unix domain socket This creates the directory for the socket file if it does not yet exists. Signed-off-by: Florian Kauer --- detd/service.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/detd/service.py b/detd/service.py index 98e0a24..3904dc0 100644 --- a/detd/service.py +++ b/detd/service.py @@ -75,6 +75,13 @@ def __init__(self, test_mode=False, log_filename=None): try: self.setup_unix_domain_socket() + # Create directory for the socket file + try: + os.makedirs(os.path.dirname(_SERVICE_UNIX_DOMAIN_SOCKET)) + except FileExistsError: + # directory already exists + pass + super().__init__(_SERVICE_UNIX_DOMAIN_SOCKET, ServiceRequestHandler) self.test_mode = test_mode From eacee9263aa99419b523adc6eaa3227428b0a70a Mon Sep 17 00:00:00 2001 From: Florian Kauer Date: Tue, 9 Jan 2024 15:43:12 +0100 Subject: [PATCH 3/6] Exit early on errors when building Debian package Currently, even when an error occurs, the package_debian.sh runs until completion and falsely reports a success. By adding set -e this can be avoided. Signed-off-by: Florian Kauer --- tools/package_debian.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/package_debian.sh b/tools/package_debian.sh index ccef0a5..930c94d 100755 --- a/tools/package_debian.sh +++ b/tools/package_debian.sh @@ -9,8 +9,7 @@ # Generates a rudimentary deb package to facilitate installations on Debian # based distributions. The package is then copied to /tmp - - +set -e # exit early on errors function usage () { echo "Usage:" From 2534c89dbfe3d602ca4c93b6b718c1bb01cfa0c5 Mon Sep 17 00:00:00 2001 From: Florian Kauer Date: Tue, 9 Jan 2024 15:44:53 +0100 Subject: [PATCH 4/6] Print actual Debian package filename The current package_debian.sh circumvents to print the full filename of the Debian package by using wildcards. However, this is not very user-friendly and can lead to confusion or bugs. Especially when you do a remote or Docker build, the filename should be explictly printed, otherwise it can be difficult to find the result. Therefore, assemble the full filename. Signed-off-by: Florian Kauer --- tools/package_debian.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tools/package_debian.sh b/tools/package_debian.sh index 930c94d..97c7f5b 100755 --- a/tools/package_debian.sh +++ b/tools/package_debian.sh @@ -49,6 +49,7 @@ function create_deb () { # E.g. detd-0.1.dev0 (PKG: detd, VERSION: 0.1.dev0) PKG=$(awk '/^name/{print $3}' ./setup.cfg) VERSION=$(awk '/^version/{print $3}' ./setup.cfg) + REVISION=1 ID="${PKG}-${VERSION}" @@ -61,7 +62,9 @@ function create_deb () { # Generate and customize the debian directory cd ${TMPDIR}/${ID} - debmake --binaryspec ':py3' --email ${EMAIL} --fullname ${FULLNAME} --spec + debmake --binaryspec ':py3' --email ${EMAIL} --fullname ${FULLNAME} --spec --revision ${REVISION} + + ARCHITECTURE=`grep -Po 'Architecture: \K\S+' debian/control` cp ${TMPDIR}/detd.service debian/ @@ -82,10 +85,11 @@ function create_deb () { # Generate the deb, make it available and perform clean-up fakeroot debian/rules binary - dpkg --contents ../*deb - dpkg -I ../*deb - cp ../*deb /tmp - echo "The deb package is now available in /tmp" + FILENAME=${PKG}_${VERSION}-${REVISION}_${ARCHITECTURE}.deb + dpkg --contents ../${FILENAME} + dpkg -I ../${FILENAME} + cp ../${FILENAME} /tmp + echo "The deb package is now available at /tmp/${FILENAME}" rm -rf ${TMPDIR} From 1a950553f4c2061e872bac9a965ae3a721de1970 Mon Sep 17 00:00:00 2001 From: Florian Kauer Date: Tue, 9 Jan 2024 15:47:58 +0100 Subject: [PATCH 5/6] Replace deprecated override_dh_systemd_start override_dh_systemd_start is deprecated and thus it does no longer build successfully in Debian Bookworm. Replace it with override_dh_installsystemd. Signed-off-by: Florian Kauer --- tools/package_debian.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/package_debian.sh b/tools/package_debian.sh index 97c7f5b..3a75f89 100755 --- a/tools/package_debian.sh +++ b/tools/package_debian.sh @@ -79,7 +79,7 @@ function create_deb () { echo -e "\tdh_installsystemd" >> debian/rules # Restart detd when the application is upgraded - echo -e "\noverride_dh_systemd_start:\n\tdh_systemd_start --restart-after-upgrade" >> debian/rules + echo -e "\noverride_dh_installsystemd:\n\tdh_installsystemd --restart-after-upgrade" >> debian/rules # Force xz for compression, to prevent installation issues with Zstandard echo -e "\noverride_dh_builddeb:\n\tdh_builddeb -- -Zxz" >> debian/rules From 93234dbfdd1b79cf3be86e4ab8b5e2bc354b8e87 Mon Sep 17 00:00:00 2001 From: Florian Kauer Date: Tue, 9 Jan 2024 15:50:48 +0100 Subject: [PATCH 6/6] Add Dockerfile for build To enable building for other distributions or without installing the build dependencies locally, provide a Dockerfile and the corresponding usage guide. Signed-off-by: Florian Kauer --- README.md | 10 ++++++++++ tools/Dockerfile | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tools/Dockerfile diff --git a/README.md b/README.md index ab74092..d8a463d 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,16 @@ tail /var/log/detd.log At this point the service is ready to receive requests. +#### Docker + +To avoid installing all build dependencies locally, you can also use Docker for building the Debian package: + +``` +docker build -f tools/Dockerfile . -t detd_builder +docker run --name detd_build_container detd_builder +docker cp detd_build_container:/tmp/detd_0.1.dev0-1_all.deb ./ +docker rm detd_build_container +``` #### pip diff --git a/tools/Dockerfile b/tools/Dockerfile new file mode 100644 index 0000000..1b43454 --- /dev/null +++ b/tools/Dockerfile @@ -0,0 +1,22 @@ +FROM debian:bookworm +ARG DEBIAN_FRONTEND=noninteractive + +RUN apt-get update --fix-missing && \ + apt-get upgrade --assume-yes --no-install-recommends && \ + apt-get install --assume-yes --no-install-recommends \ + debmake debhelper-compat dh-python \ + python3 python3-all python3-setuptools \ + protobuf-compiler python3-protobuf && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +ADD . /usr/local/src/detd/ + +WORKDIR /usr/local/src/detd/tools + +# Work around bug in debmake +RUN ln -s /usr/lib/debmake/python3.short /usr/lib/debmakepython3.short +RUN ln -s /usr/lib/debmake/python3.long /usr/lib/debmakepython3.long + +CMD ./package_debian.sh +