From 3f4a260a54b6bf8812310c3d3c320d559b8c0374 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Bonicoli Date: Wed, 15 Feb 2017 16:03:15 +0100 Subject: [PATCH] Add systemd unit files Largely inspired by https://lwn.net/Articles/584175/ "They must be suitable for every distribution to use." Packagers must define: - ExecStart=/path/to/bip_env.sh - Environment=BIP_DEFAULT_CONFIG='/path/to/default/bip' using a unit file drop-in bip-config.service.d/.conf --- Makefile.am | 3 ++- systemd/README | 20 +++++++++++++++++++ systemd/bip-config.service | 12 ++++++++++++ systemd/bip.service | 23 ++++++++++++++++++++++ systemd/bip_env.sh | 40 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 systemd/README create mode 100644 systemd/bip-config.service create mode 100644 systemd/bip.service create mode 100755 systemd/bip_env.sh diff --git a/Makefile.am b/Makefile.am index 8844519..3965c88 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,4 +24,5 @@ dist_examples_DATA = samples/bip.conf samples/bip.vim EXTRA_DIST = \ BUGS \ scripts/bip-release \ - scripts/bipgenconfig + scripts/bipgenconfig \ + systemd diff --git a/systemd/README b/systemd/README new file mode 100644 index 0000000..0f608a4 --- /dev/null +++ b/systemd/README @@ -0,0 +1,20 @@ +Notes about systemd unit files for bip. + +Distro specific commandline configuration is provided by installing a script +named. A default script named bip_env.sh is provided. + +This should write /run/sysconfig/bip based on configuration +information such as in /etc/sysconfig/bip or /etc/defaults/bip. It is run once +by bip-config.service. + +Distro specific path for bip_env.sh script must be set using a +bip-config.service.d/distrib.conf unit file drop-in: +[Service] +ExecStart=/path/to/bip_env.sh + +bip_env.sh try some default paths for the default configuration file: either +/etc/default/bip or /etc/sysconfig/bip. +Optionaly, the default configuration file path can be set too: +[Service] +ExecStart=/path/to/bip_env.sh +Environment=BIP_DEFAULT_CONFIG='/path/to/default/bip' diff --git a/systemd/bip-config.service b/systemd/bip-config.service new file mode 100644 index 0000000..da72598 --- /dev/null +++ b/systemd/bip-config.service @@ -0,0 +1,12 @@ +[Unit] +Description=Preprocess Bip configuration +After=local-fs.target +DefaultDependencies=no + +[Service] +Type=oneshot +RemainAfterExit=no +# Packagers must define: +# - ExecStart=/path/to/bip_env.sh +# - Environment='BIP_DEFAULT_CONFIG=/path/to/default/bip' +# using a unit file drop-in bip-config.service.d/.conf diff --git a/systemd/bip.service b/systemd/bip.service new file mode 100644 index 0000000..827e134 --- /dev/null +++ b/systemd/bip.service @@ -0,0 +1,23 @@ +[Unit] +Description=Bip IRC Proxy +Requires=network.target + +Wants=bip-config.service +After=bip-config.service + +[Service] +EnvironmentFile=/run/sysconfig/bip + +Type=forking +User=bip +Group=bip +ExecStartPre=/bin/sh -c '[ $ENABLED != 0 ]' +ExecStart=/usr/bin/bip $DAEMON_ARGS +ExecReload=/bin/kill -HUP $MAINPID +RuntimeDirectory=bip +RuntimeDirectoryMode=0750 +KillMode=process +Restart=on-abnormal + +[Install] +WantedBy=multi-user.target diff --git a/systemd/bip_env.sh b/systemd/bip_env.sh new file mode 100755 index 0000000..7f68769 --- /dev/null +++ b/systemd/bip_env.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# Create /run/sysconfig/bip from default configuration file, for +# bip-config.service + +# BIP_DEFAULT_CONFIG environment variable can be defined by packagers in a +# drop-in override of bip-config.service unit +if [ -n "${BIP_DEFAULT_CONFIG}" ]; then + # try some default paths + if [ -r /etc/default/bip ]; then + BIP_DEFAULT_CONFIG=/etc/default/bip + . "${BIP_DEFAULT_CONFIG}" + elif [ -r /etc/sysconfig/bip ]; then + BIP_DEFAULT_CONFIG=/etc/sysconfig/bip + . "${BIP_DEFAULT_CONFIG}" + fi +else + . "${BIP_DEFAULT_CONFIG}" +fi + +ENABLED=${ENABLED:-1} + +mkdir -p /run/sysconfig +{ +echo ENABLED=${ENABLED} +DAEMON_HOME=${DAEMON_HOME:-/var/lib/bip} +DAEMON_CONFIG=${DAEMON_CONFIG:-/etc/bip/bip.conf} +echo "DAEMON_ARGS=${DAEMON_ARGS:--f '${DAEMON_CONFIG}' -s '${DAEMON_HOME}'}" +} > /run/sysconfig/bip + +if [ ${ENABLED} = 0 ]; then + echo "INFO: BIP is explicitely disabled (ENABLED == 0) in" \ + "'${BIP_DEFAULT_CONFIG}'." +else + if [ -n "${DAEMON_USER}" -o -n "${DAEMON_GROUP}" ]; then + echo "ERROR: Using systemd, DAEMON_USER and DAEMON_GROUP could not" \ + "be defined using the default configuration file. A drop-in" \ + "override of bip-config.service unit need to be created instead." + exit 1 + fi +fi