From a91ea982b21dc4d9aa6b390df4e77e406ce415dd Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Mon, 22 Aug 2022 12:57:38 -0600 Subject: [PATCH] fix: use os-agnositc systemd detection, remove sysv in RPM packaging (#10570) --- scripts/rpm/post-install.sh | 65 +++++-------------------------------- scripts/rpm/post-remove.sh | 64 ++++++++---------------------------- 2 files changed, 22 insertions(+), 107 deletions(-) diff --git a/scripts/rpm/post-install.sh b/scripts/rpm/post-install.sh index 2bdb577be..3f0e2fe37 100644 --- a/scripts/rpm/post-install.sh +++ b/scripts/rpm/post-install.sh @@ -1,29 +1,5 @@ #!/bin/bash -BIN_DIR=/usr/bin -LOG_DIR=/var/log/telegraf -SCRIPT_DIR=/usr/lib/telegraf/scripts -LOGROTATE_DIR=/etc/logrotate.d - -function install_init { - cp -f $SCRIPT_DIR/init.sh /etc/init.d/telegraf - chmod +x /etc/init.d/telegraf -} - -function install_systemd { - cp -f $SCRIPT_DIR/telegraf.service $1 - systemctl enable telegraf || true - systemctl daemon-reload || true -} - -function install_update_rcd { - update-rc.d telegraf defaults -} - -function install_chkconfig { - chkconfig --add telegraf -} - # Remove legacy symlink, if it exists if [[ -L /etc/init.d/telegraf ]]; then rm -f /etc/init.d/telegraf @@ -48,41 +24,16 @@ if [[ ! -f /etc/telegraf/telegraf.conf ]] && [[ -f /etc/telegraf/telegraf.conf.s cp /etc/telegraf/telegraf.conf.sample /etc/telegraf/telegraf.conf fi +# Set up log directories +LOG_DIR=/var/log/telegraf test -d $LOG_DIR || mkdir -p $LOG_DIR chown -R -L telegraf:telegraf $LOG_DIR chmod 755 $LOG_DIR -# Distribution-specific logic -if [[ -f /etc/redhat-release ]] || [[ -f /etc/SuSE-release ]]; then - # RHEL-variant logic - if [[ "$(readlink /proc/1/exe)" == */systemd ]]; then - install_systemd /usr/lib/systemd/system/telegraf.service - else - # Assuming SysVinit - install_init - # Run update-rc.d or fallback to chkconfig if not available - if which update-rc.d &>/dev/null; then - install_update_rcd - else - install_chkconfig - fi - fi -elif [[ -f /etc/os-release ]]; then - source /etc/os-release - if [[ "$NAME" = "Amazon Linux" ]]; then - # Amazon Linux 2+ logic - install_systemd /usr/lib/systemd/system/telegraf.service - elif [[ "$NAME" = "Amazon Linux AMI" ]]; then - # Amazon Linux logic - install_init - # Run update-rc.d or fallback to chkconfig if not available - if which update-rc.d &>/dev/null; then - install_update_rcd - else - install_chkconfig - fi - elif [[ "$NAME" = "Solus" ]]; then - # Solus logic - install_systemd /usr/lib/systemd/system/telegraf.service - fi +# Set up systemd service - check if the systemd directory exists per: +# https://www.freedesktop.org/software/systemd/man/sd_booted.html +if [[ -d /run/systemd/system ]]; then + cp -f /usr/lib/telegraf/scripts/telegraf.service /usr/lib/systemd/system/telegraf.service + systemctl enable telegraf + systemctl daemon-reload fi diff --git a/scripts/rpm/post-remove.sh b/scripts/rpm/post-remove.sh index 08eb190d3..0d35122cb 100644 --- a/scripts/rpm/post-remove.sh +++ b/scripts/rpm/post-remove.sh @@ -1,55 +1,19 @@ #!/bin/bash -function disable_systemd { - systemctl disable telegraf - rm -f $1 -} +# Telegraf is no longer installed, remove from systemd +if [[ "$1" = "0" ]]; then + rm -f /etc/default/telegraf -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 ]] || [[ -f /etc/SuSE-release ]]; then - # RHEL-variant logic - if [[ "$1" = "0" ]]; then - # InfluxDB is no longer installed, remove from init system - rm -f /etc/default/telegraf - - if [[ "$(readlink /proc/1/exe)" == */systemd ]]; then - disable_systemd /usr/lib/systemd/system/telegraf.service - else - # Assuming sysv - disable_chkconfig - fi - fi - if [[ $1 -ge 1 ]]; then - # Package upgrade, not uninstall - - if [[ "$(readlink /proc/1/exe)" == */systemd ]]; then - systemctl try-restart telegraf.service >/dev/null 2>&1 || : - fi - fi -elif [[ -f /etc/os-release ]]; then - source /etc/os-release - if [[ "$ID" = "amzn" ]] && [[ "$1" = "0" ]]; then - # InfluxDB is no longer installed, remove from init system - rm -f /etc/default/telegraf - - if [[ "$NAME" = "Amazon Linux" ]]; then - # Amazon Linux 2+ logic - disable_systemd /usr/lib/systemd/system/telegraf.service - elif [[ "$NAME" = "Amazon Linux AMI" ]]; then - # Amazon Linux logic - disable_chkconfig - fi - elif [[ "$NAME" = "Solus" ]]; then - rm -f /etc/default/telegraf - disable_systemd /usr/lib/systemd/system/telegraf.service + if [[ -d /run/systemd/system ]]; then + systemctl disable telegraf + rm -f /usr/lib/systemd/system/telegraf.service + systemctl daemon-reload + fi +fi + +# Telegraf upgrade, restart service +if [[ $1 -ge 1 ]]; then + if [[ -d /run/systemd/system ]]; then + systemctl try-restart telegraf.service >/dev/null 2>&1 || : fi fi