Refactor update_apt_proxy_all.sh for improved readability and functionality; streamline user prompts and enhance proxy configuration handling.
This commit is contained in:
@@ -1,86 +1,105 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Colors
|
# Colors
|
||||||
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 Setup${NC}"
|
echo -e "${green}Proxmox Apt-Cacher-NG Proxy Updater${nc}"
|
||||||
|
|
||||||
# Prompt for target selection
|
# Select targets
|
||||||
while true; do
|
echo -e "${yellow}Select target to update:${nc}"
|
||||||
echo -e "${YELLOW}Select what to update:${NC}"
|
echo "1) LXC Containers"
|
||||||
echo "1) LXC Containers"
|
echo "2) VMs"
|
||||||
echo "2) VMs"
|
echo "3) All (LXC + VMs)"
|
||||||
echo "3) All (LXC + VMs)"
|
read -rp "Enter choice [1-3]: " TARGET_CHOICE
|
||||||
read -rp "Enter choice [1-3]: " TARGET_CHOICE
|
case "$TARGET_CHOICE" in
|
||||||
case $TARGET_CHOICE in
|
1) UPDATE_LXC=true; UPDATE_VM=false;;
|
||||||
1|2|3) break;;
|
2) UPDATE_LXC=false; UPDATE_VM=true;;
|
||||||
*) echo -e "${RED}Invalid choice. Please enter 1, 2, or 3.${NC}";;
|
3) UPDATE_LXC=true; UPDATE_VM=true;;
|
||||||
esac
|
*) echo -e "${red}Invalid choice. Exiting.${nc}"; exit 1;;
|
||||||
done
|
esac
|
||||||
|
|
||||||
# Prompt for updating Proxmox node itself
|
# Optionally skip specific IDs
|
||||||
while true; do
|
echo
|
||||||
read -rp "Do you want to update the Proxmox node itself with the cache? (y/n): " UPDATE_NODE
|
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}"
|
||||||
case $UPDATE_NODE in
|
read -rp "Skip IDs: " SKIP_IDS_RAW
|
||||||
y|n) break;;
|
# Convert to array for checking
|
||||||
*) echo -e "${RED}Please enter y or n.${NC}";;
|
IFS=',' read -ra SKIP_IDS <<< "$SKIP_IDS_RAW"
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# Ask for the apt-cacher-ng server IP and port
|
# Ask to update the Proxmox node itself
|
||||||
read -rp "Enter your apt-cacher-ng IP address (default: 192.168.1.10): " CACHE_IP
|
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}"
|
CACHE_IP="${CACHE_IP:-192.168.1.10}"
|
||||||
|
read -rp "Enter apt-cacher-ng port [3142]: " CACHE_PORT
|
||||||
read -rp "Enter apt-cacher-ng port (default: 3142): " CACHE_PORT
|
|
||||||
CACHE_PORT="${CACHE_PORT:-3142}"
|
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
|
# Function to update a target (container or VM)
|
||||||
read -rp "Proceed to update selected targets with this proxy? (y/n): " CONFIRM
|
update_target() {
|
||||||
[[ "$CONFIRM" != "y" ]] && echo "Aborted." && exit 1
|
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 ###
|
# Update LXC Containers
|
||||||
if pct list &>/dev/null && { [ "$TARGET_CHOICE" = "1" ] || [ "$TARGET_CHOICE" = "3" ]; }; then
|
if $UPDATE_LXC; then
|
||||||
echo -e "\n${GREEN}Updating LXC Containers...${NC}"
|
echo -e "\n${green}Updating LXC Containers...${nc}"
|
||||||
for CTID in $(pct list | awk 'NR>1 {print $1}'); do
|
for ct in $(pct list | awk 'NR>1 {print $1}'); do
|
||||||
echo -e "\n${YELLOW}Updating LXC: $CTID${NC}"
|
update_target "pct exec $ct --" "$ct"
|
||||||
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}"
|
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
### Update VMs via QEMU Guest Agent ###
|
# Update VMs via QEMU agent
|
||||||
if qm list &>/dev/null && { [ "$TARGET_CHOICE" = "2" ] || [ "$TARGET_CHOICE" = "3" ]; }; then
|
if $UPDATE_VM; then
|
||||||
echo -e "\n${GREEN}Updating VMs via QEMU Agent...${NC}"
|
echo -e "\n${green}Updating VMs...${nc}"
|
||||||
for VMID in $(qm list | awk 'NR>1 {print $1}'); do
|
for vm in $(qm list | awk 'NR>1 {print $1}'); do
|
||||||
if qm guest cmd "$VMID" get-osinfo &>/dev/null; then
|
# Only process if guest agent available
|
||||||
echo -e "\n${YELLOW}Updating VM: $VMID${NC}"
|
qm guest cmd $vm get-osinfo &>/dev/null || { echo -e "${red}VM $vm has no guest agent. Skipping.${nc}"; continue; }
|
||||||
qm guest exec "$VMID" -- bash -c "
|
update_target "qm guest exec $vm --" "$vm"
|
||||||
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
|
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
### Update Proxmox Node itself ###
|
# Update host node
|
||||||
if [ "$UPDATE_NODE" = "y" ]; then
|
if $UPDATE_NODE; then
|
||||||
echo -e "\n${GREEN}Updating Proxmox Node itself...${NC}"
|
echo -e "\n${green}Updating Proxmox host node...${nc}"
|
||||||
mkdir -p /etc/apt/apt.conf.d
|
rm -f /etc/apt/apt.conf.d/*proxy*
|
||||||
echo "${APT_PROXY}" > /etc/apt/apt.conf.d/01proxy
|
echo "$PROXY_CONF" > "/etc/apt/apt.conf.d/$CONF_FILE"
|
||||||
echo -e "${GREEN}Proxmox Node updated successfully.${NC}"
|
echo -e "${green}Host node updated successfully.${nc}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "\n${GREEN}All done. apt-cacher-ng configuration updated as selected.${NC}"
|
echo -e "\n${green}All selected updates complete.${nc}"
|
||||||
|
|||||||
Reference in New Issue
Block a user