Refactor update_apt_proxy_all.sh for improved readability and functionality; streamline user prompts and enhance proxy configuration handling.

This commit is contained in:
2025-07-29 14:34:39 +06:00
parent 617af01fd7
commit 5870ea0c17

View File

@@ -1,86 +1,105 @@
#!/bin/bash
# Colors
GREEN="\033[1;32m"
RED="\033[1;31m"
YELLOW="\033[1;33m"
NC="\033[0m"
green="\033[1;32m"
red="\033[1;31m"
yellow="\033[1;33m"
nc="\033[0m"
echo -e "${GREEN}Proxmox Apt-Cacher-NG Setup${NC}"
echo -e "${green}Proxmox Apt-Cacher-NG Proxy Updater${nc}"
# Prompt for target selection
while true; do
echo -e "${YELLOW}Select what to update:${NC}"
echo "1) LXC Containers"
echo "2) VMs"
echo "3) All (LXC + VMs)"
read -rp "Enter choice [1-3]: " TARGET_CHOICE
case $TARGET_CHOICE in
1|2|3) break;;
*) echo -e "${RED}Invalid choice. Please enter 1, 2, or 3.${NC}";;
esac
done
# Select targets
echo -e "${yellow}Select target to update:${nc}"
echo "1) LXC Containers"
echo "2) VMs"
echo "3) All (LXC + VMs)"
read -rp "Enter choice [1-3]: " TARGET_CHOICE
case "$TARGET_CHOICE" in
1) UPDATE_LXC=true; UPDATE_VM=false;;
2) UPDATE_LXC=false; UPDATE_VM=true;;
3) UPDATE_LXC=true; UPDATE_VM=true;;
*) echo -e "${red}Invalid choice. Exiting.${nc}"; exit 1;;
esac
# Prompt for updating Proxmox node itself
while true; do
read -rp "Do you want to update the Proxmox node itself with the cache? (y/n): " UPDATE_NODE
case $UPDATE_NODE in
y|n) break;;
*) echo -e "${RED}Please enter y or n.${NC}";;
esac
done
# Optionally skip specific IDs
echo
echo -e "${yellow}If you wish to skip specific IDs, enter them comma-separated (e.g. 100,102), or press Enter to skip skip:${nc}"
read -rp "Skip IDs: " SKIP_IDS_RAW
# Convert to array for checking
IFS=',' read -ra SKIP_IDS <<< "$SKIP_IDS_RAW"
# Ask for the apt-cacher-ng server IP and port
read -rp "Enter your apt-cacher-ng IP address (default: 192.168.1.10): " CACHE_IP
# Ask to update the Proxmox node itself
read -rp "Update Proxmox host node config? [y/N]: " UPDATE_NODE_REPLY
if [[ "$UPDATE_NODE_REPLY" =~ ^[Yy]$ ]]; then
UPDATE_NODE=true
else
UPDATE_NODE=false
fi
# Read apt-cacher-ng details
read -rp "Enter apt-cacher-ng IP [192.168.1.10]: " CACHE_IP
CACHE_IP="${CACHE_IP:-192.168.1.10}"
read -rp "Enter apt-cacher-ng port (default: 3142): " CACHE_PORT
read -rp "Enter apt-cacher-ng port [3142]: " CACHE_PORT
CACHE_PORT="${CACHE_PORT:-3142}"
APT_PROXY="Acquire::http::Proxy \"http://${CACHE_IP}:${CACHE_PORT}\";"
PROXY_CONF="Acquire::http::Proxy \"http://${CACHE_IP}:${CACHE_PORT}\";"
CONF_FILE="00aptproxy"
echo -e "\n${YELLOW}Using APT Proxy: ${APT_PROXY}${NC}\n"
echo -e "\n${yellow}Using proxy: http://${CACHE_IP}:${CACHE_PORT}${nc}\n"
read -rp "Proceed? [y/N]: " CONFIRM
[[ ! "$CONFIRM" =~ ^[Yy]$ ]] && echo "Aborted." && exit 1
# Confirm before proceeding
read -rp "Proceed to update selected targets with this proxy? (y/n): " CONFIRM
[[ "$CONFIRM" != "y" ]] && echo "Aborted." && exit 1
# Function to update a target (container or VM)
update_target() {
local exec_cmd="$1"
local id="$2"
echo -e "\n${yellow}Processing ID: $id${nc}"
# Check skip
for skip in "${SKIP_IDS[@]}"; do
[[ "$id" == "$skip" ]] && echo -e "${red}Skipping ID $id per user request.${nc}" && return
done
# Check distro compatibility
$exec_cmd "bash -c 'if command -v lsb_release &>/dev/null; then DIST=\$(lsb_release -is); else DIST=\$(. /etc/os-release && echo \$ID); fi; if [[ ! \$DIST =~ (ubuntu|debian) ]]; then exit 2; fi'"
case $? in
2) echo -e "${red}ID $id is not Debian/Ubuntu based. Skipping.${nc}"; return;;
esac
# Remove old proxy files
$exec_cmd "rm -f /etc/apt/apt.conf.d/*proxy*"
# If config exists, prompt before overwrite
$exec_cmd "bash -c 'if [[ -f /etc/apt/apt.conf.d/$CONF_FILE ]]; then exit 1; fi'"
if [[ $? -eq 1 ]]; then
read -rp "Config exists in ID $id. Overwrite? [y/N]: " over
[[ ! "$over" =~ ^[Yy]$ ]] && echo -e "${red}Skipped ID $id per user choice.${nc}" && return
fi
# Write new proxy
$exec_cmd "bash -c 'mkdir -p /etc/apt/apt.conf.d && echo \"$PROXY_CONF\" > /etc/apt/apt.conf.d/$CONF_FILE'"
echo -e "${green}ID $id updated successfully.${nc}"
}
### Update LXC containers ###
if pct list &>/dev/null && { [ "$TARGET_CHOICE" = "1" ] || [ "$TARGET_CHOICE" = "3" ]; }; then
echo -e "\n${GREEN}Updating LXC Containers...${NC}"
for CTID in $(pct list | awk 'NR>1 {print $1}'); do
echo -e "\n${YELLOW}Updating LXC: $CTID${NC}"
pct exec "$CTID" -- bash -c "
mkdir -p /etc/apt/apt.conf.d
echo '${APT_PROXY}' > /etc/apt/apt.conf.d/01proxy
"
echo -e "${GREEN}LXC $CTID updated successfully.${NC}"
# Update LXC Containers
if $UPDATE_LXC; then
echo -e "\n${green}Updating LXC Containers...${nc}"
for ct in $(pct list | awk 'NR>1 {print $1}'); do
update_target "pct exec $ct --" "$ct"
done
fi
### Update VMs via QEMU Guest Agent ###
if qm list &>/dev/null && { [ "$TARGET_CHOICE" = "2" ] || [ "$TARGET_CHOICE" = "3" ]; }; then
echo -e "\n${GREEN}Updating VMs via QEMU Agent...${NC}"
for VMID in $(qm list | awk 'NR>1 {print $1}'); do
if qm guest cmd "$VMID" get-osinfo &>/dev/null; then
echo -e "\n${YELLOW}Updating VM: $VMID${NC}"
qm guest exec "$VMID" -- bash -c "
mkdir -p /etc/apt/apt.conf.d
echo '${APT_PROXY}' > /etc/apt/apt.conf.d/01proxy
" &>/dev/null
echo -e "${GREEN}VM $VMID updated successfully.${NC}"
else
echo -e "${RED}Skipping VM $VMID QEMU Guest Agent not available.${NC}"
fi
# Update VMs via QEMU agent
if $UPDATE_VM; then
echo -e "\n${green}Updating VMs...${nc}"
for vm in $(qm list | awk 'NR>1 {print $1}'); do
# Only process if guest agent available
qm guest cmd $vm get-osinfo &>/dev/null || { echo -e "${red}VM $vm has no guest agent. Skipping.${nc}"; continue; }
update_target "qm guest exec $vm --" "$vm"
done
fi
### Update Proxmox Node itself ###
if [ "$UPDATE_NODE" = "y" ]; then
echo -e "\n${GREEN}Updating Proxmox Node itself...${NC}"
mkdir -p /etc/apt/apt.conf.d
echo "${APT_PROXY}" > /etc/apt/apt.conf.d/01proxy
echo -e "${GREEN}Proxmox Node updated successfully.${NC}"
# Update host node
if $UPDATE_NODE; then
echo -e "\n${green}Updating Proxmox host node...${nc}"
rm -f /etc/apt/apt.conf.d/*proxy*
echo "$PROXY_CONF" > "/etc/apt/apt.conf.d/$CONF_FILE"
echo -e "${green}Host node updated successfully.${nc}"
fi
echo -e "\n${GREEN}All done. apt-cacher-ng configuration updated as selected.${NC}"
echo -e "\n${green}All selected updates complete.${nc}"