Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving logging and error readability for rke2-uninstall.sh script #6237

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
59 changes: 55 additions & 4 deletions bundle/bin/rke2-uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
#!/bin/sh
set -ex


# helper function for timestamped logging
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}

# helper function for logging error and exiting with a message
error() {
log "ERROR: $*" >&2
exit 1
}

# make sure we run as root
if [ ! $(id -u) -eq 0 ]; then
echo "$(basename "${0}"): must be run as root" >&2
exit 1
error "$(basename "$0"): must be run as root"
fi

# check_target_mountpoint return success if the target directory is on a dedicated mount point
Expand All @@ -21,6 +32,7 @@ check_target_ro() {
RKE2_DATA_DIR=${RKE2_DATA_DIR:-/var/lib/rancher/rke2}

. /etc/os-release

if [ -r /etc/redhat-release ] || [ -r /etc/centos-release ] || [ -r /etc/oracle-release ] || [ -r /etc/amazon-linux-release ]; then
# If redhat/oracle family os is detected, double check whether installation mode is yum or tar.
# yum method assumes installation root under /usr
Expand Down Expand Up @@ -50,13 +62,15 @@ fi
uninstall_killall()
{
_killall="$(dirname "$0")/rke2-killall.sh"
log "Running killall script"
if [ -e "${_killall}" ]; then
eval "${_killall}"
fi
}

uninstall_disable_services()
{
log "Disabling rke2 services"
if command -v systemctl >/dev/null 2>&1; then
systemctl disable rke2-server || true
systemctl disable rke2-agent || true
Expand All @@ -70,13 +84,14 @@ uninstall_remove_files()
{

if [ -r /etc/redhat-release ] || [ -r /etc/centos-release ] || [ -r /etc/oracle-release ] || [ -r /etc/amazon-linux-release ]; then
log "Removing rke2 packages"
yum remove -y "rke2-*"

rm -f /etc/yum.repos.d/rancher-rke2*.repo
fi

if [ "${ID_LIKE%%[ ]*}" = "suse" ]; then
if rpm -q rke2-common >/dev/null 2>&1; then
log "Removing rke2 packages using zypper"
# rke2 rpm detected
uninstall_cmd="zypper remove -y rke2-server rke2-agent rke2-common rke2-selinux"
if [ "${TRANSACTIONAL_UPDATE=false}" != "true" ] && [ -x /usr/sbin/transactional-update ]; then
Expand All @@ -87,6 +102,7 @@ uninstall_remove_files()
fi
fi

log "Removing rke2 files"
$transactional_update find "${INSTALL_RKE2_ROOT}/lib/systemd/system" -name rke2-*.service -type f -delete
$transactional_update find "${INSTALL_RKE2_ROOT}/lib/systemd/system" -name rke2-*.env -type f -delete
find /etc/systemd/system -name rke2-*.service -type f -delete
Expand All @@ -99,10 +115,11 @@ uninstall_remove_files()
rm -rf /etc/cni
rm -rf /opt/cni/bin
rm -rf /var/lib/kubelet || true
rm -rf "${RKE2_DATA_DIR}"
rm -rf "${RKE2_DATA_DIR}" || error "Failed to remove /var/lib/rancher/rke2"
rm -d /var/lib/rancher || true

if type fapolicyd >/dev/null 2>&1; then
log "Removing fapolicyd rules"
if [ -f /etc/fapolicyd/rules.d/80-rke2.rules ]; then
rm -f /etc/fapolicyd/rules.d/80-rke2.rules
fi
Expand All @@ -111,18 +128,52 @@ uninstall_remove_files()
fi
}



uninstall_remove_self()
{
cleanup
log "Removing uninstall script"
$transactional_update rm -f "${INSTALL_RKE2_ROOT}/bin/rke2-uninstall.sh"
}

# Define a cleanup function that triggers on exit
cleanup() {
# Check if last command's exit status was not equal to 0
if [ $? -ne 0 ]; then
echo -e "\e[31mCleanup didn't complete successfully\e[0m"
iamsarthakk marked this conversation as resolved.
Show resolved Hide resolved
else
log "Cleanup completed successfully"
fi
}

# Define a cleanup function that triggers on exit
cleanup() {

# Check if last command's exit status was not equal to 0
if [ $? -ne 0 ]; then
if [ -n "$NO_COLOR" ]; then # Disable color code for error message if NO_COLOR env variable is passed
echo -e "Cleanup didn't complete successfully"
else
echo -e "\e[31mCleanup didn't complete successfully\e[0m"
fi
fi
}

uninstall_remove_policy()
{
log "Removing SELinux policy"
semodule -r rke2 || true
}

# Set a trap to log an error if the script exits unexpectedly
trap cleanup EXIT
uninstall_killall
trap - EXIT

trap uninstall_remove_self EXIT
uninstall_disable_services
uninstall_remove_files
uninstall_remove_policy

log "Cleanup completed successfully"
Loading