Refactor dependency installation logic to use a fallback mechanism for package installation across different OS types, lttng-ust1 & ttng-ust

This commit is contained in:
Salman Muin Kayser Chishti
2025-08-30 05:35:51 +01:00
parent 65dfa460ba
commit f80ce89387

View File

@@ -36,6 +36,25 @@ function print_rhel6errormessage()
echo "https://github.com/dotnet/core/blob/master/Documentation/build-and-install-rhel6-prerequisites.md" echo "https://github.com/dotnet/core/blob/master/Documentation/build-and-install-rhel6-prerequisites.md"
} }
function install_with_fallbacks() {
local install_cmd="$1"
shift
local packages=("$@")
for package in "${packages[@]}"; do
echo "Trying to install: $package"
if eval "$install_cmd $package"; then
echo "Successfully installed: $package"
return 0
else
echo "Failed to install: $package, trying next option..."
fi
done
echo "All installation attempts failed for package alternatives: ${packages[*]}"
return 1
}
if [ -e /etc/os-release ] if [ -e /etc/os-release ]
then then
echo "--------OS Information--------" echo "--------OS Information--------"
@@ -50,73 +69,42 @@ then
echo "------------------------------" echo "------------------------------"
# prefer apt-get over apt # prefer apt-get over apt
command -v apt-get if command -v apt-get > /dev/null 2>&1; then
if [ $? -eq 0 ] apt_get="apt-get"
then elif command -v apt > /dev/null 2>&1; then
apt_get=apt-get apt_get="apt"
else else
command -v apt echo "Found neither 'apt-get' nor 'apt'"
if [ $? -eq 0 ] print_errormessage
then exit 1
apt_get=apt
else
echo "Found neither 'apt-get' nor 'apt'"
print_errormessage
exit 1
fi
fi fi
# Install basic dependencies
$apt_get update && $apt_get install -y libkrb5-3 zlib1g $apt_get update && $apt_get install -y libkrb5-3 zlib1g
if [ $? -ne 0 ] if [ $? -ne 0 ]; then
then echo "'$apt_get' failed"
echo "'$apt_get' failed with exit code '$?'"
print_errormessage print_errormessage
exit 1 exit 1
fi fi
apt_get_with_fallbacks() { # Install lttng with fallbacks
$apt_get install -y $1 if ! install_with_fallbacks "$apt_get install -y" "liblttng-ust1" "liblttng-ust0"; then
fail=$?
if [ $fail -eq 0 ]
then
if [ "${1#"${1%?}"}" = '$' ]; then
dpkg -l "${1%?}" > /dev/null 2> /dev/null
fail=$?
fi
fi
if [ $fail -ne 0 ]
then
shift
if [ -n "$1" ]
then
apt_get_with_fallbacks "$@"
fi
fi
}
apt_get_with_fallbacks liblttng-ust1 liblttng-ust0
if [ $? -ne 0 ]
then
echo "'$apt_get' failed with exit code '$?'"
print_errormessage print_errormessage
exit 1 exit 1
fi fi
apt_get_with_fallbacks libssl1.1$ libssl1.0.2$ libssl1.0.0$ # Install SSL with fallbacks
if [ $? -ne 0 ] if ! install_with_fallbacks "$apt_get install -y" "libssl1.1" "libssl1.0.2" "libssl1.0.0"; then
then
echo "'$apt_get' failed with exit code '$?'"
print_errormessage print_errormessage
exit 1 exit 1
fi fi
apt_get_with_fallbacks libicu72 libicu71 libicu70 libicu69 libicu68 libicu67 libicu66 libicu65 libicu63 libicu60 libicu57 libicu55 libicu52 # Install ICU with fallbacks
if [ $? -ne 0 ] if ! install_with_fallbacks "$apt_get install -y" "libicu72" "libicu71" "libicu70" "libicu69" "libicu68" "libicu67" "libicu66" "libicu65" "libicu63" "libicu60" "libicu57" "libicu55" "libicu52"; then
then
echo "'$apt_get' failed with exit code '$?'"
print_errormessage print_errormessage
exit 1 exit 1
fi fi
elif [ -e /etc/redhat-release ] elif [ -e /etc/redhat-release ]
then then
echo "The current OS is Fedora based" echo "The current OS is Fedora based"
@@ -124,65 +112,75 @@ then
cat /etc/redhat-release cat /etc/redhat-release
echo "------------------------------" echo "------------------------------"
# use dnf on fedora if [ -e /etc/fedora-release ]; then
# use yum on centos and rhel # Fedora - use dnf
if [ -e /etc/fedora-release ] if ! command -v dnf > /dev/null 2>&1; then
then
command -v dnf
if [ $? -eq 0 ]
then
dnf install -y lttng-ust openssl-libs krb5-libs zlib libicu
if [ $? -ne 0 ]
then
echo "'dnf' failed with exit code '$?'"
print_errormessage
exit 1
fi
else
echo "Can not find 'dnf'" echo "Can not find 'dnf'"
print_errormessage print_errormessage
exit 1 exit 1
fi fi
# Install lttng with fallbacks
if ! install_with_fallbacks "dnf install -y" "lttng-ust1" "lttng-ust"; then
print_errormessage
exit 1
fi
# Install other dependencies
dnf install -y openssl-libs krb5-libs zlib libicu
if [ $? -ne 0 ]; then
echo "'dnf' failed"
print_errormessage
exit 1
fi
else else
command -v yum # RHEL/CentOS - use yum
if [ $? -eq 0 ] if ! command -v yum > /dev/null 2>&1; then
then
yum install -y lttng-ust openssl-libs krb5-libs zlib libicu
if [ $? -ne 0 ]
then
echo "'yum' failed with exit code '$?'"
print_errormessage
exit 1
fi
else
echo "Can not find 'yum'" echo "Can not find 'yum'"
print_errormessage print_errormessage
exit 1 exit 1
fi fi
# Install lttng with fallbacks
if ! install_with_fallbacks "yum install -y" "lttng-ust1" "lttng-ust"; then
print_errormessage
exit 1
fi
# Install other dependencies
yum install -y openssl-libs krb5-libs zlib libicu
if [ $? -ne 0 ]; then
echo "'yum' failed"
print_errormessage
exit 1
fi
fi fi
else else
# we might on OpenSUSE # we might be on OpenSUSE
OSTYPE=$(grep ID_LIKE /etc/os-release | cut -f2 -d=) OSTYPE=$(grep ID_LIKE /etc/os-release | cut -f2 -d=)
echo $OSTYPE echo $OSTYPE
echo $OSTYPE | grep "suse" if echo $OSTYPE | grep -q "suse"; then
if [ $? -eq 0 ]
then
echo "The current OS is SUSE based" echo "The current OS is SUSE based"
command -v zypper
if [ $? -eq 0 ] if ! command -v zypper > /dev/null 2>&1; then
then
zypper -n install lttng-ust libopenssl1_1 krb5 zlib libicu60_2
if [ $? -ne 0 ]
then
echo "'zypper' failed with exit code '$?'"
print_errormessage
exit 1
fi
else
echo "Can not find 'zypper'" echo "Can not find 'zypper'"
print_errormessage print_errormessage
exit 1 exit 1
fi fi
# Install lttng with fallbacks
if ! install_with_fallbacks "zypper -n install" "lttng-ust1" "lttng-ust"; then
print_errormessage
exit 1
fi
# Install other dependencies
zypper -n install libopenssl1_1 krb5 zlib libicu60_2
if [ $? -ne 0 ]; then
echo "'zypper' failed"
print_errormessage
exit 1
fi
else else
echo "Can't detect current OS type based on /etc/os-release." echo "Can't detect current OS type based on /etc/os-release."
print_errormessage print_errormessage