From ca9912f8de9bbbaedc704fa89e5ca41792dbf07b Mon Sep 17 00:00:00 2001 From: Andrey Kutin Date: Fri, 3 Jul 2026 09:44:39 +0300 Subject: [PATCH] Add systemd unit, example config, and deb packaging --- CMakeLists.txt | 18 +++++++++++++++++- deploy/config/cards.json.example | 14 ++++++++++++++ deploy/deb/postinst | 28 ++++++++++++++++++++++++++++ deploy/deb/prerm | 11 +++++++++++ deploy/systemd/cards.service | 21 +++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 deploy/config/cards.json.example create mode 100755 deploy/deb/postinst create mode 100755 deploy/deb/prerm create mode 100644 deploy/systemd/cards.service diff --git a/CMakeLists.txt b/CMakeLists.txt index 67e63e0..7f6a3c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,15 @@ set_target_properties(cards PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" ) +# ─── Install ────────────────────────────────────────────────────────── +install(TARGETS cards DESTINATION /usr/bin) + +install(FILES deploy/systemd/cards.service DESTINATION /usr/lib/systemd/system) + +install(FILES deploy/config/cards.json.example DESTINATION /etc/cards) + +install(FILES LICENSE DESTINATION /usr/share/doc/cards) + # ─── Packaging (CPack) ──────────────────────────────────────────────── set(CPACK_PACKAGE_NAME "cards") set(CPACK_PACKAGE_VERSION "1.0.0") @@ -62,13 +71,20 @@ set(CPACK_PACKAGE_VENDOR "cards") set(CPACK_PACKAGE_MAINTAINER "root") set(CPACK_PACKAGE_CONTACT "root@localhost") set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Cards web server") -set(CPACK_PACKAGE_DESCRIPTION "A simple C++20 web server for cards") +set(CPACK_PACKAGE_DESCRIPTION "A simple C++20 web server powered by Boost.Beast and PostgreSQL") set(CPACK_PACKAGE_SECTION "web") set(CPACK_DEBIAN_PACKAGE_SECTION "web") set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") + set(CPACK_DEBIAN_PACKAGE_DEPENDS "libpqxx-7.0 (>= 7.0), libpq5 (>= 13), libstdc++6 (>= 10), libgcc-s1" ) + +set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA + "${CMAKE_SOURCE_DIR}/deploy/deb/postinst" + "${CMAKE_SOURCE_DIR}/deploy/deb/prerm" +) + set(CPACK_DEBIAN_FILE_NAME "cards-${CPACK_PACKAGE_VERSION}-Linux.deb") set(CPACK_GENERATOR "DEB") diff --git a/deploy/config/cards.json.example b/deploy/config/cards.json.example new file mode 100644 index 0000000..bc834df --- /dev/null +++ b/deploy/config/cards.json.example @@ -0,0 +1,14 @@ +{ + "server": { + "address": "0.0.0.0", + "port": 8080, + "threads": 4 + }, + "database": { + "host": "localhost", + "port": 5432, + "dbname": "cards", + "user": "cards", + "password": "" + } +} diff --git a/deploy/deb/postinst b/deploy/deb/postinst new file mode 100755 index 0000000..c15caf3 --- /dev/null +++ b/deploy/deb/postinst @@ -0,0 +1,28 @@ +#!/bin/sh +set -e + +case "$1" in + configure) + # Create cards user/group if they don't exist + if ! getent group cards >/dev/null 2>&1; then + addgroup --system cards + fi + if ! getent passwd cards >/dev/null 2>&1; then + adduser --system --ingroup cards --no-create-home --disabled-password cards + fi + + # Copy default config if not present + if [ ! -f /etc/cards/config.json ]; then + cp /etc/cards/config.json.example /etc/cards/config.json + chown cards:cards /etc/cards/config.json + chmod 640 /etc/cards/config.json + fi + + # Enable and restart service + if [ -d /run/systemd/system ]; then + systemctl daemon-reload >/dev/null 2>&1 || true + systemctl enable cards >/dev/null 2>&1 || true + systemctl restart cards >/dev/null 2>&1 || true + fi + ;; +esac diff --git a/deploy/deb/prerm b/deploy/deb/prerm new file mode 100755 index 0000000..87511cb --- /dev/null +++ b/deploy/deb/prerm @@ -0,0 +1,11 @@ +#!/bin/sh +set -e + +case "$1" in + remove|upgrade|deconfigure) + if [ -d /run/systemd/system ]; then + systemctl stop cards >/dev/null 2>&1 || true + systemctl disable cards >/dev/null 2>&1 || true + fi + ;; +esac diff --git a/deploy/systemd/cards.service b/deploy/systemd/cards.service new file mode 100644 index 0000000..37ac752 --- /dev/null +++ b/deploy/systemd/cards.service @@ -0,0 +1,21 @@ +[Unit] +Description=Cards web server +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +User=cards +Group=cards +ExecStart=/usr/bin/cards /etc/cards/config.json +Restart=on-failure +RestartSec=5 +AmbientCapabilities=CAP_NET_BIND_SERVICE +NoNewPrivileges=yes +ProtectSystem=full +ProtectHome=yes +PrivateTmp=yes +MemoryDenyWriteExecute=yes + +[Install] +WantedBy=multi-user.target