From 19ccbed59d59ae763212e945e526f3d6a72fc137 Mon Sep 17 00:00:00 2001 From: Craig Comstock Date: Fri, 17 Oct 2025 14:51:41 -0500 Subject: [PATCH] Adjusted detection of systemd in package scriptlets to handle more valid states In the case where a package is installed but not bootstrapped several of the components: cf-execd, cf-monitord, cf-serverd will not be able to start due to lack of ${sys.inputs}/promises.cf. This will cause the `systemctl is-system-running` command to return `degraded` and a non-zero exit code. Previously this would cause the cfengine3 service to not be enabled as part of the package install and so on reboots services would not be running. Ticket: ENT-13277 Changelog: title --- packaging/common/cfengine-hub/postinstall.sh | 4 ++-- packaging/common/cfengine-hub/preinstall.sh | 2 +- packaging/common/cfengine-hub/preremove.sh | 2 +- .../common/cfengine-non-hub/postinstall.sh | 4 ++-- .../common/cfengine-non-hub/preremove.sh | 2 +- packaging/common/produce-script | 2 ++ .../script-templates/deb-script-common.sh | 2 +- .../script-templates/rpm-script-common.sh | 2 +- .../common/script-templates/script-common.sh | 4 ++-- .../common/script-templates/script-header.sh | 19 +++++++++++++++++++ 10 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 packaging/common/script-templates/script-header.sh diff --git a/packaging/common/cfengine-hub/postinstall.sh b/packaging/common/cfengine-hub/postinstall.sh index 69331397e..7723150d1 100644 --- a/packaging/common/cfengine-hub/postinstall.sh +++ b/packaging/common/cfengine-hub/postinstall.sh @@ -29,7 +29,7 @@ systemctl restart cfengine3" fi fi -if command -v systemctl 2>/dev/null && systemctl is-system-running; then +if use_systemd; then # This is important in case any of the units have been replaced by the package # and we call them in the postinstall script. if ! /bin/systemctl daemon-reload; then @@ -1073,7 +1073,7 @@ chmod g+rX "$PREFIX/httpd/php" # Register CFEngine initscript, if not yet. # if ! is_upgrade; then - if command -v systemctl 2>/dev/null && systemctl is-system-running; then + if use_systemd; then # Reload systemd config to pick up newly installed units /bin/systemctl daemon-reload > /dev/null 2>&1 # Enable cfengine3 service (starts all the other services) diff --git a/packaging/common/cfengine-hub/preinstall.sh b/packaging/common/cfengine-hub/preinstall.sh index 91f02ced1..bd56143e6 100644 --- a/packaging/common/cfengine-hub/preinstall.sh +++ b/packaging/common/cfengine-hub/preinstall.sh @@ -136,7 +136,7 @@ fi if is_upgrade; then cf_console platform_service cfengine3 stop # CFE-2278: Migrate to split units - if [ -x /bin/systemctl ] && [ -e /usr/lib/systemd/system/cfengine3-web.service ]; then + if use_systemd && [ -e /usr/lib/systemd/system/cfengine3-web.service ]; then # When using systemd, the services are split in two, and although both will # stop due to the command above, the web part may only do so after some # delay, which may cause problems in an upgrade situation, since this script diff --git a/packaging/common/cfengine-hub/preremove.sh b/packaging/common/cfengine-hub/preremove.sh index 79dc9e220..46e6bfb0f 100644 --- a/packaging/common/cfengine-hub/preremove.sh +++ b/packaging/common/cfengine-hub/preremove.sh @@ -1,5 +1,5 @@ cf_console platform_service cfengine3 stop -if [ -x /bin/systemctl ] && [ -e /usr/lib/systemd/system/cfengine3-web.service ]; then +if use_systemd && [ -e /usr/lib/systemd/system/cfengine3-web.service ]; then # When using systemd, the services are split in two, and although both will # stop due to the command above, the web part may only do so after some # delay, which may cause problems later if the binaries are gone by the time diff --git a/packaging/common/cfengine-non-hub/postinstall.sh b/packaging/common/cfengine-non-hub/postinstall.sh index b1a10ce7d..6280ab553 100644 --- a/packaging/common/cfengine-non-hub/postinstall.sh +++ b/packaging/common/cfengine-non-hub/postinstall.sh @@ -1,4 +1,4 @@ -if command -v systemctl 2>/dev/null && systemctl is-system-running; then +if use_systemd; then # This is important in case any of the units have been replaced by the package # and we call them in the postinstall script. if ! /bin/systemctl daemon-reload; then @@ -69,7 +69,7 @@ case `os_type` in # # Register CFEngine initscript, if not yet. # - if command -v systemctl 2>/dev/null && systemctl is-system-running; then + if use_systemd; then # Reload systemd config to pick up newly installed units /bin/systemctl daemon-reload > /dev/null 2>&1 # Enable cfengine3 service (starts all the other services) diff --git a/packaging/common/cfengine-non-hub/preremove.sh b/packaging/common/cfengine-non-hub/preremove.sh index f0b8093bb..bac22a095 100644 --- a/packaging/common/cfengine-non-hub/preremove.sh +++ b/packaging/common/cfengine-non-hub/preremove.sh @@ -10,7 +10,7 @@ case `os_type` in # # systemd support # - test -x /bin/systemctl && systemctl disable cfengine3.service > /dev/null 2>&1 + use_systemd && systemctl disable cfengine3.service > /dev/null 2>&1 # # Clean lock files created by initscript, if any diff --git a/packaging/common/produce-script b/packaging/common/produce-script index bb500126a..c7ac8d365 100755 --- a/packaging/common/produce-script +++ b/packaging/common/produce-script @@ -6,6 +6,7 @@ # type = # action = # +# script-header.sh # -script-common.sh # -script-common-.sh # script-common.sh @@ -33,6 +34,7 @@ include_script() fi } +include_script "$TEMPLATEDIR/script-header.sh" include_script "$TEMPLATEDIR/$PKG_TYPE-script-common.sh" case "$PKG_TYPE" in diff --git a/packaging/common/script-templates/deb-script-common.sh b/packaging/common/script-templates/deb-script-common.sh index dac4d5270..e0075649e 100644 --- a/packaging/common/script-templates/deb-script-common.sh +++ b/packaging/common/script-templates/deb-script-common.sh @@ -19,7 +19,7 @@ rc_d_path() platform_service() { - if [ -x /bin/systemctl ]; then + if use_systemd; then /bin/systemctl "$2" "$1".service else /etc/init.d/"$1" "$2" diff --git a/packaging/common/script-templates/rpm-script-common.sh b/packaging/common/script-templates/rpm-script-common.sh index 0fdd5ef8b..9a220d172 100644 --- a/packaging/common/script-templates/rpm-script-common.sh +++ b/packaging/common/script-templates/rpm-script-common.sh @@ -27,7 +27,7 @@ rc_d_path() platform_service() { - if [ -x /bin/systemctl ]; then + if use_systemd; then /bin/systemctl "$2" "$1".service else `rc_d_path`/init.d/"$1" "$2" diff --git a/packaging/common/script-templates/script-common.sh b/packaging/common/script-templates/script-common.sh index 3c9037c4b..4b7e5b9ae 100644 --- a/packaging/common/script-templates/script-common.sh +++ b/packaging/common/script-templates/script-common.sh @@ -29,7 +29,7 @@ case "$PKG_TYPE" in esac get_cfengine_state() { - if type systemctl >/dev/null 2>&1; then + if use_systemd; then systemctl list-units -l | sed -r -e '/^\s*(cf-[-a-z]+|cfengine3)\.service/!d' -e 's/\s*(cf-[-a-z]+|cfengine3)\.service.*/\1/' else platform_service cfengine3 status | awk '/is running/ { print $1 }' @@ -39,7 +39,7 @@ get_cfengine_state() { restore_cfengine_state() { # $1 -- file where the state to restore is saved (see get_cfengine_state()) - if type systemctl >/dev/null 2>&1; then + if use_systemd; then for service in `cat "$1"`; do definition=`systemctl cat "$service"` || continue # only try to start service that are defined/exist (some may be gone diff --git a/packaging/common/script-templates/script-header.sh b/packaging/common/script-templates/script-header.sh new file mode 100644 index 000000000..764a54336 --- /dev/null +++ b/packaging/common/script-templates/script-header.sh @@ -0,0 +1,19 @@ +USE_SYSTEMD=0 +_use_systemd=$(command -v systemctl 2>&1 >/dev/null && systemctl is-system-running) +case "$_use_systemd" in + offline|unknown) + USE_SYSTEMD=0 + ;; + "") + USE_SYSTEMD=0 + ;; + *) + USE_SYSTEMD=1 + ;; +esac + +use_systemd() +{ + test $USE_SYSTEMD = 1 +} +