update
This commit is contained in:
parent
1170088345
commit
230fbd0b8c
3 changed files with 323 additions and 218 deletions
322
maintenance/crowdsec_removal.sh
Normal file
322
maintenance/crowdsec_removal.sh
Normal file
|
@ -0,0 +1,322 @@
|
|||
#!/bin/bash
|
||||
|
||||
#-----------------------------------#
|
||||
# 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_name="CrowdSec Removal Script"
|
||||
formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh"
|
||||
scriptname=$0
|
||||
|
||||
# Script version
|
||||
VERSION="2.0.0"
|
||||
|
||||
# Initialize success flag
|
||||
success=0
|
||||
|
||||
# Determine the user (use the first argument if provided, otherwise fallback)
|
||||
USER_TO_RUN_AS="${1:-$SUDO_USER}"
|
||||
USER_HOME=$(eval echo ~$USER_TO_RUN_AS)
|
||||
|
||||
# Logging and backup setup
|
||||
LOGFILE="/var/log/crowdsec_removal_$(date +%Y%m%d_%H%M%S).log"
|
||||
BACKUP_DIR="/var/backup/crowdsec_$(date +%Y%m%d_%H%M%S)"
|
||||
|
||||
#-----------------------------------#
|
||||
# FORMATTER #
|
||||
#-----------------------------------#
|
||||
|
||||
# Download and source the formatter with error handling
|
||||
download_formatter() {
|
||||
if [ ! -f "task_formatter.sh" ]; then
|
||||
if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then
|
||||
echo "Error: Failed to download task_formatter.sh"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -f "task_formatter.sh" ]; then
|
||||
echo "Error: task_formatter.sh not found after download attempt"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Make the formatter executable
|
||||
chmod +x task_formatter.sh
|
||||
|
||||
# Source the formatter with error checking
|
||||
source ./task_formatter.sh || { echo "Error: Failed to source task_formatter.sh"; exit 1; }
|
||||
|
||||
# Check if print_header is available after sourcing
|
||||
if ! declare -f print_header > /dev/null; then
|
||||
echo "Error: print_header function not found after sourcing."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Call the download_formatter function
|
||||
download_formatter
|
||||
|
||||
#-----------------------------------#
|
||||
# FUNCTIONS #
|
||||
#-----------------------------------#
|
||||
|
||||
# Function to check if a command exists
|
||||
command_exists() {
|
||||
command -v "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Function to check if running as root
|
||||
check_root() {
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
echo -e "Please run as root $CROSS_MARK"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "Running as root $CHECK_MARK"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to create backup
|
||||
create_backup() {
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
|
||||
# Backup configuration files
|
||||
if [ -d "/etc/crowdsec" ]; then
|
||||
cp -r "/etc/crowdsec" "$BACKUP_DIR/"
|
||||
fi
|
||||
|
||||
if [ -d "/var/lib/crowdsec" ]; then
|
||||
cp -r "/var/lib/crowdsec" "$BACKUP_DIR/"
|
||||
fi
|
||||
|
||||
# Backup service files
|
||||
if [ -d "/etc/systemd/system" ]; then
|
||||
find "/etc/systemd/system" -name "*crowdsec*" -exec cp {} "$BACKUP_DIR/" \;
|
||||
fi
|
||||
|
||||
echo -e "Backup created at $BACKUP_DIR $CHECK_MARK"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to check services
|
||||
check_services() {
|
||||
systemctl list-units --type=service --all | grep -i crowdsec | awk '{print $1}' > /tmp/crowdsec_services.tmp
|
||||
|
||||
if [ ! -s /tmp/crowdsec_services.tmp ]; then
|
||||
echo -e "No CrowdSec services found $CHECK_MARK"
|
||||
services=""
|
||||
return 0
|
||||
else
|
||||
services=$(cat /tmp/crowdsec_services.tmp)
|
||||
echo "Found CrowdSec services:"
|
||||
cat /tmp/crowdsec_services.tmp
|
||||
echo -e "Service check completed $CHECK_MARK"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check packages
|
||||
check_packages() {
|
||||
if command_exists dpkg; then
|
||||
dpkg -l | grep -i crowdsec | awk '{print $2}' > /tmp/crowdsec_packages.tmp
|
||||
elif command_exists rpm; then
|
||||
rpm -qa | grep -i crowdsec > /tmp/crowdsec_packages.tmp
|
||||
else
|
||||
echo -e "Unable to determine package manager $CROSS_MARK"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ ! -s /tmp/crowdsec_packages.tmp ]; then
|
||||
echo -e "No CrowdSec packages found $CHECK_MARK"
|
||||
packages=""
|
||||
return 0
|
||||
else
|
||||
packages=$(cat /tmp/crowdsec_packages.tmp)
|
||||
echo "Found CrowdSec packages:"
|
||||
cat /tmp/crowdsec_packages.tmp
|
||||
echo -e "Package check completed $CHECK_MARK"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to disable services
|
||||
disable_services() {
|
||||
if [ -z "$services" ]; then
|
||||
echo -e "No CrowdSec services to disable $CHECK_MARK"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local success=0
|
||||
echo "$services" | while read -r service; do
|
||||
if ! systemctl stop "$service" 2>/dev/null || ! systemctl disable "$service" 2>/dev/null; then
|
||||
echo -e "Failed to disable $service $CROSS_MARK"
|
||||
success=1
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $success -eq 0 ]; then
|
||||
echo -e "Services disabled successfully $CHECK_MARK"
|
||||
return 0
|
||||
else
|
||||
echo -e "Some services failed to disable $CROSS_MARK"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to remove packages
|
||||
remove_packages() {
|
||||
if [ -z "$packages" ]; then
|
||||
echo -e "No CrowdSec packages to remove $CHECK_MARK"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local remove_cmd=""
|
||||
local cleanup_cmd=""
|
||||
if command_exists apt-get; then
|
||||
remove_cmd="apt-get remove --purge -y"
|
||||
cleanup_cmd="apt-get autoremove -y"
|
||||
elif command_exists yum; then
|
||||
remove_cmd="yum remove -y"
|
||||
cleanup_cmd="yum autoremove -y"
|
||||
elif command_exists dnf; then
|
||||
remove_cmd="dnf remove -y"
|
||||
cleanup_cmd="dnf autoremove -y"
|
||||
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
|
||||
}
|
||||
|
||||
# Function to remove configuration
|
||||
remove_config() {
|
||||
local config_dirs=(
|
||||
"/etc/crowdsec"
|
||||
"/var/lib/crowdsec"
|
||||
"/usr/local/lib/crowdsec"
|
||||
"/usr/share/crowdsec"
|
||||
)
|
||||
|
||||
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"
|
||||
success=1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $success -eq 0 ]; then
|
||||
echo -e "Configuration removed successfully $CHECK_MARK"
|
||||
return 0
|
||||
else
|
||||
echo -e "Some configuration directories failed to remove $CROSS_MARK"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Remove created files on cleanup
|
||||
cleanup_files() {
|
||||
rm -f /tmp/crowdsec_services.tmp /tmp/crowdsec_packages.tmp
|
||||
echo -e "Cleaned up temporary files $CHECK_MARK"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Remove the script itself
|
||||
remove_script() {
|
||||
if [ -f "$0" ]; then
|
||||
echo "Removing monitoring script..."
|
||||
rm -- "$0"
|
||||
fi
|
||||
if [ -f "task_formatter.sh" ]; then
|
||||
rm task_formatter.sh
|
||||
fi
|
||||
echo -e "Cleaned up $CHECK_MARK"
|
||||
return 0
|
||||
}
|
||||
|
||||
#-----------------------------------#
|
||||
# MAIN LOGIC #
|
||||
#-----------------------------------#
|
||||
|
||||
# Print header
|
||||
print_header "$this_script_name" "$this_script_url"
|
||||
|
||||
echo -e "Running as User: $USER_TO_RUN_AS\nUser Home: $USER_HOME\n"
|
||||
|
||||
# Run with formatted output
|
||||
if ! format_output check_root "Checking root privileges"; then
|
||||
cleanup_files
|
||||
success=1
|
||||
fi
|
||||
|
||||
if ! format_output check_services "Checking CrowdSec services"; then
|
||||
cleanup_files
|
||||
success=1
|
||||
fi
|
||||
|
||||
if ! format_output check_packages "Checking CrowdSec packages"; then
|
||||
cleanup_files
|
||||
success=1
|
||||
fi
|
||||
|
||||
if [ -z "$services" ] && [ -z "$packages" ]; then
|
||||
echo -e "No CrowdSec components found on your system $CHECK_MARK"
|
||||
format_output cleanup_files "Cleaning up temporary files"
|
||||
format_output remove_script "Removing script"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "What would you like to do?"
|
||||
echo "1) Disable CrowdSec services"
|
||||
echo "2) Remove CrowdSec packages"
|
||||
echo "3) Remove everything (services, packages, and configuration)"
|
||||
echo "4) Exit without changes"
|
||||
|
||||
read -r -p "Enter your choice (1-4): " choice
|
||||
|
||||
case $choice in
|
||||
1) format_output disable_services "Disabling CrowdSec services" ;;
|
||||
2) format_output remove_packages "Removing CrowdSec packages" ;;
|
||||
3)
|
||||
format_output create_backup "Creating backup"
|
||||
format_output disable_services "Disabling CrowdSec services"
|
||||
format_output remove_packages "Removing CrowdSec packages"
|
||||
format_output remove_config "Removing CrowdSec configuration"
|
||||
;;
|
||||
4)
|
||||
echo -e "Exiting without changes $CHECK_MARK"
|
||||
success=0
|
||||
;;
|
||||
*)
|
||||
echo -e "Invalid choice $CROSS_MARK"
|
||||
success=1
|
||||
;;
|
||||
esac
|
||||
|
||||
format_output cleanup_files "Cleaning up temporary files"
|
||||
format_output remove_script "Removing script"
|
||||
|
||||
# Exit with appropriate status
|
||||
exit $success
|
|
@ -1,217 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Run this script with:
|
||||
# wget https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/master.sh && sudo bash master.sh
|
||||
|
||||
# Variables for URLs and script info
|
||||
this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/master.sh"
|
||||
this_script_name="HHF Script Management Tool"
|
||||
BRANCH="main"
|
||||
|
||||
forgejo_url="git.hhf.technology"
|
||||
repo_owner="hhf"
|
||||
repo_name="script-management-cloudpanel"
|
||||
|
||||
# Colors for output formatting
|
||||
COLOR_RED='\033[0;31m'
|
||||
COLOR_GREEN='\033[0;32m'
|
||||
COLOR_BLUE='\033[0;34m'
|
||||
COLOR_RESET='\033[0m'
|
||||
|
||||
# Formatting functions
|
||||
print_header() {
|
||||
local script_name=$1
|
||||
local script_url=$2
|
||||
printf "\n${COLOR_GREEN}=== %s ===${COLOR_RESET}\n" "$script_name"
|
||||
printf "${COLOR_BLUE}Script URL: %s${COLOR_RESET}\n\n" "$script_url"
|
||||
}
|
||||
|
||||
format_output() {
|
||||
local func=$1
|
||||
local message=$2
|
||||
printf "${COLOR_BLUE}%s...${COLOR_RESET}\n" "$message"
|
||||
$func
|
||||
printf "${COLOR_GREEN}Done!${COLOR_RESET}\n"
|
||||
}
|
||||
|
||||
final_message() {
|
||||
local script_name=$1
|
||||
local success=$2
|
||||
if [ $success -eq 0 ]; then
|
||||
printf "\n${COLOR_GREEN}=== %s completed successfully ===${COLOR_RESET}\n" "$script_name"
|
||||
else
|
||||
printf "\n${COLOR_RED}=== %s failed with errors ===${COLOR_RESET}\n" "$script_name"
|
||||
fi
|
||||
}
|
||||
|
||||
# Check if the script is being run with sudo
|
||||
if [ "$EUID" -eq 0 ]; then
|
||||
ORIGINAL_USER=${SUDO_USER:-$(whoami)}
|
||||
else
|
||||
ORIGINAL_USER=$(whoami)
|
||||
fi
|
||||
|
||||
# Function to check and install sudo if not present
|
||||
install_sudo() {
|
||||
if ! command which sudo &> /dev/null; then
|
||||
echo "sudo is not installed. Installing sudo..."
|
||||
if [ -x "$(command -v apt-get)" ]; then
|
||||
apt-get update > /dev/null 2>&1 && apt-get install -y sudo > /dev/null 2>&1
|
||||
elif [ -x "$(command -v yum)" ]; then
|
||||
yum install -y sudo > /dev/null 2>&1
|
||||
else
|
||||
echo "Could not install sudo. Please install it manually."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check and install curl if not present
|
||||
install_curl() {
|
||||
if ! command -v curl &> /dev/null; then
|
||||
echo "curl is not installed. Installing curl..."
|
||||
if [ -x "$(command -v apt-get)" ]; then
|
||||
sudo apt-get update > /dev/null 2>&1 && sudo apt-get install -y curl > /dev/null 2>&1
|
||||
elif [ -x "$(command -v yum)" ]; then
|
||||
sudo yum install -y curl > /dev/null 2>&1
|
||||
else
|
||||
echo "Could not install curl. Please install it manually."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check and install jq if not present
|
||||
install_jq() {
|
||||
if ! command -v jq &> /dev/null; then
|
||||
echo "jq is not installed. Installing jq..."
|
||||
if [ -x "$(command -v apt-get)" ]; then
|
||||
sudo apt-get update > /dev/null 2>&1 && sudo apt-get install -y jq > /dev/null 2>&1
|
||||
elif [ -x "$(command -v yum)" ]; then
|
||||
sudo yum install -y jq > /dev/null 2>&1
|
||||
else
|
||||
echo "Could not install jq. Please install it manually."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to clean up temporary files in /tmp
|
||||
cleanup_tmp_files() {
|
||||
echo "Cleaning up temporary files..."
|
||||
sudo rm -rf /tmp/$forgejo_url* > /dev/null 2>&1
|
||||
echo "Temporary files cleaned."
|
||||
}
|
||||
|
||||
remove_script() {
|
||||
rm -- "$0" 2>/dev/null
|
||||
}
|
||||
|
||||
# Function to fetch the list of scripts from the repository
|
||||
fetch_scripts() {
|
||||
# Forgejo/Gitea API endpoint for listing repository contents
|
||||
scripts_recursive=$(curl -k -s "https://$forgejo_url/api/v1/repos/$repo_owner/$repo_name/contents?ref=$BRANCH" | jq -r '.[] | select(.type == "file" and .name and (.name | type == "string") and (.name | endswith(".sh"))) | .path')
|
||||
echo "$scripts_recursive" | sort -u
|
||||
}
|
||||
|
||||
# Function to download and run a selected script
|
||||
run_script() {
|
||||
local script_path=$1
|
||||
local encoded_script_path=$(echo "$script_path" | sed 's/\//%2F/g')
|
||||
local script_name=$(basename "$script_path")
|
||||
local url="https://$forgejo_url/$repo_owner/$repo_name/raw/branch/$BRANCH/$script_path"
|
||||
|
||||
# If verbose mode, print the URL
|
||||
if [[ "$verbose" == "true" ]]; then
|
||||
echo "Requesting URL: $url"
|
||||
fi
|
||||
|
||||
# Create necessary directories for the script
|
||||
mkdir -p "/tmp/$(dirname "$script_path")"
|
||||
|
||||
# Make the request and capture the HTTP status code
|
||||
http_status=$(curl -k -sL -w "%{http_code}" -o "/tmp/$script_path" "$url")
|
||||
|
||||
# If verbose mode, print the HTTP status code
|
||||
if [[ "$verbose" == "true" ]]; then
|
||||
echo "Request completed with status code: $http_status"
|
||||
fi
|
||||
|
||||
if [[ "$http_status" == "200" ]]; then
|
||||
chmod +x "/tmp/$script_path"
|
||||
sudo bash "/tmp/$script_path" "$ORIGINAL_USER"
|
||||
else
|
||||
echo "Failed to download script: $script_name (HTTP status code: $http_status)"
|
||||
fi
|
||||
}
|
||||
|
||||
# Main function to display the script selection menu and run the selected scripts
|
||||
run_scripts() {
|
||||
printf "${COLOR_GREEN}Fetching list of available scripts from Forgejo repository...${COLOR_RESET}\n"
|
||||
scripts=$(fetch_scripts)
|
||||
|
||||
if [ -z "$scripts" ]; then
|
||||
printf "${COLOR_RED}No scripts found in the repository.${COLOR_RESET}\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
while true; do
|
||||
printf "${COLOR_BLUE}\nAvailable scripts:${COLOR_RESET}\n"
|
||||
select script in $scripts "Quit"; do
|
||||
if [ "$script" == "Quit" ]; then
|
||||
break 2
|
||||
elif [ -n "$script" ]; then
|
||||
printf "${COLOR_BLUE}You selected $script. Running script...${COLOR_RESET}\n"
|
||||
run_script "$script"
|
||||
break
|
||||
else
|
||||
printf "${COLOR_RED}Invalid selection. Please try again.${COLOR_RESET}\n"
|
||||
fi
|
||||
done
|
||||
|
||||
printf "${COLOR_BLUE}Would you like to run more scripts? (y/n)${COLOR_RESET}\n"
|
||||
read -r choice
|
||||
if [[ "$choice" != "y" ]]; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Main script logic
|
||||
clear
|
||||
|
||||
# Check for verbose flag
|
||||
verbose="false"
|
||||
if [[ "$1" == "-v" ]]; then
|
||||
verbose="true"
|
||||
fi
|
||||
|
||||
# Print header
|
||||
print_header "$this_script_name" "$this_script_url"
|
||||
|
||||
# Initialize success flag
|
||||
success=0
|
||||
install_sudo
|
||||
install_curl
|
||||
install_jq
|
||||
run_scripts
|
||||
|
||||
# Cleanup created files
|
||||
cleanup_tmp_files
|
||||
|
||||
# Clean up the master script
|
||||
format_output remove_script "Cleaning up"
|
||||
|
||||
# Print final message
|
||||
final_message() {
|
||||
local script_name=$1
|
||||
local success=$2
|
||||
if [[ $success -eq 0 ]]; then
|
||||
log "${CHECK_MARK} $script_name completed successfully."
|
||||
else
|
||||
log "${CROSS_MARK} $script_name encountered errors."
|
||||
fi
|
||||
}
|
||||
|
||||
# Exit with appropriate status
|
||||
exit $success
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
# Variables for URLs and script info
|
||||
this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/master.sh"
|
||||
this_script_name="Forgejo Script Management Tool"
|
||||
this_script_name="HHF Script Management Tool"
|
||||
BRANCH="main"
|
||||
|
||||
forgejo_url="git.hhf.technology"
|
||||
|
|
Loading…
Reference in a new issue