From e77bc8de255938a1af384ecb63d82e8e7a4b6b7a Mon Sep 17 00:00:00 2001 From: Sam Gleske Date: Sun, 9 Sep 2018 12:36:48 -0700 Subject: [PATCH] Add systemd integration for docker-compose service --- emby.service | 19 +++++++++ install-emby-service.sh | 88 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 emby.service create mode 100755 install-emby-service.sh diff --git a/emby.service b/emby.service new file mode 100644 index 0000000..2b55604 --- /dev/null +++ b/emby.service @@ -0,0 +1,19 @@ +# http://container-solutions.com/running-docker-containers-with-systemd/ +# install in /etc/systemd/system/emby.service +[Unit] +Description=Emby Media Server +After=docker.service +Requires=docker.service + +[Service] +WorkingDirectory=${EMBY_DOCKER_HOME} +TimeoutStartSec=0 +Restart=always +EnvironmentFile=-/etc/sysconfig/docker.emby +# execstartpre is not necessary when using docker-compose +#ExecStartPre=/usr/local/sbin/docker-compose pull +ExecStart=/usr/local/sbin/docker-compose run --service-ports emby +ExecStop=/usr/local/sbin/docker-compose down + +[Install] +WantedBy=multi-user.target diff --git a/install-emby-service.sh b/install-emby-service.sh new file mode 100755 index 0000000..703b6f1 --- /dev/null +++ b/install-emby-service.sh @@ -0,0 +1,88 @@ +#!/bin/bash +# Created by Sam Gleske +# Installs the emby.service file as a systemd service + +set -aueEo pipefail + +# +# ENVIRONMENT +# + +DESTINATION=/etc/systemd/system/emby.service +export EMBY_DOCKER_HOME="${PWD}" + +# +# FUNCTIONS +# + +function msg() { + echo "${@}" >&2 +} + +function die() { + msg "${@}" + exit 1 +}; declare -rf die + +function checksum() { + envsubst < emby.service | sha256sum | awk '{ print $1 }' +}; declare -rf checksum + +function sha256sum() { + if type -P sha256sum > /dev/null; then + command sha256sum "${@}" + elif type -P shasum > /dev/null; then + command shasum -a 256 "${@}" + else + echo "ERROR: could not find a sha256sum program." + exit 1 + fi +}; declare -rf sha256sum + +# +# MAIN EXECUTION +# + +# pre-flight checks before modifying the system +[ "${USER:-$(whoami)}" = root ] || + die 'ERROR: must be run as root to install the systemd service.' +[ -d /lib/systemd ] && type -P systemctl > /dev/null || + die 'ERROR: must be run on a system which uses systemd for init.' +type -P envsubst > /dev/null || + die 'ERROR: missing gettext package. "yum install gettext" or "apt install gettext"' +[ -r emby.service ] || + die 'ERROR: no emby.service found. Are you in the right working directory?' + +# Try to install the service. +if [ -f "${DESTINATION}" ] && sha256sum -c - <<< "$(checksum) ${DESTINATION}"; then + msg 'SKIPPED: emby.service is already installed.' +else + msg 'Installing systemd service.' + envsubst < emby.service > "${DESTINATION}" + systemctl daemon-reload +fi + +# Give the final help message. +cat >&2 <<'EOF' +With emby.service installed you are now ready to control the service. This +script only installs the service and does not control the service for you. + +This script is idempotent and can be safely run multiple times to see this +message agin. + +=== SERVICE CONTROL === +Start the service. + systemctl start emby.service +Stop the service. + systemctl stop emby.service +Ensure the service autostarts on reboot. + systemctl enable emby.service +Stop the service from autostarting on reboot. + systemctl disable emby.service + +=== DEBUGGING EMBY SERVICE === +View the current service status. + systemctl status emby.service +View the systemd logs for the service. + journalctl -u emby.service +EOF