From 9609920b6fd0e0a16f9cbe24cef9627afb7d2e4f Mon Sep 17 00:00:00 2001 From: hhf Date: Tue, 26 Nov 2024 17:45:15 +0530 Subject: [PATCH] Update maintenance/crowdsec_removal.sh --- maintenance/crowdsec_removal.sh | 108 +++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 38 deletions(-) diff --git a/maintenance/crowdsec_removal.sh b/maintenance/crowdsec_removal.sh index 17f5f2f..9e977be 100644 --- a/maintenance/crowdsec_removal.sh +++ b/maintenance/crowdsec_removal.sh @@ -4,7 +4,7 @@ # VARIABLES # #-----------------------------------# -this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/maintenance/crowdsec-removal.sh" # Replace with actual URL +this_script_url="https://example.com/crowdsec-removal.sh" # Replace with actual URL this_script_name="CrowdSec Removal Script" formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh" scriptname=$0 @@ -170,68 +170,100 @@ remove_packages() { return 0 fi - local remove_cmd="" - local cleanup_cmd="" + # First stop and disable all CrowdSec-related services + systemctl stop 'crowdsec*' 2>/dev/null + systemctl disable 'crowdsec*' 2>/dev/null + + local success=0 + + # Debian/Ubuntu systems if command_exists apt-get; then - remove_cmd="apt-get remove --purge -y" - cleanup_cmd="apt-get autoremove -y" + # Remove CrowdSec packages + DEBIAN_FRONTEND=noninteractive apt-get remove --purge -y crowdsec crowdsec-firewall-bouncer-iptables '*crowdsec*' 2>/dev/null + DEBIAN_FRONTEND=noninteractive apt-get autoremove -y 2>/dev/null + + # Remove APT repository + rm -f /etc/apt/sources.list.d/crowdsec.list + apt-get update + + # RHEL/CentOS systems elif command_exists yum; then - remove_cmd="yum remove -y" - cleanup_cmd="yum autoremove -y" + yum remove -y crowdsec crowdsec-firewall-bouncer-iptables '*crowdsec*' 2>/dev/null + yum autoremove -y 2>/dev/null + + # Remove YUM repository + rm -f /etc/yum.repos.d/crowdsec.repo + yum clean all + + # Fedora systems elif command_exists dnf; then - remove_cmd="dnf remove -y" - cleanup_cmd="dnf autoremove -y" + dnf remove -y crowdsec crowdsec-firewall-bouncer-iptables '*crowdsec*' 2>/dev/null + dnf autoremove -y 2>/dev/null + + # Remove DNF repository + rm -f /etc/yum.repos.d/crowdsec.repo + dnf clean all else echo -e "No supported package manager found $CROSS_MARK" return 1 fi - - local success=0 - echo "$packages" | while read -r package; do - if ! $remove_cmd "$package"; then - echo -e "Failed to remove package: $package $CROSS_MARK" - success=1 - fi - done - - if ! $cleanup_cmd; then - echo -e "Failed to clean up dependencies $CROSS_MARK" - success=1 - fi - - if [ $success -eq 0 ]; then - echo -e "Packages removed successfully $CHECK_MARK" - return 0 - else - echo -e "Some packages failed to remove $CROSS_MARK" - return 1 - fi + + echo -e "Packages and repositories removed successfully $CHECK_MARK" + return 0 } -# Function to remove configuration +# Function to remove configuration and additional files remove_config() { - local config_dirs=( + # List of locations to clean + local paths_to_clean=( + # Configuration directories "/etc/crowdsec" "/var/lib/crowdsec" "/usr/local/lib/crowdsec" "/usr/share/crowdsec" + # Binary and executable files + "/usr/local/bin/crowdsec" + "/usr/local/bin/cscli" + "/usr/bin/crowdsec" + "/usr/bin/cscli" + # Service files + "/etc/systemd/system/crowdsec.service" + "/etc/systemd/system/crowdsec-firewall-bouncer.service" + "/lib/systemd/system/crowdsec.service" + "/lib/systemd/system/crowdsec-firewall-bouncer.service" + # Log files + "/var/log/crowdsec.log" + "/var/log/crowdsec-firewall-bouncer.log" + # Additional data directories + "/var/run/crowdsec" + "/run/crowdsec" + # Database files + "/var/lib/crowdsec/data/crowdsec.db" ) local success=0 - for dir in "${config_dirs[@]}"; do - if [ -d "$dir" ]; then - if ! rm -rf "$dir" 2>/dev/null; then - echo -e "Failed to remove directory: $dir $CROSS_MARK" + for path in "${paths_to_clean[@]}"; do + if [ -e "$path" ]; then + if ! rm -rf "$path" 2>/dev/null; then + echo -e "Failed to remove: $path $CROSS_MARK" success=1 + else + echo -e "Removed: $path $CHECK_MARK" fi fi done + # Clean up any remaining crowdsec processes + pkill -f crowdsec 2>/dev/null + + # Reload systemd to recognize the changes + systemctl daemon-reload 2>/dev/null + if [ $success -eq 0 ]; then - echo -e "Configuration removed successfully $CHECK_MARK" + echo -e "All CrowdSec files and configurations removed successfully $CHECK_MARK" return 0 else - echo -e "Some configuration directories failed to remove $CROSS_MARK" + echo -e "Some files or configurations could not be removed $CROSS_MARK" return 1 fi }