ctdb-scripts: Update detect_init_style to use /etc/os-release
authorMartin Schwenke <mschwenke@ddn.com>
Tue, 19 Sep 2023 07:34:55 +0000 (17:34 +1000)
committerAmitay Isaacs <amitay@samba.org>
Mon, 30 Oct 2023 09:19:11 +0000 (09:19 +0000)
/etc/os-release is quite universal.  It can be found on most Linux
distros and on FreeBSD.

Attempt to use /etc/os-release to detect Red Hat, SUSE and Debian
based distros.  If /etc/os-release exists but distro is unknown then
$ID is printed as the detected distro, which will probably result in
sub-optimal behaviour, but when tracing it will at least indicate that
a new distro needs to be handled.

The only way to handle missing /etc/os-release is to set
CTDB_INIT_STYLE - see ctdb.sysconfig(5) for details.

The event script unit tests are updated to use /etc/os-release so
the new logic is exercised.

Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
Autobuild-User(master): Amitay Isaacs <amitay@samba.org>
Autobuild-Date(master): Mon Oct 30 09:19:11 UTC 2023 on atb-devel-224

ctdb/config/functions
ctdb/tests/UNIT/eventscripts/etc-ctdb/rc.local
ctdb/tests/UNIT/eventscripts/etc/os-release [new file with mode: 0644]

index d8f7f57b84c8fb0b3f31612bf402db66a2f83163..a40b276e2b854da3794caa139b1b97d4c0f77213 100755 (executable)
@@ -160,18 +160,48 @@ ctdb_check_args()
 # determine on what type of system (init style) we are running
 detect_init_style()
 {
-       # only do detection if not already set:
-       if [ -n "$CTDB_INIT_STYLE" ]; then
-               return
-       fi
+       _init_style_file="${CTDB_SCRIPT_VARDIR}/init-style"
 
-       if [ -x /sbin/startproc ]; then
-               CTDB_INIT_STYLE="suse"
-       elif [ -x /sbin/start-stop-daemon ]; then
-               CTDB_INIT_STYLE="debian"
-       else
-               CTDB_INIT_STYLE="redhat"
+       if [ ! -f "$_init_style_file" ]; then
+               if [ -n "$CTDB_INIT_STYLE" ]; then
+                       echo "$CTDB_INIT_STYLE" >"$_init_style_file"
+                       return
+               fi
+
+               # Subshell to contain variables in os-release file
+               (
+                       _os_release="${CTDB_SYS_ETCDIR}/os-release"
+                       if [ -f "$_os_release" ]; then
+                               . "$_os_release"
+                               case "$ID" in
+                               centos | fedora | rhel)
+                                       echo "redhat"
+                                       ;;
+                               debian | ubuntu)
+                                       echo "debian"
+                                       ;;
+                               sles | suse)
+                                       echo "suse"
+                                       ;;
+                               *)
+                                       case "$ID_LIKE" in
+                                       *centos* | *rhel*)
+                                               echo "redhat"
+                                               ;;
+                                       *)
+                                               echo "$ID"
+                                               ;;
+                                       esac
+                                       ;;
+                               esac
+                       else
+                               echo "WARNING: unknown distribution ${ID}" >&2
+                               echo "unknown"
+                       fi
+               ) >"$_init_style_file"
        fi
+
+       read -r CTDB_INIT_STYLE <"$_init_style_file"
 }
 
 ######################################################
index aa9b8b22fecd75fcebe3168491e600050e7a83b7..777aeaff8b3159867cf488666603198ca62d8210 100755 (executable)
@@ -51,4 +51,6 @@ background_with_logging ()
     "$@" 2>&1 </dev/null | sed -e 's@^@\&@'
 }
 
-CTDB_INIT_STYLE="${EVENTSCRIPT_TESTS_INIT_STYLE:-redhat}"
+if [ -n "$EVENTSCRIPT_TESTS_INIT_STYLE" ]; then
+       CTDB_INIT_STYLE="$EVENTSCRIPT_TESTS_INIT_STYLE"
+fi
diff --git a/ctdb/tests/UNIT/eventscripts/etc/os-release b/ctdb/tests/UNIT/eventscripts/etc/os-release
new file mode 100644 (file)
index 0000000..f0057cc
--- /dev/null
@@ -0,0 +1,2 @@
+ID="rocky"
+ID_LIKE="rhel centos fedora"