Last active
June 5, 2021 16:15
-
-
Save archisgore/c61f57a920496fd5231d89cd92ddb138 to your computer and use it in GitHub Desktop.
Make everything old and install more oldest-possible-packages and start all services.
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 | |
local -r version_entry="$1" | |
# What yum operation do you want? | |
local -r operation="$2" | |
echo " Attempting to install version entry: $version_entry" | |
# This would be: NetworkManager.x86_64 | |
local -r package_arch="$(echo $version_entry | awk -F" " '{print $1}')" | |
# This would be: NetworkManager (we remove the .x86_64 or any architecture) | |
local -r package="$(echo "$package_arch" | rev | cut -d'.' -f2- | rev)" | |
# This would be: 1:1.14.0-14.el8 | |
local -r version_with_redhat_embellishment="$(echo $version_entry | awk -F" " '{print $2}')" | |
# This would be: 1.14.0-14.el8 | |
local -r version=$(echo "$version_with_redhat_embellishment" | cut -d':' -f2-) | |
# This should be: NetworkManager-1.14.0-14.el8 | |
local -r package_version="$package-$version" | |
echo " Normalized install package name: $package_version" | |
yum $operation -y $package_version 1>/dev/null 2>/dev/null | |
[ $? -ne 0 ] && echo " FAIL!" && return 1 | |
echo " SUCCESS!" | |
} | |
install_oldest_version() { | |
local -r package="$1" | |
# What operation is this? Install or downgrage? | |
local -r operation="$2" | |
echo " ${operation}ing oldest version of package $package..." | |
local -r output=$(yum --showduplicates list available $package 2>/dev/null) | |
#Set the field separator to new line | |
IFS=$'\n' | |
local found_available_packages="" | |
local oldest_package_line="" | |
local line | |
for line in $output; do | |
if [ "$found_available_packages" == "true" ]; then | |
install_version "$line" "$operation" | |
[ $? -eq 0 ] && return 0 | |
elif [[ "$line" == *"Available Packages"* ]]; then | |
found_available_packages="true" | |
fi | |
done | |
# Nothing installed | |
return 1 | |
} | |
skip_package() { | |
local -r package="$1" | |
# skip authselect since it can prevent any logins, especially on EC2. | |
if [[ "$package" == *"authselect"* ]]; then | |
return 1 | |
elif [[ "$package" == *"pam"* ]]; then | |
return 1 | |
fi | |
} | |
yum_downgrade_all() { | |
echo "Downgrading all packages to oldest possible versions." | |
echo " Finding installed packages..." | |
local -r installed=$(yum list installed | awk -F" " '{print $1}') | |
echo " Found $(echo $installed | wc -w) packages installed." | |
echo "" | |
echo " For each package, attempting to downgrade to oldest possible version..." | |
local something_changed="" | |
local package | |
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" "downgrade" | |
[ $? -eq 0 ] && something_changed="true" | |
# test ssh works | |
ssh -i PolyverseDevelopmentKey.pem ec2-user@localhost "exit" | |
[ $? -ne 0 ] && echo "SSH failed!!!" && exit 1 | |
done | |
if [[ "$something_changed" == "true" ]]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
yum_install_all() { | |
echo "Installing all available packages to oldest possible versions." | |
echo " Finding available packages..." | |
local -r available=$(yum list available | awk -F" " '{print $1}') | |
echo " Found $(echo $installed | wc -w) packages available." | |
echo " For each package, attempting to install oldest possible version..." | |
local something_changed="" | |
local package | |
for package in $available; do | |
# should this package be skipped for whatever reason? | |
skip_package "$package" | |
[ $? -ne 0 ] && echo " Skipping package $package" && continue | |
install_oldest_version "$package" "install" | |
[ $? -eq 0 ] && something_changed="true" | |
# test ssh works | |
ssh -i PolyverseDevelopmentKey.pem ec2-user@localhost "exit" | |
[ $? -ne 0 ] && echo "SSH failed!!!" && exit 1 | |
done | |
if [[ "$something_changed" == "true" ]]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
systemctl_start_all() { | |
echo "Starting all possible services..." | |
systemctl list-unit-files --type=service \ | |
| grep disabled \ | |
| awk -F" " '{print $1}' \ | |
| grep -v "initial-setup" \ | |
| grep -v "redboot-auto-reboot" \ | |
| grep -v "redboot-task-runner" \ | |
| grep -v "iptables" \ | |
| grep -v "ip6tables" \ | |
| grep -v "arptables" \ | |
| grep -v "inkscape" \ | |
| xargs -I % sh -c 'echo "Starting Service %" && systemctl start % && sleep 1' | |
} | |
echo "Making this system the oldest possible set of packages and maximum services." | |
yum_downgrade_all | |
[ $? -eq 0 ] && something_changed="true" | |
yum_install_all | |
[ $? -eq 0 ] && something_changed="true" | |
systemctl_start_all | |
[ $? -eq 0 ] && something_changed="true" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment