Last active
June 2, 2021 17:35
-
-
Save archisgore/49f0e0021dea33b8cc84d3c1d2679f30 to your computer and use it in GitHub Desktop.
Downgrades all packages on a yum/redhat based system to oldest possible versions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
install_version() { | |
# This would be: NetworkManager.x86_64 1:1.14.0-14.el8 rhel-8-baseos-rhui-rpms | |
version_entry="$1" | |
echo " Attempting to install version entry: $version_entry" | |
# This would be: NetworkManager.x86_64 | |
package_arch="$(echo $version_entry | awk -F" " '{print $1}')" | |
# This would be: NetworkManager (we remove the .x86_64 or any architecture) | |
package="$(echo "$package_arch" | rev | cut -d'.' -f2- | rev)" | |
# This would be: 1:1.14.0-14.el8 | |
version_with_redhat_embellishment="$(echo $version_entry | awk -F" " '{print $2}')" | |
# This would be: 1.14.0-14.el8 | |
version=$(echo "$version_with_redhat_embellishment" | cut -d':' -f2-) | |
# This should be: NetworkManager-1.14.0-14.el8 | |
package_version="$package-$version" | |
echo " Normalized install package name: $package_version" | |
yum downgrade -y $package_version 1>/dev/null 2>/dev/null | |
[ $? -ne 0 ] && echo " FAIL!" && return 1 | |
echo " SUCCESS!" | |
} | |
install_oldest_version() { | |
package="$1" | |
echo " Downgrading package $package..." | |
output=$(yum --showduplicates list available $package 2>/dev/null) | |
#Set the field separator to new line | |
IFS=$'\n' | |
found_available_packages="" | |
oldest_package_line="" | |
for line in $output; do | |
if [ "$found_available_packages" == "true" ]; then | |
install_version "$line" | |
[ $? -eq 0 ] && break | |
elif [[ "$line" == *"Available Packages"* ]]; then | |
found_available_packages="true" | |
fi | |
done | |
} | |
skip_package() { | |
package="$1" | |
# skip authselect since it can prevent any logins, especially on EC2. | |
if [[ "$package" == *"authselect"* ]]; then | |
return 1 | |
fi | |
} | |
echo "Downgrades all packages to oldest possible versions." | |
echo "Finding installed packages..." | |
installed=$(yum list installed | awk -F" " '{print $1}') | |
echo " Found $(echo $installed | wc -c) packages installed." | |
echo "" | |
echo "For each package, attempting to downgrade to oldest possible version..." | |
for package in $installed; do | |
# should this package be skipped for whatever reason? | |
skip_package "$package" | |
[ $? -ne 0 ] && echo "Skipping package $package" && continue | |
install_oldest_version "$package" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment