**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:
@@ -1,105 +1,146 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Colors
|
# Colors for output
|
||||||
green="\033[1;32m"
|
green="\033[1;32m"
|
||||||
red="\033[1;31m"
|
red="\033[1;31m"
|
||||||
yellow="\033[1;33m"
|
yellow="\033[1;33m"
|
||||||
nc="\033[0m"
|
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
|
# Choose operation
|
||||||
echo -e "${yellow}Select target to update:${nc}"
|
echo -e "${yellow}Select operation:${nc}"
|
||||||
echo "1) LXC Containers"
|
echo "1) Apply APT proxy"
|
||||||
echo "2) VMs"
|
echo "2) Remove APT proxy"
|
||||||
echo "3) All (LXC + VMs)"
|
read -rp "Enter choice [1-2]: " OP
|
||||||
read -rp "Enter choice [1-3]: " TARGET_CHOICE
|
case "$OP" in
|
||||||
case "$TARGET_CHOICE" in
|
1) ACTION="apply";;
|
||||||
1) UPDATE_LXC=true; UPDATE_VM=false;;
|
2) ACTION="remove";;
|
||||||
2) UPDATE_LXC=false; UPDATE_VM=true;;
|
|
||||||
3) UPDATE_LXC=true; UPDATE_VM=true;;
|
|
||||||
*) echo -e "${red}Invalid choice. Exiting.${nc}"; exit 1;;
|
*) echo -e "${red}Invalid choice. Exiting.${nc}"; exit 1;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Optionally skip specific IDs
|
# Select targets
|
||||||
echo
|
echo -e "\n${yellow}Select target to process:${nc}"
|
||||||
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}"
|
echo "1) LXC containers"
|
||||||
read -rp "Skip IDs: " SKIP_IDS_RAW
|
echo "2) VMs"
|
||||||
# Convert to array for checking
|
echo "3) Both LXC + VMs"
|
||||||
IFS=',' read -ra SKIP_IDS <<< "$SKIP_IDS_RAW"
|
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
|
# Skip specific IDs (for LXC/VM)
|
||||||
read -rp "Update Proxmox host node config? [y/N]: " UPDATE_NODE_REPLY
|
read -rp "\nEnter IDs to skip (comma-separated), or press Enter: " SKIP_RAW
|
||||||
if [[ "$UPDATE_NODE_REPLY" =~ ^[Yy]$ ]]; then
|
IFS=',' read -ra SKIP_IDS <<< "$SKIP_RAW"
|
||||||
UPDATE_NODE=true
|
|
||||||
else
|
# If applying, ask for proxy details
|
||||||
UPDATE_NODE=false
|
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
|
fi
|
||||||
|
|
||||||
# Read apt-cacher-ng details
|
confirm() {
|
||||||
read -rp "Enter apt-cacher-ng IP [192.168.1.10]: " CACHE_IP
|
read -rp "$1 [y/N]: " y && [[ "$y" =~ ^[Yy]$ ]]
|
||||||
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}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Update LXC Containers
|
process_lxc() {
|
||||||
if $UPDATE_LXC; then
|
local id=$1
|
||||||
echo -e "\n${green}Updating LXC Containers...${nc}"
|
# skip?
|
||||||
for ct in $(pct list | awk 'NR>1 {print $1}'); do
|
for s in "${SKIP_IDS[@]}"; do [[ "$id" = "$s" ]] && echo "- Skipping LXC $id" && return; done
|
||||||
update_target "pct exec $ct --" "$ct"
|
echo -e "\nProcessing LXC $id"
|
||||||
done
|
# 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
|
fi
|
||||||
|
|
||||||
# Update VMs via QEMU agent
|
if [ "$DO_VM" = true ]; then
|
||||||
if $UPDATE_VM; then
|
echo -e "\n${green}-- VMs --${nc}"
|
||||||
echo -e "\n${green}Updating VMs...${nc}"
|
for id in $(qm list | awk 'NR>1{print $1}'); do process_vm "$id"; done
|
||||||
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
|
fi
|
||||||
|
|
||||||
# Update host node
|
if [ "$DO_HOST" = true ]; then
|
||||||
if $UPDATE_NODE; then
|
echo -e "\n${green}-- Proxmox Host --${nc}"
|
||||||
echo -e "\n${green}Updating Proxmox host node...${nc}"
|
if [ "$ACTION" = "apply" ]; then
|
||||||
rm -f /etc/apt/apt.conf.d/*proxy*
|
if [ -f /etc/apt/apt.conf.d/00aptproxy ]; then
|
||||||
echo "$PROXY_CONF" > "/etc/apt/apt.conf.d/$CONF_FILE"
|
confirm "Proxy exists on host. Overwrite?" || { echo "- Skipped host"; exit; }
|
||||||
echo -e "${green}Host node updated successfully.${nc}"
|
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
|
fi
|
||||||
|
|
||||||
echo -e "\n${green}All selected updates complete.${nc}"
|
echo -e "\n${green}Operation complete.${nc}"
|
||||||
Reference in New Issue
Block a user