Skip to content

Commit

Permalink
feat: systemd enabling and running
Browse files Browse the repository at this point in the history
Enable or disable the service in SystemD using the hooks, also start or
restart the service in case of install or update.
  • Loading branch information
faabiosr committed Oct 22, 2023
1 parent dafcfe5 commit e0b5ac8
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ nfpms:
license: MIT
formats:
- deb
contents:
- src: "env/debian/movies.service"
dst: "/lib/systemd/system/movies.service"
- src: "env/debian/movies.conf"
dst: "/etc/default/movies"
type: config
scripts:
preremove: "env/debian/pre-remove.sh"
postinstall: "env/debian/post-install.sh"
postremove: "env/debian/post-remove.sh"

Expand Down
14 changes: 14 additions & 0 deletions env/debian/movies.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ---------------------------------
# Movies configuration file
# ---------------------------------
#
# The commented-out settings shown in this file represent the default values.
#
# This file is read when movies service start
#
# -----------------------------------------------------------------------------
# Datastore
# -----------------------------------------------------------------------------

# Database path configuration
MOVIES_DB_PATH="/var/lib/movies-demo"
14 changes: 14 additions & 0 deletions env/debian/movies.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=Manages movie collection throught API
Documentation="https://github.com/faabiosr/go-movies-demo"

[Service]
EnvironmentFile=/etc/default/movies
ExecStart=/usr/bin/movies
Restart=on-failure
User=movies-demo
Group=movies-demo
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target
23 changes: 22 additions & 1 deletion env/debian/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e

MOVIES_DB_PATH=/var/lib/movies-demo
MOVIES_USER=movies-demo
MOVIES_SERVICE=movies.service

if [ "$1" = "configure" ]; then
# creating user and group
Expand All @@ -20,5 +21,25 @@ if [ "$1" = "configure" ]; then
chown $MOVIES_USER:$MOVIES_USER $MOVIES_DB_PATH
fi

exit 0
# enable systemd service
deb-systemd-helper unmask $MOVIES_SERVICE >/dev/null || true

if deb-systemd-helper --quiet was-enabled $MOVIES_SERVICE; then
deb-systemd-helper enable $MOVIES_SERVICE >/dev/null || true
else
deb-systemd-helper update-state $MOVIES_SERVICE >/dev/null || true
fi

# starting service
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true

if [ -n "$2" ]; then
_dh_action=restart
else
_dh_action=start
fi

deb-systemd-invoke $_dh_action $MOVIES_SERVICE >/dev/null || true
fi
fi
14 changes: 13 additions & 1 deletion env/debian/post-remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -e

MOVIES_DB_PATH=/var/lib/movies-demo
MOVIES_SERVICE=movies.service

if [ "$1" = "remove" ]; then
if [ -f "$MOVIES_DB_PATH/catalog.db" ]; then
Expand All @@ -12,5 +13,16 @@ if [ "$1" = "remove" ]; then
rm -fr $MOVIES_DB_PATH
fi

exit 0
# disabling service
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
fi

deb-systemd-helper mask $MOVIES_SERVICE >/dev/null || true
fi

if [ "$1" = "purge" ]; then
# disabling service
deb-systemd-helper purge $MOVIES_SERVICE >/dev/null || true
deb-systemd-helper unmask $MOVIES_SERVICE >/dev/null || true
fi
10 changes: 10 additions & 0 deletions env/debian/pre-remove.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -e

MOVIES_SERVICE=movies.service

# stopping service
if [ -d /run/systemd/system ]; then
deb-systemd-invoke stop $MOVIES_SERVICE >/dev/null || true
fi

0 comments on commit e0b5ac8

Please sign in to comment.