From f0252fba146617f386e676c0b11cc85f879d14f1 Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Tue, 23 Feb 2016 18:25:07 +0100 Subject: [PATCH] Improved install script for packaged telegraf: * Start/stop service on Debian/Ubuntu * Disable init-script/Systemd-unit on package removal --- scripts/build.py | 6 ++++++ scripts/post-install.sh | 3 +++ scripts/post-remove.sh | 46 +++++++++++++++++++++++++++++++++++++++++ scripts/pre-remove.sh | 15 ++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 scripts/post-remove.sh create mode 100644 scripts/pre-remove.sh diff --git a/scripts/build.py b/scripts/build.py index 15d7e8e41..0998bb7df 100755 --- a/scripts/build.py +++ b/scripts/build.py @@ -31,6 +31,8 @@ DEFAULT_CONFIG = "etc/telegraf.conf" DEFAULT_WINDOWS_CONFIG = "etc/telegraf_windows.conf" POSTINST_SCRIPT = "scripts/post-install.sh" PREINST_SCRIPT = "scripts/pre-install.sh" +POSTREMOVE_SCRIPT = "scripts/post-remove.sh" +PREREMOVE_SCRIPT = "scripts/pre-remove.sh" # Default AWS S3 bucket for uploads DEFAULT_BUCKET = "get.influxdb.org/telegraf" @@ -61,6 +63,8 @@ fpm_common_args = "-f -s dir --log error \ --config-files {} \ --after-install {} \ --before-install {} \ + --after-remove {} \ + --before-remove {} \ --description \"{}\"".format( VENDOR, PACKAGE_URL, @@ -70,6 +74,8 @@ fpm_common_args = "-f -s dir --log error \ LOGROTATE_DIR + '/telegraf', POSTINST_SCRIPT, PREINST_SCRIPT, + POSTREMOVE_SCRIPT, + PREREMOVE_SCRIPT, DESCRIPTION) targets = { diff --git a/scripts/post-install.sh b/scripts/post-install.sh index 0982dc855..d4c5df443 100644 --- a/scripts/post-install.sh +++ b/scripts/post-install.sh @@ -13,6 +13,7 @@ function install_init { function install_systemd { cp -f $SCRIPT_DIR/telegraf.service /lib/systemd/system/telegraf.service systemctl enable telegraf + systemctl daemon-reload || true } function install_update_rcd { @@ -63,10 +64,12 @@ elif [[ -f /etc/debian_version ]]; then which systemctl &>/dev/null if [[ $? -eq 0 ]]; then install_systemd + deb-systemd-invoke restart telegraf.service else # Assuming sysv install_init install_update_rcd + invoke-rc.d telegraf restart fi elif [[ -f /etc/os-release ]]; then source /etc/os-release diff --git a/scripts/post-remove.sh b/scripts/post-remove.sh new file mode 100644 index 000000000..96b178f4d --- /dev/null +++ b/scripts/post-remove.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +function disable_systemd { + systemctl disable telegraf + rm -f /lib/systemd/system/telegraf.service +} + +function disable_update_rcd { + update-rc.d -f telegraf remove + rm -f /etc/init.d/telegraf +} + +function disable_chkconfig { + chkconfig --del telegraf + rm -f /etc/init.d/telegraf +} + +if [[ -f /etc/redhat-release ]]; then + # RHEL-variant logic + if [[ "$1" = "0" ]]; then + # InfluxDB is no longer installed, remove from init system + rm -f /etc/default/telegraf + + which systemctl &>/dev/null + if [[ $? -eq 0 ]]; then + disable_systemd + else + # Assuming sysv + disable_chkconfig + fi + fi +elif [[ -f /etc/debian_version ]]; then + # Debian/Ubuntu logic + if [[ "$1" != "upgrade" ]]; then + # Remove/purge + rm -f /etc/default/telegraf + + which systemctl &>/dev/null + if [[ $? -eq 0 ]]; then + disable_systemd + else + # Assuming sysv + disable_update_rcd + fi + fi +fi diff --git a/scripts/pre-remove.sh b/scripts/pre-remove.sh new file mode 100644 index 000000000..a57184630 --- /dev/null +++ b/scripts/pre-remove.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +BIN_DIR=/usr/bin + +# Distribution-specific logic +if [[ -f /etc/debian_version ]]; then + # Debian/Ubuntu logic + which systemctl &>/dev/null + if [[ $? -eq 0 ]]; then + deb-systemd-invoke stop telegraf.service + else + # Assuming sysv + invoke-rc.d telegraf stop + fi +fi