From 9a2546f89767af2f87c0df841c58b3b5d79b377e Mon Sep 17 00:00:00 2001 From: Cody Lee Date: Sun, 4 Dec 2022 08:26:04 -0600 Subject: [PATCH 1/3] nfpm tips to fix this for systemd systems --- .goreleaser.yaml | 5 +- scripts/after-install.sh | 29 ------------ scripts/before-remove.sh | 17 ------- scripts/post-install.sh | 100 +++++++++++++++++++++++++++++++++++++++ scripts/post-remove.sh | 45 ++++++++++++++++++ scripts/pre-remove.sh | 22 +++++++++ 6 files changed, 170 insertions(+), 48 deletions(-) delete mode 100755 scripts/after-install.sh delete mode 100755 scripts/before-remove.sh create mode 100755 scripts/post-install.sh create mode 100644 scripts/post-remove.sh create mode 100755 scripts/pre-remove.sh diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 7b45f7f9..f4636441 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -327,8 +327,9 @@ nfpms: # signing scripts: - postinstall: "scripts/after-install.sh" - preremove: "scripts/before-remove.sh" + postinstall: "scripts/post-install.sh" + preremove: "scripts/pre-remove.sh" + postremove: "scripts/post-remove.sh" signs: - id: default diff --git a/scripts/after-install.sh b/scripts/after-install.sh deleted file mode 100755 index 4332ccee..00000000 --- a/scripts/after-install.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -# This file is used by deb, rpm and BSD packages. -# FPM adds this as the after-install script. -# Edit this file as needed for your application. -# This file is only installed if FORMULA is set to service. - -OS="$(uname -s)" - -if [ "${OS}" = "Linux" ]; then - # Make a user and group for this app, but only if it does not already exist. - id unpoller >/dev/null 2>&1 || \ - useradd --system --user-group --no-create-home --home-dir /tmp --shell /bin/false unpoller -elif [ "${OS}" = "OpenBSD" ]; then - id unpoller >/dev/null 2>&1 || \ - useradd -g =uid -d /tmp -s /bin/false unpoller -elif [ "${OS}" = "FreeBSD" ]; then - id unpoller >/dev/null 2>&1 || \ - pw useradd unpoller -d /tmp -w no -s /bin/false -else - echo "Unknown OS: ${OS}, please add system user unpoller manually." -fi - -if [ -x "/bin/systemctl" ]; then - # Reload and restart - this starts the application as user nobody. - /bin/systemctl daemon-reload - /bin/systemctl enable unpoller - /bin/systemctl restart unpoller -fi diff --git a/scripts/before-remove.sh b/scripts/before-remove.sh deleted file mode 100755 index 83c4264c..00000000 --- a/scripts/before-remove.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -# This file is used by rpm and deb packages. FPM use. -# Edit this file as needed for your application. -# This file is only installed if FORMULA is set to service. - -if [ "$1" = "upgrade" ] || [ "$1" = "1" ] ; then - exit 0 -fi - -if [ -x "/bin/systemctl" ]; then - /bin/systemctl stop unpoller - /bin/systemctl disable unpoller -elif [ -x /usr/sbin/service ]; then - /usr/sbin/service unpoller stop - /usr/sbin/service unpoller disable -fi diff --git a/scripts/post-install.sh b/scripts/post-install.sh new file mode 100755 index 00000000..c61436c1 --- /dev/null +++ b/scripts/post-install.sh @@ -0,0 +1,100 @@ +#!/bin/sh + +# Step 1, decide if we should use systemd or init/upstart +use_systemctl="True" +systemd_version=0 +if ! command -V systemctl >/dev/null 2>&1; then + use_systemctl="False" +else + systemd_version=$(systemctl --version | head -1 | sed 's/systemd //g') +fi + +cleanup() { + # This is where you remove files that were not needed on this platform / system + if [ "${use_systemctl}" = "False" ]; then + rm -f /etc/systemd/service/unpoller.service + else + rm -f /etc/chkconfig/unpoller + rm -f /etc/init.d/unpoller + fi +} + +OS="$(uname -s)" + +cleanInstall() { + printf "\033[32m Post Install of an clean install\033[0m\n" + # Step 3 (clean install), enable the service in the proper way for this platform + + + if [ "${OS}" = "Linux" ]; then + # Make a user and group for this app, but only if it does not already exist. + id unpoller >/dev/null 2>&1 || \ + useradd --system --user-group --no-create-home --home-dir /tmp --shell /bin/false unpoller + elif [ "${OS}" = "OpenBSD" ]; then + id unpoller >/dev/null 2>&1 || \ + useradd -g =uid -d /tmp -s /bin/false unpoller + elif [ "${OS}" = "FreeBSD" ]; then + id unpoller >/dev/null 2>&1 || \ + pw useradd unpoller -d /tmp -w no -s /bin/false + else + echo "Unknown OS: ${OS}, please add system user unpoller manually." + fi + + if [ "${use_systemctl}" = "False" ]; then + if command -V chkconfig >/dev/null 2>&1; then + chkconfig --add unpoller + fi + + service unpoller restart ||: + else + # rhel/centos7 cannot use ExecStartPre=+ to specify the pre start should be run as root + # even if you want your service to run as non root. + if [ "${systemd_version}" -lt 231 ]; then + printf "\033[31m systemd version %s is less then 231, fixing the service file \033[0m\n" "${systemd_version}" + sed -i "s/=+/=/g" /etc/systemd/service/unpoller.service + fi + printf "\033[32m Reload the service unit from disk\033[0m\n" + systemctl daemon-reload ||: + printf "\033[32m Unmask the service\033[0m\n" + systemctl unmask unpoller ||: + printf "\033[32m Set the preset flag for the service unit\033[0m\n" + systemctl preset unpoller ||: + printf "\033[32m Set the enabled flag for the service unit\033[0m\n" + systemctl enable unpoller ||: + systemctl restart unpoller ||: + fi +} + +upgrade() { + printf "\033[32m Post Install of an upgrade\033[0m\n" + # Step 3(upgrade), do what you need + +} + +# Step 2, check if this is a clean install or an upgrade +action="$1" +if [ "$1" = "configure" ] && [ -z "$2" ]; then + # Alpine linux does not pass args, and deb passes $1=configure + action="install" +elif [ "$1" = "configure" ] && [ -n "$2" ]; then + # deb passes $1=configure $2= + action="upgrade" +fi + +case "$action" in + "1" | "install") + cleanInstall + ;; + "2" | "upgrade") + printf "\033[32m Post Install of an upgrade\033[0m\n" + upgrade + ;; + *) + # $1 == version being installed + printf "\033[32m Alpine\033[0m" + cleanInstall + ;; +esac + +# Step 4, clean up unused files, yes you get a warning when you remove the package, but that is ok. +cleanup diff --git a/scripts/post-remove.sh b/scripts/post-remove.sh new file mode 100644 index 00000000..84d1e64e --- /dev/null +++ b/scripts/post-remove.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +# Step 1, decide if we should use systemd or init/upstart +use_systemctl="True" +systemd_version=0 +if ! command -V systemctl >/dev/null 2>&1; then + use_systemctl="False" +else + systemd_version=$(systemctl --version | head -1 | sed 's/systemd //g') +fi + +remove() { + printf "\033[32m Post Remove of a normal remove\033[0m\n" + echo "Remove" > /tmp/postremove-proof +} + +purge() { + printf "\033[32m Post Remove purge, deb only\033[0m\n" + echo "Purge" > /tmp/postremove-proof +} + +upgrade() { + printf "\033[32m Post Remove of an upgrade\033[0m\n" + echo "Upgrade" > /tmp/postremove-proof +} + +echo "$@" + +action="$1" + +case "$action" in + "0" | "remove") + remove + ;; + "1" | "upgrade") + upgrade + ;; + "purge") + purge + ;; + *) + printf "\033[32m Alpine\033[0m" + remove + ;; +esac diff --git a/scripts/pre-remove.sh b/scripts/pre-remove.sh new file mode 100755 index 00000000..7e809c0d --- /dev/null +++ b/scripts/pre-remove.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Step 1, decide if we should use systemd or init/upstart +use_systemctl="True" +systemd_version=0 +if ! command -V systemctl >/dev/null 2>&1; then + use_systemctl="False" +else + systemd_version=$(systemctl --version | head -1 | sed 's/systemd //g') +fi + +if [ "$1" = "upgrade" ] || [ "$1" = "1" ] ; then + exit 0 +fi + +if [ "${use_systemctl}" = "False" ]; then + service unpoller stop + service unpoller disable +else + systemctl stop unpoller + systemctl disable unpoller +fi From e2f62a85269aa3134775c11fbf0366af4cf48d63 Mon Sep 17 00:00:00 2001 From: Cody Lee Date: Sun, 4 Dec 2022 08:57:25 -0600 Subject: [PATCH 2/3] parsing fix --- scripts/post-install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/post-install.sh b/scripts/post-install.sh index c61436c1..289b9280 100755 --- a/scripts/post-install.sh +++ b/scripts/post-install.sh @@ -6,7 +6,7 @@ systemd_version=0 if ! command -V systemctl >/dev/null 2>&1; then use_systemctl="False" else - systemd_version=$(systemctl --version | head -1 | sed 's/systemd //g') + systemd_version=$(systemctl --version | head -1 | sed 's/systemd //g' | sed 's/ .*//') fi cleanup() { @@ -49,7 +49,7 @@ cleanInstall() { else # rhel/centos7 cannot use ExecStartPre=+ to specify the pre start should be run as root # even if you want your service to run as non root. - if [ "${systemd_version}" -lt 231 ]; then + if [] "${systemd_version}" -lt 231 ]; then printf "\033[31m systemd version %s is less then 231, fixing the service file \033[0m\n" "${systemd_version}" sed -i "s/=+/=/g" /etc/systemd/service/unpoller.service fi From fcaf4958fe58941ecf6461d7e4b6295192e54c1e Mon Sep 17 00:00:00 2001 From: Cody Lee Date: Sun, 4 Dec 2022 09:02:40 -0600 Subject: [PATCH 3/3] fix location --- .goreleaser.yaml | 2 +- scripts/post-install.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index f4636441..80488465 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -286,7 +286,7 @@ nfpms: # systemd service - src: init/systemd/unpoller.service - dst: /etc/systemd/service/unpoller.service + dst: /etc/systemd/system/unpoller.service type: config # freebsd rc service diff --git a/scripts/post-install.sh b/scripts/post-install.sh index 289b9280..33598fbd 100755 --- a/scripts/post-install.sh +++ b/scripts/post-install.sh @@ -12,7 +12,7 @@ fi cleanup() { # This is where you remove files that were not needed on this platform / system if [ "${use_systemctl}" = "False" ]; then - rm -f /etc/systemd/service/unpoller.service + rm -f /etc/systemd/system/unpoller.service else rm -f /etc/chkconfig/unpoller rm -f /etc/init.d/unpoller @@ -51,7 +51,7 @@ cleanInstall() { # even if you want your service to run as non root. if [] "${systemd_version}" -lt 231 ]; then printf "\033[31m systemd version %s is less then 231, fixing the service file \033[0m\n" "${systemd_version}" - sed -i "s/=+/=/g" /etc/systemd/service/unpoller.service + sed -i "s/=+/=/g" /etc/systemd/system/unpoller.service fi printf "\033[32m Reload the service unit from disk\033[0m\n" systemctl daemon-reload ||: