From a872d6f55498142daad2286a1ff21ac10a0221ad Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <fabio@manganiello.tech>
Date: Fri, 22 Sep 2023 19:02:13 +0200
Subject: [PATCH] Added Fedora to the available Docker base images.

---
 platypush/install/docker/fedora.Dockerfile  | 26 +++++++++++++++++++
 platypush/install/scripts/fedora/PKGCMD     |  1 +
 platypush/install/scripts/fedora/install.sh | 28 +++++++++++++++++++++
 platypush/platydock/__init__.py             |  1 +
 platypush/utils/manifest.py                 | 10 ++++++++
 5 files changed, 66 insertions(+)
 create mode 100644 platypush/install/docker/fedora.Dockerfile
 create mode 100644 platypush/install/scripts/fedora/PKGCMD
 create mode 100755 platypush/install/scripts/fedora/install.sh

diff --git a/platypush/install/docker/fedora.Dockerfile b/platypush/install/docker/fedora.Dockerfile
new file mode 100644
index 000000000..d355f8d72
--- /dev/null
+++ b/platypush/install/docker/fedora.Dockerfile
@@ -0,0 +1,26 @@
+FROM fedora
+
+ADD . /install
+WORKDIR /var/lib/platypush
+
+ARG DOCKER_CTX=1
+ENV DOCKER_CTX=1
+
+# Enable the RPM Fusion repository
+RUN dnf install -y \
+  https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
+
+RUN /install/platypush/install/scripts/fedora/install.sh
+RUN cd /install && pip install -U --no-input --no-cache-dir .
+RUN rm -rf /install
+RUN dnf clean all -y
+
+EXPOSE 8008
+
+VOLUME /etc/platypush
+VOLUME /var/lib/platypush
+
+CMD platypush \
+  --start-redis \
+  --config /etc/platypush/config.yaml \
+  --workdir /var/lib/platypush
diff --git a/platypush/install/scripts/fedora/PKGCMD b/platypush/install/scripts/fedora/PKGCMD
new file mode 100644
index 000000000..6d37a3957
--- /dev/null
+++ b/platypush/install/scripts/fedora/PKGCMD
@@ -0,0 +1 @@
+dnf install -y
diff --git a/platypush/install/scripts/fedora/install.sh b/platypush/install/scripts/fedora/install.sh
new file mode 100755
index 000000000..5eb688269
--- /dev/null
+++ b/platypush/install/scripts/fedora/install.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+# This script parses the system requirements for a specific OS and it runs the
+# appropriate package manager command to install them.
+
+# This script is usually symlinked in the folders of the individual operating
+# systems, and it's not supposed to be invoked directly.
+# Instead, it will be called either by the root install.sh script or by a
+# Dockerfile.
+
+SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
+OS="$(basename "$SCRIPT_PATH")"
+CMD="$(cat "${SCRIPT_PATH}/PKGCMD")"
+REQUIREMENTS="$(cat "${SCRIPT_PATH}/../../requirements/${OS}.txt" | tr '\n' ' ')"
+SUDO=
+
+# If we aren't running in a Docker context, or the user is not root, we should
+# use sudo to install system packages.
+if [ $(id -u) -ne 0 ] || [ -z "$DOCKER_CTX" ]; then
+  if ! type sudo >/dev/null; then
+    echo "sudo executable not found, I can't install system packages" >&2
+    exit 1
+  fi
+
+  SUDO="sudo"
+fi
+
+${SUDO_ARGS} ${CMD} ${REQUIREMENTS}
diff --git a/platypush/platydock/__init__.py b/platypush/platydock/__init__.py
index ed97dbffb..638e2a953 100755
--- a/platypush/platydock/__init__.py
+++ b/platypush/platydock/__init__.py
@@ -35,6 +35,7 @@ class DockerBuilder(BaseBuilder):
     _pkg_manager_by_base_image = {
         BaseImage.ALPINE: PackageManagers.APK,
         BaseImage.DEBIAN: PackageManagers.APT,
+        BaseImage.FEDORA: PackageManagers.DNF,
         BaseImage.UBUNTU: PackageManagers.APT,
     }
 
diff --git a/platypush/utils/manifest.py b/platypush/utils/manifest.py
index 421fead48..8ca220469 100644
--- a/platypush/utils/manifest.py
+++ b/platypush/utils/manifest.py
@@ -42,6 +42,7 @@ class BaseImage(Enum):
 
     ALPINE = 'alpine'
     DEBIAN = 'debian'
+    FEDORA = 'fedora'
     UBUNTU = 'ubuntu'
 
     def __str__(self) -> str:
@@ -129,6 +130,15 @@ class PackageManagers(Enum):
         parse_list_line=lambda line: line.split('/')[0],
     )
 
+    DNF = PackageManager(
+        executable='dnf',
+        install=('dnf', 'install', '-y'),
+        uninstall=('dnf', 'remove', '-y'),
+        list=('dnf', 'list', '--installed'),
+        default_os='fedora',
+        parse_list_line=lambda line: re.split(r'\s+', line)[0].split('.')[0],
+    )
+
     PACMAN = PackageManager(
         executable='pacman',
         install=('pacman', '-S', '--noconfirm', '--needed'),