From e161f707104c0f37500dd0467eb1ccd366f6023f Mon Sep 17 00:00:00 2001 From: Mark Cooper Date: Fri, 24 Sep 2021 19:37:35 -0500 Subject: [PATCH] Debian package builder --- build/debian/DEBIAN/conffiles | 1 + build/debian/DEBIAN/control | 8 ++ build/debian/DEBIAN/postinst | 84 +++++++++++++++++++ build/debian/DEBIAN/prerm | 26 ++++++ build/debian/etc/default/audiobookshelf | 4 + .../lib/systemd/system/audiobookshelf.service | 16 ++++ package.json | 8 +- 7 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 build/debian/DEBIAN/conffiles create mode 100644 build/debian/DEBIAN/control create mode 100644 build/debian/DEBIAN/postinst create mode 100644 build/debian/DEBIAN/prerm create mode 100644 build/debian/etc/default/audiobookshelf create mode 100644 build/debian/lib/systemd/system/audiobookshelf.service diff --git a/build/debian/DEBIAN/conffiles b/build/debian/DEBIAN/conffiles new file mode 100644 index 00000000..a731efb1 --- /dev/null +++ b/build/debian/DEBIAN/conffiles @@ -0,0 +1 @@ +/etc/default/audiobookshelf \ No newline at end of file diff --git a/build/debian/DEBIAN/control b/build/debian/DEBIAN/control new file mode 100644 index 00000000..6a414b13 --- /dev/null +++ b/build/debian/DEBIAN/control @@ -0,0 +1,8 @@ +Package: audiobookshelf +Version: 1.2.1 +Section: base +Priority: optional +Architecture: amd64 +Depends: +Maintainer: advplyr +Description: Self-hosted audiobook server for managing and playing audiobooks \ No newline at end of file diff --git a/build/debian/DEBIAN/postinst b/build/debian/DEBIAN/postinst new file mode 100644 index 00000000..92af782d --- /dev/null +++ b/build/debian/DEBIAN/postinst @@ -0,0 +1,84 @@ +#!/bin/bash +set -e +set -o pipefail + +declare -r init_type='auto' +declare -ri no_rebuild='0' + +add_user() { + : "${1:?'User was not defined'}" + declare -r user="$1" + declare -r uid="$2" + + if [ -z "$uid" ]; then + declare -r uid_flags="" + else + declare -r uid_flags="--uid $uid" + fi + + declare -r group="${3:-$user}" + declare -r descr="${4:-No description}" + declare -r shell="${5:-/bin/false}" + + if ! getent passwd | grep -q "^$user:"; then + echo "Creating system user: $user in $group with $descr and shell $shell" + useradd $uid_flags --gid $group --no-create-home --system --shell $shell -c "$descr" $user + fi +} + +add_group() { + : "${1:?'Group was not defined'}" + declare -r group="$1" + declare -r gid="$2" + + if [ -z "$gid" ]; then + declare -r gid_flags="" + else + declare -r gid_flags="--gid $gid" + fi + + if ! getent group | grep -q "^$group:" ; then + echo "Creating system group: $group" + groupadd $gid_flags --system $group + fi +} + +start_service () { + : "${1:?'Service name was not defined'}" + declare -r service_name="$1" + + if hash systemctl 2> /dev/null; then + if [[ "$init_type" == 'auto' || "$init_type" == 'systemd' ]]; then + { + systemctl enable "$service_name.service" && \ + systemctl start "$service_name.service" + } || echo "$service_name could not be registered or started" + fi + elif hash service 2> /dev/null; then + if [[ "$init_type" == 'auto' || "$init_type" == 'upstart' || "$init_type" == 'sysv' ]]; then + service "$service_name" start || echo "$service_name could not be registered or started" + fi + elif hash start 2> /dev/null; then + if [[ "$init_type" == 'auto' || "$init_type" == 'upstart' ]]; then + start "$service_name" || echo "$service_name could not be registered or started" + fi + elif hash update-rc.d 2> /dev/null; then + if [[ "$init_type" == 'auto' || "$init_type" == 'sysv' ]]; then + { + update-rc.d "$service_name" defaults && \ + "/etc/init.d/$service_name" start + } || echo "$service_name could not be registered or started" + fi + else + echo 'Your system does not appear to use systemd, Upstart, or System V, so the service could not be started' + fi +} + + +add_group 'audiobookshelf' '' +add_user 'audiobookshelf' '' 'audiobookshelf' 'audiobookshelf user-daemon' '/bin/false' + +mkdir -p '/var/log/audiobookshelf' +chown -R 'audiobookshelf:audiobookshelf' '/var/log/audiobookshelf' + +start_service 'audiobookshelf' diff --git a/build/debian/DEBIAN/prerm b/build/debian/DEBIAN/prerm new file mode 100644 index 00000000..42ecc715 --- /dev/null +++ b/build/debian/DEBIAN/prerm @@ -0,0 +1,26 @@ +#!/bin/bash +set -e +set -o pipefail + +declare -r init_type='auto' +declare -r service_name='audiobookshelf' + +if [[ "$init_type" == 'auto' || "$init_type" == 'systemd' || "$init_type" == 'upstart' || "$init_type" == 'sysv' ]]; then + if hash systemctl 2> /dev/null; then + systemctl disable "$service_name.service" && \ + systemctl stop "$service_name.service" || \ + echo "$service_name wasn't even running!" + elif hash service 2> /dev/null; then + service "$service_name" stop || echo "$service_name wasn't even running!" + elif hash stop 2> /dev/null; then + stop "$service_name" || echo "$service_name wasn't even running!" + elif hash update-rc.d 2> /dev/null; then + { + update-rc.d "$service_name" remove && \ + "/etc/init.d/$service_name" stop + } || "$service_name wasn't even running!" + else + echo "Your system does not appear to use upstart, systemd or sysv, so $service_name could not be stopped" + echo 'Unless these systems were removed since install, no processes have been left running' + fi +fi \ No newline at end of file diff --git a/build/debian/etc/default/audiobookshelf b/build/debian/etc/default/audiobookshelf new file mode 100644 index 00000000..8be8b132 --- /dev/null +++ b/build/debian/etc/default/audiobookshelf @@ -0,0 +1,4 @@ +AUDIOBOOK_PATH=/usr/share/audiobookshelf/audiobooks +METADATA_PATH=/usr/share/audiobookshelf/metadata +CONFIG_PATH=/usr/share/audiobookshelf/config +PORT=1337 \ No newline at end of file diff --git a/build/debian/lib/systemd/system/audiobookshelf.service b/build/debian/lib/systemd/system/audiobookshelf.service new file mode 100644 index 00000000..528a2c30 --- /dev/null +++ b/build/debian/lib/systemd/system/audiobookshelf.service @@ -0,0 +1,16 @@ +[Unit] +Description=Self-hosted audiobook server for managing and playing audiobooks +Requires=network.target + +[Service] +Type=simple +EnvironmentFile=/etc/default/audiobookshelf +WorkingDirectory=/usr/share/audiobookshelf +ExecStart=/usr/share/audiobookshelf/audiobookshelf +ExecReload=/bin/kill -HUP $MAINPID +Restart=always +User=audiobookshelf +PermissionsStartOnly=true + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/package.json b/package.json index 50a7de1e..56f2bd17 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "audiobookshelf", "version": "1.2.1", - "description": "Self-hosted audiobook server for managing and playing audiobooks.", + "description": "Self-hosted audiobook server for managing and playing audiobooks", "main": "index.js", "scripts": { "dev": "node index.js", @@ -9,12 +9,12 @@ "client": "cd client && npm install && npm run generate-client", "generate-client": "cd client && npm run generate", "prod": "npm run client && npm install && node prod.js", - "build-client": "cd client && rm -rf node_modules && npm i --unsafe-perm=true --allow-root && npm run generate", - "build-server": "rm -rf node_modules && npm i --unsafe-perm=true --allow-root && npm i ffmpeg-static --unsafe-perm=true --allow-root", + "build-client": "cd client && rm -rf node_modules && npm ci --unsafe-perm=true --allow-root && npm run generate", + "build-server": "rm -rf node_modules && npm ci --unsafe-perm=true --allow-root && npm i ffmpeg-static --unsafe-perm=true --allow-root", "build-prep": "npm run build-client && npm run build-server", "build-win": "npm run build-prep && pkg -t node12-win-x64 -o ./dist/win/audiobookshelf .", "build-linuxarm": "npm run build-prep && pkg -t node12-linux-arm64 -o ./dist/linuxarm/audiobookshelf .", - "build-linux": "npm run build-prep && pkg -t node12-linux-x64 -o ./dist/linux/audiobookshelf ." + "build-linux": "npm run build-prep && pkg -t node12-linux-x64 -o dist/linux/audiobookshelf . && cp dist/linux/audiobookshelf build/debian/usr/share/" }, "bin": "prod.js", "pkg": {