From 02520ca3a8cedfb3081b7be48a9724699009b246 Mon Sep 17 00:00:00 2001
From: Teon Banek <teon.banek@memgraph.io>
Date: Tue, 27 Mar 2018 15:14:47 +0200
Subject: [PATCH] Add basic support for RPM packaging

Summary:
This is just the basic stuff needed to produce a RPM package of
Memgraph. Further work needs to be done in order for this to be up to
snuff.

Reviewers: mferencevic, buda

Reviewed By: mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1318
---
 CMakeLists.txt       | 15 +++++++++++++++
 release/rpm/postinst | 10 ++++++++++
 release/rpm/preinst  |  6 ++++++
 release/rpm/prerm    |  2 ++
 4 files changed, 33 insertions(+)
 create mode 100644 release/rpm/postinst
 create mode 100644 release/rpm/preinst
 create mode 100644 release/rpm/prerm

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fa7a1b5ce..f4d1fb266 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -232,6 +232,7 @@ set(CPACK_PACKAGE_VERSION_MINOR ${memgraph_VERSION_MINOR})
 set(CPACK_PACKAGE_VERSION_PATCH ${memgraph_VERSION_PATCH})
 set(CPACK_PACKAGE_VERSION_TWEAK ${memgraph_VERSION_TWEAK})
 set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${memgraph_VERSION}-${COMMIT_HASH}${CPACK_SYSTEM_NAME})
+
 # DEB specific
 # Instead of using "name <email>" format, we use "email (name)" to prevent
 # errors due to full stop, '.' at the end of "Ltd". (See: RFC 822)
@@ -250,6 +251,20 @@ set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION_SUMMARY}
  Contains Memgraph, the graph database. It aims to deliver developers the
  speed, simplicity and scale required to build the next generation of
  applications driver by real-time connected data.")
+
+# RPM specific
+set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION /var /var/lib /var/log /etc/logrotate.d /lib /lib/systemd /lib/systemd/system)
+set(CPACK_RPM_PACKAGE_REQUIRES_PRE "shadow-utils,systemd")
+# set(CPACK_RPM_USER_BINARY_SPECFILE "${CMAKE_SOURCE_DIR}/release/rpm/memgraph.spec.in")
+set(CPACK_RPM_PRE_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/release/rpm/preinst")
+set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/release/rpm/postinst")
+set(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/release/rpm/prerm")
+# Description formatting is important, no line must be greater than 80 characters.
+set(CPACK_RPM_PACKAGE_DESCRIPTION "Contains Memgraph, the graph database.
+            : It aims to deliver developers the speed, simplicity and scale
+            : required to build the next generation of applications driver by
+            : real-time connected data.")
+
 # All variables must be set before including.
 include(CPack)
 # ---- End Setup CPack ----
diff --git a/release/rpm/postinst b/release/rpm/postinst
new file mode 100644
index 000000000..aaf05dc53
--- /dev/null
+++ b/release/rpm/postinst
@@ -0,0 +1,10 @@
+# Hackish way to get the sytemd unit at the right place
+mv /lib/systemd/system/memgraph.service %{_unitdir}
+# memgraph user and group must be set in preinst
+chown memgraph:memgraph /var/lib/memgraph || exit 1
+chmod 750 /var/lib/memgraph || exit 1
+chown memgraph:adm /var/log/memgraph || exit 1
+chmod 750 /var/log/memgraph || exit 1
+# Make examples directory immutable (optional)
+chattr +i -R /usr/share/memgraph/examples || true
+
diff --git a/release/rpm/preinst b/release/rpm/preinst
new file mode 100644
index 000000000..877c097d9
--- /dev/null
+++ b/release/rpm/preinst
@@ -0,0 +1,6 @@
+# Add the 'memgraph' user and group
+getent group memgraph >/dev/null || groupadd -r memgraph || exit 1
+getent passwd memgraph >/dev/null || \
+  useradd -r -g memgraph -d /var/lib/memgraph -s /bin/bash memgraph || exit 1
+echo "Don't forget to switch to the 'memgraph' user to use Memgraph" || exit 1
+
diff --git a/release/rpm/prerm b/release/rpm/prerm
new file mode 100644
index 000000000..7c1f0df15
--- /dev/null
+++ b/release/rpm/prerm
@@ -0,0 +1,2 @@
+# Remove optional immutability from examples directory to allow removal
+chattr -i -R /usr/share/memgraph/examples || true