**Added:**

* Interactive option to **remove all existing APT proxy configurations**, restoring default behavior for all selected targets.
* Support for **selective skipping** of LXC/VM IDs via comma-separated input.
* Prompt to **overwrite existing config** if `00aptproxy` already exists inside target.
* Interactive mode to **apply** or **remove** proxy across:

  * LXC containers
  * VMs with QEMU Guest Agent
  * Proxmox host node

**Changed:**

* Refactored script for **simplified structure** with clearer flow and prompts.
* Improved compatibility check: now uses `/etc/os-release` for accurate distro detection (Ubuntu/Debian only).
* Replaced legacy check via `lsb_release` for faster and leaner execution.

**Fixed:**

* Old proxy files like `01proxy` are now reliably **detected and removed** before applying a new config.
* Ensured **port and proxy format** strictly follow APT specification:
  `Acquire::http::Proxy "http://<host>:<port>";`
This commit is contained in:
2025-07-29 15:00:04 +06:00
parent 5870ea0c17
commit 9831d0b36f

View File

@@ -1,105 +1,146 @@
#!/bin/bash
# Colors
# Colors for output
green="\033[1;32m"
red="\033[1;31m"
yellow="\033[1;33m"
nc="\033[0m"
echo -e "${green}Proxmox Apt-Cacher-NG Proxy Updater${nc}"
echo -e "${green}Proxmox Apt-Cacher-NG Proxy Manager${nc}"
# 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;;
# Choose operation
echo -e "${yellow}Select operation:${nc}"
echo "1) Apply APT proxy"
echo "2) Remove APT proxy"
read -rp "Enter choice [1-2]: " OP
case "$OP" in
1) ACTION="apply";;
2) ACTION="remove";;
*) echo -e "${red}Invalid choice. Exiting.${nc}"; exit 1;;
esac
# 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"
# Select targets
echo -e "\n${yellow}Select target to process:${nc}"
echo "1) LXC containers"
echo "2) VMs"
echo "3) Both LXC + VMs"
echo "4) Proxmox host only"
echo "5) All (LXC + VMs + Host)"
read -rp "Enter choice [1-5]: " T
case "$T" in
1) DO_LXC=true; DO_VM=false; DO_HOST=false;;
2) DO_LXC=false; DO_VM=true; DO_HOST=false;;
3) DO_LXC=true; DO_VM=true; DO_HOST=false;;
4) DO_LXC=false; DO_VM=false; DO_HOST=true;;
5) DO_LXC=true; DO_VM=true; DO_HOST=true;;
*) echo -e "${red}Invalid choice. Exiting.${nc}"; exit 1;;
esac
# 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
# Skip specific IDs (for LXC/VM)
read -rp "\nEnter IDs to skip (comma-separated), or press Enter: " SKIP_RAW
IFS=',' read -ra SKIP_IDS <<< "$SKIP_RAW"
# If applying, ask for proxy details
if [ "$ACTION" = "apply" ]; then
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 [3142]: " CACHE_PORT
CACHE_PORT="${CACHE_PORT:-3142}"
PROXY="Acquire::http::Proxy \"http://${CACHE_IP}:${CACHE_PORT}\";"
echo -e "\n${yellow}Configured proxy: ${PROXY}${nc}\n"
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 [3142]: " CACHE_PORT
CACHE_PORT="${CACHE_PORT:-3142}"
PROXY_CONF="Acquire::http::Proxy \"http://${CACHE_IP}:${CACHE_PORT}\";"
CONF_FILE="00aptproxy"
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
# 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}"
confirm() {
read -rp "$1 [y/N]: " y && [[ "$y" =~ ^[Yy]$ ]]
}
# 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
process_lxc() {
local id=$1
# skip?
for s in "${SKIP_IDS[@]}"; do [[ "$id" = "$s" ]] && echo "- Skipping LXC $id" && return; done
echo -e "\nProcessing LXC $id"
# ensure running
if ! pct status "$id" | grep -q "running"; then
echo "- Not running, skipping"
return
fi
if [ "$ACTION" = "apply" ]; then
# check distro
pct exec "$id" -- sh -c 'export ID=$(awk -F= "/^ID=/ {print \$2}" /etc/os-release) && case "$ID" in debian|ubuntu) exit 0;; *) exit 1;; esac'
if [ $? -ne 0 ]; then echo "- Not Debian/Ubuntu, skipping"; return; fi
# existing?
if pct exec "$id" -- test -f /etc/apt/apt.conf.d/00aptproxy; then
confirm "Proxy exists in LXC $id. Overwrite?" || { echo "- Skipped"; return; }
fi
pct exec "$id" -- sh -c 'rm -f /etc/apt/apt.conf.d/*proxy*'
pct exec "$id" -- sh -c "mkdir -p /etc/apt/apt.conf.d && echo '$PROXY' > /etc/apt/apt.conf.d/00aptproxy"
echo "- Proxy applied to LXC $id"
else
# remove
if pct exec "$id" -- test -f /etc/apt/apt.conf.d/00aptproxy; then
pct exec "$id" -- sh -c 'rm -f /etc/apt/apt.conf.d/*proxy*'
echo "- Proxy removed from LXC $id"
else
echo "- No proxy config in LXC $id"
fi
fi
}
process_vm() {
local id=$1
for s in "${SKIP_IDS[@]}"; do [[ "$id" = "$s" ]] && echo "- Skipping VM $id" && return; done
echo -e "\nProcessing VM $id"
if ! qm guest cmd "$id" ping &>/dev/null; then
echo "- Guest agent not available, skipping"
return
fi
if [ "$ACTION" = "apply" ]; then
qm guest exec "$id" -- sh -c 'export ID=$(awk -F= "/^ID=/ {print \$2}" /etc/os-release) && case "$ID" in debian|ubuntu) exit 0;; *) exit 1;; esac'
if [ $? -ne 0 ]; then echo "- Not Debian/Ubuntu, skipping"; return; fi
if qm guest exec "$id" -- test -f /etc/apt/apt.conf.d/00aptproxy; then
confirm "Proxy exists in VM $id. Overwrite?" || { echo "- Skipped"; return; }
fi
qm guest exec "$id" -- sh -c 'rm -f /etc/apt/apt.conf.d/*proxy*'
qm guest exec "$id" -- sh -c "mkdir -p /etc/apt/apt.conf.d && echo '$PROXY' > /etc/apt/apt.conf.d/00aptproxy"
echo "- Proxy applied to VM $id"
else
if qm guest exec "$id" -- test -f /etc/apt/apt.conf.d/00aptproxy; then
qm guest exec "$id" -- sh -c 'rm -f /etc/apt/apt.conf.d/*proxy*'
echo "- Proxy removed from VM $id"
else
echo "- No proxy config in VM $id"
fi
fi
}
# Process items
if [ "$DO_LXC" = true ]; then
echo -e "\n${green}-- LXC Containers --${nc}"
for id in $(pct list | awk 'NR>1{print $1}'); do process_lxc "$id"; done
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
if [ "$DO_VM" = true ]; then
echo -e "\n${green}-- VMs --${nc}"
for id in $(qm list | awk 'NR>1{print $1}'); do process_vm "$id"; done
fi
# 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}"
if [ "$DO_HOST" = true ]; then
echo -e "\n${green}-- Proxmox Host --${nc}"
if [ "$ACTION" = "apply" ]; then
if [ -f /etc/apt/apt.conf.d/00aptproxy ]; then
confirm "Proxy exists on host. Overwrite?" || { echo "- Skipped host"; exit; }
fi
rm -f /etc/apt/apt.conf.d/*proxy*
echo "$PROXY" > /etc/apt/apt.conf.d/00aptproxy
echo "- Proxy applied on host"
else
if [ -f /etc/apt/apt.conf.d/00aptproxy ]; then
rm -f /etc/apt/apt.conf.d/*proxy*
echo "- Proxy removed from host"
else
echo "- No proxy config on host"
fi
fi
fi
echo -e "\n${green}All selected updates complete.${nc}"
echo -e "\n${green}Operation complete.${nc}"