Skip to content

Commit

Permalink
integrate systemd unit file into Debian package
Browse files Browse the repository at this point in the history
  • Loading branch information
Montellese committed Sep 3, 2021
1 parent 914174c commit b12bd33
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Prepare to build a Debian source package
run: |
sudo apt-get -y install python3-all debhelper dh-python
sudo apt-get -y install python3-all debhelper dh-python dh-systemd
- name: Build a Debian source package
run: |
Expand Down
2 changes: 1 addition & 1 deletion debian/compat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9
10
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Source: smartmeter-datacollector
Maintainer: Supercomputing Systems AG <[email protected]>
Section: python
Priority: optional
Build-Depends: python3-setuptools, python3-all, debhelper (>= 9)
Build-Depends: python3-setuptools, python3-all, debhelper (>= 10), dh-systemd (>= 1.5)
Standards-Version: 3.9.1
Homepage: https://github.com/scs/smartmeter-datacollector

Expand Down
45 changes: 45 additions & 0 deletions debian/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
# postinst script for python3-smartmeter-datacollector
#
# see: dh_installdeb(1)

# exit on any error
set -e

USERNAME=smartmeter
GROUP=dialout

# make sure the dialout group already exists
if ! getent group | grep -q "^${GROUP}:"; then
>&2 echo "Expected group \"${GROUP}\" doesn't exist!"
exit 1
fi

# add new group "smartmeter" if it doesn't already exist
if ! getent group | grep -q "^${USERNAME}:"; then
echo -n "Adding group ${USERNAME}.."
addgroup --quiet --system ${USERNAME} 2>/dev/null || true
echo "..done"
fi

# add new user "smartmeter" if it doesn't already exist
if ! getent passwd | grep -q "^${USERNAME}:"; then
echo -n "Adding system user ${USERNAME}.."
adduser --quiet \
--system \
--ingroup ${USERNAME} \
--no-create-home \
--disabled-login \
--disabled-password \
${USERNAME} 2>/dev/null || true
echo "..done"
fi

# add the new user "smartmeter" to the dialout group
if ! groups ${USERNAME} | cut -d: -f2 | grep -qw ${GROUP}; then
adduser ${USERNAME} ${GROUP} > /dev/null 2>&1
fi

#DEBHELPER#

exit 0
11 changes: 11 additions & 0 deletions debian/python3-smartmeter-datacollector.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Smart Meter Data Collector Service

[Service]
Type=simple
User=smartmeter
Restart=on-failure
ExecStart=smartmeter-datacollector -c /var/lib/smartmeter-datacollector/datacollector.ini

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion debian/rules
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/make -f

%:
dh $@ --with python3 --buildsystem=python_distutils
dh $@ --with python3 --buildsystem=python_distutils --with systemd

override_dh_auto_clean:
python3 setup.py clean -a
Expand Down
61 changes: 60 additions & 1 deletion scripts/debianize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,65 @@ python setup.py \
--command-packages=stdeb.command debianize \
--with-python2=false \
--with-python3=true \
--no-python2-scripts=true
--no-python2-scripts=true \
--with-dh-systemd \
--compat=10 \
--build-depends="dh-systemd (>= 1.5)"

# fix the debhelper compatibility level in debian/control
sed -i 's/>= 9/>= 10/' debian/control

# write the debian/postinst file
cat <<EOT >> debian/postinst
#!/bin/sh
# postinst script for python3-smartmeter-datacollector
#
# see: dh_installdeb(1)
# exit on any error
set -e
USERNAME=smartmeter
GROUP=dialout
# make sure the dialout group already exists
if ! getent group | grep -q "^\${GROUP}:"; then
>&2 echo "Expected group \"\${GROUP}\" doesn't exist!"
exit 1
fi
# add new group "smartmeter" if it doesn't already exist
if ! getent group | grep -q "^\${USERNAME}:"; then
echo -n "Adding group \${USERNAME}.."
addgroup --quiet --system \${USERNAME} 2>/dev/null || true
echo "..done"
fi
# add new user "smartmeter" if it doesn't already exist
if ! getent passwd | grep -q "^\${USERNAME}:"; then
echo -n "Adding system user \${USERNAME}.."
adduser --quiet \\
--system \\
--ingroup \${USERNAME} \\
--no-create-home \\
--disabled-login \\
--disabled-password \\
\${USERNAME} 2>/dev/null || true
echo "..done"
fi
# add the new user "smartmeter" to the dialout group
if ! groups \${USERNAME} | cut -d: -f2 | grep -qw \${GROUP}; then
adduser \${USERNAME} \${GROUP} > /dev/null 2>&1
fi
#DEBHELPER#
exit 0
EOT

# copy the systemd unit file to the generated debian directory
SYSTEMD_UNIT_FILE=$(find . -maxdepth 1 -type f -name '*.service' | cut -c 3-)
cp ${SYSTEMD_UNIT_FILE} debian/python3-${SYSTEMD_UNIT_FILE}

echo "SUCCESS: Project has been successfully debianized at debian/"
1 change: 1 addition & 0 deletions smartmeter-datacollector.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Description=Smart Meter Data Collector Service

[Service]
Type=simple
User=smartmeter
Restart=on-failure
ExecStart=smartmeter-datacollector -c /var/lib/smartmeter-datacollector/datacollector.ini

Expand Down

0 comments on commit b12bd33

Please sign in to comment.