Refactor update script to enhance user prompts and streamline target selection for LXC containers and VMs

This commit is contained in:
2025-07-29 12:11:53 +06:00
parent aaf0b5e862
commit e9dbc27570

View File

@@ -6,7 +6,29 @@ RED="\033[1;31m"
YELLOW="\033[1;33m"
NC="\033[0m"
echo -e "${GREEN}Proxmox Apt-Cacher-NG Setup for All LXC/VM${NC}"
echo -e "${GREEN}Proxmox Apt-Cacher-NG Setup${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
# 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
# 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
@@ -20,39 +42,45 @@ APT_PROXY="Acquire::http::Proxy \"http://${CACHE_IP}:${CACHE_PORT}\";"
echo -e "\n${YELLOW}Using APT Proxy: ${APT_PROXY}${NC}\n"
# Confirm before proceeding
read -rp "Proceed to update all LXC containers and VMs with this proxy? (y/n): " CONFIRM
read -rp "Proceed to update selected targets with this proxy? (y/n): " CONFIRM
[[ "$CONFIRM" != "y" ]] && echo "Aborted." && exit 1
### Update LXC containers ###
echo -e "\n${GREEN}Updating LXC Containers...${NC}"
for CTID in $(pct list | awk 'NR>1 {print $1}'); do
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}"
done
done
fi
### Update VMs via QEMU Guest Agent ###
echo -e "\n${GREEN}Updating VMs via QEMU Agent...${NC}"
for VMID in $(qm list | awk 'NR>1 {print $1}'); do
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
done
done
fi
echo -e "\n${GREEN}All done. apt-cacher-ng configuration updated on containers and agent-enabled VMs.${NC}"
### 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}"
fi
echo -e "\n${GREEN}All done. apt-cacher-ng configuration updated as selected.${NC}"