Skip to content

Commit

Permalink
Improving logging and error readability for rke2-uninstall.sh script (#…
Browse files Browse the repository at this point in the history
…6237)

* Added Descriptive logging and messaging in case the script fails

* Removing redundant piped error

* Added support for NO_COLOR env variable for default no printing of color codes

* Add data-dir to uninstall and killall scripts

Signed-off-by: Jake Hyde <[email protected]>
Co-authored-by: Sarthak Kumar <[email protected]>
Co-authored-by: Jake Hyde <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent 93064fd commit 721ecb2
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 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/system-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 @@ -71,12 +85,12 @@ uninstall_remove_files()

if [ -r /etc/redhat-release ] || [ -r /etc/centos-release ] || [ -r /etc/oracle-release ] || [ -r /etc/system-release ]; then
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 +101,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 +114,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 +127,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"
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"

0 comments on commit 721ecb2

Please sign in to comment.