|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright (c) 2021-2025 community-scripts ORG |
| 4 | +# Author: MickLesk (CanbiZ) |
| 5 | +# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE |
| 6 | + |
| 7 | +function header_info { |
| 8 | + clear |
| 9 | + cat <<"EOF" |
| 10 | + ____ ____ ____ _ __ __ _______ _ __ __ _ ________ ____ ________ __________________ |
| 11 | + / __ \/ __ \/ __ \ |/ // |/ / __ \ |/ / / / | |/ / ____/ / __ \/ ____/ / / ____/_ __/ ____/ |
| 12 | + / /_/ / /_/ / / / / // /|_/ / / / / / / / | / / / / / / __/ / / / __/ / / / __/ |
| 13 | + / ____/ _, _/ /_/ / |/ / / / /_/ / | / /___/ / /___ / /_/ / /___/ /___/ /___ / / / /___ |
| 14 | +/_/ /_/ |_|\____/_/|_/_/ /_/\____/_/|_| /_____/_/|_\____/ /_____/_____/_____/_____/ /_/ /_____/ |
| 15 | + |
| 16 | +EOF |
| 17 | +} |
| 18 | + |
| 19 | +spinner() { |
| 20 | + local pid=$1 |
| 21 | + local delay=0.1 |
| 22 | + local spinstr='|/-\' |
| 23 | + while ps -p $pid > /dev/null; do |
| 24 | + printf " [%c] " "$spinstr" |
| 25 | + spinstr=${spinstr#?}${spinstr%"${spinstr#?}"} |
| 26 | + sleep $delay |
| 27 | + printf "\r" |
| 28 | + done |
| 29 | + printf " \r" |
| 30 | +} |
| 31 | + |
| 32 | +set -eEuo pipefail |
| 33 | +YW=$(echo "\033[33m") |
| 34 | +BL=$(echo "\033[36m") |
| 35 | +RD=$(echo "\033[01;31m") |
| 36 | +CM='\xE2\x9C\x94\033' |
| 37 | +GN=$(echo "\033[1;92m") |
| 38 | +CL=$(echo "\033[m") |
| 39 | + |
| 40 | +header_info |
| 41 | +echo "Loading..." |
| 42 | +whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Deletion" --yesno "This Will Delete LXC Containers. Proceed?" 10 58 || exit |
| 43 | + |
| 44 | +NODE=$(hostname) |
| 45 | + |
| 46 | +# Get list of containers with ID and hostname |
| 47 | +containers=$(pct list | tail -n +2 | awk '{print $0 " " $4}') |
| 48 | + |
| 49 | +# Exit if no containers are found |
| 50 | +if [ -z "$containers" ]; then |
| 51 | + whiptail --title "LXC Container Delete" --msgbox "There are no LXC Container available!" 10 60 |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +menu_items=() |
| 56 | +FORMAT="%-10s %-15s %-10s" |
| 57 | + |
| 58 | +# Format container data for menu display |
| 59 | +while read -r container; do |
| 60 | + container_id=$(echo $container | awk '{print $1}') |
| 61 | + container_name=$(echo $container | awk '{print $2}') |
| 62 | + container_status=$(echo $container | awk '{print $3}') |
| 63 | + formatted_line=$(printf "$FORMAT" "$container_name" "$container_status") |
| 64 | + menu_items+=("$container_id" "$formatted_line" "OFF") |
| 65 | +done <<< "$containers" |
| 66 | + |
| 67 | +# Display selection menu |
| 68 | +CHOICES=$(whiptail --title "LXC Container Delete" \ |
| 69 | + --checklist "Choose LXC container to delete:" 25 60 13 \ |
| 70 | + "${menu_items[@]}" 3>&2 2>&1 1>&3) |
| 71 | + |
| 72 | +if [ -z "$CHOICES" ]; then |
| 73 | + whiptail --title "LXC Container Delete" \ |
| 74 | + --msgbox "No containers have been selected!" 10 60 |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +# Process selected containers |
| 79 | +selected_ids=$(echo "$CHOICES" | tr -d '"' | tr -s ' ' '\n') |
| 80 | + |
| 81 | +for container_id in $selected_ids; do |
| 82 | + status=$(pct status $container_id) |
| 83 | + |
| 84 | + # Stop container if running |
| 85 | + if [ "$status" == "status: running" ]; then |
| 86 | + echo -e "${BL}[Info]${GN} Stop container $container_id...${CL}" |
| 87 | + pct stop $container_id & |
| 88 | + sleep 5 |
| 89 | + echo -e "${BL}[Info]${GN} Container $container_id stopped.${CL}" |
| 90 | + fi |
| 91 | + |
| 92 | + # Confirm deletion |
| 93 | + read -p "Are you sure you want to delete Container $container_id? (y/N): " CONFIRM |
| 94 | + if [[ "$CONFIRM" =~ ^[Yy]$ ]]; then |
| 95 | + echo -e "${BL}[Info]${GN} Deleting container $container_id...${CL}" |
| 96 | + pct destroy "$container_id" -f & |
| 97 | + pid=$! |
| 98 | + spinner $pid |
| 99 | + if [ $? -eq 0 ]; then |
| 100 | + echo "Container $container_id was successfully deleted." |
| 101 | + else |
| 102 | + whiptail --title "Error" --msgbox "Error deleting container $container_id." 10 60 |
| 103 | + fi |
| 104 | + elif [[ "$CONFIRM" =~ ^[Nn]$ ]]; then |
| 105 | + echo -e "${BL}[Info]${RD} Skipping container $container_id...${CL}" |
| 106 | + else |
| 107 | + echo -e "${RD}[Error]${CL} Invalid input, skipping container $container_id." |
| 108 | + fi |
| 109 | +done |
| 110 | + |
| 111 | +header_info |
| 112 | +echo -e "${GN}The deletion process has been completed.${CL}\n" |
0 commit comments