diff --git a/bundle/bin/rke2-uninstall.sh b/bundle/bin/rke2-uninstall.sh index b8cb05d788..217ba6f0bf 100755 --- a/bundle/bin/rke2-uninstall.sh +++ b/bundle/bin/rke2-uninstall.sh @@ -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 @@ -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 @@ -50,6 +62,7 @@ fi uninstall_killall() { _killall="$(dirname "$0")/rke2-killall.sh" + log "Running killall script" if [ -e "${_killall}" ]; then eval "${_killall}" fi @@ -57,6 +70,7 @@ uninstall_killall() 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 @@ -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 @@ -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 @@ -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 @@ -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" + 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" \ No newline at end of file