From 71e5024d7a90a63e934ab935c1ef9f7d9ff75222 Mon Sep 17 00:00:00 2001 From: MichaIng Date: Tue, 21 Feb 2023 16:40:38 +0100 Subject: [PATCH] Packaging: Stop and disable service on DEB package removal (#59580) * Stop and disable service on DEB package removal The condition for this code to run applies on package removal only, not on upgrade or reinstall which generally invoke the prerm script as well. Currently the service keeps running after package removal, failing at some point due to missing files. And if enabled, the attempted service start on every boot is doomed to fail. If the invoke-rc.d command exists, it can be assumed that update-rc.d exists as well, as both are part of the same init-system-helpers package in dpkg distro repositories. If neither systemd, nor the init system helper package is installed, the service is not tried to be disabled, since we cannot know a safe method to do so. Compared to the postinst script, "set -e" is skipped here, since we do not run any command which is allowed to fail the whole package removal. Signed-off-by: MichaIng * Add postrm script to package build Signed-off-by: MichaIng * Remove redundant check Co-authored-by: Dan Cech --------- Signed-off-by: MichaIng Co-authored-by: Dan Cech --- packaging/deb/control/postinst | 2 +- packaging/deb/control/prerm | 17 +++++++++++++++++ pkg/build/packaging/grafana.go | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packaging/deb/control/prerm diff --git a/packaging/deb/control/postinst b/packaging/deb/control/postinst index bb7a040a5fd..2245b56c131 100755 --- a/packaging/deb/control/postinst +++ b/packaging/deb/control/postinst @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env sh set -e diff --git a/packaging/deb/control/prerm b/packaging/deb/control/prerm new file mode 100644 index 00000000000..21bfb065a37 --- /dev/null +++ b/packaging/deb/control/prerm @@ -0,0 +1,17 @@ +#!/usr/bin/env sh + +if [ "$1" = 'remove' ] +then + echo 'Stopping and disabling grafana-server service...' + if command -v systemctl >/dev/null; then + systemctl stop grafana-server || true + systemctl disable grafana-server || true + elif [ -x '/etc/init.d/grafana-server' ]; then + if command -v invoke-rc.d >/dev/null; then + invoke-rc.d grafana-server stop || true + update-rc.d -f grafana-server remove || true + else + /etc/init.d/grafana-server stop || true + fi + fi +fi diff --git a/pkg/build/packaging/grafana.go b/pkg/build/packaging/grafana.go index 4500f72f569..3537b2cd335 100644 --- a/pkg/build/packaging/grafana.go +++ b/pkg/build/packaging/grafana.go @@ -379,6 +379,9 @@ func executeFPM(options linuxPackageOptions, packageRoot, srcDir string) error { "--vendor", vendor, "-a", string(options.packageArch), } + if options.prermSrc != "" { + args = append(args, "--before-remove", options.prermSrc) + } if options.edition == config.EditionEnterprise || options.edition == config.EditionEnterprise2 || options.goArch == config.ArchARMv6 { args = append(args, "--conflicts", "grafana") } @@ -727,6 +730,7 @@ func realPackageVariant(ctx context.Context, v config.Variant, edition config.Ed initdScriptFilePath: "/etc/init.d/grafana-server", systemdServiceFilePath: "/usr/lib/systemd/system/grafana-server.service", postinstSrc: filepath.Join(grafanaDir, "packaging", "deb", "control", "postinst"), + prermSrc: filepath.Join(grafanaDir, "packaging", "deb", "control", "prerm"), initdScriptSrc: filepath.Join(grafanaDir, "packaging", "deb", "init.d", "grafana-server"), defaultFileSrc: filepath.Join(grafanaDir, "packaging", "deb", "default", "grafana-server"), systemdFileSrc: filepath.Join(grafanaDir, "packaging", "deb", "systemd", "grafana-server.service"), @@ -843,6 +847,7 @@ type linuxPackageOptions struct { initdScriptFilePath string systemdServiceFilePath string postinstSrc string + prermSrc string initdScriptSrc string defaultFileSrc string systemdFileSrc string